-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostmodel.cpp
184 lines (151 loc) · 5.03 KB
/
postmodel.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "postmodel.h"
PostModel::PostModel(QString path)
{
points = 0;
modelFilePath = path;
loadModel();
}
int PostModel::rowCount(const QModelIndex &parent) const
{
return postData.size();
}
QHash<int, QByteArray> PostModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[TitleRole] = "title";
roles[DateRole] = "date";
roles[ContentRole] = "content";
roles[ReactionRole] = "reaction";
roles[WeightRole] = "weight";
roles[CaloriesRole] = "calories";
roles[RunRole] = "run";
roles[PhotosRole] = "photos";
return roles;
}
QVariant PostModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
if (row < 0 || row >= rowCount())
return QVariant();
Post p = postData[row];
switch (role)
{
case TitleRole: return p.getTitle();
case DateRole: return p.getDate();
case ContentRole: return p.getContent();
case ReactionRole: return p.getReaction();
case WeightRole: return p.getWeight();
case CaloriesRole: return p.getCalories();
case RunRole: return p.getRun();
case PhotosRole: return QVariant::fromValue(p.getPhotos());
default: return QVariant();
}
}
void PostModel::insertPost(QString title, QDateTime date, QString content, QString reaction, quint16 weight, quint16 calories, bool run, QList<QUrl> photos)
{
beginResetModel();
Post post(title, date, content, reaction, weight, calories, run, photos);
postData.insert(postData.begin(), post);
// postData.push_back(post);
endResetModel();
}
void PostModel::editPost(int index, QString title, QDateTime date, QString content, QString reaction, quint16 weight, quint16 calories, bool run, QList<QUrl> photos)
{
beginResetModel();
int size = postData.size();
if(index > -1 && index < size)
{
postData[index].setTitle(title);
postData[index].setDate(date);
postData[index].setContent(content);
postData[index].setReaction(reaction);
postData[index].setWeight(weight);
postData[index].setCalories(calories);
postData[index].setRun(run);
postData[index].setPhotos(photos);
}
endResetModel();
}
void PostModel::deletePost(int index)
{
beginResetModel();
vector<Post>::iterator itr = postData.begin();
advance(itr, index);
if(index != -1)
postData.erase(itr);
endResetModel();
}
void PostModel::addPoints(int points)
{
this->points += points;
}
void PostModel::loadModel()
{
cout << "Loading post model..." << endl;
QFile qfile(modelFilePath);
QTextStream qtxstream(&qfile);
qfile.open(QIODevice::ReadOnly | QIODevice::Text);
points = qtxstream.readLine().toInt();
int rows = qtxstream.readLine().toInt();
for (int i=0; i<rows; i++)
{
QString title = qtxstream.readLine();
QDateTime date = QDateTime::fromString(qtxstream.readLine(), "dd-MM-yyyy hh:mm");
// reads the multi row content
QString line, content;
while (true) {
line = qtxstream.readLine();
if (line == END_OF_CONTENT)
break;
content.append(line + "\n");
}
QString reaction = qtxstream.readLine();
quint16 weight = qtxstream.readLine().toInt();
quint16 calories = qtxstream.readLine().toInt();
bool run = qtxstream.readLine().toInt() > 0 ? true: false;
int photos_n = qtxstream.readLine().toInt();
std::cout << "photos_n=" << photos_n << std::endl;
QList<QUrl> photos = QList<QUrl>();
for (int j=0; j<photos_n; j++)
{
QUrl url(qtxstream.readLine());
photos.append(url);
}
insertPost(title, date, content, reaction, weight, calories, run, photos);
}
qfile.close();
}
void PostModel::saveModel()
{
cout << "Saving post model..." << endl;
QFile qfile(modelFilePath);
QTextStream qtxstream(&qfile);
qfile.open(QIODevice::WriteOnly | QIODevice::Text);
// write the number of points
qtxstream << points << endl;
// write the number of rows
qtxstream << rowCount() << endl;
// write each post data
vector<Post>::reverse_iterator itr;
for (itr = postData.rbegin(); itr != postData.rend(); itr++)
{
qtxstream << itr->getTitle().trimmed() << endl;
qtxstream << itr->getDate().toString("dd-MM-yyyy hh:mm") << endl;
qtxstream << itr->getContent().trimmed() << endl << END_OF_CONTENT << endl;
qtxstream << itr->getReaction() << endl;
qtxstream << itr->getWeight() << endl;
qtxstream << itr->getCalories() << endl;
qtxstream << (itr->getRun() ? 1: 0) << endl;
QList<QUrl> photos = itr->getPhotos();
qtxstream << photos.size() << endl;
if (!photos.isEmpty())
{
QList<QUrl>::iterator p_itr;
for (p_itr = photos.begin(); p_itr != photos.end(); p_itr++)
{
qtxstream << p_itr->toString().toUtf8() << endl;
}
}
}
qfile.close();
}