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(AbstractGroupableItem *)), this, SLOT(handleItem(AbstractGroupableItem *)));
00084 connect(group, SIGNAL(itemAdded(AbstractGroupableItem *)), this, SLOT(check()));
00085 connect(group, SIGNAL(destroyed()), this, SLOT(removeGroup()));
00086 ItemList sortedList = group->members();
00087 sortItems(sortedList);
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
00108 if (item->isGroupItem()) {
00109 handleGroup(qobject_cast<TaskGroup*>(item));
00110 } else if (!(qobject_cast<TaskItem*>(item))->task()) {
00111 connect(item, SIGNAL(gotTaskPointer()), this, SLOT(check()));
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
00132
00133 if (!item->isGroupItem()) {
00134 if (!(qobject_cast<TaskItem*>(item))->task()) {
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
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--;
00182 }
00183
00184 if (oldIndex != newIndex) {
00185 return item->parentGroup()->moveItem(oldIndex, newIndex);
00186 }
00187
00188 return -1;
00189 }
00190
00191 }
00192
00193 #include "abstractsortingstrategy.moc"