not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / applets / tasks / windowtaskitem.cpp
blobaaaabbd6f0184da89bda17446b5d5cda2d7cbd5e
1 /***************************************************************************
2 * Copyright (C) 2007 by Robert Knight <robertknight@gmail.com> *
3 * Copyright (C) 2008 by Alexis Ménard <darktears31@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"
23 #include "taskgroupitem.h"
25 // Qt
26 #include <QGraphicsSceneContextMenuEvent>
27 #include <QStyleOptionGraphicsItem>
28 #include <QGraphicsView>
29 #include <QTimer>
30 #include <QApplication>
32 // KDE
33 #include <KAuthorized>
34 #include <KDebug>
35 #include <KIcon>
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>
51 #include "tasks.h"
53 WindowTaskItem::WindowTaskItem(QGraphicsWidget *parent, Tasks *applet, const bool showTooltip)
54 : AbstractTaskItem(parent, applet, showTooltip),
55 m_task(0)
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.
74 //kDebug();
75 if (m_task) {
76 m_task->task()->activateRaiseOrIconify();
77 // emit windowSelected(this);
81 void WindowTaskItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
83 if (event->buttons() & Qt::MidButton) {
84 if (isGrouped()) {
85 parentGroup()->collapse();
87 } else {
88 AbstractTaskItem::mousePressEvent(event);
91 event->accept();
94 //destroy this item
95 void WindowTaskItem::close()
97 //kDebug();
98 m_task = 0;
101 void WindowTaskItem::publishIconGeometry() const
103 if (!m_task) {
104 return;
107 QRect rect = iconGeometry();
108 m_task->task()->publishIconGeometry(rect);
111 void WindowTaskItem::publishIconGeometry(const QRect &rect) const
113 if (m_task) {
114 m_task->task()->publishIconGeometry(rect);
118 void WindowTaskItem::updateTask(::TaskManager::TaskChanges changes)
120 if (!m_task) {
121 return;
124 // task flags
125 bool needsUpdate = false;
126 TaskFlags flags = m_flags;
127 if (m_task->isActive()) {
128 flags |= TaskHasFocus;
129 emit activated(this);
130 } else {
131 flags &= ~TaskHasFocus;
134 if (m_task->demandsAttention()) {
135 flags |= TaskWantsAttention;
136 } else {
137 flags &= ~TaskWantsAttention;
140 if (m_task->isMinimized()) {
141 flags |= TaskIsMinimized;
142 } else {
143 flags &= ~TaskIsMinimized;
146 if (m_flags != flags) {
147 needsUpdate = true;
148 setTaskFlags(flags);
151 // basic title and icon
152 if (changes & TaskManager::IconChanged) {
153 needsUpdate = true;
154 setIcon(m_task->icon());
157 if (changes & TaskManager::NameChanged) {
158 needsUpdate = true;
159 setText(m_task->name());
162 if (m_showingTooltip &&
163 (changes & TaskManager::IconChanged ||
164 changes & TaskManager::NameChanged ||
165 changes & TaskManager::DesktopChanged)) {
166 updateToolTip();
169 if (needsUpdate) {
170 //redraw
171 //kDebug() << m_task->name();
172 queueUpdate();
176 void WindowTaskItem::updateToolTip()
178 if (!m_task) {
179 return;
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)
193 //kDebug();
194 if (!task->startup()) {
195 kDebug() << "Error";
196 return;
198 m_abstractItem = qobject_cast<TaskManager::AbstractGroupableItem *>(task);
199 if (!m_abstractItem) {
200 kDebug() << "error";
202 connect(task, SIGNAL(gotTaskPointer()), this, SLOT(gotTaskPointer()));
203 setText(task->startup()->text());
204 setIcon(KIcon(task->startup()->icon()));
207 void WindowTaskItem::gotTaskPointer()
209 //kDebug();
210 TaskManager::TaskItem *item = qobject_cast<TaskManager::TaskItem*>(sender());
211 if (item) {
212 setWindowTask(item);
217 void WindowTaskItem::setWindowTask(TaskManager::TaskItem* taskItem)
219 if (m_task) {
220 disconnect(m_task->task().constData(), 0, this, 0);
222 m_task = taskItem;
223 m_abstractItem = qobject_cast<TaskManager::AbstractGroupableItem *>(taskItem);
224 if (!m_abstractItem) {
225 kDebug() << "error";
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()) {
240 kDebug() << "Error";
241 return;
244 if (!taskItem->task()) {
245 setStartupTask(taskItem);
246 } else {
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);
261 return;
264 QList <QAction*> actionList;
265 QAction *a(0);
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);
273 menu.adjustSize();
274 Q_ASSERT(m_applet->containment());
275 Q_ASSERT(m_applet->containment()->corona());
276 menu.exec(m_applet->containment()->corona()->popupPosition(this, menu.size()));
277 delete a;
282 bool WindowTaskItem::isWindowItem() const
284 return true;
287 bool WindowTaskItem::isActive() const
289 if (!m_task) {
290 //kDebug() << "no task set";
291 return false;
293 return m_task->isActive();
296 void WindowTaskItem::setAdditionalMimeData(QMimeData* mimeData)
298 m_task->addMimeData(mimeData);
301 #include "windowtaskitem.moc"