libtaskmanager
abstractsortingstrategy.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00077 if (d->managedGroups.contains(group) || !group) {
00078 return;
00079 }
00080
00081 d->managedGroups.append(group);
00082 disconnect(group, 0, this, 0);
00083 connect(group, SIGNAL(itemAdded(AbstractItemPtr)), this, SLOT(handleItem(AbstractItemPtr)));
00084 connect(group, SIGNAL(destroyed()), this, SLOT(removeGroup()));
00085 ItemList sortedList = group->members();
00086 sortItems(sortedList);
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
00107 if (item->isGroupItem()) {
00108 handleGroup(qobject_cast<TaskGroup*>(item));
00109 } else if (!(qobject_cast<TaskItem*>(item))->task()) {
00110 connect(item, SIGNAL(gotTaskPointer()), this, SLOT(check()));
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
00131
00132 if (!item->isGroupItem()) {
00133 if (!(qobject_cast<TaskItem*>(item))->task()) {
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
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--;
00174 }
00175
00176 if (oldIndex != newIndex) {
00177 return item->parentGroup()->moveItem(oldIndex, newIndex);
00178 }
00179
00180 return -1;
00181 }
00182
00183 }
00184
00185 #include "abstractsortingstrategy.moc"