-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtaskmodel.cpp
85 lines (68 loc) · 1.92 KB
/
taskmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "taskmodel.h"
#include <QSettings>
#include <QDebug>
namespace model {
TaskModel::TaskModel(QObject *parent)
: QAbstractListModel(parent)
{
}
TaskModel::~TaskModel()
{
m_taskList.clear();
}
int TaskModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : m_taskList.size();
}
QVariant TaskModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if ((index.row() >= 0 || index.row() < m_taskList.count()) ) {
if (role == TaskTextRole) {
return m_taskList[index.row()]->title();
} else if (role == TaskIsDoneRole) {
return m_taskList[index.row()]->done();
} else if (role == TaskIndexRole) {
return index.row();
} else if (role == ObjectRole) {
return QVariant::fromValue((QObject*)m_taskList[index.row()]);
}
}
return QVariant();
}
QHash<int, QByteArray> TaskModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[TaskTextRole] = "taskText";
roles[TaskIsDoneRole] = "isDone";
roles[TaskIndexRole] = "taskIndex";
roles[ObjectRole] = "object";
return roles;
}
void TaskModel::addTask(TaskItem *task)
{
for(int i = task->order(); i < size(); ++i) {
m_taskList.at(i)->setOrder(i+1);
}
beginInsertRows(QModelIndex(), task->order(), task->order());
m_taskList.insert(task->order(), task);
endInsertRows();
sizeChanged(size());
}
bool TaskModel::takeTask(TaskItem *task)
{
if (m_taskList.contains(task)) {
for(int i = task->order() + 1; i < size(); ++i) {
m_taskList.at(i)->setOrder(i-1);
}
beginRemoveRows(QModelIndex(), m_taskList.indexOf(task), m_taskList.indexOf(task));
m_taskList.removeOne(task);
endRemoveRows();
sizeChanged(size());
return true;
} else {
return false;
}
}
} // namespace model