Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / plasma / applets / tasks / windowtaskitem.cpp
blobbfbbe8e69cff27f45eec26857117f55dc20cad00
1 /***************************************************************************
2 * Copyright (C) 2007 by Robert Knight *
3 * robertknight@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
19 ***************************************************************************/
21 // Own
22 #include "windowtaskitem.h"
24 // Qt
25 #include <QGraphicsSceneContextMenuEvent>
26 #include <QGraphicsView>
27 #include <QTimer>
29 // KDE
30 #include <KAuthorized>
31 #include <KDebug>
32 #include <KIcon>
33 #include <KLocalizedString>
34 #include <taskmanager/taskrmbmenu.h>
36 WindowTaskItem::WindowTaskItem(QGraphicsItem *parent, QObject *parentObject, const bool showTooltip)
37 : AbstractTaskItem(parent, parentObject),
38 _activateTimer(0)
40 setAcceptDrops(true);
41 _showTooltip = showTooltip;
44 void WindowTaskItem::activate()
46 // the Task class has a method called activateRaiseOrIconify() which
47 // should perform the required action here.
49 // however it currently does not minimize the task's window if the item
50 // is clicked whilst the window is active probably because the active window by
51 // the time the mouse is released over the window task button is not the
52 // task's window but instead the desktop window
54 // TODO: the Kicker panel in KDE 3.x has a feature whereby clicking on it
55 // does not take away the focus from the active window (unless clicking
56 // in a widget such as a line edit which does accept the focus)
57 // this needs to be implemented for Plasma's own panels.
58 if (_task) {
59 _task->activateRaiseOrIconify();
60 emit windowSelected(this);
64 void WindowTaskItem::close()
66 if (_task) {
67 _task->close();
69 finished();
72 void WindowTaskItem::setShowTooltip(const bool showit)
74 _showTooltip = showit;
75 updateTask();
78 void WindowTaskItem::updateTask()
80 Q_ASSERT( _task );
82 // task flags
83 if (_task->isActive()) {
84 setTaskFlags(taskFlags() | TaskHasFocus);
85 emit activated(this);
86 } else {
87 setTaskFlags(taskFlags() & ~TaskHasFocus);
90 if (_task->demandsAttention()) {
91 setTaskFlags(taskFlags() | TaskWantsAttention);
92 } else {
93 setTaskFlags(taskFlags() & ~TaskWantsAttention);
96 // basic title and icon
97 QPixmap iconPixmap = _task->icon(preferredIconSize().width(),
98 preferredIconSize().height(),
99 true);
100 if (_showTooltip) {
101 Plasma::ToolTipData data;
102 data.mainText = _task->visibleName();
103 data.subText = i18nc("Which virtual desktop a window is currently on", "On %1", KWindowSystem::desktopName(_task->desktop()));
104 data.image = iconPixmap;
105 data.windowToPreview = _task->window();
106 setToolTip(data);
107 } else {
108 Plasma::ToolTipData data;
109 setToolTip(data); // Clear
111 setIcon(QIcon(iconPixmap));
112 setText(_task->visibleName());
113 //redraw
114 queueUpdate();
117 void WindowTaskItem::setStartupTask(TaskManager::StartupPtr task)
119 setText(task->text());
120 setIcon(KIcon(task->icon()));
122 if (_showTooltip) {
123 Plasma::ToolTipData tip;
124 tip.mainText = task->text();
125 tip.image = task->icon();
126 setToolTip(tip);
130 void WindowTaskItem::setWindowTask(TaskManager::TaskPtr task)
132 if (_task)
133 disconnect(_task.constData(), 0, this, 0);
135 _task = task;
137 connect(task.constData(), SIGNAL(changed()),
138 this, SLOT(updateTask()));
139 connect(task.constData(), SIGNAL(iconChanged()),
140 this, SLOT(updateTask()));
142 updateTask();
143 publishIconGeometry();
145 //kDebug() << "Task added, isActive = " << task->isActive();
148 TaskManager::TaskPtr WindowTaskItem::windowTask() const
150 return _task;
153 void WindowTaskItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *e)
155 if (!KAuthorized::authorizeKAction("kwin_rmb") || _task.isNull()) {
156 AbstractTaskItem::contextMenuEvent(e);
157 return;
160 TaskManager::TaskRMBMenu menu(_task);
161 menu.exec(e->screenPos());
164 void WindowTaskItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
166 event->accept();
167 if (!_activateTimer) {
168 _activateTimer = new QTimer();
169 _activateTimer->setSingleShot(true);
170 _activateTimer->setInterval(300);
171 connect(_activateTimer, SIGNAL(timeout()), this, SLOT(activate()));
173 _activateTimer->start();
176 void WindowTaskItem::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
178 Q_UNUSED(event);
180 // restart the timer so that activate() is only called after the mouse
181 // stops moving
182 _activateTimer->start();
185 void WindowTaskItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
187 Q_UNUSED(event);
189 delete _activateTimer;
190 _activateTimer = 0;
193 void WindowTaskItem::setGeometry(const QRectF& geometry)
195 AbstractTaskItem::setGeometry(geometry);
196 publishIconGeometry();
199 void WindowTaskItem::publishIconGeometry()
201 QGraphicsView *parentView = view();
202 if (!parentView || !_task) {
203 return;
205 QRect rect = mapToView(parentView, boundingRect());
206 rect.moveTopLeft(parentView->mapToGlobal(rect.topLeft()));
207 _task->publishIconGeometry(rect);
210 #include "windowtaskitem.moc"