-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatamanipulation.cpp
More file actions
339 lines (264 loc) · 10.8 KB
/
Copy pathdatamanipulation.cpp
File metadata and controls
339 lines (264 loc) · 10.8 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "datamanipulation.h"
#include <QString>
#include <QFile>
#include <QMessageBox>
#include <QStringList>
#include <QIODevice>
#include <QtXml/QtXml>
#include <QtXml/QDomDocument>
#include <QByteArray>
#include <QMap>
#include <QDataStream>
#include <QStandardPaths>
#include <QDir>
#include "routine.h"
typedef QString RoutineName;
typedef QString ExerciseName;
DataManipulation::DataManipulation() {
fileLocation();
}
void DataManipulation::fileLocation() {
QDir dir = QDir::root();
QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
if (!QDir(path).exists())
dir.mkpath(path);
filePath = path + "/data.xml";
file = new QFile(filePath);
if (!file->open(QIODevice::ReadWrite)) {
}
file->close();
if (file->size()==0) {
presetLibrary();
}
return;
}
void DataManipulation::presetLibrary() {
if(!file->open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate) ){
}
QByteArray content = file->readAll();
QDomDocument doc;
doc.setContent(content);
QDomProcessingInstruction instr = doc.createProcessingInstruction(
"xml", "version=\"1.0\" encoding=\"UTF-8\"");
doc.appendChild(instr);
QDomElement data = doc.createElement("data");
//---- writing exercises ----//
QDomElement exLib = doc.createElement("ExerciseLibrary");
QList<ExerciseName> exer;
exer.append("Bench Press");
exer.append("Bicep Curl");
exer.append("Crunch");
exer.append("Deadlift");
exer.append("Dips");
exer.append("Leg Press");
exer.append("Leg Throw");
exer.append("Lunge");
exer.append("Plank");
exer.append("Pull Up");
exer.append("Russian Twist");
exer.append("Sit Up");
exer.append("Squat");
exer.append("Tricep Extension");
int numE = exer.count();
for (int i = 1; i <= numE; i++) {
QDomElement newEx = doc.createElement("Exercise");
newEx.setAttribute("drill", i);
QDomText name = doc.createTextNode(exer[i-1]);
newEx.appendChild(name);
exLib.appendChild(newEx);
}
data.appendChild(exLib);
doc.appendChild(data);
QTextStream out(file);
out << doc.toString();
file->close();
return;
}
void DataManipulation::loadXML() {
if (!file->open(QIODevice::ReadOnly)) {
file->close();
return;
}
QByteArray content = file->readAll();
if (!doc.setContent(content)) {
file->close();
return;
}
file->close();
}
void DataManipulation::save(QMap<RoutineName, Routine> routinesData,
QMap<ExerciseName, Exercise> exercisesData) {
if( !file->open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate) )
return;
QByteArray content = file->readAll();
QDomDocument outDoc;
outDoc.setContent(content);
QDomProcessingInstruction instr = outDoc.createProcessingInstruction(
"xml", "version=\"1.0\" encoding=\"UTF-8\"");
outDoc.appendChild(instr);
QDomElement data = outDoc.createElement("data");
//---- saving exercises ----//
QDomElement exLib = outDoc.createElement("ExerciseLibrary");
QMap<ExerciseName, Exercise>::iterator e;
int numE = 1;
for (e = exercisesData.begin(); e != exercisesData.end(); ++e) {
QDomElement newEx = outDoc.createElement("Exercise");
newEx.setAttribute("drill", numE);
QDomText name = outDoc.createTextNode(e.key());
newEx.appendChild(name);
exLib.appendChild(newEx);
numE++;
}
data.appendChild(exLib);
//---- saving routines and the exercises in them ----//
QDomElement routineLib = outDoc.createElement("RoutineLibrary");
QMap<RoutineName, Routine>::iterator r;
int numR = 1;
for (r = routinesData.begin(); r != routinesData.end(); ++r) {
QDomElement newRoutine = outDoc.createElement("Routine");
newRoutine.setAttribute("num", numR);
QDomElement nameNode = outDoc.createElement("Name");
QDomText name = outDoc.createTextNode(r.key());
nameNode.appendChild(name);
newRoutine.appendChild(nameNode);
QStringList exInThisRoutine = r.value().getExercises();
for (int x = 0; x < exInThisRoutine.count(); x++) {
QDomElement ex = outDoc.createElement("Exercise");
ex.setAttribute("num", x+1);
QDomText exName = outDoc.createTextNode(exInThisRoutine[x]);
ex.appendChild(exName);
newRoutine.appendChild(ex);
}
routineLib.appendChild(newRoutine);
numR++;
}
//---- saving progress data----//
QDomElement trackLib = outDoc.createElement("TrackLibrary");
for (r = routinesData.begin(); r != routinesData.end(); ++r) {
QDomElement routine = outDoc.createElement("Routine");
routine.setAttribute("name", r->getName());
QList<QDate>::iterator d;
QList<QDate> dates = r->getDates();
for (d = dates.begin(); d != dates.end(); ++d) {
QDomElement workout = outDoc.createElement("Workout");
workout.setAttribute("date", d->toString(Qt::ISODate));
routine.appendChild(workout);
WorkoutData thisWorkout = r->getSpecificWorkoutByDate(*d);
int setInWorkout = thisWorkout.getSets();
QDomElement numSet = outDoc.createElement("numSet");
QDomText setNum = outDoc.createTextNode(QString::number(setInWorkout));
numSet.appendChild(setNum);
workout.appendChild(numSet);
QList<indivExerciseData>::iterator i;
QList<indivExerciseData> allExData = thisWorkout.getWorkoutData();
for (i = allExData.begin(); i != allExData.end(); ++i) {
QString exName = i->getName();
QDomElement ex = outDoc.createElement("Exercise");
ex.setAttribute("name", exName);
for (int s = 1; s <= setInWorkout; s++) {
indivExerciseData thisIndivEx = thisWorkout.getIndivEx(exName);
QList<int> allReps = thisIndivEx.getReps();
QList<int> allWeights = thisIndivEx.getWeight();
QDomElement set = outDoc.createElement("Set");
set.setAttribute("ID", s);
QDomElement rep = outDoc.createElement("Rep");
QDomText numRep = outDoc.createTextNode(QString::number(allReps[s-1]));
rep.appendChild(numRep);
QDomElement weight = outDoc.createElement("Weight");
QDomText numWeight = outDoc.createTextNode(QString::number(allWeights[s-1]));
weight.appendChild(numWeight);
set.appendChild(weight);
set.appendChild(rep);
ex.appendChild(set);
}
workout.appendChild(ex);
}
trackLib.appendChild(routine);
}
}
data.appendChild(routineLib);
data.appendChild(trackLib);
outDoc.appendChild(data);
QTextStream out(file);
out << outDoc.toString();
file->close();
return;
}
QMap<RoutineName, Routine> DataManipulation::getRoutineInfo(){
QMap<RoutineName, Routine> routinesInLib;
//---- loading the name of and the exercises in the routine ----//
QDomElement parent = doc.documentElement();
QDomNode routineLib = parent.firstChild().nextSibling();
QDomNode indivRoutine = routineLib.firstChild();
while (!indivRoutine.isNull()) {
QStringList exercisesInRoutine;
QDomElement name = indivRoutine.firstChild().toElement();
QDomNode exercise = name.nextSibling();
while (!exercise.isNull()) {
QDomElement nameExercise = exercise.toElement();
exercisesInRoutine.append(nameExercise.text());
exercise = exercise.nextSibling();
}
Routine routine;
routine.setNameAndExercises(name.text(),exercisesInRoutine);
routinesInLib[name.text()] = routine;
indivRoutine = indivRoutine.nextSibling();
}
//---- loading the progress data associated with different routines ----//
QDomNode trackLib = routineLib.nextSibling();
QDomNode routineData = trackLib.firstChild();
QList<WorkoutData> listRoutineData;
while (!routineData.isNull()) {
QString nameR = routineData.toElement().attributeNode("name").value();
Routine *thisRoutine = &routinesInLib[nameR];
QDomElement dateOfWorkout = routineData.firstChildElement();
while(!dateOfWorkout.isNull()) {
QDomAttr dateAttr = dateOfWorkout.attributeNode("date");
QDate date = QDate::fromString(dateAttr.value(),Qt::ISODate);
int numSet = dateOfWorkout.firstChild().toElement().text().toInt();
WorkoutData *thisWorkout = thisRoutine->appendToWorkoutDataList
(WorkoutData(date, numSet));
QList<indivExerciseData> exsData;
QDomElement indivExNameElement = dateOfWorkout.firstChild().nextSiblingElement();
while (!indivExNameElement.isNull()) {
QList<QPair<rep, weight> > repWeight;
QDomAttr nameAttr = indivExNameElement.attributeNode("name");
QString indivExName = nameAttr.value();
QDomNode diffSet = indivExNameElement.firstChild();
while (!diffSet.isNull()) {
QDomElement weightElement = diffSet.firstChildElement();
int weightValue = weightElement.text().toInt();
QDomElement repElement = weightElement.nextSiblingElement();
int repValue = repElement.text().toInt();
QPair<rep,weight> repWeightPerSet;
repWeightPerSet.first = repValue;
repWeightPerSet.second = weightValue;
repWeight.append(repWeightPerSet);
diffSet = diffSet.nextSibling();
}
indivExerciseData *thisExercise = new indivExerciseData(indivExName, numSet, repWeight);
exsData.push_back(*thisExercise);
indivExNameElement = indivExNameElement.nextSiblingElement();
}
thisWorkout->inputWorkoutData(exsData);
dateOfWorkout = dateOfWorkout.nextSiblingElement();
}
routineData = routineData.nextSibling();
}
return routinesInLib;
}
QMap<ExerciseName, Exercise> DataManipulation::getExercisesInfo() {
QMap<ExerciseName, Exercise> exercisesInLib;
QDomElement parent = doc.documentElement();
QDomNode exerciseLib = parent.firstChild();
QDomNode indivEx = exerciseLib.firstChild();
while (!indivEx.isNull()) {
QDomElement name = indivEx.toElement();
Exercise exercise;
exercisesInLib[name.text()] = exercise;
indivEx = indivEx.nextSibling();
}
return exercisesInLib;
}
DataManipulation::~DataManipulation() {
}