Added edit score functionality
[scorecard] / src / data.cpp
1 #include "data.h"
2
3 ////////////////////////////////////////////////////////////////////////
4 // Hole
5 ////////////////////////////////////////////////////////////////////////
6
7 Hole::Hole(const QXmlAttributes &attrs) {
8   if (attrs.index("num") != -1)
9     num = attrs.value("num");
10   if (attrs.index("shots") != -1)
11     shots = attrs.value("shots");
12   if (attrs.index("putts") != -1)
13     putts = attrs.value("putts");
14   if (attrs.index("hcp") != -1)
15     hcp = attrs.value("hcp");
16   if (attrs.index("length") != -1)
17     length = attrs.value("length");
18   if (attrs.index("par") != -1)
19     par = attrs.value("par");
20 }    
21
22 Hole::Hole(const QDomElement node) {
23   num = node.attribute("num", "");
24   shots = node.attribute("shots", "");
25   putts = node.attribute("putts", "");
26   hcp = node.attribute("hcp", "");
27   length = node.attribute("length", "");
28   par = node.attribute("par", "");
29 }
30
31 Hole::Hole(int num, QString &par, QString &hcp)
32 {
33   this->num = QString::number(num);
34   this->par = par;
35   this->hcp = hcp;
36 }
37
38 Hole::Hole(int num, QString &shots)
39 {
40   this->num = QString::number(num);
41   this->shots = shots;
42 }
43
44 QDomElement Hole::toElement(QDomDocument doc)
45 {
46   QDomElement node = doc.createElement("hole");
47
48   if (!num.isEmpty())
49     node.setAttribute("num", num);
50   if (!shots.isEmpty())
51     node.setAttribute("shots", shots);
52   if (!putts.isEmpty())
53     node.setAttribute("putts", putts);
54   if (!hcp.isEmpty())
55     node.setAttribute("hcp", hcp);
56   if (!length.isEmpty())
57     node.setAttribute("length", length);
58   if (!par.isEmpty())
59     node.setAttribute("par", par);
60
61   return node;
62 }
63
64 QString Hole::getShots() {
65   return shots;
66 }
67
68 void Hole::setShots(QString& s) {
69   shots = s;
70 }
71
72 QString Hole::getHcp() {
73   return hcp;
74 }
75
76 QString Hole::getPar() {
77   return par;
78 }
79
80 void Hole::dump() {
81   qDebug() << num << "(" << par << ") : " << shots << "/" << putts ; 
82 }
83
84 ////////////////////////////////////////////////////////////////////////
85 // Score
86 ////////////////////////////////////////////////////////////////////////
87
88 Score::Score(const QXmlAttributes &attrs) 
89 {
90   club = attrs.value("club");
91   course = attrs.value("course");
92   date = attrs.value("date");
93 }
94
95 Score::Score(QString &iClub, QString &iCourse, QString &iDate) 
96 {
97   club = iClub;
98   course = iCourse;
99   date = iDate;
100 }
101
102 Score::Score(QVector<QString> scores, QString &club, QString &course, QString &date) 
103 {
104   this->club = club;
105   this->course = course;
106   this->date = date;
107
108   for (int i = 0; i < scores.size(); i++) {
109     Hole *hole = new Hole(i+1, scores[i]);
110     holeList << hole;
111   }
112 }
113
114 Score::Score(const QDomElement node) {
115   club = node.attribute("club", "");
116   course = node.attribute("course", "");
117   date = node.attribute("date", "");
118 }
119
120 QDomElement Score::toElement(QDomDocument doc)
121 {
122   QDomElement node = doc.createElement("score");
123
124   node.setAttribute("club", club);
125   node.setAttribute("course", course);
126   node.setAttribute("date", date);
127
128   for (int i=0; i < holeList.size(); i++) {
129     Hole *hole = holeList.at(i);
130     node.appendChild(hole->toElement(doc));
131   }
132   return node;
133 }
134
135 int Score::update(QVector<QString> scores)
136 {
137   for (int i = 0; i < scores.size(); i++) {
138     Hole *hole = holeList.at(i);
139     if (hole->getShots() != scores[i])
140       hole->setShots(scores[i]);
141   }
142   return 0;
143 }
144
145 void Score::addHole(Hole *iHole) {
146   holeList << iHole;
147 }
148   
149 QString Score::getScore(int i) const 
150 {
151   if (i >= 0 && i < holeList.size())
152     return holeList.at(i)->getShots();
153   else
154     return QString("-");
155 }
156   
157 QString Score::getTotal(int what) const
158 {
159   int tot = 0;
160
161   if (what == Total)
162     for (int i=0; i <= 17; i++)
163       tot += holeList.at(i)->getShots().toInt();
164
165   if (what == TotalOut)
166     for (int i=0; i <= 8; i++)
167       tot += holeList.at(i)->getShots().toInt();
168  
169   if (what == TotalIn)
170     for (int i=9; i <= 17; i++)
171       tot += holeList.at(i)->getShots().toInt();
172
173   return QString("%1").arg(tot);
174 }
175
176 const QString& Score::getClubName() const
177 {
178   return club;
179 }
180
181 const QString& Score::getCourseName() const
182 {
183   return course;
184 }
185
186 const QString& Score::getDate() const
187 {
188   return date;
189 }
190
191 void Score::dump()
192 {
193   qDebug() << club << " " << course << " " << date ; 
194   for (int i=0; i<holeList.size(); i++)
195     holeList.at(i)->dump();
196 }
197
198 ////////////////////////////////////////////////////////////////////////
199 // Course
200 ////////////////////////////////////////////////////////////////////////
201
202 Course::Course(const QXmlAttributes &attrs) {
203   name = attrs.value("name");
204 }
205
206 Course::Course(const QDomElement node) {
207   name = node.attribute("name", "");
208 }
209
210 Course::Course(QString &name, 
211                QVector<QString> &par,
212                QVector<QString> &hcp)
213 {
214   this->name = name;
215   
216   for (int i = 0; i < par.size(); i++) {
217     Hole *hole = new Hole(i+1, par[i], hcp[i]);
218     holeList << hole;
219   }
220 }
221
222 QDomElement Course::toElement(QDomDocument doc)
223 {
224   QDomElement node = doc.createElement("course");
225
226   node.setAttribute("name", name);
227
228   for (int i=0; i < holeList.size(); i++) {
229     Hole *hole = holeList.at(i);
230     node.appendChild(hole->toElement(doc));
231   }
232   return node;
233 }
234
235 void Course::addHole(Hole *iHole) {
236   holeList << iHole;
237 }
238
239 QString Course::getPar(int i) {
240   if (i >= 0 && i < holeList.size())
241     return holeList.at(i)->getPar();
242   else
243     return QString("-");
244 }
245
246 QString Course::getHcp(int i) {
247   if (i >= 0 && i < holeList.size())
248     return holeList.at(i)->getHcp();
249   else
250     return QString("-");
251 }
252   
253 QString& Course::getName() 
254
255   return name; 
256 }
257
258 QString Course::getTotal(int what) {
259   int tot = 0;
260
261   if (what == Total)
262     for (int i = 0; i < 18; i++)
263       tot += holeList.at(i)->getPar().toInt();
264
265   if (what == TotalOut)
266     for (int i = 0; i < 9; i++)
267       tot += holeList.at(i)->getPar().toInt();
268  
269   if (what == TotalIn)
270     for (int i = 9; i < 18; i++)
271       tot += holeList.at(i)->getPar().toInt();
272
273   return QString("%1").arg(tot);
274 }
275
276
277 void Course::dump() {
278   qDebug() << " " << name;
279   for (int i=0; i<holeList.size(); i++)
280     holeList.at(i)->dump();
281 }
282
283 ////////////////////////////////////////////////////////////////////////
284 // Club
285 ////////////////////////////////////////////////////////////////////////
286
287 Club::Club(const QXmlAttributes &attrs) {
288   name = attrs.value("name");
289 }
290
291 Club::Club(const QDomElement node) {
292   name = node.attribute("name", "");
293 }
294
295 Club::Club(QString &name)
296 {
297   this->name = name;
298 }
299
300 void Club::addCourse(Course *iCourse) {
301   courseList << iCourse;
302 }
303
304 QDomElement Club::toElement(QDomDocument doc)
305 {
306   QDomElement node = doc.createElement("club");
307
308   node.setAttribute("name", name);
309
310   for (int i=0; i < courseList.size(); i++) {
311     Course *course = courseList.at(i);
312     node.appendChild(course->toElement(doc));
313   }
314   return node;
315 }
316   
317 void Club::dump() {
318   qDebug() << name;
319   for (int i=0; i<courseList.size(); i++)
320     courseList.at(i)->dump();
321 }
322
323 QString& Club::getName() { return name; }
324
325 Course *Club::getCourse(int pos) {
326   return courseList.at(pos);
327 }
328
329 Course *Club::getCourse(const QString &courseName) 
330 {
331   QListIterator<Course *> i(courseList);
332   Course *c = 0;
333
334   while (i.hasNext()) {
335     c = i.next();
336     if (c->getName() == courseName) {
337       return c;
338     }
339   }
340   return 0;
341 }