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"
27 #include "taskgroup.h"
28 #include "taskmanager.h"
30 #include <QtAlgorithms>
39 class AbstractSortingStrategy::Private
43 : type(GroupManager::NoSorting
)
47 QList
<TaskGroup
*> managedGroups
;
48 GroupManager::TaskSortingStrategy type
;
52 AbstractSortingStrategy::AbstractSortingStrategy(QObject
*parent
)
59 AbstractSortingStrategy::~AbstractSortingStrategy()
64 GroupManager::TaskSortingStrategy
AbstractSortingStrategy::type() const
69 void AbstractSortingStrategy::setType(GroupManager::TaskSortingStrategy type
)
74 void AbstractSortingStrategy::handleGroup(TaskGroup
*group
)
77 if (d
->managedGroups
.contains(group
) || !group
) {
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
) {
93 void AbstractSortingStrategy::removeGroup()
95 TaskGroup
*group
= dynamic_cast<TaskGroup
*>(sender());
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
117 void AbstractSortingStrategy::check(AbstractItemPtr itemToCheck
)
119 AbstractItemPtr item
;
121 item
= dynamic_cast<AbstractItemPtr
>(sender());
127 kDebug() << "invalid item";
130 //kDebug() << item->name();
132 if (!item
->isGroupItem()) {
133 if (!(qobject_cast
<TaskItem
*>(item
))->task()) { //ignore startup tasks
138 if (!item
->parentGroup()) {
139 kDebug() << "No parent group";
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
)
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";
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
);
185 #include "abstractsortingstrategy.moc"