5 #include "branchitem.h"
10 TaskModel::TaskModel(QObject *parent) : QAbstractTableModel(parent)
14 QSize size = QSize(22, 22);
15 QSize size2 = QSize(44, 22);
18 QIcon(QPixmap(":/flag-arrow-up.svg").scaled(size, Qt::KeepAspectRatio));
19 arrow_2up_icon = QIcon(
20 QPixmap(":/flag-arrow-2up.svg").scaled(size, Qt::KeepAspectRatio));
23 QIcon(QPixmap(":/flag-task-new.svg").scaled(size, Qt::KeepAspectRatio));
24 task_new_morning_icon = QIcon(QPixmap(":/flag-task-new-morning.svg")
25 .scaled(size, Qt::KeepAspectRatio));
26 task_new_sleeping_icon = QIcon(QPixmap(":/flag-task-new-sleeping.svg")
27 .scaled(size, Qt::KeepAspectRatio));
30 QIcon(QPixmap(":/flag-task-wip.svg").scaled(size, Qt::KeepAspectRatio));
31 task_wip_morning_icon = QIcon(QPixmap(":/flag-task-wip-morning.svg")
32 .scaled(size, Qt::KeepAspectRatio));
33 task_wip_sleeping_icon = QIcon(QPixmap(":/flag-task-wip-sleeping.svg")
34 .scaled(size, Qt::KeepAspectRatio));
36 task_finished_icon = QIcon(
37 QPixmap(":/flag-task-finished.svg").scaled(size, Qt::KeepAspectRatio));
39 taskfilter_stopsign_icon =
40 QIcon(QPixmap(":/flag-stopsign.svg").scaled(size, Qt::KeepAspectRatio));
41 taskfilter_stopsign_arrow_up_icon =
42 QIcon(QPixmap(":/flag-stopsign-arrow-up.png")
43 .scaled(size2, Qt::KeepAspectRatio));
44 taskfilter_stopsign_arrow_2up_icon =
45 QIcon(QPixmap(":/flag-stopsign-arrow-2up.png")
46 .scaled(size2, Qt::KeepAspectRatio));
49 QModelIndex TaskModel::index(Task *t) const
51 int n = tasks.indexOf(t);
55 return createIndex(n, 0, t);
58 QModelIndex TaskModel::indexRowEnd(Task *t)
60 int n = tasks.indexOf(t);
64 return createIndex(n, 8, t);
67 Task *TaskModel::getTask(const QModelIndex &ix) const
70 return tasks.at(ix.row());
75 Task *TaskModel::getTask(const int i) const
77 if (i >= 0 && i < count())
78 return getTask(createIndex(i, 0));
83 int TaskModel::rowCount(const QModelIndex &parent) const
89 int TaskModel::columnCount(const QModelIndex &parent) const
95 QVariant TaskModel::data(const QModelIndex &index, int role) const
100 if (index.row() >= tasks.size() || index.row() < 0)
103 BranchItem *bi = tasks.at(index.row())->getBranch();
104 Task *t = tasks.at(index.row());
106 if (role == Qt::DisplayRole) {
107 if (index.column() == 0)
108 return t->getPriority();
109 else if (index.column() == 1)
110 return t->getPriorityDelta();
111 else if (index.column() == 2)
112 return QString(); // return t->getStatusString() + " - "
113 // +t->getAwakeString();
114 else if (index.column() == 3)
115 return t->getAgeCreation();
116 else if (index.column() == 4)
117 return t->getAgeModification();
118 else if (index.column() == 5) {
119 if (t->getDaysSleep() > 0)
120 return t->getDaysSleep();
124 else if (index.column() == 6) {
125 QString s = bi->getModel()->getMapName();
129 return bi->getModel()->getMapName();
131 else if (index.column() == 8) {
132 BranchItem *bi = tasks.at(index.row())->getBranch();
133 return bi->getHeadingPlainWithParents(showParentsLevel);
136 else if (role == Qt::DecorationRole && index.column() == 2) {
137 QString s = t->getIconString();
139 return task_new_icon;
140 else if (s == "task-new-morning")
141 return task_new_morning_icon;
142 else if (s == "task-new-sleeping")
143 return task_new_sleeping_icon;
144 else if (s == "task-wip")
145 return task_wip_icon;
146 else if (s == "task-wip-sleeping")
147 return task_wip_sleeping_icon;
148 else if (s == "task-wip-morning")
149 return task_wip_morning_icon;
150 else if (s == "task-finished")
151 return task_finished_icon;
153 qWarning() << "Unknown task type in TaskModel::data: " << s;
157 else if (role == Qt::DecorationRole && index.column() == 7) {
158 BranchItem *bi = t->getBranch();
159 if (bi->hasActiveFlag("stopsign")) {
160 if (bi->hasActiveFlag("2arrow-up"))
161 return taskfilter_stopsign_arrow_2up_icon;
162 else if (bi->hasActiveFlag("arrow-up"))
163 return taskfilter_stopsign_arrow_up_icon;
165 return taskfilter_stopsign_icon;
168 if (bi->hasActiveFlag("2arrow-up"))
169 return arrow_2up_icon;
170 else if (bi->hasActiveFlag("arrow-up"))
171 return arrow_up_icon;
175 else // role != Qt::DisplayRole
177 if (role == Qt::EditRole && index.column() == 1) // DeltaPrio
178 return t->getPriorityDelta();
179 if (role == Qt::ForegroundRole && bi)
180 return bi->getHeadingColor();
181 if (role == Qt::BackgroundRole && bi) {
182 BranchItem *frameBI = bi->getFramedParentBranch(bi);
183 if (frameBI && index.column() != 5) {
184 BranchObj *bo = frameBI->getBranchObj();
186 // Return frame background
187 return bo->getFrameBrushColor();
190 // Return map background
191 return bi->getModel()->getMapBackgroundColor();
199 QVariant TaskModel::headerData(int section, Qt::Orientation orientation,
202 if (role != Qt::DisplayRole)
205 if (orientation == Qt::Horizontal) {
208 return tr("Prio", "TaskEditor");
210 return tr("Delta", "TaskEditor");
212 return tr("Status", "TaskEditor");
214 return tr("Age total", "TaskEditor");
216 return tr("Age mod.", "TaskEditor");
218 return tr("Sleep", "TaskEditor");
220 return tr("Map", "TaskEditor");
222 return tr("Flags", "TaskEditor");
224 return tr("Task", "TaskEditor");
232 bool TaskModel::insertRows(int position, int rows, const QModelIndex &index,
236 beginInsertRows(QModelIndex(), position, position + rows - 1);
238 for (int row = 0; row < rows; row++)
239 tasks.insert(position, t);
245 bool TaskModel::removeRows(int position, int rows, const QModelIndex &index)
248 beginRemoveRows(QModelIndex(), position, position + rows - 1);
250 for (int row = 0; row < rows; ++row)
251 delete (tasks.takeAt(position));
257 bool TaskModel::setData(const QModelIndex &index, const QVariant &value,
260 if (index.isValid() && role == Qt::EditRole) {
261 Task *t = tasks.at(index.row());
263 qWarning() << "TaskModel::setData no task found";
267 if (index.column() == 1) // set Delta Priority
269 BranchItem *bi = t->getBranch();
270 VymModel *m = bi->getModel();
271 m->setTaskPriorityDelta(value.toInt(), bi);
273 emit(dataChanged(index, index));
276 if (index.column() == 8) // set Heading
278 BranchItem *bi = t->getBranch();
279 VymModel *m = bi->getModel();
280 m->setHeadingPlainText(value.toString(), bi);
281 emit(dataChanged(index, index));
289 void TaskModel::emitDataChanged(Task *t)
291 QModelIndex ix = index(t);
295 while (col < columnCount(QModelIndex())) {
296 ix = createIndex(row, col, t);
298 emit(dataChanged(ix, ix));
304 Qt::ItemFlags TaskModel::flags(const QModelIndex &index) const
306 if (!index.isValid())
307 return Qt::ItemIsEnabled;
309 return QAbstractTableModel::flags(index) | Qt::ItemIsDragEnabled |
310 Qt::ItemIsDropEnabled | Qt::ItemIsEditable;
313 int TaskModel::count(VymModel *model) const
318 foreach (Task *t, tasks)
319 if (t->getBranch()->getModel() == model)
324 Task *TaskModel::createTask(BranchItem *bi)
327 foreach (Task *t, tasks) {
328 if (t->getBranch() == bi) {
329 qWarning() << "TaskModel::createTask Branch exists already!";
333 Task *task = new Task(this);
335 task->setAwake(Task::Morning);
336 insertRows(tasks.count(), 1, QModelIndex(), task);
342 qWarning() << "TaskEditor::addItem - item exists";
346 void TaskModel::deleteTask(Task *t)
348 int pos = tasks.indexOf(t);
350 removeRows(pos, 1, QModelIndex());
353 bool TaskModel::updateAwake(bool force)
355 bool awake_changed = false;
356 foreach (Task *t, tasks) {
357 if (t->updateAwake() || force) {
358 t->getBranch()->updateTaskFlag();
359 awake_changed = true;
362 return awake_changed;
365 void TaskModel::recalcPriorities()
367 emit(layoutAboutToBeChanged());
368 int minPrio = 1000000;
369 foreach (Task *t, tasks) {
371 BranchItem *bi = t->getBranch();
374 switch (t->getStatus()) {
375 case Task::NotStarted:
385 // Awake and sleeping
386 switch (t->getAwake()) {
390 case Task::WideAwake:
393 //p += 1000 + t->getDaysSleep();
397 // Color (importance)
398 QColor c = bi->getHeadingColor();
400 // light blueish green
401 if (c == QColor("#00aa7f"))
404 // green (e.g. from vym < 2.6.3 with #005500)
405 if (c.red() == 0 && c.blue() == 0 && c.green() < 160)
409 if (c == QColor("#d95100"))
413 if (c == QColor("#ff0000"))
417 if (bi->hasActiveFlag("stopsign"))
419 if (bi->hasActiveFlag("2arrow-up"))
421 if (bi->hasActiveFlag("arrow-up"))
425 p -= t->getAgeModification();
426 p -= t->getAgeCreation() * 1.0 / 365 *
427 80; // After a year, this is as important as "red"
429 // Position in subtree
432 // Priority delta (set menually)
433 p -= t->getPriorityDelta();
435 // Set priority finally
440 // Normalize, so that most important task has prio 1
441 foreach (Task *t, tasks) {
442 t->setPriority(1 - minPrio + t->getPriority());
445 emit(layoutChanged());
448 void TaskModel::setShowParentsLevel(uint i)
450 showParentsLevel = i;
451 recalcPriorities(); // Triggers update of view
454 uint TaskModel::getShowParentsLevel() { return showParentsLevel; }
456 Qt::DropActions TaskModel::supportedDropActions() const
458 return Qt::MoveAction;
461 QStringList TaskModel::mimeTypes() const
464 types << "application/vnd.text.list";
468 QMimeData *TaskModel::mimeData(const QModelIndexList &indexes) const
470 QMimeData *mimeData = new QMimeData();
471 QByteArray encodedData;
473 QDataStream stream(&encodedData, QIODevice::WriteOnly);
475 if (indexes.count() > 0 && indexes.first().isValid()) {
476 Task *task = getTask(indexes.first());
479 QString text = task->getBranch()->getHeadingPlain();
483 stream << QString::number(index(task).row());
486 mimeData->setData("application/vnd.text.list", encodedData);
490 bool TaskModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
491 int row, int column, const QModelIndex &parent)
495 if (action == Qt::IgnoreAction)
498 if (!data->hasFormat("application/vnd.text.list"))
504 QByteArray encodedData = data->data("application/vnd.text.list");
505 QDataStream stream(&encodedData, QIODevice::ReadOnly);
506 QStringList newItems;
509 while (!stream.atEnd()) {
516 Task *dst = getTask(parent);
517 Task *src = getTask(newItems[1].toInt());
519 // qDebug() << "Dropping: " << src->getBranch()->getHeadingPlain() << " on
520 // " << dst->getBranch()->getHeadingPlain();
522 int delta_p = dst->getPriority() - src->getPriority();
524 src->setPriorityDelta(src->getPriorityDelta() - delta_p + 1);
525 BranchItem *bi = src->getBranch();
526 bi->getModel()->emitDataChanged(bi);
527 bi->getModel()->setChanged();