• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libtaskmanager

tasklmbmenu.cpp

Go to the documentation of this file.
00001 /*****************************************************************
00002 
00003 Copyright (c) 2001 Matthias Elter <elter@kde.org>
00004 Copyright (c) 2002 John Firebaugh <jfirebaugh@kde.org>
00005 
00006 Permission is hereby granted, free of charge, to any person obtaining a copy
00007 of this software and associated documentation files (the "Software"), to deal
00008 in the Software without restriction, including without limitation the rights
00009 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010 copies of the Software, and to permit persons to whom the Software is
00011 furnished to do so, subject to the following conditions:
00012 
00013 The above copyright notice and this permission notice shall be included in
00014 all copies or substantial portions of the Software.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00019 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00020 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00021 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022 
00023 ******************************************************************/
00024 
00025 #include "tasklmbmenu.h"
00026 #include "tasklmbmenu.moc"
00027 
00028 #include <QPainter>
00029 #include <QStyle>
00030 #include <QMenuItem>
00031 #include <QDragLeaveEvent>
00032 #include <QDragMoveEvent>
00033 #include <QList>
00034 #include <QDragEnterEvent>
00035 #include <QMouseEvent>
00036 
00037 #include <kdebug.h>
00038 #include <kglobalsettings.h>
00039 
00040 #include <kmenu.h>
00041 
00042 //#include "utils.h"
00043 
00044 #if 0
00045 TaskMenuItem::TaskMenuItem(const QString &text,
00046                            bool active, bool minimized, bool attention)
00047   : QCustomMenuItem(),
00048     m_text(text),
00049     m_isActive(active),
00050     m_isMinimized(minimized),
00051     m_demandsAttention(attention),
00052     m_attentionState(true)
00053 {
00054 }
00055 
00056 TaskMenuItem::~TaskMenuItem()
00057 {
00058 }
00059 
00060 void TaskMenuItem::paint(QPainter *p, const QColorGroup &cg,
00061                          bool highlighted, bool /*enabled*/,
00062                          int x, int y, int w, int h )
00063 {
00064     if (m_isActive)
00065     {
00066         QFont font = p->font();
00067         font.setBold(true);
00068         p->setFont(font);
00069     }
00070 
00071     if (highlighted)
00072     {
00073         p->setPen(cg.highlightedText());
00074     }
00075     else if (m_isMinimized)
00076     {
00077         p->setPen(QPen(Plasma::blendColors(cg.background(), cg.text())));
00078     }
00079     else if (m_demandsAttention && !m_attentionState)
00080     {
00081         p->setPen(cg.mid());
00082     }
00083 
00084     p->drawText(x, y, w, h, Qt::AlignLeft|Qt::AlignVCenter|Qt::TextDontClip|Qt::TextShowMnemonic, m_text);
00085 }
00086 
00087 QSize TaskMenuItem::sizeHint()
00088 {
00089     QFont font = QFont();
00090     if (m_isActive)
00091     {
00092         font.setBold(true);
00093     }
00094     return QFontMetrics(font).size(AlignAuto|AlignVCenter|DontClip|ShowPrefix,
00095                                    m_text);
00096 }
00097 #endif
00098 
00099 /*****************************************************************************/
00100 
00101 TaskLMBMenu::TaskLMBMenu(const Task::List& tasks, QWidget *parent, const char *name)
00102   : QMenu(parent),
00103     m_tasks(tasks),
00104     m_lastDragId(-1),
00105     m_attentionState(false)
00106 {
00107     setObjectName(name);
00108     fillMenu();
00109 
00110     setAcceptDrops(true); // Always enabled to activate task during drag&drop.
00111 
00112     m_dragSwitchTimer = new QTimer(this, "DragSwitchTimer");
00113     m_dragSwitchTimer->setSingleShot(true);
00114     connect(m_dragSwitchTimer, SIGNAL(timeout()), SLOT(dragSwitch()));
00115 }
00116 
00117 void TaskLMBMenu::fillMenu()
00118 {
00119 
00120     Task::List::iterator itEnd = m_tasks.end();
00121     for (Task::List::iterator it = m_tasks.begin(); it != itEnd; ++it)
00122     {
00123         Task::TaskPtr t = (*it);
00124 
00125         QString text = t->visibleName().replace("&", "&&");
00126 
00127         //### KDE4
00128 /*        TaskMenuItem *menuItem = new TaskMenuItem(text,
00129                                                   t->isActive(),
00130                                                   t->isIconified(),
00131                                                   t->demandsAttention());*/
00132         //QAction* menuItem = 
00133         int id = insertItem(QIcon(t->pixmap()), text);
00134         connectItem(id, t.data(), SLOT(activateRaiseOrIconify()));
00135         setItemChecked(id, t->isActive());
00136 
00137         if (t->demandsAttention())
00138         {
00139             m_attentionState = true;
00140             m_attentionMap.append(actions().at(indexOf(id)));
00141         }
00142     }
00143 
00144     if (m_attentionState)
00145     {
00146         m_attentionTimer = new QTimer(this, "AttentionTimer");
00147         connect(m_attentionTimer, SIGNAL(timeout()), SLOT(attentionTimeout()));
00148         m_attentionTimer->setSingleShot(true);
00149         m_attentionTimer->start(500);
00150     }
00151 }
00152 
00153 void TaskLMBMenu::attentionTimeout()
00154 {
00155     m_attentionState = !m_attentionState;
00156 
00157     //### KDE4
00158 #if 0
00159     foreach (TaskMenuItem* item, m_attentionMap)
00160     {
00161         item->setAttentionState(m_attentionState);
00162     }
00163 #endif
00164 
00165     update();
00166 
00167     m_attentionTimer->start(500);
00168 }
00169 
00170 void TaskLMBMenu::dragEnterEvent( QDragEnterEvent* e )
00171 {
00172     // ignore task drags
00173     if (TaskDrag::canDecode(e->mimeData()))
00174     {
00175         return;
00176     }
00177 
00178     int id = static_cast<QMenuItem*>(actionAt(e->pos()))->id();
00179 
00180     if (id == -1)
00181     {
00182         m_dragSwitchTimer->stop();
00183         m_lastDragId = -1;
00184     }
00185     else if (id != m_lastDragId)
00186     {
00187         m_lastDragId = id;
00188         m_dragSwitchTimer->start(1000);
00189     }
00190 
00191     QMenu::dragEnterEvent( e );
00192 }
00193 
00194 void TaskLMBMenu::dragLeaveEvent( QDragLeaveEvent* e )
00195 {
00196     m_dragSwitchTimer->stop();
00197     m_lastDragId = -1;
00198 
00199     QMenu::dragLeaveEvent(e);
00200 
00201     hide();
00202 }
00203 
00204 void TaskLMBMenu::dragMoveEvent( QDragMoveEvent* e )
00205 {
00206     // ignore task drags
00207     if (TaskDrag::canDecode(e->mimeData()))
00208     {
00209         return;
00210     }
00211 
00212     int id = static_cast<QMenuItem*>(actionAt(e->pos()))->id();
00213 
00214     if (id == -1)
00215     {
00216         m_dragSwitchTimer->stop();
00217         m_lastDragId = -1;
00218     }
00219     else if (id != m_lastDragId)
00220     {
00221         m_lastDragId = id;
00222         m_dragSwitchTimer->start(1000);
00223     }
00224 
00225     QMenu::dragMoveEvent(e);
00226 }
00227 
00228 void TaskLMBMenu::dragSwitch()
00229 {
00230     Task::TaskPtr t = m_tasks.at(indexOf(m_lastDragId));
00231     if (t)
00232     {
00233         t->activate();
00234 
00235         for (unsigned int i = 0; i < count(); ++i)
00236         {
00237             setItemChecked(idAt(i), false );
00238         }
00239 
00240         setItemChecked( m_lastDragId, true );
00241     }
00242 }
00243 
00244 void TaskLMBMenu::mousePressEvent( QMouseEvent* e )
00245 {
00246     if (e->button() == Qt::LeftButton)
00247     {
00248         m_dragStartPos = e->pos();
00249     }
00250     else
00251     {
00252         m_dragStartPos = QPoint();
00253     }
00254 
00255     QMenu::mousePressEvent(e);
00256 }
00257 
00258 void TaskLMBMenu::mouseReleaseEvent(QMouseEvent* e)
00259 {
00260     m_dragStartPos = QPoint();
00261     QMenu::mouseReleaseEvent(e);
00262 }
00263 
00264 void TaskLMBMenu::mouseMoveEvent(QMouseEvent* e)
00265 {
00266     if (m_dragStartPos.isNull())
00267     {
00268         QMenu::mouseMoveEvent(e);
00269         return;
00270     }
00271 
00272     int delay = KGlobalSettings::dndEventDelay();
00273     QPoint newPos(e->pos());
00274 
00275     if ((m_dragStartPos - newPos).manhattanLength() > delay)
00276     {
00277         int index = actions().indexOf(actionAt(e->pos()));
00278         if (index != -1)
00279         {
00280             Task::TaskPtr task = m_tasks.at(index);
00281             if (task)
00282             {
00283                 Task::List tasks;
00284                 tasks.append(task);
00285                 TaskDrag* drag = new TaskDrag(tasks, this);
00286                 drag->setPixmap(task->pixmap());
00287                 drag->start();
00288             }
00289         }
00290     }
00291 
00292     QMenu::mouseMoveEvent(e);
00293 }
00294 

libtaskmanager

Skip menu "libtaskmanager"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal