not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / libs / taskmanager / strategies / manualsortingstrategy.cpp
blobf6b6a83264e34a7b1d7c1761a023d88b2a449e82
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 "manualsortingstrategy.h"
26 #include "taskitem.h"
27 #include "taskgroup.h"
28 #include "taskmanager.h"
30 #include <QMap>
31 #include <QtAlgorithms>
33 #include <KDebug>
36 namespace TaskManager
39 class ManualSortingStrategy::Private
41 public:
42 Private()
45 GroupManager *groupingStrategy;
47 itemHashTable *managedItems;
48 desktopHashTable *desktops;
49 int oldDesktop;
53 ManualSortingStrategy::ManualSortingStrategy(GroupManager *parent)
54 :AbstractSortingStrategy(parent),
55 d(new Private)
57 d->groupingStrategy = parent;
58 setType(GroupManager::ManualSorting);
60 d->desktops = new desktopHashTable();
61 //TODO add a screenHashTable
62 d->oldDesktop = TaskManager::TaskManager::self()->currentDesktop();
64 if (d->groupingStrategy->showOnlyCurrentDesktop()) {
65 d->desktops->insert(TaskManager::TaskManager::self()->currentDesktop(), new itemHashTable());
66 d->managedItems = d->desktops->value(TaskManager::TaskManager::self()->currentDesktop());
67 } else {
68 d->desktops->insert(0,new itemHashTable());
69 d->managedItems = d->desktops->value(0);
73 ManualSortingStrategy::~ManualSortingStrategy()
75 if (d->desktops) {
76 foreach(itemHashTable *table, *d->desktops) {
77 if (table) {
78 delete table;
81 delete d->desktops;
83 delete d;
86 void ManualSortingStrategy::storePositions(TaskGroup *group)
88 Q_ASSERT(group);
89 for(int i = 0; i < group->members().size(); i++) {
90 AbstractGroupableItem *item = group->members().at(i);
91 Q_ASSERT(item);
92 if (item->isGroupItem()) {
93 d->managedItems->insert(item, i);
94 storePositions(dynamic_cast<TaskGroup*>(item));
95 } else {
96 d->managedItems->insert(item, i);
98 kDebug() << item << i;
102 //Here we should store all infos about the sorting
103 void ManualSortingStrategy::desktopChanged(int newDesktop)
105 kDebug() << "Desktop changed" << d->oldDesktop << newDesktop;
106 //store positions of old desktop
107 d->managedItems->clear();
108 storePositions(d->groupingStrategy->rootGroup());
109 d->desktops->insert(d->oldDesktop, d->managedItems);
111 //load positions of new desktop
112 if (d->desktops->contains(newDesktop)) {
113 d->managedItems = d->desktops->value(newDesktop);
114 } else {
115 d->managedItems = new itemHashTable();
118 d->oldDesktop = newDesktop;
121 void ManualSortingStrategy::sortItems(ItemList &items)
123 kDebug();
125 QMap<int, AbstractGroupableItem*> map;
126 int i = 1000;
127 foreach (AbstractGroupableItem *item, items) {
128 if (d->managedItems->contains(item)) {
129 map.insertMulti(d->managedItems->value(item), item);
130 } else {//make sure unkwown items are appended
131 kDebug() << "item not found in managedItems";
132 map.insertMulti(i, item);
133 i++;
136 items.clear();
137 items = map.values();
140 //since we have no way of knowing about a desktop change before it happens we have to track every single change....
141 void ManualSortingStrategy::handleItem(AbstractItemPtr item)
143 if (d->managedItems->contains(item)) {
144 if (item->isGroupItem()) {
145 handleGroup(qobject_cast<TaskGroup*>(item));
147 check(item);
148 } else {
149 Q_ASSERT(item->parentGroup());
150 d->managedItems->insert(item, item->parentGroup()->members().indexOf(item));
154 } //namespace
156 #include "manualsortingstrategy.moc"