Add (and install) svg for the new krunner interface.
[kdebase/uwolfer.git] / workspace / plasma / applets / tasks / tasks.cpp
bloba4a25c7a8bec3e4d93ca557464653c060481a824
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 "tasks.h"
23 #include "taskgroupitem.h"
24 #include "windowtaskitem.h"
25 #include "ui_tasksConfig.h"
27 // KDE
28 #include <KDialog>
30 // Qt
31 #include <QGraphicsSceneWheelEvent>
32 #include <QTimeLine>
34 // Plasma
35 #include <plasma/containment.h>
36 #include <plasma/theme.h>
37 #include <plasma/layouts/boxlayout.h>
38 #include <plasma/layouts/layoutanimator.h>
40 Tasks::Tasks(QObject* parent, const QVariantList &arguments)
41 : Plasma::Applet(parent, arguments),
42 m_dialog(0)
44 setHasConfigurationInterface(true);
45 setAspectRatioMode(Qt::IgnoreAspectRatio);
46 setContentSize(500, 48);
48 m_screenTimer.setSingleShot(true);
49 m_screenTimer.setInterval(300);
50 connect(&m_screenTimer, SIGNAL(timeout()), this, SLOT(checkScreenChange()));
51 connect(Plasma::Theme::self(), SIGNAL(changed()), this, SLOT(themeRefresh()));
54 Tasks::~Tasks()
56 delete m_dialog;
59 void Tasks::init()
61 Plasma::BoxLayout *layout = new Plasma::BoxLayout(Plasma::BoxLayout::LeftToRight, this);
62 layout->setMargin(0);
63 m_rootTaskGroup = new TaskGroupItem(this, this);
64 m_rootTaskGroup->resize(contentSize());
65 connect(m_rootTaskGroup, SIGNAL(activated(AbstractTaskItem*)),
66 this, SLOT(launchActivated()));
68 // set up the animator used in the root item
69 m_animator = new Plasma::LayoutAnimator(this);
70 m_animator->setAutoDeleteOnRemoval(true);
71 m_animator->setEffect(Plasma::LayoutAnimator::InsertedState,
72 Plasma::LayoutAnimator::FadeEffect);
73 m_animator->setEffect(Plasma::LayoutAnimator::StandardState,
74 Plasma::LayoutAnimator::MoveEffect);
75 m_animator->setEffect(Plasma::LayoutAnimator::RemovedState,
76 Plasma::LayoutAnimator::FadeEffect);
77 m_animator->setTimeLine(new QTimeLine(100, this));
79 layout->addItem(m_rootTaskGroup);
81 m_rootTaskGroup->setBorderStyle(TaskGroupItem::NoBorder);
82 // m_rootTaskGroup->setColor( QColor(100,120,130) );
83 m_rootTaskGroup->setText("Root Group");
85 KConfigGroup cg = config();
86 m_showTooltip = cg.readEntry("showTooltip", true);
87 m_showOnlyCurrentDesktop = cg.readEntry("showOnlyCurrentDesktop", false);
88 m_showOnlyCurrentScreen = cg.readEntry("showOnlyCurrentScreen", false);
90 reconnect();
92 // listen for addition and removal of window tasks
93 connect(TaskManager::TaskManager::self(), SIGNAL(taskAdded(TaskPtr)),
94 this, SLOT(addWindowTask(TaskPtr)));
95 connect(TaskManager::TaskManager::self(), SIGNAL(taskRemoved(TaskPtr)),
96 this, SLOT(removeWindowTask(TaskPtr)));
98 // listen for addition and removal of starting tasks
99 connect(TaskManager::TaskManager::self(), SIGNAL(startupAdded(StartupPtr)),
100 this, SLOT(addStartingTask(StartupPtr)));
101 connect(TaskManager::TaskManager::self(), SIGNAL(startupRemoved(StartupPtr)),
102 this, SLOT(removeStartingTask(StartupPtr)));
104 // add the animator once we're initialized to avoid animating like mad on start up
105 m_rootTaskGroup->layout()->setAnimator(m_animator);
108 void Tasks::addStartingTask(StartupPtr task)
110 WindowTaskItem* item = new WindowTaskItem(m_rootTaskGroup, m_rootTaskGroup, m_showTooltip);
111 item->setStartupTask(task);
112 m_startupTaskItems.insert(task, item);
114 addItemToRootGroup(item);
117 void Tasks::removeStartingTask(StartupPtr task)
119 if (m_startupTaskItems.contains(task)) {
120 AbstractTaskItem *item = m_startupTaskItems.take(task);
121 removeItemFromRootGroup(item);
125 void Tasks::registerWindowTasks()
127 TaskManager::TaskManager *manager = TaskManager::TaskManager::self();
129 TaskManager::TaskDict tasks = manager->tasks();
130 QMapIterator<WId,TaskPtr> iter(tasks);
132 while (iter.hasNext()) {
133 iter.next();
134 addWindowTask(iter.value());
138 void Tasks::addItemToRootGroup(AbstractTaskItem *item)
140 item->setFlag(QGraphicsItem::ItemIsSelectable);
141 m_rootTaskGroup->insertTask(item);
144 void Tasks::removeItemFromRootGroup(AbstractTaskItem *item)
146 Q_ASSERT( item );
148 m_rootTaskGroup->removeTask(item);
150 // TEMPORARY
151 // scene()->removeItem(item);
152 // item->deleteLater();
155 void Tasks::addWindowTask(TaskPtr task)
157 if (!task->showInTaskbar()) {
158 return;
161 if (m_showOnlyCurrentDesktop && !task->isOnCurrentDesktop()) {
162 return;
164 if (m_showOnlyCurrentScreen && !isOnMyScreen(task)) {
165 return;
168 WindowTaskItem *item = 0;
169 foreach (StartupPtr startup, m_startupTaskItems.keys()) {
170 if (startup->matchesWindow(task->window())) {
171 item = dynamic_cast<WindowTaskItem *>(m_startupTaskItems.take(startup));
172 break;
176 if (!item) {
177 item = new WindowTaskItem(m_rootTaskGroup, m_rootTaskGroup, m_showTooltip);
180 item->setWindowTask(task);
181 m_windowTaskItems.insert(task, item);
183 addItemToRootGroup(item);
186 void Tasks::removeWindowTask(TaskPtr task)
188 if (m_windowTaskItems.contains(task)) {
189 AbstractTaskItem *item = m_windowTaskItems.take(task);
190 removeItemFromRootGroup(item);
194 void Tasks::removeAllWindowTasks()
196 while (!m_windowTaskItems.isEmpty()) {
197 removeItemFromRootGroup(m_windowTaskItems.take(m_windowTaskItems.constBegin().key()));
201 void Tasks::constraintsUpdated(Plasma::Constraints constraints)
203 if (constraints & Plasma::LocationConstraint) {
204 if (formFactor() == Plasma::Vertical) {
205 m_rootTaskGroup->setDirection(Plasma::BoxLayout::TopToBottom);
206 } else {
207 m_rootTaskGroup->setDirection(Plasma::BoxLayout::LeftToRight);
210 foreach (AbstractTaskItem *taskItem, m_windowTaskItems) {
211 WindowTaskItem *windowTaskItem = dynamic_cast<WindowTaskItem *>(taskItem);
212 if (windowTaskItem) {
213 windowTaskItem->publishIconGeometry();
219 void Tasks::wheelEvent(QGraphicsSceneWheelEvent *e)
221 m_rootTaskGroup->cycle(e->delta());
224 void Tasks::currentDesktopChanged(int)
226 if (!m_showOnlyCurrentDesktop) {
227 return;
230 removeAllWindowTasks();
231 registerWindowTasks();
234 void Tasks::taskMovedDesktop(TaskPtr task)
236 if (!m_showOnlyCurrentDesktop) {
237 return;
240 if (!task->isOnCurrentDesktop()) {
241 removeWindowTask(task);
242 } else if (!m_windowTaskItems.contains(task)) {
243 addWindowTask(task);
247 void Tasks::windowChangedGeometry(TaskPtr task)
249 if (!m_tasks.contains(task)) {
250 m_tasks.append(task);
252 if (!m_screenTimer.isActive()) {
253 m_screenTimer.start();
257 void Tasks::checkScreenChange()
259 foreach (TaskPtr task, m_tasks) {
260 if (!isOnMyScreen(task)) {
261 removeWindowTask(task);
262 } else if (!m_windowTaskItems.contains(task)) {
263 addWindowTask(task);
266 m_tasks.clear();
269 bool Tasks::isOnMyScreen(TaskPtr task)
271 Plasma::Containment* appletContainment = containment();
273 if (appletContainment) {
274 if (appletContainment->screen() != -1) {
275 if (!TaskManager::TaskManager::isOnScreen(appletContainment->screen(),
276 task->window())) {
277 return false;
281 return true;
284 void Tasks::showConfigurationInterface()
286 if (m_dialog == 0) {
287 m_dialog = new KDialog;
288 m_dialog->setCaption(i18n("Configure Taskbar"));
290 QWidget *widget = new QWidget;
291 m_ui.setupUi(widget);
292 m_dialog->setMainWidget(widget);
293 m_dialog->setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
295 connect(m_dialog, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
296 connect(m_dialog, SIGNAL(okClicked()), this, SLOT(configAccepted()));
299 m_ui.showTooltip->setChecked(m_showTooltip);
300 m_ui.showOnlyCurrentDesktop->setChecked(m_showOnlyCurrentDesktop);
301 m_ui.showOnlyCurrentScreen->setChecked(m_showOnlyCurrentScreen);
302 m_dialog->show();
305 void Tasks::configAccepted()
307 bool changed = false;
309 if (m_showOnlyCurrentDesktop != (m_ui.showOnlyCurrentDesktop->isChecked())) {
310 m_showOnlyCurrentDesktop = !m_showOnlyCurrentDesktop;
311 KConfigGroup cg = config();
312 cg.writeEntry("showOnlyCurrentDesktop", m_showOnlyCurrentDesktop);
313 changed = true;
315 if (m_showOnlyCurrentScreen != (m_ui.showOnlyCurrentScreen->isChecked())) {
316 m_showOnlyCurrentScreen = !m_showOnlyCurrentScreen;
317 KConfigGroup cg = config();
318 cg.writeEntry("showOnlyCurrentScreen", m_showOnlyCurrentScreen);
319 changed = true;
322 if (changed) {
323 reconnect();
326 if (m_showTooltip != (m_ui.showTooltip->checkState() == Qt::Checked)) {
327 m_showTooltip = !m_showTooltip;
328 foreach (AbstractTaskItem *taskItem, m_windowTaskItems) {
329 WindowTaskItem *windowTaskItem = dynamic_cast<WindowTaskItem *>(taskItem);
330 if (windowTaskItem) {
331 windowTaskItem->setShowTooltip(m_showTooltip);
334 KConfigGroup cg = config();
335 cg.writeEntry("showTooltip", m_showTooltip);
336 changed = true;
339 if (changed) {
340 update();
341 emit configNeedsSaving();
345 void Tasks::reconnect()
347 disconnect(TaskManager::TaskManager::self(), SIGNAL(desktopChanged(int)),
348 this, SLOT(currentDesktopChanged(int)));
349 disconnect(TaskManager::TaskManager::self(), SIGNAL(windowChanged(TaskPtr)),
350 this, SLOT(taskMovedDesktop(TaskPtr)));
351 if (m_showOnlyCurrentDesktop) {
352 // listen to the relevant task manager signals
353 connect(TaskManager::TaskManager::self(), SIGNAL(desktopChanged(int)),
354 this, SLOT(currentDesktopChanged(int)));
355 connect(TaskManager::TaskManager::self(), SIGNAL(windowChanged(TaskPtr)),
356 this, SLOT(taskMovedDesktop(TaskPtr)));
359 disconnect(TaskManager::TaskManager::self(), SIGNAL(windowChangedGeometry(TaskPtr)),
360 this, SLOT(windowChangedGeometry(TaskPtr)));
361 if (m_showOnlyCurrentScreen) {
362 // listen to the relevant task manager signals
363 connect(TaskManager::TaskManager::self(), SIGNAL(windowChangedGeometry(TaskPtr)),
364 this, SLOT(windowChangedGeometry(TaskPtr)));
365 TaskManager::TaskManager::self()->trackGeometry();
368 removeAllWindowTasks();
369 registerWindowTasks();
372 void Tasks::themeRefresh()
374 foreach (AbstractTaskItem *taskItem, m_windowTaskItems) {
375 taskItem->update();
380 #include "tasks.moc"