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"
34 class AbstractGroupingStrategy::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
),
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();
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
);
80 GroupManager::TaskGroupingStrategy
AbstractGroupingStrategy::type() const
85 void AbstractGroupingStrategy::setType(GroupManager::TaskGroupingStrategy type
)
90 void AbstractGroupingStrategy::desktopChanged(int newDesktop
)
95 QList
<QAction
*> AbstractGroupingStrategy::strategyActions(QObject
*parent
, AbstractGroupableItem
*item
)
99 return QList
<QAction
*>();
102 TaskGroup
* AbstractGroupingStrategy::createGroup(ItemList items
)
106 if (!items
.isEmpty() && items
.first()->isGrouped()) {
107 oldGroup
= items
.first()->parentGroup();
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
) {
119 oldGroup
->add(newGroup
);
123 void AbstractGroupingStrategy::closeGroup(TaskGroup
*group
)
126 disconnect(group
, 0, this, 0);
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();
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());
154 if (group
->members().size() <= 0) {
159 bool AbstractGroupingStrategy::addItemToGroup(AbstractGroupableItem
*item
, TaskGroup
*group
)
161 if (editableGroupProperties() & Members
) {
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
);
181 //Returns 6 free names
182 QList
<QString
> AbstractGroupingStrategy::nameSuggestions(TaskGroup
*)
184 QList
<QString
> nameList
;
187 while (nameList
.count() < 6) {
188 if (!d
->usedNames
.contains("Group"+QString::number(i
))) {
189 nameList
.append("Group"+QString::number(i
));
194 if (nameList
.isEmpty()) {
195 nameList
.append("default");
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
);
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
);
236 bool AbstractGroupingStrategy::setIcon(const QIcon
&icon
, TaskGroup
*group
)
238 if (editableGroupProperties() & Icon
) {
239 group
->setIcon(icon
);
246 QList
<QIcon
> AbstractGroupingStrategy::iconSuggestions(TaskGroup
*)
248 QList
<QIcon
> iconList
;
249 iconList
.append(KIcon("xorg"));
255 #include "abstractgroupingstrategy.moc"