• Skip to content
  • Skip to link menu
KDE 4.5 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(AbstractGroupableItem *)), this, SLOT(handleItem(AbstractGroupableItem *)));
00084     connect(group, SIGNAL(itemAdded(AbstractGroupableItem *)), this, SLOT(check())); //groups don't have the full windowlist from the beginning, recheck them (for manual sorting)
00085     connect(group, SIGNAL(destroyed()), this, SLOT(removeGroup())); //FIXME necessary?
00086     ItemList sortedList = group->members();
00087     sortItems(sortedList); //the sorting doesn't work with totally unsorted lists, therefore we sort it in the correct order the first time
00088 
00089     foreach (AbstractGroupableItem *item, sortedList) {
00090         handleItem(item);
00091     }
00092 }
00093 
00094 void AbstractSortingStrategy::removeGroup()
00095 {
00096     TaskGroup *group = dynamic_cast<TaskGroup*>(sender());
00097 
00098     if (!group) {
00099         return;
00100     }
00101 
00102     d->managedGroups.removeAll(group);
00103 }
00104 
00105 void AbstractSortingStrategy::handleItem(AbstractGroupableItem *item)
00106 {
00107     //kDebug() << item->name();
00108     if (item->isGroupItem()) {
00109         handleGroup(qobject_cast<TaskGroup*>(item));
00110     } else if (!(qobject_cast<TaskItem*>(item))->task()) { //ignore startup tasks
00111         connect(item, SIGNAL(gotTaskPointer()), this, SLOT(check())); //sort the task as soon as it is a real one
00112         return;
00113     }
00114 
00115     check(item);
00116 }
00117 
00118 void AbstractSortingStrategy::check(AbstractGroupableItem *itemToCheck)
00119 {
00120     AbstractGroupableItem *item;
00121     if (!itemToCheck) {
00122         item = dynamic_cast<AbstractGroupableItem *>(sender());
00123     } else {
00124         item = itemToCheck;
00125     }
00126 
00127     if (!item) {
00128         kDebug() << "invalid item";
00129         return;
00130     }
00131     //kDebug() << item->name();
00132 
00133     if (!item->isGroupItem()) {
00134         if (!(qobject_cast<TaskItem*>(item))->task()) { //ignore startup tasks
00135             return;
00136         }
00137     }
00138 
00139     if (!item->parentGroup()) {
00140         kDebug() << "No parent group";
00141         return;
00142     }
00143 
00144     ItemList sortedList = item->parentGroup()->members();
00145     sortItems(sortedList);
00146 
00147     int oldIndex = item->parentGroup()->members().indexOf(item);
00148     int newIndex = sortedList.indexOf(item);
00149     if (oldIndex != newIndex) {
00150         item->parentGroup()->moveItem(oldIndex, newIndex);
00151     }
00152 }
00153 
00154 void AbstractSortingStrategy::sortItems(ItemList &items)
00155 {
00156     Q_UNUSED(items)
00157 }
00158 
00159 bool AbstractSortingStrategy::manualSortingRequest(AbstractGroupableItem *item, int newIndex)
00160 {
00161     Q_UNUSED(item);
00162     Q_UNUSED(newIndex);
00163     return false;
00164 }
00165 
00166 bool AbstractSortingStrategy::moveItem(AbstractGroupableItem *item, int newIndex)
00167 {
00168     //kDebug() << "move to " << newIndex;
00169     if (!item->parentGroup()) {
00170         kDebug() << "error: no parentgroup but the item was asked to move";
00171         return false;
00172     }
00173 
00174     const ItemList list = item->parentGroup()->members();
00175     if ((newIndex < 0) || (newIndex >= list.size())) {
00176         newIndex = list.size();
00177     }
00178 
00179     int oldIndex = list.indexOf(item);
00180     if (newIndex > oldIndex) {
00181         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
00182     }
00183 
00184     if (oldIndex != newIndex) {
00185         return item->parentGroup()->moveItem(oldIndex, newIndex);
00186     }
00187 
00188     return -1;
00189 }
00190 
00191 } //namespace
00192 
00193 #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"
  • KStyles
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •     Animators
  •     Applets
  •     Engines
  • Solid Modules
  • System Settings
  •   SystemSettingsView
Generated for API Reference by doxygen 1.5.9-20090814
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