-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskmodel.cpp
92 lines (74 loc) · 1.73 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
86
87
88
89
90
91
#include "taskmodel.h"
TaskModel::TaskModel(QObject *parent) : QAbstractListModel(parent)
{
dbClient = new DbClient(this);
tasks = dbClient->getAll();
}
QVariant TaskModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return {};
}
Task task = tasks[index.row()];
switch (role) {
case Qt::DisplayRole:
return {};
case IdRole:
return QVariant::fromValue(task.id);
case SecondsRole:
return QVariant::fromValue(task.seconds);
case NameRole:
return QVariant::fromValue(task.name);
default:
return {};
}
}
int TaskModel::rowCount(const QModelIndex&) const
{
return tasks.size();
}
QHash<int, QByteArray> TaskModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[IdRole] = "id";
roles[SecondsRole] = "seconds";
roles[NameRole] = "name";
return roles;
}
void TaskModel::update(const long id, const long seconds, const QString &name)
{
for (Task &task : tasks) {
if (task.id == id) {
task.name = name;
task.seconds = seconds;
dbClient->update(task);
this->beginResetModel();
this->endResetModel();
return;
}
}
}
void TaskModel::create()
{
Task task = dbClient->insert("", 0);
this->beginResetModel();
tasks.append(task);
this->endResetModel();
}
void TaskModel::remove(const long id)
{
dbClient->remove(id);
int index = -1;
for (int i = 0; i < tasks.size(); ++i) {
if (tasks[i].id == id) {
index = i;
break;
}
}
if (index < 0) {
return;
}
this->beginResetModel();
tasks.removeAt(index);
this->endResetModel();
}