1 /***************************************************************************
2 * Copyright (C) 2007 by Robert Knight *
3 * robertknight@gmail.com *
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. *
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. *
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 ***************************************************************************/
22 #include "windowtaskitem.h"
25 #include <QGraphicsSceneContextMenuEvent>
26 #include <QGraphicsView>
30 #include <KAuthorized>
33 #include <KLocalizedString>
34 #include <taskmanager/taskrmbmenu.h>
36 WindowTaskItem::WindowTaskItem(QGraphicsItem
*parent
, QObject
*parentObject
, const bool showTooltip
)
37 : AbstractTaskItem(parent
, parentObject
),
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.
59 _task
->activateRaiseOrIconify();
60 emit
windowSelected(this);
64 void WindowTaskItem::close()
72 void WindowTaskItem::setShowTooltip(const bool showit
)
74 _showTooltip
= showit
;
78 void WindowTaskItem::updateTask()
83 if (_task
->isActive()) {
84 setTaskFlags(taskFlags() | TaskHasFocus
);
87 setTaskFlags(taskFlags() & ~TaskHasFocus
);
90 if (_task
->demandsAttention()) {
91 setTaskFlags(taskFlags() | TaskWantsAttention
);
93 setTaskFlags(taskFlags() & ~TaskWantsAttention
);
96 // basic title and icon
97 QPixmap iconPixmap
= _task
->icon(preferredIconSize().width(),
98 preferredIconSize().height(),
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();
108 Plasma::ToolTipData data
;
109 setToolTip(data
); // Clear
111 setIcon(QIcon(iconPixmap
));
112 setText(_task
->visibleName());
117 void WindowTaskItem::setStartupTask(TaskManager::StartupPtr task
)
119 setText(task
->text());
120 setIcon(KIcon(task
->icon()));
123 Plasma::ToolTipData tip
;
124 tip
.mainText
= task
->text();
125 tip
.image
= task
->icon();
130 void WindowTaskItem::setWindowTask(TaskManager::TaskPtr task
)
133 disconnect(_task
.constData(), 0, this, 0);
137 connect(task
.constData(), SIGNAL(changed()),
138 this, SLOT(updateTask()));
139 connect(task
.constData(), SIGNAL(iconChanged()),
140 this, SLOT(updateTask()));
143 publishIconGeometry();
145 //kDebug() << "Task added, isActive = " << task->isActive();
148 TaskManager::TaskPtr
WindowTaskItem::windowTask() const
153 void WindowTaskItem::contextMenuEvent(QGraphicsSceneContextMenuEvent
*e
)
155 if (!KAuthorized::authorizeKAction("kwin_rmb") || _task
.isNull()) {
156 AbstractTaskItem::contextMenuEvent(e
);
160 TaskManager::TaskRMBMenu
menu(_task
);
161 menu
.exec(e
->screenPos());
164 void WindowTaskItem::dragEnterEvent(QGraphicsSceneDragDropEvent
*event
)
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
)
180 // restart the timer so that activate() is only called after the mouse
182 _activateTimer
->start();
185 void WindowTaskItem::dragLeaveEvent(QGraphicsSceneDragDropEvent
*event
)
189 delete _activateTimer
;
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
) {
205 QRect rect
= mapToView(parentView
, boundingRect());
206 rect
.moveTopLeft(parentView
->mapToGlobal(rect
.topLeft()));
207 _task
->publishIconGeometry(rect
);
210 #include "windowtaskitem.moc"