5 #include "branchitem.h"
10 TaskModel::TaskModel(QObject *parent)
11 : QAbstractTableModel(parent)
16 QModelIndex TaskModel::index (Task* t)
18 int n=tasks.indexOf (t);
22 return createIndex (n,0,t);
25 QModelIndex TaskModel::indexRowEnd (Task* t)
27 int n=tasks.indexOf (t);
31 return createIndex (n,6,t);
34 Task* TaskModel::getTask (const QModelIndex &ix) const
37 return tasks.at (ix.row() );
42 int TaskModel::rowCount(const QModelIndex &parent) const
48 int TaskModel::columnCount(const QModelIndex &parent) const
54 QVariant TaskModel::data(const QModelIndex &index, int role) const
59 if (index.row() >= tasks.size() || index.row() < 0)
62 BranchItem *bi=tasks.at(index.row())->getBranch();
63 Task *t=tasks.at(index.row() );
65 if (role == Qt::DisplayRole)
67 if (index.column() == 0)
68 return t->getPriority();
69 else if (index.column() == 1)
70 return t->getStatusString() + " - " +t->getAwakeString();
71 else if (index.column() == 2)
72 return t->getAgeCreation();
73 else if (index.column() == 3)
74 return t->getAgeModified();
75 else if (index.column() == 4)
76 return t->getDaysSleep();
77 else if (index.column() == 5)
79 QString s = bi->getModel()->getMapName();
83 return bi->getModel()->getMapName();
85 else if (index.column() == 6)
87 BranchItem *bi = tasks.at(index.row())->getBranch();
88 return bi->getHeadingPlainWithParents( showParentsLevel );
90 } else if (role == Qt::DecorationRole && index.column() == 1)
92 return QIcon(":/flag-" + t->getIconString() + ".png");
94 else // role != Qt::DisplayRole
96 if (role == Qt::ForegroundRole && bi )
97 return bi->getHeadingColor();
98 if (role == Qt::BackgroundRole && bi )
100 BranchItem *frameBI=bi->getFramedParentBranch(bi);
101 if (frameBI && index.column() != 5)
103 BranchObj *bo=frameBI->getBranchObj();
105 // Return frame background
106 return bo->getFrameBrushColor();
110 // Return map background
111 return bi->getModel()->getMapBackgroundColor();
119 QVariant TaskModel::headerData(int section, Qt::Orientation orientation, int role) const
121 if (role != Qt::DisplayRole)
124 if (orientation == Qt::Horizontal) {
127 return tr("Prio","TaskEditor");
129 return tr("Status","TaskEditor");
131 return tr("Age total","TaskEditor");
133 return tr("Age mod.","TaskEditor");
135 return tr("Sleep","TaskEditor");
137 return tr("Map","TaskEditor");
139 return tr("Task","TaskEditor");
147 bool TaskModel::insertRows(int position, int rows, const QModelIndex &index, Task* t)
150 beginInsertRows(QModelIndex(), position, position+rows-1);
152 for (int row=0; row < rows; row++)
153 tasks.insert(position, t);
159 bool TaskModel::removeRows(int position, int rows, const QModelIndex &index)
162 beginRemoveRows(QModelIndex(), position, position+rows-1);
164 for (int row=0; row < rows; ++row)
165 delete (tasks.takeAt(position)) ;
171 bool TaskModel::setData(const QModelIndex &index, Task* t, int role)
173 if (index.isValid() && role == Qt::EditRole)
175 int row = index.row();
177 tasks.replace(row, t);
178 emit(dataChanged(index, index));
186 void TaskModel::emitDataChanged (Task* t)
188 QModelIndex ix=index (t);
193 while (col<columnCount(QModelIndex() ) )
195 ix=createIndex (row, col, t);
196 if (ix.isValid() ) emit(dataChanged(ix,ix) );
202 Qt::ItemFlags TaskModel::flags(const QModelIndex &index) const
204 if (!index.isValid())
205 return Qt::ItemIsEnabled;
207 return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
210 int TaskModel::count (VymModel *model)
212 if (!model) return tasks.size();
214 foreach (Task *t,tasks) if (t->getBranch()->getModel()==model) n++;
218 Task* TaskModel::createTask (BranchItem *bi)
222 foreach (Task* t, tasks)
224 if (t->getBranch()==bi)
226 qWarning()<<"TaskModel::createTask Branch exists already!";
230 Task* task=new Task(this);
231 task->setBranch (bi);
232 task->setAwake(Task::Morning);
233 insertRows(tasks.count(), 1, QModelIndex(),task);
239 qWarning()<<"TaskEditor::addItem - item exists";
243 void TaskModel::deleteTask (Task* t)
245 int pos=tasks.indexOf(t);
247 removeRows(pos, 1,QModelIndex() );
250 void TaskModel::recalcPriorities()
252 emit (layoutAboutToBeChanged() );
254 foreach (Task *t,tasks)
257 BranchItem *bi=t->getBranch();
260 switch (t->getStatus() )
262 case Task::NotStarted: break;
263 case Task::WIP: p+=10; break;
264 case Task::Finished: p+=2000; break;
267 // Awake and sleeping
268 switch (t->getAwake() )
270 case Task::Morning: p-=1000; break;
271 case Task::WideAwake: break;
272 case Task::Sleeping: p+=1000 + t->getDaysSleep(); break;
275 // Color (importance)
276 QColor c = bi->getHeadingColor();
278 // light blueish green
279 if (c == QColor ("#00aa7f") ) p -= 20;
281 // green (e.g. from vym < 2.6.3 with #005500)
282 if (c.red() == 0 && c.blue() == 0 && c.green() < 160) p -= 40;
285 if (c == QColor ("#d95100") ) p -= 60;
288 if (c == QColor ("#ff0000") ) p -= 80;
291 if (bi->hasActiveStandardFlag ("stopsign") ) p-=800;
294 p-=t->getAgeModified();
295 p-=t->getAgeCreation() * 1.0 / 365 * 80; // After a year, this is as important as "red"
297 // Position in subtree
301 if (p<minPrio) minPrio=p;
303 // Normalize, so that most important task has prio 1
304 foreach (Task *t,tasks)
306 t->setPriority (1- minPrio + t->getPriority() );
307 //emitDataChanged (t);
310 emit (layoutChanged() );
313 void TaskModel::setShowParentsLevel(uint i)
315 showParentsLevel = i;
316 recalcPriorities(); // Triggers update of view
319 uint TaskModel::getShowParentsLevel()
321 return showParentsLevel;