• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libtaskmanager

programgroupingstrategy.cpp

Go to the documentation of this file.
00001 /*****************************************************************
00002 
00003 Copyright 2008 Christian Mollekopf <chrigi_1@hotmail.com>
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011 
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00018 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00019 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00020 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00021 
00022 ******************************************************************/
00023 
00024 #include "programgroupingstrategy.h"
00025 
00026 #include <QAction>
00027 
00028 #include <KDebug>
00029 #include <KLocale>
00030 
00031 #include "abstractgroupingstrategy.h"
00032 #include "groupmanager.h"
00033 
00034 namespace TaskManager
00035 {
00036 
00037 class ProgramGroupingStrategy::Private
00038 {
00039 public:
00040     Private()
00041         :editableGroupProperties(AbstractGroupingStrategy::None)
00042     {
00043     }
00044     GroupManager *groupManager;
00045     AbstractGroupingStrategy::EditableGroupProperties editableGroupProperties;
00046     AbstractGroupableItem *tempItem;
00047     QStringList blackList; //Programs in this list should not be grouped
00048 };
00049 
00050 
00051 ProgramGroupingStrategy::ProgramGroupingStrategy(GroupManager *groupManager)
00052     :AbstractGroupingStrategy(groupManager),
00053      d(new Private)
00054 {
00055     d->groupManager = groupManager;
00056     setType(GroupManager::ProgramGrouping);
00057 }
00058 
00059 ProgramGroupingStrategy::~ProgramGroupingStrategy()
00060 {
00061     delete d;
00062 }
00063 
00064 QList<QAction*> ProgramGroupingStrategy::strategyActions(QObject *parent, AbstractGroupableItem *item)
00065 {
00066     QAction *a = new QAction(parent);
00067     QString name = className(item);
00068     if (d->blackList.contains(name)) {
00069         a->setText(i18n("Allow This Program to Be Grouped"));
00070     } else {
00071         a->setText(i18n("Do Not Allow This Program to Be Grouped"));
00072     }
00073     connect(a, SIGNAL(triggered()), this, SLOT(toggleGrouping()));
00074 
00075     QList<QAction*> actionList;
00076     actionList.append(a);
00077     d->tempItem = item;
00078     return actionList;
00079 }
00080 
00081 QString ProgramGroupingStrategy::className(AbstractGroupableItem *item)
00082 {
00083     QString name;
00084     if (item->isGroupItem()) { //maybe add the condition that the subgroup was created by programGrouping
00085         TaskGroup *group = qobject_cast<TaskGroup*>(item);
00086         TaskItem *task = qobject_cast<TaskItem*>(group->members().first()); //There are only TaskItems in programGrouping groups
00087         return task->task()->classClass();
00088     }
00089 
00090     return (qobject_cast<TaskItem*>(item))->task()->classClass();
00091 }
00092 
00093 void ProgramGroupingStrategy::toggleGrouping()
00094 {
00095     QString name = className(d->tempItem);
00096 
00097     if (d->blackList.contains(name)) {
00098         d->blackList.removeAll(name);
00099         if (d->tempItem->isGroupItem()) {
00100             foreach (AbstractGroupableItem *item, (qobject_cast<TaskGroup*>(d->tempItem))->members()) {
00101                 handleItem(item);
00102             }
00103         } else {
00104             handleItem(d->tempItem);
00105         }
00106     } else {
00107         d->blackList.append(name);
00108         if (d->tempItem->isGroupItem()) {
00109             closeGroup(qobject_cast<TaskGroup*>(d->tempItem));
00110         } else {
00111             d->groupManager->rootGroup()->add(d->tempItem);
00112         }
00113     }
00114     d->tempItem = 0;
00115 }
00116 
00117 void ProgramGroupingStrategy::handleItem(AbstractItemPtr item)
00118 {
00119     if (item->isGroupItem()) {
00120         d->groupManager->rootGroup()->add(item);
00121         return;
00122     } else if (d->blackList.contains((qobject_cast<TaskItem*>(item))->task()->classClass())) {
00123         d->groupManager->rootGroup()->add(item);
00124         return;
00125     }
00126 
00127     TaskItem *task = dynamic_cast<TaskItem*>(item);
00128     if (task && !programGrouping(task, d->groupManager->rootGroup())) {
00129         //kDebug() << "joined rootGroup ";
00130         d->groupManager->rootGroup()->add(item);
00131     }
00132 }
00133 
00134 bool ProgramGroupingStrategy::programGrouping(TaskItem* taskItem, TaskGroup* groupItem)
00135 {
00136     //kDebug();
00137     QHash <QString,AbstractItemPtr> itemMap;
00138 
00139     foreach (AbstractItemPtr item, groupItem->members()) { //search for an existing group
00140         if (item->isGroupItem()) { //maybe add the condition that the subgroup was created by programGrouping
00141             if (programGrouping(taskItem, static_cast<TaskGroup*>(item))) {
00142                 //kDebug() << "joined subGroup";
00143                 return true;
00144             }
00145         } else {
00146             TaskItem *task = static_cast<TaskItem*>(item);
00147             if (task->task()) { //omit startup tasks
00148                 QString name = task->task()->classClass();
00149                 itemMap.insertMulti(name,item);
00150             }
00151         }
00152     }
00153 
00154     if (!itemMap.values().contains(taskItem)) {
00155         itemMap.insertMulti(taskItem->task()->classClass(), taskItem);
00156     }
00157 
00158     QString name = taskItem->task()->classClass();
00159     if (itemMap.count(name) >= groupItem->members().count()) { //join this group
00160         //kDebug() << "joined this Group";
00161         groupItem->add(taskItem);
00162         return true;
00163     } else if (itemMap.count(name) >= 2) { //create new subgroup with at least 2 other task
00164         //kDebug() << "create Group";
00165         QIcon icon = taskItem->task()->icon();
00166         QList <AbstractItemPtr> list(itemMap.values(name));
00167         TaskGroup* group = createGroup(list);
00168         group->setName(name);
00169         group->setColor(Qt::red);
00170         group->setIcon(icon);
00171         return true;
00172     }
00173     return false;
00174 }
00175 
00176 void ProgramGroupingStrategy::checkGroup()
00177 {
00178     TaskGroup *group = qobject_cast<TaskGroup*>(sender()); 
00179     if (!group) {
00180         return;
00181     }
00182     if (group->members().size() <= 1) {
00183         closeGroup(group);
00184     }
00185 }
00186 
00187 }//namespace
00188 
00189 #include "programgroupingstrategy.moc"
00190 

libtaskmanager

Skip menu "libtaskmanager"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal