not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / libs / taskmanager / abstractgroupingstrategy.cpp
blobcac176a4b545b15a8b503b21013c4c33a9199e97
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 "abstractgroupingstrategy.h"
26 #include <KDebug>
27 #include <KIcon>
29 #include "task.h"
31 namespace TaskManager
34 class AbstractGroupingStrategy::Private
36 public:
37 Private()
38 : type(GroupManager::NoGrouping)
42 GroupManager *groupManager;
43 QStringList usedNames;
44 QList<QColor> usedColors;
45 QList<TaskGroup*> createdGroups;
46 GroupManager::TaskGroupingStrategy type;
49 AbstractGroupingStrategy::AbstractGroupingStrategy(GroupManager *groupManager)
50 : QObject(groupManager),
51 d(new Private)
53 d->groupManager = groupManager;
56 AbstractGroupingStrategy::~AbstractGroupingStrategy()
58 foreach (TaskGroup *group, d->createdGroups) { //cleanup all created groups
59 disconnect(group, 0, this, 0);
61 TaskGroup *parentGroup = group->parentGroup();
62 if (!parentGroup) {
63 parentGroup = d->groupManager->rootGroup();
66 foreach (const AbstractItemPtr& item, group->members()) {
67 if (!item->isGroupItem()) {
68 parentGroup->add(item);
72 parentGroup->remove(group);
73 emit groupRemoved(group);
76 qDeleteAll(d->createdGroups);
77 delete d;
80 GroupManager::TaskGroupingStrategy AbstractGroupingStrategy::type() const
82 return d->type;
85 void AbstractGroupingStrategy::setType(GroupManager::TaskGroupingStrategy type)
87 d->type = type;
90 void AbstractGroupingStrategy::desktopChanged(int newDesktop)
92 Q_UNUSED(newDesktop)
95 QList<QAction*> AbstractGroupingStrategy::strategyActions(QObject *parent, AbstractGroupableItem *item)
97 Q_UNUSED(parent)
98 Q_UNUSED(item)
99 return QList<QAction*>();
102 TaskGroup* AbstractGroupingStrategy::createGroup(ItemList items)
104 kDebug();
105 GroupPtr oldGroup;
106 if (!items.isEmpty() && items.first()->isGrouped()) {
107 oldGroup = items.first()->parentGroup();
108 } else {
109 oldGroup = d->groupManager->rootGroup();
112 TaskGroup *newGroup = new TaskGroup(d->groupManager);
113 d->createdGroups.append(newGroup);
114 connect(newGroup, SIGNAL(itemRemoved(AbstractItemPtr)), this, SLOT(checkGroup()));
115 foreach (const AbstractItemPtr& item, items) {
116 newGroup->add(item);
119 oldGroup->add(newGroup);
120 return newGroup;
123 void AbstractGroupingStrategy::closeGroup(TaskGroup *group)
125 Q_ASSERT(group);
126 disconnect(group, 0, this, 0);
127 kDebug();
128 d->createdGroups.removeAll(group);
129 d->usedNames.removeAll(group->name());
130 d->usedColors.removeAll(group->color());
131 //d->usedIcons.removeAll(group->icon());//TODO
133 TaskGroup *parentGroup = group->parentGroup();
134 if (!parentGroup) {
135 parentGroup = d->groupManager->rootGroup();
138 foreach (const AbstractItemPtr& item, group->members()) {
139 parentGroup->add(item);
142 parentGroup->remove(group);
143 emit groupRemoved(group);
144 group->deleteLater();
147 void AbstractGroupingStrategy::checkGroup()
149 TaskGroup *group = qobject_cast<TaskGroup*>(sender());
150 if (!group) {
151 return;
154 if (group->members().size() <= 0) {
155 closeGroup(group);
159 bool AbstractGroupingStrategy::addItemToGroup(AbstractGroupableItem *item, TaskGroup *group)
161 if (editableGroupProperties() & Members) {
162 group->add(item);
163 return true;
166 return false;
169 bool AbstractGroupingStrategy::setName(const QString &name, TaskGroup *group)
171 d->usedNames.removeAll(group->name());
172 if ((editableGroupProperties() & Name) && (!d->usedNames.contains(name))) {
173 //TODO editableGroupProperties shouldn't be tested here i think
174 d->usedNames.append(name);
175 group->setName(name);
176 return true;
178 return false;
181 //Returns 6 free names
182 QList<QString> AbstractGroupingStrategy::nameSuggestions(TaskGroup *)
184 QList<QString> nameList;
185 int i = 1;
187 while (nameList.count() < 6) {
188 if (!d->usedNames.contains("Group"+QString::number(i))) {
189 nameList.append("Group"+QString::number(i));
191 i++;
194 if (nameList.isEmpty()) {
195 nameList.append("default");
198 return nameList;
201 bool AbstractGroupingStrategy::setColor(const QColor &color, TaskGroup *group)
203 d->usedColors.removeAll(group->color());
205 if (editableGroupProperties() && (!d->usedColors.contains(color))) {
206 d->usedColors.append(color);
207 group->setColor(color);
208 return true;
211 return false;
214 QList<QColor> AbstractGroupingStrategy::colorSuggestions(TaskGroup *)
216 QList<QColor> colorPool;
217 //colorPool.append(Qt::red);
218 colorPool.append(Qt::blue);
219 colorPool.append(Qt::green);
220 colorPool.append(Qt::yellow);
222 QList<QColor> colorList;
223 foreach (const QColor &color, colorPool) {
224 if (!d->usedColors.contains(color)) {
225 colorList.append(color);
229 if (colorList.isEmpty()) {
230 colorList.append(Qt::red);
233 return colorList;
236 bool AbstractGroupingStrategy::setIcon(const QIcon &icon, TaskGroup *group)
238 if (editableGroupProperties() & Icon) {
239 group->setIcon(icon);
240 return true;
243 return false;
246 QList <QIcon> AbstractGroupingStrategy::iconSuggestions(TaskGroup *)
248 QList <QIcon> iconList;
249 iconList.append(KIcon("xorg"));
250 return iconList;
253 }//namespace
255 #include "abstractgroupingstrategy.moc"