File: abstractsortingstrategy.cpp

package info (click to toggle)
kde-workspace 4%3A4.11.13-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 67,756 kB
  • ctags: 47,705
  • sloc: cpp: 358,638; ansic: 34,695; xml: 5,231; perl: 1,598; sh: 1,307; ruby: 1,135; python: 651; asm: 566; makefile: 37
file content (191 lines) | stat: -rw-r--r-- 5,259 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*****************************************************************

Copyright 2008 Christian Mollekopf <chrigi_1@hotmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************/

#include "abstractsortingstrategy.h"

#include "taskitem.h"
#include "taskgroup.h"
#include "taskmanager.h"
#include "abstractgroupableitem.h"

#include <QtAlgorithms>
#include <QList>

#include <KDebug>


namespace TaskManager
{

class AbstractSortingStrategy::Private
{
public:
    Private()
        : type(GroupManager::NoSorting) {
    }

    QList<TaskGroup*> managedGroups;
    GroupManager::TaskSortingStrategy type;
};


AbstractSortingStrategy::AbstractSortingStrategy(QObject *parent)
    : QObject(parent),
      d(new Private)
{

}

AbstractSortingStrategy::~AbstractSortingStrategy()
{
    delete d;
}

GroupManager::TaskSortingStrategy AbstractSortingStrategy::type() const
{
    return d->type;
}

void AbstractSortingStrategy::setType(GroupManager::TaskSortingStrategy type)
{
    d->type = type;
}

void AbstractSortingStrategy::handleGroup(TaskGroup *group)
{
    //kDebug();
    if (d->managedGroups.contains(group) || !group) {
        return;
    }

    d->managedGroups.append(group);
    disconnect(group, 0, this, 0); //To avoid duplicate connections
    connect(group, SIGNAL(destroyed()), this, SLOT(removeGroup())); //FIXME necessary?
    ItemList sortedList = group->members();
    sortItems(sortedList); //the sorting doesn't work with totally unsorted lists, therefore we sort it in the correct order the first time

    foreach (AbstractGroupableItem * item, sortedList) {
        handleItem(item);
    }
}

void AbstractSortingStrategy::removeGroup()
{
    TaskGroup *group = dynamic_cast<TaskGroup*>(sender());

    if (!group) {
        return;
    }

    d->managedGroups.removeAll(group);
}

void AbstractSortingStrategy::handleItem(AbstractGroupableItem *item)
{
    //kDebug() << item->name();
    if (item->itemType() == GroupItemType) {
        handleGroup(qobject_cast<TaskGroup*>(item));
    } else if (item->itemType() == TaskItemType && !(qobject_cast<TaskItem*>(item))->task()) { //ignore startup tasks
        connect(item, SIGNAL(gotTaskPointer()), this, SLOT(check())); //sort the task as soon as it is a real one
        return;
    }

    check(item);
}

void AbstractSortingStrategy::check(AbstractGroupableItem *itemToCheck)
{
    AbstractGroupableItem *item;
    if (!itemToCheck) {
        item = dynamic_cast<AbstractGroupableItem *>(sender());
    } else {
        item = itemToCheck;
    }

    if (!item) {
        kDebug() << "invalid item";
        return;
    }
    //kDebug() << item->name();

    if (item->itemType() == TaskItemType) {
        if (!(qobject_cast<TaskItem*>(item))->task()) { //ignore startup tasks
            return;
        }
    }

    if (!item->parentGroup()) {
        //kDebug() << "No parent group";
        return;
    }

    ItemList sortedList = item->parentGroup()->members();
    sortItems(sortedList);

    int oldIndex = item->parentGroup()->members().indexOf(item);
    int newIndex = sortedList.indexOf(item);
    if (oldIndex != newIndex) {
        item->parentGroup()->moveItem(oldIndex, newIndex);
    }
}

void AbstractSortingStrategy::sortItems(ItemList &items)
{
    Q_UNUSED(items)
}

bool AbstractSortingStrategy::manualSortingRequest(AbstractGroupableItem *item, int newIndex)
{
    Q_UNUSED(item);
    Q_UNUSED(newIndex);
    return false;
}

bool AbstractSortingStrategy::moveItem(AbstractGroupableItem *item, int newIndex)
{
    //kDebug() << "move to " << newIndex;
    if (!item->parentGroup()) {
        kDebug() << "error: no parentgroup but the item was asked to move";
        return false;
    }

    const ItemList list = item->parentGroup()->members();
    if ((newIndex < 0) || (newIndex >= list.size())) {
        newIndex = list.size();
    }

    int oldIndex = list.indexOf(item);
    if (newIndex > oldIndex) {
        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
    }

    if (oldIndex != newIndex) {
        return item->parentGroup()->moveItem(oldIndex, newIndex);
    }

    return false;
}

} //namespace

#include "abstractsortingstrategy.moc"