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 "programgroupingstrategy.h"
31 #include "abstractgroupingstrategy.h"
32 #include "groupmanager.h"
37 class ProgramGroupingStrategy::Private
41 :editableGroupProperties(AbstractGroupingStrategy::None
)
44 GroupManager
*groupManager
;
45 AbstractGroupingStrategy::EditableGroupProperties editableGroupProperties
;
46 AbstractGroupableItem
*tempItem
;
47 QStringList blackList
; //Programs in this list should not be grouped
51 ProgramGroupingStrategy::ProgramGroupingStrategy(GroupManager
*groupManager
)
52 :AbstractGroupingStrategy(groupManager
),
55 d
->groupManager
= groupManager
;
56 setType(GroupManager::ProgramGrouping
);
59 ProgramGroupingStrategy::~ProgramGroupingStrategy()
64 QList
<QAction
*> ProgramGroupingStrategy::strategyActions(QObject
*parent
, AbstractGroupableItem
*item
)
66 QAction
*a
= new QAction(parent
);
67 QString name
= className(item
);
68 if (d
->blackList
.contains(name
)) {
69 a
->setText(i18n("Allow This Program to Be Grouped"));
71 a
->setText(i18n("Do Not Allow This Program to Be Grouped"));
73 connect(a
, SIGNAL(triggered()), this, SLOT(toggleGrouping()));
75 QList
<QAction
*> actionList
;
81 QString
ProgramGroupingStrategy::className(AbstractGroupableItem
*item
)
84 if (item
->isGroupItem()) { //maybe add the condition that the subgroup was created by programGrouping
85 TaskGroup
*group
= qobject_cast
<TaskGroup
*>(item
);
86 TaskItem
*task
= qobject_cast
<TaskItem
*>(group
->members().first()); //There are only TaskItems in programGrouping groups
87 return task
->task()->classClass();
90 return (qobject_cast
<TaskItem
*>(item
))->task()->classClass();
93 void ProgramGroupingStrategy::toggleGrouping()
95 QString name
= className(d
->tempItem
);
97 if (d
->blackList
.contains(name
)) {
98 d
->blackList
.removeAll(name
);
99 if (d
->tempItem
->isGroupItem()) {
100 foreach (AbstractGroupableItem
*item
, (qobject_cast
<TaskGroup
*>(d
->tempItem
))->members()) {
104 handleItem(d
->tempItem
);
107 d
->blackList
.append(name
);
108 if (d
->tempItem
->isGroupItem()) {
109 closeGroup(qobject_cast
<TaskGroup
*>(d
->tempItem
));
111 d
->groupManager
->rootGroup()->add(d
->tempItem
);
117 void ProgramGroupingStrategy::handleItem(AbstractItemPtr item
)
119 if (item
->isGroupItem()) {
120 d
->groupManager
->rootGroup()->add(item
);
122 } else if (d
->blackList
.contains((qobject_cast
<TaskItem
*>(item
))->task()->classClass())) {
123 d
->groupManager
->rootGroup()->add(item
);
127 TaskItem
*task
= dynamic_cast<TaskItem
*>(item
);
128 if (task
&& !programGrouping(task
, d
->groupManager
->rootGroup())) {
129 //kDebug() << "joined rootGroup ";
130 d
->groupManager
->rootGroup()->add(item
);
134 bool ProgramGroupingStrategy::programGrouping(TaskItem
* taskItem
, TaskGroup
* groupItem
)
137 QHash
<QString
,AbstractItemPtr
> itemMap
;
139 foreach (AbstractItemPtr item
, groupItem
->members()) { //search for an existing group
140 if (item
->isGroupItem()) { //maybe add the condition that the subgroup was created by programGrouping
141 if (programGrouping(taskItem
, static_cast<TaskGroup
*>(item
))) {
142 //kDebug() << "joined subGroup";
146 TaskItem
*task
= static_cast<TaskItem
*>(item
);
147 if (task
->task()) { //omit startup tasks
148 QString name
= task
->task()->classClass();
149 itemMap
.insertMulti(name
,item
);
154 if (!itemMap
.values().contains(taskItem
)) {
155 itemMap
.insertMulti(taskItem
->task()->classClass(), taskItem
);
158 QString name
= taskItem
->task()->classClass();
159 if (itemMap
.count(name
) >= groupItem
->members().count()) { //join this group
160 //kDebug() << "joined this Group";
161 groupItem
->add(taskItem
);
163 } else if (itemMap
.count(name
) >= 2) { //create new subgroup with at least 2 other task
164 //kDebug() << "create Group";
165 QIcon icon
= taskItem
->task()->icon();
166 QList
<AbstractItemPtr
> list(itemMap
.values(name
));
167 TaskGroup
* group
= createGroup(list
);
168 group
->setName(name
);
169 group
->setColor(Qt::red
);
170 group
->setIcon(icon
);
176 void ProgramGroupingStrategy::checkGroup()
178 TaskGroup
*group
= qobject_cast
<TaskGroup
*>(sender());
182 if (group
->members().size() <= 1) {
189 #include "programgroupingstrategy.moc"