not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / libs / taskmanager / taskactions.cpp
blobc713dbfe9eb5d01d1feb80be1c78fb7398a1a29d
1 /*****************************************************************
3 Copyright 2008 Christian Mollekopf <chrigi_1@hotmail.com>
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 ******************************************************************/
24 #include "taskactions.h"
25 #include "taskactions_p.h"
27 // Own
29 #include "taskgroup.h"
30 #include "task.h"
31 #include "taskitem.h"
32 #include "taskmanager.h"
33 #include "abstractgroupingstrategy.h"
35 // KDE
36 #include <kicon.h>
37 #include <klocale.h>
38 #include <KDebug>
40 namespace TaskManager
44 QAction *standardGroupableAction(GroupableAction action, AbstractItemPtr item, QObject *parent, int desktop)
46 Q_ASSERT(item);
48 switch (action) {
49 case MaximizeAction:
50 return new MaximizeActionImpl(parent, item);
51 break;
52 case MinimizeAction:
53 return new MinimizeActionImpl(parent, item);
54 break;
55 case ToCurrentDesktopAction:
56 return new ToCurrentDesktopActionImpl(parent, item);
57 break;
58 case ToDesktopAction:
59 return new ToDesktopActionImpl(parent, item, desktop);
60 break;
61 case ShadeAction:
62 return new ShadeActionImpl(parent, item);
63 break;
64 case CloseAction:
65 return new CloseActionImpl(parent, item);
66 break;
67 case ViewFullscreenAction:
68 return new ViewFullscreenActionImpl(parent, item);
69 break;
70 case KeepBelowAction:
71 return new KeepBelowActionImpl(parent, item);
72 break;
75 return 0;
78 QAction* standardTaskAction(TaskAction action, TaskItem *item, QObject *parent)
80 Q_ASSERT(item);
82 switch (action) {
83 case ResizeAction:
84 return new ResizeActionImpl(parent, item);
85 break;
86 case MoveAction:
87 return new MoveActionImpl(parent, item);
88 break;
91 return 0;
94 QAction* standardGroupingAction(GroupingAction action, AbstractItemPtr item, GroupManager *strategy, QObject *parent)
96 Q_ASSERT(item);
97 Q_ASSERT(strategy);
99 switch (action) {
100 case LeaveGroupAction:
101 return new LeaveGroupActionImpl(parent, item, strategy);
102 break;
105 return 0;
108 MinimizeActionImpl::MinimizeActionImpl(QObject *parent, AbstractItemPtr item)
109 : QAction(parent)
111 connect(this, SIGNAL(triggered()), item, SLOT(toggleMinimized()));
112 setText(i18n("Mi&nimize"));
113 setCheckable(true);
114 setChecked(item->isMinimized());
115 setEnabled(item->isActionSupported(NET::ActionMinimize));
119 MaximizeActionImpl::MaximizeActionImpl(QObject *parent, AbstractItemPtr item)
120 : QAction(parent)
122 connect(this, SIGNAL(triggered()), item, SLOT(toggleMaximized()));
123 setText(i18n("Ma&ximize"));
124 setCheckable(true);
125 setChecked(item->isMaximized());
126 setEnabled(item->isActionSupported(NET::ActionMax));
129 ShadeActionImpl::ShadeActionImpl(QObject *parent, AbstractItemPtr item)
130 : QAction(parent)
132 connect(this, SIGNAL(triggered()), item, SLOT(toggleShaded()));
133 setText(i18n("&Shade"));
134 setCheckable(true);
135 setChecked(item->isShaded());
136 setEnabled(item->isActionSupported(NET::ActionShade));
139 ResizeActionImpl::ResizeActionImpl(QObject *parent, TaskItem* item)
140 : QAction(parent)
142 TaskPtr task = item->task();
143 connect(this, SIGNAL(triggered()), task.data(), SLOT(resize()));
144 setText(i18n("Re&size"));
145 setEnabled(item->isActionSupported(NET::ActionResize));
148 MoveActionImpl::MoveActionImpl(QObject *parent, TaskItem* item)
149 : QAction(parent)
151 TaskPtr task = item->task();
152 connect(this, SIGNAL(triggered()), task.data(), SLOT(move()));
153 setText(i18n("&Move"));
154 setIcon(KIcon("transform-move"));
155 setEnabled(item->isActionSupported(NET::ActionMove));
158 CloseActionImpl::CloseActionImpl(QObject *parent, AbstractItemPtr item)
159 : QAction(parent)
161 connect(this, SIGNAL(triggered()), item, SLOT(close()));
162 setText(i18n("&Close"));
163 setIcon(KIcon("window-close"));
164 setEnabled(item->isActionSupported(NET::ActionClose));
168 ToCurrentDesktopActionImpl::ToCurrentDesktopActionImpl(QObject *parent, AbstractItemPtr item)
169 : QAction(parent),
170 m_item(item)
172 connect(this, SIGNAL(triggered()), this, SLOT(slotToCurrentDesktop()));
173 setText(i18n("&To Current Desktop"));
174 setEnabled(!item->isOnCurrentDesktop() && item->isActionSupported(NET::ActionChangeDesktop));
177 void ToCurrentDesktopActionImpl::slotToCurrentDesktop()
179 m_item->toDesktop(KWindowSystem::currentDesktop());
184 ToDesktopActionImpl::ToDesktopActionImpl(QObject *parent, AbstractItemPtr item, int desktop)
185 : QAction(parent),
186 m_desktop(desktop),
187 m_item(item)
189 connect(this, SIGNAL(triggered()), this, SLOT(slotToDesktop()));
190 setCheckable(true);
191 if (!desktop) { //to All Desktops
192 setText(i18n("&All Desktops"));
193 setChecked(item->isOnAllDesktops());
194 } else {
195 QString name = QString("&%1 %2").arg(desktop).arg(TaskManager::self()->desktopName(desktop).replace('&', "&&"));
196 setText(name);
197 setChecked(!item->isOnAllDesktops() && item->desktop() == desktop);
202 void ToDesktopActionImpl::slotToDesktop()
204 m_item->toDesktop(m_desktop);
209 DesktopsMenu::DesktopsMenu(QWidget *parent, AbstractItemPtr item)
210 : QMenu(parent)
212 setTitle( i18n("To &Desktop") );
213 addAction( new ToDesktopActionImpl(this,item,0) ); //0 means all desktops
214 addSeparator();
215 for (int i = 1; i <= TaskManager::self()->numberOfDesktops(); i++) {
216 addAction( new ToDesktopActionImpl(this,item,i) );
218 setEnabled(item->isActionSupported(NET::ActionChangeDesktop));
221 KeepAboveActionImpl::KeepAboveActionImpl(QObject *parent, AbstractItemPtr item)
222 : QAction(parent)
224 connect(this, SIGNAL(triggered()), item, SLOT(toggleAlwaysOnTop()));
225 setText(i18n("Keep &Above Others"));
226 setIcon(KIcon("go-up"));
227 setCheckable(true);
228 setChecked(item->isAlwaysOnTop());
231 KeepBelowActionImpl::KeepBelowActionImpl(QObject *parent, AbstractItemPtr item)
232 : QAction(parent)
234 connect(this, SIGNAL(triggered()), item, SLOT(toggleKeptBelowOthers()));
235 setText(i18n("Keep &Below Others"));
236 setIcon(KIcon("go-down"));
237 setCheckable(true);
238 setChecked(item->isKeptBelowOthers());
241 ViewFullscreenActionImpl::ViewFullscreenActionImpl(QObject *parent, AbstractItemPtr item)
242 : QAction(parent)
244 connect(this, SIGNAL(triggered()), item, SLOT(toggleFullScreen()));
245 setText(i18n("&Fullscreen"));
246 setIcon(KIcon("view-fullscreen"));
247 setCheckable(true);
248 setChecked(item->isFullScreen());
249 setEnabled(item->isActionSupported(NET::ActionFullScreen));
252 AdvancedMenu::AdvancedMenu(QWidget *parent, AbstractItemPtr item)
253 :QMenu(parent)
255 setTitle(i18n("Ad&vanced"));
256 addAction(new KeepAboveActionImpl(this, item));
257 addAction(new KeepBelowActionImpl(this, item));
258 addAction(new ViewFullscreenActionImpl(this, item));
261 LeaveGroupActionImpl::LeaveGroupActionImpl(QObject *parent, AbstractItemPtr item, GroupManager *strategy)
262 : QAction(parent), abstractItem(item), groupingStrategy(strategy)
264 Q_ASSERT(strategy);
265 connect(this, SIGNAL(triggered()), this, SLOT(leaveGroup()));
266 setText(i18n("&Leave Group"));
267 setIcon(KIcon("window-close"));
268 setEnabled(item->isGrouped());
271 void LeaveGroupActionImpl::leaveGroup()
273 groupingStrategy->manualGroupingRequest(abstractItem,abstractItem->parentGroup()->parentGroup());
276 EditGroupActionImpl::EditGroupActionImpl(QObject *parent, TaskGroup *group, GroupManager *groupManager)
277 : QAction(parent)
279 Q_ASSERT(groupManager);
280 connect(this, SIGNAL(triggered()), group, SIGNAL(groupEditRequest()));
281 setText(i18n("&Edit Group"));
282 //setIcon(KIcon("window-close"));
283 if (groupManager->groupingStrategy()) {
284 setEnabled(groupManager->taskGrouper()->editableGroupProperties());
285 } else {
286 setEnabled(false);
290 GroupingStrategyMenu::GroupingStrategyMenu(QWidget *parent, AbstractGroupableItem* item, GroupManager *strategy)
291 : QMenu(parent)
293 Q_ASSERT(item);
294 Q_ASSERT(strategy);
296 setTitle("Grouping strategy actions");
297 if (strategy->taskGrouper()) {
298 QList<QAction*> groupingStrategyActions = strategy->taskGrouper()->strategyActions(this, item);
299 if (!groupingStrategyActions.empty()) {
300 addSeparator();
301 foreach (QAction *action, groupingStrategyActions) {
302 addAction(action);
310 BasicMenu::BasicMenu(QWidget *parent, TaskItem* item, GroupManager *strategy, QList<QAction *> visualizationActions)
311 : QMenu(parent)
313 Q_ASSERT(item);
314 Q_ASSERT(strategy);
316 setTitle(item->name());
317 setIcon(item->icon());
318 addMenu(new AdvancedMenu(this, item));
320 if (TaskManager::self()->numberOfDesktops() > 1) {
321 addMenu(new DesktopsMenu(this, item));
322 addAction(new ToCurrentDesktopActionImpl(this, item));
325 addAction(new MoveActionImpl(this, item));
326 addAction(new ResizeActionImpl(this, item));
327 addAction(new MinimizeActionImpl(this, item));
328 addAction(new MaximizeActionImpl(this, item));
329 addAction(new ShadeActionImpl(this, item));
331 if (strategy->taskGrouper()) {
332 QList<QAction*> groupingStrategyActions = strategy->taskGrouper()->strategyActions(this, item);
333 if (!groupingStrategyActions.isEmpty()) {
334 addSeparator();
335 foreach (QAction *action, groupingStrategyActions) {
336 addAction(action);
338 // delete groupingStrategyActions;
342 foreach (QAction *action, visualizationActions) {
343 addAction(action);
346 addSeparator();
347 addAction(new CloseActionImpl(this, item));
350 BasicMenu::BasicMenu(QWidget *parent, TaskGroup* group, GroupManager *strategy, QList <QAction*> visualizationActions)
351 : QMenu(parent)
353 Q_ASSERT(group);
354 Q_ASSERT(strategy);
356 setTitle(group->name());
357 setIcon(group->icon());
358 foreach (AbstractGroupableItem *item, group->members()) {
359 if (item->isGroupItem()) {
360 addMenu(new BasicMenu(this, dynamic_cast<TaskGroup*>(item), strategy));
361 } else {
362 addMenu(new BasicMenu(this, dynamic_cast<TaskItem*>(item), strategy));
365 addSeparator();
366 addMenu(new AdvancedMenu(this, group));
368 if (TaskManager::self()->numberOfDesktops() > 1) {
369 addMenu(new DesktopsMenu(this, group));
370 addAction(new ToCurrentDesktopActionImpl(this, group));
373 addAction(new MinimizeActionImpl(this, group));
374 addAction(new MaximizeActionImpl(this, group));
375 addAction(new ShadeActionImpl(this, group));
377 if (strategy->taskGrouper()) {
378 QList<QAction*> groupingStrategyActions = strategy->taskGrouper()->strategyActions(this, group);
379 if (!groupingStrategyActions.isEmpty()) {
380 addSeparator();
381 foreach (QAction *action, groupingStrategyActions) {
382 addAction(action);
386 addAction(new EditGroupActionImpl(this, group, strategy));
387 foreach(QAction *action, visualizationActions) {
388 addAction(action);
391 addSeparator();
392 addAction(new CloseActionImpl(this, group));
395 GroupPopupMenu::GroupPopupMenu(QWidget *parent, TaskGroup *group, GroupManager *groupManager)
396 :QMenu(parent)
398 setTitle(group->name());
399 setIcon(group->icon());
400 foreach (AbstractGroupableItem *item, group->members()) {
401 if (!item) {
402 kDebug() << "invalid Item";
403 continue;
406 if (item->isGroupItem()) {
407 QMenu* menu = new GroupPopupMenu (this, qobject_cast<TaskGroup*>(item), groupManager);
408 addMenu(menu);
409 } else {
410 QAction* action = new QAction(item->icon(), item->name(), this);
411 connect(action, SIGNAL(triggered(bool)), (qobject_cast<TaskItem*>(item))->task().data() , SLOT(activateRaiseOrIconify()));
412 addAction(action);
417 } // TaskManager namespace
419 #include "taskactions.moc"
420 #include "taskactions_p.moc"