1 /***************************************************************************
2 * Copyright (C) 2007 by Robert Knight <robertknight@gmail.com> *
3 * Copyright (C) 2008 by Alexis Ménard <darktears31@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"
23 #include "taskgroupitem.h"
26 #include <QGraphicsSceneContextMenuEvent>
27 #include <QStyleOptionGraphicsItem>
28 #include <QGraphicsView>
30 #include <QApplication>
33 #include <KAuthorized>
36 #include <KLocalizedString>
37 #include <KGlobalSettings>
38 #include <KIconLoader>
40 #include <taskmanager/taskactions.h>
41 #include <taskmanager/task.h>
42 #include <taskmanager/taskmanager.h>
43 #include <taskmanager/taskgroup.h>
45 #include <Plasma/Theme>
46 #include <Plasma/FrameSvg>
47 #include <Plasma/ToolTipManager>
48 #include <Plasma/Corona>
49 #include <Plasma/Containment>
53 WindowTaskItem::WindowTaskItem(QGraphicsWidget
*parent
, Tasks
*applet
, const bool showTooltip
)
54 : AbstractTaskItem(parent
, applet
, showTooltip
),
60 void WindowTaskItem::activate()
62 // the Task class has a method called activateRaiseOrIconify() which
63 // should perform the required action here.
65 // however it currently does not minimize the task's window if the item
66 // is clicked whilst the window is active probably because the active window by
67 // the time the mouse is released over the window task button is not the
68 // task's window but instead the desktop window
70 // TODO: the Kicker panel in KDE 3.x has a feature whereby clicking on it
71 // does not take away the focus from the active window (unless clicking
72 // in a widget such as a line edit which does accept the focus)
73 // this needs to be implemented for Plasma's own panels.
76 m_task
->task()->activateRaiseOrIconify();
77 // emit windowSelected(this);
81 void WindowTaskItem::mousePressEvent(QGraphicsSceneMouseEvent
*event
)
83 if (event
->buttons() & Qt::MidButton
) {
85 parentGroup()->collapse();
88 AbstractTaskItem::mousePressEvent(event
);
95 void WindowTaskItem::close()
101 void WindowTaskItem::publishIconGeometry() const
107 QRect rect
= iconGeometry();
108 m_task
->task()->publishIconGeometry(rect
);
111 void WindowTaskItem::publishIconGeometry(const QRect
&rect
) const
114 m_task
->task()->publishIconGeometry(rect
);
118 void WindowTaskItem::updateTask(::TaskManager::TaskChanges changes
)
125 bool needsUpdate
= false;
126 TaskFlags flags
= m_flags
;
127 if (m_task
->isActive()) {
128 flags
|= TaskHasFocus
;
129 emit
activated(this);
131 flags
&= ~TaskHasFocus
;
134 if (m_task
->demandsAttention()) {
135 flags
|= TaskWantsAttention
;
137 flags
&= ~TaskWantsAttention
;
140 if (m_task
->isMinimized()) {
141 flags
|= TaskIsMinimized
;
143 flags
&= ~TaskIsMinimized
;
146 if (m_flags
!= flags
) {
151 // basic title and icon
152 if (changes
& TaskManager::IconChanged
) {
154 setIcon(m_task
->icon());
157 if (changes
& TaskManager::NameChanged
) {
159 setText(m_task
->name());
162 if (m_showingTooltip
&&
163 (changes
& TaskManager::IconChanged
||
164 changes
& TaskManager::NameChanged
||
165 changes
& TaskManager::DesktopChanged
)) {
171 //kDebug() << m_task->name();
176 void WindowTaskItem::updateToolTip()
182 Plasma::ToolTipContent
data(m_task
->name(),
183 i18nc("Which virtual desktop a window is currently on", "On %1",
184 KWindowSystem::desktopName(m_task
->desktop())),
185 m_task
->task()->icon(KIconLoader::SizeSmall
, KIconLoader::SizeSmall
, false));
186 data
.setWindowToPreview(m_task
->task()->window());
188 Plasma::ToolTipManager::self()->setContent(this, data
);
191 void WindowTaskItem::setStartupTask(TaskItem
*task
)
194 if (!task
->startup()) {
198 m_abstractItem
= qobject_cast
<TaskManager::AbstractGroupableItem
*>(task
);
199 if (!m_abstractItem
) {
202 connect(task
, SIGNAL(gotTaskPointer()), this, SLOT(gotTaskPointer()));
203 setText(task
->startup()->text());
204 setIcon(KIcon(task
->startup()->icon()));
207 void WindowTaskItem::gotTaskPointer()
210 TaskManager::TaskItem
*item
= qobject_cast
<TaskManager::TaskItem
*>(sender());
217 void WindowTaskItem::setWindowTask(TaskManager::TaskItem
* taskItem
)
220 disconnect(m_task
->task().constData(), 0, this, 0);
223 m_abstractItem
= qobject_cast
<TaskManager::AbstractGroupableItem
*>(taskItem
);
224 if (!m_abstractItem
) {
228 connect(m_task
, SIGNAL(changed(::TaskManager::TaskChanges
)),
229 this, SLOT(updateTask(::TaskManager::TaskChanges
)));
231 updateTask(::TaskManager::EverythingChanged
);
232 publishIconGeometry();
234 //kDebug() << "Task added, isActive = " << task->isActive();
237 void WindowTaskItem::setTask(TaskManager::TaskItem
* taskItem
)
239 if (!taskItem
->startup() && !taskItem
->task()) {
244 if (!taskItem
->task()) {
245 setStartupTask(taskItem
);
247 setWindowTask(taskItem
);
252 TaskManager::TaskPtr
WindowTaskItem::windowTask() const
254 return m_task
->task();
257 void WindowTaskItem::contextMenuEvent(QGraphicsSceneContextMenuEvent
*e
)
259 if (!KAuthorized::authorizeKAction("kwin_rmb") || !m_task
) {
260 QGraphicsWidget::contextMenuEvent(e
);
264 QList
<QAction
*> actionList
;
266 if (m_task
->isGrouped()) {
267 a
= new QAction(i18n("Collapse Parent Group"), this);
268 actionList
.append(a
);
269 //connect(a, SIGNAL(triggered()), m_applet->groupItem(m_task->parentGroup()), SLOT(collapse())); FIXME
272 TaskManager::BasicMenu
menu(0, m_task
, &m_applet
->groupManager(), actionList
);
274 Q_ASSERT(m_applet
->containment());
275 Q_ASSERT(m_applet
->containment()->corona());
276 menu
.exec(m_applet
->containment()->corona()->popupPosition(this, menu
.size()));
282 bool WindowTaskItem::isWindowItem() const
287 bool WindowTaskItem::isActive() const
290 //kDebug() << "no task set";
293 return m_task
->isActive();
296 void WindowTaskItem::setAdditionalMimeData(QMimeData
* mimeData
)
298 m_task
->addMimeData(mimeData
);
301 #include "windowtaskitem.moc"