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"
31 #include <QDragLeaveEvent>
32 #include <QDragMoveEvent>
34 #include <QDragEnterEvent>
35 #include <QMouseEvent>
38 #include <kglobalsettings.h>
45 TaskMenuItem::TaskMenuItem(const QString
&text
,
46 bool active
, bool minimized
, bool attention
)
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
)
66 QFont font
= p
->font();
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
)
84 p
->drawText(x
, y
, w
, h
, Qt::AlignLeft
|Qt::AlignVCenter
|Qt::TextDontClip
|Qt::TextShowMnemonic
, m_text
);
87 QSize
TaskMenuItem::sizeHint()
94 return QFontMetrics(font
).size(AlignAuto
|AlignVCenter
|DontClip
|ShowPrefix
,
99 /*****************************************************************************/
101 TaskLMBMenu::TaskLMBMenu(const Task::List
& tasks
, QWidget
*parent
, const char *name
)
105 m_attentionState(false)
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("&", "&&");
128 /* TaskMenuItem *menuItem = new TaskMenuItem(text,
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
;
159 foreach (TaskMenuItem
* item
, m_attentionMap
)
161 item
->setAttentionState(m_attentionState
);
167 m_attentionTimer
->start(500);
170 void TaskLMBMenu::dragEnterEvent( QDragEnterEvent
* e
)
173 if (TaskDrag::canDecode(e
->mimeData()))
178 int id
= static_cast<QMenuItem
*>(actionAt(e
->pos()))->id();
182 m_dragSwitchTimer
->stop();
185 else if (id
!= m_lastDragId
)
188 m_dragSwitchTimer
->start(1000);
191 QMenu::dragEnterEvent( e
);
194 void TaskLMBMenu::dragLeaveEvent( QDragLeaveEvent
* e
)
196 m_dragSwitchTimer
->stop();
199 QMenu::dragLeaveEvent(e
);
204 void TaskLMBMenu::dragMoveEvent( QDragMoveEvent
* e
)
207 if (TaskDrag::canDecode(e
->mimeData()))
212 int id
= static_cast<QMenuItem
*>(actionAt(e
->pos()))->id();
216 m_dragSwitchTimer
->stop();
219 else if (id
!= m_lastDragId
)
222 m_dragSwitchTimer
->start(1000);
225 QMenu::dragMoveEvent(e
);
228 void TaskLMBMenu::dragSwitch()
230 Task::TaskPtr t
= m_tasks
.at(indexOf(m_lastDragId
));
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();
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
);
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()));
280 Task::TaskPtr task
= m_tasks
.at(index
);
285 TaskDrag
* drag
= new TaskDrag(tasks
, this);
286 drag
->setPixmap(task
->pixmap());
292 QMenu::mouseMoveEvent(e
);