~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to libs/taskmanager/abstractsortingstrategy.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************
 
2
 
 
3
Copyright 2008 Christian Mollekopf <chrigi_1@hotmail.com>
 
4
 
 
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:
 
11
 
 
12
The above copyright notice and this permission notice shall be included in
 
13
all copies or substantial portions of the Software.
 
14
 
 
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.
 
21
 
 
22
******************************************************************/
 
23
 
 
24
#include "abstractsortingstrategy.h"
 
25
 
 
26
#include "taskitem.h"
 
27
#include "taskgroup.h"
 
28
#include "taskmanager.h"
 
29
#include "abstractgroupableitem.h"
 
30
 
 
31
#include <QtAlgorithms>
 
32
#include <QList>
 
33
 
 
34
#include <KDebug>
 
35
 
 
36
 
 
37
namespace TaskManager
 
38
{
 
39
 
 
40
class AbstractSortingStrategy::Private
 
41
{
 
42
public:
 
43
    Private()
 
44
        : type(GroupManager::NoSorting)
 
45
    {
 
46
    }
 
47
 
 
48
    QList<TaskGroup*> managedGroups;
 
49
    GroupManager::TaskSortingStrategy type;
 
50
};
 
51
 
 
52
 
 
53
AbstractSortingStrategy::AbstractSortingStrategy(QObject *parent)
 
54
    :QObject(parent),
 
55
    d(new Private)
 
56
{
 
57
 
 
58
}
 
59
 
 
60
AbstractSortingStrategy::~AbstractSortingStrategy()
 
61
{
 
62
    delete d;
 
63
}
 
64
 
 
65
GroupManager::TaskSortingStrategy AbstractSortingStrategy::type() const
 
66
{
 
67
    return d->type;
 
68
}
 
69
 
 
70
void AbstractSortingStrategy::setType(GroupManager::TaskSortingStrategy type)
 
71
{
 
72
    d->type = type;
 
73
}
 
74
 
 
75
void AbstractSortingStrategy::handleGroup(TaskGroup *group)
 
76
{
 
77
    //kDebug();
 
78
    if (d->managedGroups.contains(group) || !group) {
 
79
        return;
 
80
    }
 
81
 
 
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
 
89
 
 
90
    foreach (AbstractGroupableItem *item, sortedList) {
 
91
        handleItem(item);
 
92
    }
 
93
}
 
94
 
 
95
void AbstractSortingStrategy::removeGroup()
 
96
{
 
97
    TaskGroup *group = dynamic_cast<TaskGroup*>(sender());
 
98
 
 
99
    if (!group) {
 
100
        return;
 
101
    }
 
102
 
 
103
    d->managedGroups.removeAll(group);
 
104
}
 
105
 
 
106
void AbstractSortingStrategy::handleItem(AbstractGroupableItem *item)
 
107
{
 
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
 
113
        return;
 
114
    }
 
115
 
 
116
    check(item);
 
117
}
 
118
 
 
119
void AbstractSortingStrategy::check(AbstractGroupableItem *itemToCheck)
 
120
{
 
121
    AbstractGroupableItem *item;
 
122
    if (!itemToCheck) {
 
123
        item = dynamic_cast<AbstractGroupableItem *>(sender());
 
124
    } else {
 
125
        item = itemToCheck;
 
126
    }
 
127
 
 
128
    if (!item) {
 
129
        kDebug() << "invalid item";
 
130
        return;
 
131
    }
 
132
    //kDebug() << item->name();
 
133
 
 
134
    if (item->itemType() == TaskItemType) {
 
135
        if (!(qobject_cast<TaskItem*>(item))->task()) { //ignore startup tasks
 
136
            return;
 
137
        }
 
138
    }
 
139
 
 
140
    if (!item->parentGroup()) {
 
141
        //kDebug() << "No parent group";
 
142
        return;
 
143
    }
 
144
 
 
145
    ItemList sortedList = item->parentGroup()->members();
 
146
    sortItems(sortedList);
 
147
 
 
148
    int oldIndex = item->parentGroup()->members().indexOf(item);
 
149
    int newIndex = sortedList.indexOf(item);
 
150
    if (oldIndex != newIndex) {
 
151
        item->parentGroup()->moveItem(oldIndex, newIndex);
 
152
    }
 
153
}
 
154
 
 
155
void AbstractSortingStrategy::sortItems(ItemList &items)
 
156
{
 
157
    Q_UNUSED(items)
 
158
}
 
159
 
 
160
bool AbstractSortingStrategy::manualSortingRequest(AbstractGroupableItem *item, int newIndex)
 
161
{
 
162
    Q_UNUSED(item);
 
163
    Q_UNUSED(newIndex);
 
164
    return false;
 
165
}
 
166
 
 
167
bool AbstractSortingStrategy::moveItem(AbstractGroupableItem *item, int newIndex)
 
168
{
 
169
    //kDebug() << "move to " << newIndex;
 
170
    if (!item->parentGroup()) {
 
171
        kDebug() << "error: no parentgroup but the item was asked to move";
 
172
        return false;
 
173
    }
 
174
 
 
175
    const ItemList list = item->parentGroup()->members();
 
176
    if ((newIndex < 0) || (newIndex >= list.size())) {
 
177
        newIndex = list.size();
 
178
    }
 
179
 
 
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
 
183
    }
 
184
 
 
185
    if (oldIndex != newIndex) {
 
186
        return item->parentGroup()->moveItem(oldIndex, newIndex);
 
187
    }
 
188
 
 
189
    return -1;
 
190
}
 
191
 
 
192
} //namespace
 
193
 
 
194
#include "abstractsortingstrategy.moc"