Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / libs / taskmanager / old / tasklmbmenu.cpp
blobda87b3b1bc1d5c57fe8aa9d85fbd874db8ad3970
1 /*****************************************************************
3 Copyright (c) 2001 Matthias Elter <elter@kde.org>
4 Copyright (c) 2002 John Firebaugh <jfirebaugh@kde.org>
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 ******************************************************************/
25 #include "tasklmbmenu.h"
26 #include "tasklmbmenu.moc"
28 #include <QPainter>
29 #include <QStyle>
30 #include <QMenuItem>
31 #include <QDragLeaveEvent>
32 #include <QDragMoveEvent>
33 #include <QList>
34 #include <QDragEnterEvent>
35 #include <QMouseEvent>
37 #include <kdebug.h>
38 #include <kglobalsettings.h>
40 #include <kmenu.h>
42 //#include "utils.h"
44 #if 0
45 TaskMenuItem::TaskMenuItem(const QString &text,
46 bool active, bool minimized, bool attention)
47 : QCustomMenuItem(),
48 m_text(text),
49 m_isActive(active),
50 m_isMinimized(minimized),
51 m_demandsAttention(attention),
52 m_attentionState(true)
56 TaskMenuItem::~TaskMenuItem()
60 void TaskMenuItem::paint(QPainter *p, const QColorGroup &cg,
61 bool highlighted, bool /*enabled*/,
62 int x, int y, int w, int h )
64 if (m_isActive)
66 QFont font = p->font();
67 font.setBold(true);
68 p->setFont(font);
71 if (highlighted)
73 p->setPen(cg.highlightedText());
75 else if (m_isMinimized)
77 p->setPen(QPen(Plasma::blendColors(cg.background(), cg.text())));
79 else if (m_demandsAttention && !m_attentionState)
81 p->setPen(cg.mid());
84 p->drawText(x, y, w, h, Qt::AlignLeft|Qt::AlignVCenter|Qt::TextDontClip|Qt::TextShowMnemonic, m_text);
87 QSize TaskMenuItem::sizeHint()
89 QFont font = QFont();
90 if (m_isActive)
92 font.setBold(true);
94 return QFontMetrics(font).size(AlignAuto|AlignVCenter|DontClip|ShowPrefix,
95 m_text);
97 #endif
99 /*****************************************************************************/
101 TaskLMBMenu::TaskLMBMenu(const Task::List& tasks, QWidget *parent, const char *name)
102 : QMenu(parent),
103 m_tasks(tasks),
104 m_lastDragId(-1),
105 m_attentionState(false)
107 setObjectName(name);
108 fillMenu();
110 setAcceptDrops(true); // Always enabled to activate task during drag&drop.
112 m_dragSwitchTimer = new QTimer(this, "DragSwitchTimer");
113 m_dragSwitchTimer->setSingleShot(true);
114 connect(m_dragSwitchTimer, SIGNAL(timeout()), SLOT(dragSwitch()));
117 void TaskLMBMenu::fillMenu()
120 Task::List::iterator itEnd = m_tasks.end();
121 for (Task::List::iterator it = m_tasks.begin(); it != itEnd; ++it)
123 Task::TaskPtr t = (*it);
125 QString text = t->visibleName().replace("&", "&&");
127 //### KDE4
128 /* TaskMenuItem *menuItem = new TaskMenuItem(text,
129 t->isActive(),
130 t->isIconified(),
131 t->demandsAttention());*/
132 //QAction* menuItem =
133 int id = insertItem(QIcon(t->pixmap()), text);
134 connectItem(id, t.data(), SLOT(activateRaiseOrIconify()));
135 setItemChecked(id, t->isActive());
137 if (t->demandsAttention())
139 m_attentionState = true;
140 m_attentionMap.append(actions().at(indexOf(id)));
144 if (m_attentionState)
146 m_attentionTimer = new QTimer(this, "AttentionTimer");
147 connect(m_attentionTimer, SIGNAL(timeout()), SLOT(attentionTimeout()));
148 m_attentionTimer->setSingleShot(true);
149 m_attentionTimer->start(500);
153 void TaskLMBMenu::attentionTimeout()
155 m_attentionState = !m_attentionState;
157 //### KDE4
158 #if 0
159 foreach (TaskMenuItem* item, m_attentionMap)
161 item->setAttentionState(m_attentionState);
163 #endif
165 update();
167 m_attentionTimer->start(500);
170 void TaskLMBMenu::dragEnterEvent( QDragEnterEvent* e )
172 // ignore task drags
173 if (TaskDrag::canDecode(e->mimeData()))
175 return;
178 int id = static_cast<QMenuItem*>(actionAt(e->pos()))->id();
180 if (id == -1)
182 m_dragSwitchTimer->stop();
183 m_lastDragId = -1;
185 else if (id != m_lastDragId)
187 m_lastDragId = id;
188 m_dragSwitchTimer->start(1000);
191 QMenu::dragEnterEvent( e );
194 void TaskLMBMenu::dragLeaveEvent( QDragLeaveEvent* e )
196 m_dragSwitchTimer->stop();
197 m_lastDragId = -1;
199 QMenu::dragLeaveEvent(e);
201 hide();
204 void TaskLMBMenu::dragMoveEvent( QDragMoveEvent* e )
206 // ignore task drags
207 if (TaskDrag::canDecode(e->mimeData()))
209 return;
212 int id = static_cast<QMenuItem*>(actionAt(e->pos()))->id();
214 if (id == -1)
216 m_dragSwitchTimer->stop();
217 m_lastDragId = -1;
219 else if (id != m_lastDragId)
221 m_lastDragId = id;
222 m_dragSwitchTimer->start(1000);
225 QMenu::dragMoveEvent(e);
228 void TaskLMBMenu::dragSwitch()
230 Task::TaskPtr t = m_tasks.at(indexOf(m_lastDragId));
231 if (t)
233 t->activate();
235 for (unsigned int i = 0; i < count(); ++i)
237 setItemChecked(idAt(i), false );
240 setItemChecked( m_lastDragId, true );
244 void TaskLMBMenu::mousePressEvent( QMouseEvent* e )
246 if (e->button() == Qt::LeftButton)
248 m_dragStartPos = e->pos();
250 else
252 m_dragStartPos = QPoint();
255 QMenu::mousePressEvent(e);
258 void TaskLMBMenu::mouseReleaseEvent(QMouseEvent* e)
260 m_dragStartPos = QPoint();
261 QMenu::mouseReleaseEvent(e);
264 void TaskLMBMenu::mouseMoveEvent(QMouseEvent* e)
266 if (m_dragStartPos.isNull())
268 QMenu::mouseMoveEvent(e);
269 return;
272 int delay = KGlobalSettings::dndEventDelay();
273 QPoint newPos(e->pos());
275 if ((m_dragStartPos - newPos).manhattanLength() > delay)
277 int index = actions().indexOf(actionAt(e->pos()));
278 if (index != -1)
280 Task::TaskPtr task = m_tasks.at(index);
281 if (task)
283 Task::List tasks;
284 tasks.append(task);
285 TaskDrag* drag = new TaskDrag(tasks, this);
286 drag->setPixmap(task->pixmap());
287 drag->start();
292 QMenu::mouseMoveEvent(e);