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"
29
#include "abstractgroupableitem.h"
31
#include <QtAlgorithms>
40
class AbstractSortingStrategy::Private
44
: type(GroupManager::NoSorting)
48
QList<TaskGroup*> managedGroups;
49
GroupManager::TaskSortingStrategy type;
53
AbstractSortingStrategy::AbstractSortingStrategy(QObject *parent)
60
AbstractSortingStrategy::~AbstractSortingStrategy()
65
GroupManager::TaskSortingStrategy AbstractSortingStrategy::type() const
70
void AbstractSortingStrategy::setType(GroupManager::TaskSortingStrategy type)
75
void AbstractSortingStrategy::handleGroup(TaskGroup *group)
78
if (d->managedGroups.contains(group) || !group) {
82
d->managedGroups.append(group);
83
disconnect(group, 0, this, 0); //To avoid duplicate connections
84
connect(group, SIGNAL(itemAdded(AbstractGroupableItem *)), this, SLOT(handleItem(AbstractGroupableItem *)));
85
connect(group, SIGNAL(itemAdded(AbstractGroupableItem *)), this, SLOT(check())); //groups don't have the full windowlist from the beginning, recheck them (for manual sorting)
86
connect(group, SIGNAL(destroyed()), this, SLOT(removeGroup())); //FIXME necessary?
87
ItemList sortedList = group->members();
88
sortItems(sortedList); //the sorting doesn't work with totally unsorted lists, therefore we sort it in the correct order the first time
90
foreach (AbstractGroupableItem *item, sortedList) {
95
void AbstractSortingStrategy::removeGroup()
97
TaskGroup *group = dynamic_cast<TaskGroup*>(sender());
103
d->managedGroups.removeAll(group);
106
void AbstractSortingStrategy::handleItem(AbstractGroupableItem *item)
108
//kDebug() << item->name();
109
if (item->itemType() == GroupItemType) {
110
handleGroup(qobject_cast<TaskGroup*>(item));
111
} else if (item->itemType() == TaskItemType && !(qobject_cast<TaskItem*>(item))->task()) { //ignore startup tasks
112
connect(item, SIGNAL(gotTaskPointer()), this, SLOT(check())); //sort the task as soon as it is a real one
119
void AbstractSortingStrategy::check(AbstractGroupableItem *itemToCheck)
121
AbstractGroupableItem *item;
123
item = dynamic_cast<AbstractGroupableItem *>(sender());
129
kDebug() << "invalid item";
132
//kDebug() << item->name();
134
if (item->itemType() == TaskItemType) {
135
if (!(qobject_cast<TaskItem*>(item))->task()) { //ignore startup tasks
140
if (!item->parentGroup()) {
141
//kDebug() << "No parent group";
145
ItemList sortedList = item->parentGroup()->members();
146
sortItems(sortedList);
148
int oldIndex = item->parentGroup()->members().indexOf(item);
149
int newIndex = sortedList.indexOf(item);
150
if (oldIndex != newIndex) {
151
item->parentGroup()->moveItem(oldIndex, newIndex);
155
void AbstractSortingStrategy::sortItems(ItemList &items)
160
bool AbstractSortingStrategy::manualSortingRequest(AbstractGroupableItem *item, int newIndex)
167
bool AbstractSortingStrategy::moveItem(AbstractGroupableItem *item, int newIndex)
169
//kDebug() << "move to " << newIndex;
170
if (!item->parentGroup()) {
171
kDebug() << "error: no parentgroup but the item was asked to move";
175
const ItemList list = item->parentGroup()->members();
176
if ((newIndex < 0) || (newIndex >= list.size())) {
177
newIndex = list.size();
180
int oldIndex = list.indexOf(item);
181
if (newIndex > oldIndex) {
182
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
185
if (oldIndex != newIndex) {
186
return item->parentGroup()->moveItem(oldIndex, newIndex);
194
#include "abstractsortingstrategy.moc"