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 ******************************************************************/
25 #include "taskgroup.h"
26 #include "taskmanager.h"
28 #include "groupmanager.h"
37 #include <ksharedptr.h>
45 class TaskGroup::Private
52 QList
<WId
> winIds() const;
59 GroupManager
*groupingStrategy
;
62 TaskGroup::TaskGroup(GroupManager
*parent
,const QString
&name
, const QColor
&color
)
63 : AbstractGroupableItem(parent
),
66 d
->groupingStrategy
= parent
;
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
),
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()
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";
109 if (d
->groupName
.isEmpty()) {
110 TaskItem
*taskItem
= qobject_cast
<TaskItem
*>(item
);
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
)));
126 /* foreach (AbstractGroupableItem *item, d->members) {
127 if (item->isGroupItem()) {
128 kDebug() << (dynamic_cast<TaskGroup*>(item))->name();
130 kDebug() << (dynamic_cast<TaskItem*>(item))->task()->visibleName();
133 emit
itemAdded(item
);
136 void TaskGroup::remove(AbstractItemPtr 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();
154 kDebug() << (dynamic_cast<TaskItem*>(item))->task()->visibleName();
158 if (!d
->members
.contains(item
)) {
159 //kDebug() << "couldn't find item";
163 disconnect(item
, 0, this, 0);
165 d
->members
.removeAll(item
);
166 item
->setParentGroup(0);
167 /*if(d->members.isEmpty()){
171 emit
itemRemoved(item
);
174 void TaskGroup::clear()
176 // kDebug() << "size " << d->members.size();
177 foreach(AbstractGroupableItem
*item
, d
->members
) {
180 if (item
->isGroupItem()) {
181 (dynamic_cast<GroupPtr
>(item
))->clear();
186 if (!d
->members
.isEmpty()) {
187 kDebug() << "clear doesn't work";
191 ItemList
TaskGroup::members() const
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
212 void TaskGroup::setName(const QString
&newName
)
214 d
->groupName
= newName
;
215 emit
changed(NameChanged
);
218 QIcon
TaskGroup::icon() const
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
244 TaskGroup
*group
= item
->parentGroup();
249 group
= group
->parentGroup();
254 /** Returns Direct Member group if the passed item is in a subgroup */
255 AbstractItemPtr
TaskGroup::directMember(AbstractItemPtr item
) const
257 AbstractItemPtr tempItem
= item
;
259 if (d
->members
.contains(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()) {
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()) {
309 QList
<WId
> TaskGroup::Private::winIds() const
313 foreach (AbstractGroupableItem
*groupable
, members
) {
314 if (groupable
->isGroupItem()) {
315 TaskGroup
*group
= dynamic_cast<TaskGroup
*>(groupable
);
317 ids
<< group
->d
->winIds();
320 TaskItem
* item
= dynamic_cast<TaskItem
*>(groupable
);
322 ids
<< item
->task()->info().win();
330 void TaskGroup::addMimeData(QMimeData
*mimeData
) const
332 //kDebug() << d->members.count();
333 if (d
->members
.isEmpty()) {
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));
343 foreach (WId id
, ids
) {
344 //kDebug() << "adding" << id;
345 memcpy(data
.data() + sizeof(int) + sizeof(WId
) * i
, &id
, sizeof(WId
));
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()) {
363 //return 0 if tasks are on different desktops or on all dektops
364 int TaskGroup::desktop() const
366 if (d
->members
.isEmpty()) {
370 int desk
= d
->members
.first()->desktop();
371 foreach (AbstractItemPtr item
, d
->members
) {
372 if (item
->desktop() != desk
) {
375 desk
= item
->desktop();
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()) {
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()) {
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()) {
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()) {
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()) {
490 bool TaskGroup::isActionSupported(NET::Action action
) const
492 if (KWindowSystem::allowedActionsSupported()) {
493 foreach (AbstractItemPtr item
, d
->members
) {
494 if (!item
->isActionSupported(action
)) {
503 void TaskGroup::close()
505 foreach (AbstractItemPtr item
, d
->members
) {
510 bool TaskGroup::isActive() const
512 foreach (AbstractItemPtr item
, d
->members
) {
513 if (item
->isActive()) {
521 bool TaskGroup::demandsAttention() const
523 foreach (AbstractItemPtr item
, d
->members
) {
524 if (item
->demandsAttention()) {
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";
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
);
548 } // TaskManager namespace
550 #include "taskgroup.moc"