not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / libs / taskmanager / taskgroup.cpp
blob036c098d23f9cd2ffc75dadcfa30c7ed28deed3b
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 // Own
25 #include "taskgroup.h"
26 #include "taskmanager.h"
27 #include "taskitem.h"
28 #include "groupmanager.h"
30 // Qt
31 #include <QColor>
32 #include <QMimeData>
34 // KDE
35 #include <KDebug>
36 #include <KIcon>
37 #include <ksharedptr.h>
41 namespace TaskManager
45 class TaskGroup::Private
47 public:
48 Private()
52 QList<WId> winIds() const;
54 ItemList members;
55 QString groupName;
56 QColor groupColor;
57 QIcon groupIcon;
58 bool aboutToDie;
59 GroupManager *groupingStrategy;
62 TaskGroup::TaskGroup(GroupManager *parent,const QString &name, const QColor &color)
63 : AbstractGroupableItem(parent),
64 d(new Private)
66 d->groupingStrategy = parent;
67 d->groupName = name;
68 d->groupColor = color;
69 d->groupIcon = KIcon("xorg");
70 connect(this, SLOT(editRequest()), this, SIGNAL(groupEditRequest()));
71 //kDebug() << "Group Created: Name: " << d->groupName << "Color: " << d->groupColor;
74 TaskGroup::TaskGroup(GroupManager *parent)
75 : AbstractGroupableItem(parent),
76 d(new Private)
78 d->groupingStrategy = parent;
79 // d->groupName = "default";
80 d->groupColor = Qt::red;
81 d->groupIcon = KIcon("xorg");
82 connect(this, SLOT(editRequest()), this, SIGNAL(groupEditRequest()));
83 //kDebug() << "Group Created: Name: " << d->groupName << "Color: " << d->groupColor;
87 TaskGroup::~TaskGroup()
89 //kDebug() << name();
90 delete d;
94 void TaskGroup::add(AbstractItemPtr item)
96 /* if (!item->isGroupItem()) {
97 if ((dynamic_cast<TaskItem*>(item))->task()) {
98 kDebug() << "Add item" << (dynamic_cast<TaskItem*>(item))->task()->visibleName();
100 kDebug() << " to Group " << name();
104 if (d->members.contains(item)) {
105 //kDebug() << "already in this group";
106 return;
109 if (d->groupName.isEmpty()) {
110 TaskItem *taskItem = qobject_cast<TaskItem*>(item);
111 if (taskItem) {
112 d->groupName = taskItem->task()->classClass();
116 if (item->parentGroup()) {
117 item->parentGroup()->remove(item);
120 d->members.append(item);
121 item->setParentGroup(this);
123 connect(item, SIGNAL(changed(::TaskManager::TaskChanges)),
124 this, SIGNAL(changed(::TaskManager::TaskChanges)));
125 //For debug
126 /* foreach (AbstractGroupableItem *item, d->members) {
127 if (item->isGroupItem()) {
128 kDebug() << (dynamic_cast<TaskGroup*>(item))->name();
129 } else {
130 kDebug() << (dynamic_cast<TaskItem*>(item))->task()->visibleName();
133 emit itemAdded(item);
136 void TaskGroup::remove(AbstractItemPtr item)
138 Q_ASSERT(item);
141 if (item->isGroupItem()) {
142 kDebug() << "Remove group" << (dynamic_cast<TaskGroup*>(item))->name();
143 } else if ((dynamic_cast<TaskItem*>(item))->task()) {
144 kDebug() << "Remove item" << (dynamic_cast<TaskItem*>(item))->task()->visibleName();
146 kDebug() << "from Group: " << name();
149 /* kDebug() << "GroupMembers: ";
150 foreach (AbstractGroupableItem *item, d->members) {
151 if (item->isGroupItem()) {
152 kDebug() << (dynamic_cast<TaskGroup*>(item))->name();
153 } else {
154 kDebug() << (dynamic_cast<TaskItem*>(item))->task()->visibleName();
158 if (!d->members.contains(item)) {
159 //kDebug() << "couldn't find item";
160 return;
163 disconnect(item, 0, this, 0);
165 d->members.removeAll(item);
166 item->setParentGroup(0);
167 /*if(d->members.isEmpty()){
168 kDebug() << "empty";
169 emit empty(this);
171 emit itemRemoved(item);
174 void TaskGroup::clear()
176 // kDebug() << "size " << d->members.size();
177 foreach(AbstractGroupableItem *item, d->members) {
178 // kDebug();
179 Q_ASSERT(item);
180 if (item->isGroupItem()) {
181 (dynamic_cast<GroupPtr>(item))->clear();
183 remove(item);
186 if (!d->members.isEmpty()) {
187 kDebug() << "clear doesn't work";
191 ItemList TaskGroup::members() const
193 return d->members;
196 void TaskGroup::setColor(const QColor &color)
198 d->groupColor = color;
199 emit changed(ColorChanged);
202 QColor TaskGroup::color() const
204 return d->groupColor;
207 QString TaskGroup::name() const
209 return d->groupName;
212 void TaskGroup::setName(const QString &newName)
214 d->groupName = newName;
215 emit changed(NameChanged);
218 QIcon TaskGroup::icon() const
220 return d->groupIcon;
223 void TaskGroup::setIcon(const QIcon &newIcon)
225 d->groupIcon = newIcon;
226 emit changed(IconChanged);
229 bool TaskGroup::isRootGroup() const
231 return !parentGroup();
234 /** only true if item is in this group */
235 bool TaskGroup::hasDirectMember(AbstractItemPtr item) const
237 return d->members.contains(item);
240 /** true if item is in this or any sub group */
241 bool TaskGroup::hasMember(AbstractItemPtr item) const
243 //kDebug();
244 TaskGroup *group = item->parentGroup();
245 while (group) {
246 if (group == this) {
247 return true;
249 group = group->parentGroup();
251 return false;
254 /** Returns Direct Member group if the passed item is in a subgroup */
255 AbstractItemPtr TaskGroup::directMember(AbstractItemPtr item) const
257 AbstractItemPtr tempItem = item;
258 while (tempItem) {
259 if (d->members.contains(item)) {
260 return item;
262 tempItem = tempItem->parentGroup();
265 kDebug() << "item not found";
266 return AbstractItemPtr();
269 void TaskGroup::setShaded(bool state)
271 foreach (AbstractItemPtr item, d->members) {
272 item->setShaded(state);
276 void TaskGroup::toggleShaded()
278 setShaded(!isShaded());
281 bool TaskGroup::isShaded() const
283 foreach (AbstractItemPtr item, d->members) {
284 if (!item->isShaded()) {
285 return false;
288 return true;
291 void TaskGroup::toDesktop(int desk)
293 foreach (AbstractItemPtr item, d->members) {
294 item->toDesktop(desk);
296 emit movedToDesktop(desk);
299 bool TaskGroup::isOnCurrentDesktop() const
301 foreach (AbstractItemPtr item, d->members) {
302 if (!item->isOnCurrentDesktop()) {
303 return false;
306 return true;
309 QList<WId> TaskGroup::Private::winIds() const
311 QList<WId> ids;
313 foreach (AbstractGroupableItem *groupable, members) {
314 if (groupable->isGroupItem()) {
315 TaskGroup *group = dynamic_cast<TaskGroup*>(groupable);
316 if (group) {
317 ids << group->d->winIds();
319 } else {
320 TaskItem * item = dynamic_cast<TaskItem*>(groupable);
321 if (item) {
322 ids << item->task()->info().win();
327 return ids;
330 void TaskGroup::addMimeData(QMimeData *mimeData) const
332 //kDebug() << d->members.count();
333 if (d->members.isEmpty()) {
334 return;
337 QByteArray data;
338 QList<WId> ids = d->winIds();
339 int count = ids.count();
340 data.resize(sizeof(int) + sizeof(WId) * count);
341 memcpy(data.data(), &count, sizeof(int));
342 int i = 0;
343 foreach (WId id, ids) {
344 //kDebug() << "adding" << id;
345 memcpy(data.data() + sizeof(int) + sizeof(WId) * i, &id, sizeof(WId));
346 ++i;
349 //kDebug() << "done:" << data.size() << count;
350 mimeData->setData(Task::groupMimetype(), data);
353 bool TaskGroup::isOnAllDesktops() const
355 foreach (AbstractItemPtr item, d->members) {
356 if (!item->isOnAllDesktops()) {
357 return false;
360 return true;
363 //return 0 if tasks are on different desktops or on all dektops
364 int TaskGroup::desktop() const
366 if (d->members.isEmpty()) {
367 return 0;
370 int desk = d->members.first()->desktop();
371 foreach (AbstractItemPtr item, d->members) {
372 if (item->desktop() != desk) {
373 return 0;
375 desk = item->desktop();
377 return desk;
380 void TaskGroup::setMaximized(bool state)
382 foreach (AbstractItemPtr item, d->members) {
383 item->setMaximized(state);
387 void TaskGroup::toggleMaximized()
389 setMaximized(!isMaximized());
392 bool TaskGroup::isMaximized() const
394 foreach (AbstractItemPtr item, d->members) {
395 if (!item->isMaximized()) {
396 return false;
399 return true;
402 void TaskGroup::setMinimized(bool state)
404 foreach (AbstractItemPtr item, d->members) {
405 item->setMinimized(state);
409 void TaskGroup::toggleMinimized()
411 setMinimized(!isMinimized());
414 bool TaskGroup::isMinimized() const
416 foreach (AbstractItemPtr item, d->members) {
417 if (!item->isMinimized()) {
418 return false;
421 return true;
424 void TaskGroup::setFullScreen(bool state)
426 foreach (AbstractItemPtr item, d->members) {
427 item->setFullScreen(state);
431 void TaskGroup::toggleFullScreen()
433 setFullScreen(!isFullScreen());
436 bool TaskGroup::isFullScreen() const
438 foreach (AbstractItemPtr item, d->members) {
439 if (!item->isFullScreen()) {
440 return false;
443 return true;
446 void TaskGroup::setKeptBelowOthers(bool state)
448 foreach (AbstractItemPtr item, d->members) {
449 item->setKeptBelowOthers(state);
453 void TaskGroup::toggleKeptBelowOthers()
455 setKeptBelowOthers(!isKeptBelowOthers());
458 bool TaskGroup::isKeptBelowOthers() const
460 foreach (AbstractItemPtr item, d->members) {
461 if (!item->isKeptBelowOthers()) {
462 return false;
465 return true;
468 void TaskGroup::setAlwaysOnTop(bool state)
470 foreach (AbstractItemPtr item, d->members) {
471 item->setAlwaysOnTop(state);
475 void TaskGroup::toggleAlwaysOnTop()
477 setAlwaysOnTop(!isAlwaysOnTop());
480 bool TaskGroup::isAlwaysOnTop() const
482 foreach (AbstractItemPtr item, d->members) {
483 if (!item->isAlwaysOnTop()) {
484 return false;
487 return true;
490 bool TaskGroup::isActionSupported(NET::Action action) const
492 if (KWindowSystem::allowedActionsSupported()) {
493 foreach (AbstractItemPtr item, d->members) {
494 if (!item->isActionSupported(action)) {
495 return false;
498 return true;
500 return false;
503 void TaskGroup::close()
505 foreach (AbstractItemPtr item, d->members) {
506 item->close();
510 bool TaskGroup::isActive() const
512 foreach (AbstractItemPtr item, d->members) {
513 if (item->isActive()) {
514 return true;
518 return false;
521 bool TaskGroup::demandsAttention() const
523 foreach (AbstractItemPtr item, d->members) {
524 if (item->demandsAttention()) {
525 return true;
529 return false;
532 bool TaskGroup::moveItem(int oldIndex, int newIndex)
534 //kDebug() << oldIndex << newIndex;
535 if ((d->members.count() <= newIndex) || (newIndex < 0) ||
536 (d->members.count() <= oldIndex || oldIndex < 0)) {
537 kDebug() << "index out of bounds";
538 return false;
541 AbstractItemPtr item = d->members.at(oldIndex);
542 d->members.move(oldIndex, newIndex);
543 //kDebug() << "new index " << d->members.indexOf(item);
544 emit itemPositionChanged(item);
545 return true;
548 } // TaskManager namespace
550 #include "taskgroup.moc"