not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / libs / taskmanager / abstractsortingstrategy.cpp
bloba6624101048695039d606f641b5c9a3fec326f77
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 "abstractsortingstrategy.h"
26 #include "taskitem.h"
27 #include "taskgroup.h"
28 #include "taskmanager.h"
30 #include <QtAlgorithms>
31 #include <QList>
33 #include <KDebug>
36 namespace TaskManager
39 class AbstractSortingStrategy::Private
41 public:
42 Private()
43 : type(GroupManager::NoSorting)
47 QList<TaskGroup*> managedGroups;
48 GroupManager::TaskSortingStrategy type;
52 AbstractSortingStrategy::AbstractSortingStrategy(QObject *parent)
53 :QObject(parent),
54 d(new Private)
59 AbstractSortingStrategy::~AbstractSortingStrategy()
61 delete d;
64 GroupManager::TaskSortingStrategy AbstractSortingStrategy::type() const
66 return d->type;
69 void AbstractSortingStrategy::setType(GroupManager::TaskSortingStrategy type)
71 d->type = type;
74 void AbstractSortingStrategy::handleGroup(TaskGroup *group)
76 //kDebug();
77 if (d->managedGroups.contains(group) || !group) {
78 return;
81 d->managedGroups.append(group);
82 disconnect(group, 0, this, 0); //To avoid duplicate connections
83 connect(group, SIGNAL(itemAdded(AbstractItemPtr)), this, SLOT(handleItem(AbstractItemPtr)));
84 connect(group, SIGNAL(destroyed()), this, SLOT(removeGroup())); //FIXME necessary?
85 ItemList sortedList = group->members();
86 sortItems(sortedList); //the sorting doesn't work with totally unsorted lists, therefore we sort it in the correct order the first time
88 foreach (AbstractItemPtr item, sortedList) {
89 handleItem(item);
93 void AbstractSortingStrategy::removeGroup()
95 TaskGroup *group = dynamic_cast<TaskGroup*>(sender());
97 if (!group) {
98 return;
101 d->managedGroups.removeAll(group);
104 void AbstractSortingStrategy::handleItem(AbstractItemPtr item)
106 //kDebug() << item->name();
107 if (item->isGroupItem()) {
108 handleGroup(qobject_cast<TaskGroup*>(item));
109 } else if (!(qobject_cast<TaskItem*>(item))->task()) { //ignore startup tasks
110 connect(item, SIGNAL(gotTaskPointer()), this, SLOT(check())); //sort the task as soon its a real one
111 return;
114 check(item);
117 void AbstractSortingStrategy::check(AbstractItemPtr itemToCheck)
119 AbstractItemPtr item;
120 if (!itemToCheck) {
121 item = dynamic_cast<AbstractItemPtr>(sender());
122 } else {
123 item = itemToCheck;
126 if (!item) {
127 kDebug() << "invalid item";
128 return;
130 //kDebug() << item->name();
132 if (!item->isGroupItem()) {
133 if (!(qobject_cast<TaskItem*>(item))->task()) { //ignore startup tasks
134 return;
138 if (!item->parentGroup()) {
139 kDebug() << "No parent group";
140 return;
143 ItemList sortedList = item->parentGroup()->members();
144 sortItems(sortedList);
146 int oldIndex = item->parentGroup()->members().indexOf(item);
147 int newIndex = sortedList.indexOf(item);
148 if (oldIndex != newIndex) {
149 item->parentGroup()->moveItem(oldIndex, newIndex);
153 void AbstractSortingStrategy::desktopChanged(int newDesktop)
155 Q_UNUSED(newDesktop)
158 bool AbstractSortingStrategy::moveItem(AbstractItemPtr item, int newIndex)
160 //kDebug() << "move to " << newIndex;
161 if (!item->parentGroup()) {
162 kDebug() << "error: no parentgroup but the item was asked to move";
163 return false;
166 const ItemList list = item->parentGroup()->members();
167 if ((newIndex < 0) || (newIndex >= list.size())) {
168 newIndex = list.size();
171 int oldIndex = list.indexOf(item);
172 if (newIndex > oldIndex) {
173 newIndex--; //the index has to be adjusted if we move the item from right to left because the item on the left is removed first
176 if (oldIndex != newIndex) {
177 return item->parentGroup()->moveItem(oldIndex, newIndex);
180 return -1;
183 } //namespace
185 #include "abstractsortingstrategy.moc"