-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivitymodel.cpp
More file actions
167 lines (134 loc) · 4.71 KB
/
activitymodel.cpp
File metadata and controls
167 lines (134 loc) · 4.71 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "activitymodel.h"
#include <QDebug>
#include <QMessageBox>
ActivityModel::ActivityModel(QObject* parent) : QAbstractListModel(parent) {}
void ActivityModel::addActivity(Activity* activity) {
beginInsertRows(QModelIndex(), rowCount(), rowCount());
activities.append(activity);
endInsertRows();
}
int ActivityModel::rowCount(const QModelIndex & parent) const {
Q_UNUSED(parent);
return activities.count();
}
QVariant ActivityModel::data(const QModelIndex& index, int role) const {
if (index.row() < 0 || index.row() >= activities.count()) {
return QVariant();
}
Activity* activity = activities[index.row()];
if (role == NameRole) {
return activity->getName();
} else if (role == DurationRole) {
return activity->getDuration();
} else if (role == LockingRole) {
return activity->isLocking();
} else if (role == NotifyingRole) {
return activity->isNotifying();
} else {
return QVariant();
}
}
void ActivityModel::setData(const int& index, const QVariant &value, int role) {
if (index < 0 || index >= activities.count()) {
return;
}
Activity* activity = activities[index];
if (role == NameRole) {
activity->setName(value.toString());
} else if (role == DurationRole) {
activity->setDuration(value.toInt());
} else if (role == LockingRole) {
activity->setLocking(value.toBool());
} else if (role == NotifyingRole) {
activity->setNotifying(value.toBool());
}
saveActivitiesToFile(getActivityFile());
}
QString ActivityModel::getActivityFile() {
return qApp->applicationDirPath() + "/activities.xml";
}
QHash<int, QByteArray> ActivityModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
roles[DurationRole] = "duration";
roles[LockingRole] = "locking";
roles[NotifyingRole] = "notifying";
return roles;
}
QList<Activity*> ActivityModel::getActivities() {
return activities;
}
void ActivityModel::loadActivitiesFromFile(const QString& filename) {
// Open file
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(0, "Failed to open settings file", QString("Failed to open settings file at: '%1', possibly the file is corrupt or non-readable %2").arg(filename, file.errorString()));
return;
}
// Start parsing of XML
QXmlStreamReader xmlReader;
xmlReader.setDevice(&file);
// Read all activities into list (keep in memory, otherwise
// the model will attempt to save the xml file we are currently
// reading)
QList<Activity*> tmpActivities;
if (xmlReader.readNextStartElement() && xmlReader.name() == "activities") {
while (xmlReader.readNextStartElement()) {
// Parse activity
if (xmlReader.name() == "activity") {
Activity* activity = new Activity("", 0, false);
foreach (QXmlStreamAttribute attribute, xmlReader.attributes()) {
if (attribute.name() == "duration") {
activity->setDuration(attribute.value().toInt());
} else if (attribute.name() == "locking") {
activity->setLocking((bool) attribute.value().toInt());
} else if (attribute.name() == "notifying") {
activity->setNotifying((bool) attribute.value().toInt());
}
}
activity->setName(xmlReader.readElementText());
tmpActivities.append(activity);
}
}
}
if (xmlReader.hasError()) {
qDebug() << "XML error: " << xmlReader.errorString().data();
}
// Close file
file.close();
// Add all activities to model
foreach (Activity* activity, tmpActivities) {
addActivity(activity);
}
}
void ActivityModel::addNewActivity() {
addActivity(new Activity("Untitled activity", 10, false));
}
void ActivityModel::saveActivitiesToFile(const QString& filename) {
// Open file
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::critical(0, "Failed to open settings file", QString("Failed to open settings file at: '%1', possibly the file is corrupt or non-writable").arg(filename));
return;
}
// Start XML document
QXmlStreamWriter xmlWriter;
xmlWriter.setAutoFormatting(true);
xmlWriter.setDevice(&file);
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement("activities");
// Write activity
foreach(Activity* activity, activities) {
xmlWriter.writeStartElement("activity");
xmlWriter.writeAttribute("duration", QString::number(activity->getDuration()));
xmlWriter.writeAttribute("locking", QString::number(activity->isLocking()));
xmlWriter.writeAttribute("notifying", QString::number(activity->isNotifying()));
xmlWriter.writeCharacters(activity->getName());
xmlWriter.writeEndElement();
}
// Finalize XML document
xmlWriter.writeEndElement();
xmlWriter.writeEndDocument();
// Close file
file.close();
}