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

libtaskmanager

abstractsortingstrategy.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 "abstractsortingstrategy.h"
00025 
00026 #include "taskitem.h"
00027 #include "taskgroup.h"
00028 #include "taskmanager.h"
00029 
00030 #include <QtAlgorithms>
00031 #include <QList>
00032 
00033 #include <KDebug>
00034 
00035 
00036 namespace TaskManager
00037 {
00038 
00039 class AbstractSortingStrategy::Private
00040 {
00041 public:
00042     Private()
00043         : type(GroupManager::NoSorting)
00044     {
00045     }
00046 
00047     QList<TaskGroup*> managedGroups;
00048     GroupManager::TaskSortingStrategy type;
00049 };
00050 
00051 
00052 AbstractSortingStrategy::AbstractSortingStrategy(QObject *parent)
00053     :QObject(parent),
00054     d(new Private)
00055 {
00056 
00057 }
00058 
00059 AbstractSortingStrategy::~AbstractSortingStrategy()
00060 {
00061     delete d;
00062 }
00063 
00064 GroupManager::TaskSortingStrategy AbstractSortingStrategy::type() const
00065 {
00066     return d->type;
00067 }
00068 
00069 void AbstractSortingStrategy::setType(GroupManager::TaskSortingStrategy type)
00070 {
00071     d->type = type;
00072 }
00073 
00074 void AbstractSortingStrategy::handleGroup(TaskGroup *group)
00075 {
00076     //kDebug();
00077     if (d->managedGroups.contains(group) || !group) {
00078         return;
00079     }
00080 
00081     d->managedGroups.append(group);
00082     disconnect(group, 0, this, 0); //To avoid duplicate connections
00083     connect(group, SIGNAL(itemAdded(AbstractItemPtr)), this, SLOT(handleItem(AbstractItemPtr)));
00084     connect(group, SIGNAL(destroyed()), this, SLOT(removeGroup())); //FIXME necessary?
00085     ItemList sortedList = group->members();
00086     sortItems(sortedList); //the sorting doesn't work with totally unsorted lists, therefore we sort it in the correct order the first time
00087 
00088     foreach (AbstractItemPtr item, sortedList) {
00089         handleItem(item);
00090     }
00091 }
00092 
00093 void AbstractSortingStrategy::removeGroup()
00094 {
00095     TaskGroup *group = dynamic_cast<TaskGroup*>(sender());
00096 
00097     if (!group) {
00098         return;
00099     }
00100 
00101     d->managedGroups.removeAll(group);
00102 }
00103 
00104 void AbstractSortingStrategy::handleItem(AbstractItemPtr item)
00105 {
00106     //kDebug() << item->name();
00107     if (item->isGroupItem()) {
00108         handleGroup(qobject_cast<TaskGroup*>(item));
00109     } else if (!(qobject_cast<TaskItem*>(item))->task()) { //ignore startup tasks
00110         connect(item, SIGNAL(gotTaskPointer()), this, SLOT(check())); //sort the task as soon its a real one
00111         return;
00112     }
00113 
00114     check(item);
00115 }
00116 
00117 void AbstractSortingStrategy::check(AbstractItemPtr itemToCheck)
00118 {
00119     AbstractItemPtr item;
00120     if (!itemToCheck) {
00121         item = dynamic_cast<AbstractItemPtr>(sender());
00122     } else {
00123         item = itemToCheck;
00124     }
00125 
00126     if (!item) {
00127         kDebug() << "invalid item";
00128         return;
00129     }
00130     //kDebug() << item->name();
00131 
00132     if (!item->isGroupItem()) {
00133         if (!(qobject_cast<TaskItem*>(item))->task()) { //ignore startup tasks
00134             return;
00135         }
00136     }
00137 
00138     if (!item->parentGroup()) {
00139         kDebug() << "No parent group";
00140         return;
00141     }
00142 
00143     ItemList sortedList = item->parentGroup()->members();
00144     sortItems(sortedList);
00145 
00146     int oldIndex = item->parentGroup()->members().indexOf(item);
00147     int newIndex = sortedList.indexOf(item);
00148     if (oldIndex != newIndex) {
00149         item->parentGroup()->moveItem(oldIndex, newIndex);
00150     }
00151 }
00152 
00153 void AbstractSortingStrategy::desktopChanged(int newDesktop)
00154 {
00155     Q_UNUSED(newDesktop)
00156 }
00157 
00158 bool AbstractSortingStrategy::moveItem(AbstractItemPtr item, int newIndex)
00159 {
00160     //kDebug() << "move to " << newIndex;
00161     if (!item->parentGroup()) {
00162         kDebug() << "error: no parentgroup but the item was asked to move";
00163         return false;
00164     }
00165 
00166     const ItemList list = item->parentGroup()->members();
00167     if ((newIndex < 0) || (newIndex >= list.size())) {
00168         newIndex = list.size();
00169     }
00170 
00171     int oldIndex = list.indexOf(item);
00172     if (newIndex > oldIndex) {
00173         newIndex--; //the index has to be adjusted if we move the item from right to left because the item on the left is removed first
00174     }
00175 
00176     if (oldIndex != newIndex) {
00177         return item->parentGroup()->moveItem(oldIndex, newIndex);
00178     }
00179 
00180     return -1;
00181 }
00182 
00183 } //namespace
00184 
00185 #include "abstractsortingstrategy.moc"

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