-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.cpp
164 lines (137 loc) · 4.33 KB
/
user.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
#include "user.h"
#include <fstream>
#include <qtxml/qdomdocument>
#include <QFile.h>
/****************************************/
User::User(
const QString& name,
const QString& log_directory,
double weight,
int hr_zone1,
int hr_zone2,
int hr_zone3,
int hr_zone4,
int hr_zone5):
_name(name),
_log_directory(log_directory),
_weight(weight),
_hr_zone1(hr_zone1),
_hr_zone2(hr_zone2),
_hr_zone3(hr_zone3),
_hr_zone4(hr_zone4),
_hr_zone5(hr_zone5)
{}
/****************************************/
User::User()
{
}
/****************************************/
User::~User()
{}
/****************************************/
const QString& User::name() const
{
return _name;
}
/****************************************/
const QString& User::logDirectory() const
{
return _log_directory;
}
/****************************************/
double User::weight() const
{
return _weight;
}
/****************************************/
int User::zone1() const
{
return _hr_zone1;
}
/****************************************/
int User::zone2() const
{
return _hr_zone2;
}
/****************************************/
int User::zone3() const
{
return _hr_zone3;
}
/****************************************/
int User::zone4() const
{
return _hr_zone4;
}
/****************************************/
int User::zone5() const
{
return _hr_zone5;
}
/****************************************/
bool User::readFromFile(const QString& filename)
{
QDomDocument dom_document;
QString error_msg;
int error_line, error_column;
QFile file(filename);
bool read_success = dom_document.setContent(&file, &error_msg, &error_line, &error_column);
if (read_success)
{
QDomElement user = dom_document.documentElement();
_name = user.firstChildElement("Name").firstChild().nodeValue();
_log_directory = user.firstChildElement("LogDirectory").firstChild().nodeValue();
_weight = user.firstChildElement("Weight").firstChild().nodeValue().toDouble();
_hr_zone1 = user.firstChildElement("HRZone1").firstChild().nodeValue().toDouble();
_hr_zone2 = user.firstChildElement("HRZone2").firstChild().nodeValue().toDouble();
_hr_zone3 = user.firstChildElement("HRZone3").firstChild().nodeValue().toDouble();
_hr_zone4 = user.firstChildElement("HRZone4").firstChild().nodeValue().toDouble();
_hr_zone5 = user.firstChildElement("HRZone5").firstChild().nodeValue().toDouble();
}
return read_success;
}
/****************************************/
void User::writeToFile(const QString& filename) const
{
QDomDocument dom_document;
QDomElement user = dom_document.createElement("User");
dom_document.appendChild(user);
QDomElement name = dom_document.createElement("Name");
user.appendChild(name);
QDomText text = dom_document.createTextNode(_name);
name.appendChild(text);
QDomElement log_dir = dom_document.createElement("LogDirectory");
user.appendChild(log_dir);
text = dom_document.createTextNode(_log_directory);
log_dir.appendChild(text);
QDomElement weight = dom_document.createElement("Weight");
user.appendChild(weight);
text = dom_document.createTextNode(QString::number(_weight,'f',2));
weight.appendChild(text);
QDomElement hr_zone1 = dom_document.createElement("HRZone1");
user.appendChild(hr_zone1);
text = dom_document.createTextNode(QString::number(_hr_zone1,'f',2));
hr_zone1.appendChild(text);
QDomElement hr_zone2 = dom_document.createElement("HRZone2");
user.appendChild(hr_zone2);
text = dom_document.createTextNode(QString::number(_hr_zone2,'f',2));
hr_zone2.appendChild(text);
QDomElement hr_zone3 = dom_document.createElement("HRZone3");
user.appendChild(hr_zone3);
text = dom_document.createTextNode(QString::number(_hr_zone3,'f',2));
hr_zone3.appendChild(text);
QDomElement hr_zone4 = dom_document.createElement("HRZone4");
user.appendChild(hr_zone4);
text = dom_document.createTextNode(QString::number(_hr_zone4,'f',2));
hr_zone4.appendChild(text);
QDomElement hr_zone5 = dom_document.createElement("HRZone5");
user.appendChild(hr_zone5);
text = dom_document.createTextNode(QString::number(_hr_zone5,'f',2));
hr_zone5.appendChild(text);
const int indent = 4;
QString xml = dom_document.toString(indent);
std::ofstream file;
file.open(filename.toStdString().c_str());
file << xml.toStdString();
file.close();
}