Low level support for more details in scores including putts, sand saves, fairwayhits...
[scorecard] / src / data.cpp
1 /*
2  * Copyright (C) 2009 Sakari Poussa
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, version 2.
7  */
8
9 #include "data.h"
10
11 ////////////////////////////////////////////////////////////////////////
12 // Hole
13 ////////////////////////////////////////////////////////////////////////
14 Hole::Hole(int num, QString &par, QString &hcp)
15 {
16     this->m_num = QString::number(num);
17     this->m_par = par;
18     this->m_hcp = hcp;
19 }
20
21 Hole::Hole(int num, QString &shots)
22 {
23     this->m_num = QString::number(num);
24     this->m_shots = shots;
25 }
26
27 // XML to internal converions
28 Hole::Hole(const QDomElement node) 
29 {
30     m_num = node.attribute("num", "");
31     m_hcp = node.attribute("hcp", "");
32     m_length = node.attribute("length", "");
33     m_par = node.attribute("par", "");
34     m_shots = node.attribute("shots", "");
35     m_putts = node.attribute("putts", "");
36     m_greenInRegulation = node.attribute("green-in-regulation", "");
37     m_fairwayHit = node.attribute("fairway-hit", "");
38     m_sandSave = node.attribute("sand-save", "");
39     m_penalty = node.attribute("penalty", "");
40 }
41
42 // Internal to XML conversion
43 QDomElement Hole::toElement(QDomDocument doc)
44 {
45     QDomElement node = doc.createElement("hole");
46
47     if (!m_num.isEmpty())
48         node.setAttribute("num", m_num);
49     if (!m_hcp.isEmpty())
50         node.setAttribute("hcp", m_hcp);
51     if (!m_length.isEmpty())
52         node.setAttribute("length", m_length);
53     if (!m_par.isEmpty())
54         node.setAttribute("par", m_par);
55     if (!m_shots.isEmpty())
56         node.setAttribute("shots", m_shots);
57     if (!m_putts.isEmpty())
58         node.setAttribute("putts", m_putts);
59     if (!m_greenInRegulation.isEmpty())
60         node.setAttribute("green-in-regulation", m_greenInRegulation);
61     if (!m_fairwayHit.isEmpty())
62         node.setAttribute("fairway-hit", m_fairwayHit);
63     if (!m_sandSave.isEmpty())
64         node.setAttribute("sand-save", m_sandSave);
65     if (!m_penalty.isEmpty())
66         node.setAttribute("penalty", m_penalty);
67
68     return node;
69 }
70
71 QString Hole::hcp() 
72 {
73     return m_hcp;
74 }
75
76 void Hole::setHcp(QString& s) 
77 {
78     m_hcp = s;
79 }
80
81 QString Hole::par() 
82 {
83     return m_par;
84 }
85
86 void Hole::setPar(QString& s) 
87 {
88     m_par = s;
89 }
90
91 QString Hole::shots() 
92 {
93     return m_shots;
94 }
95
96 void Hole::setShots(QString& s) 
97 {
98     m_shots = s;
99 }
100
101 QString Hole::putts() 
102 {
103     return m_putts;
104 }
105
106 void Hole::setPutts(QString& s) 
107 {
108     m_putts = s;
109 }
110
111 QString Hole::greenInRegulation() 
112 {
113     return m_greenInRegulation;
114 }
115
116 void Hole::setGreenInRegulation(QString& s) 
117 {
118     m_greenInRegulation = s;
119 }
120
121 QString Hole::fairwayHit() 
122 {
123     return m_fairwayHit;
124 }
125
126 void Hole::setFairwayHit(QString& s) 
127 {
128     m_fairwayHit = s;
129 }
130
131 QString Hole::sandSave() 
132 {
133     return m_sandSave;
134 }
135
136 void Hole::setSandSave(QString& s) 
137 {
138     m_sandSave = s;
139 }
140
141 QString Hole::penalty() 
142 {
143     return m_penalty;
144 }
145
146 void Hole::setPenalty(QString& s) 
147 {
148     m_penalty = s;
149 }
150
151 void Hole::dump() 
152 {
153     qDebug() << m_num << "(" << m_par << ") : " << m_shots << m_putts << m_greenInRegulation << m_fairwayHit << m_sandSave << m_penalty; 
154 }
155
156 ////////////////////////////////////////////////////////////////////////
157 // Score
158 ////////////////////////////////////////////////////////////////////////
159
160 Score::Score(const QXmlAttributes &attrs) 
161 {
162     club = attrs.value("club");
163     course = attrs.value("course");
164     date = attrs.value("date");
165 }
166
167 Score::Score(QString &iClub, QString &iCourse, QString &iDate) 
168 {
169     club = iClub;
170     course = iCourse;
171     date = iDate;
172 }
173
174 Score::Score(QVector<QString> scores, QString &club, QString &course, QString &date) 
175 {
176     this->club = club;
177     this->course = course;
178     this->date = date;
179
180     for (int i = 0; i < scores.size(); i++) {
181         Hole *hole = new Hole(i+1, scores[i]);
182         holeList << hole;
183     }
184 }
185
186 Score::Score(const QDomElement node) {
187     club = node.attribute("club", "");
188     course = node.attribute("course", "");
189     date = node.attribute("date", "");
190 }
191
192 QDomElement Score::toElement(QDomDocument doc)
193 {
194     QDomElement node = doc.createElement("score");
195
196     node.setAttribute("club", club);
197     node.setAttribute("course", course);
198     node.setAttribute("date", date);
199
200     for (int i=0; i < holeList.size(); i++) {
201         Hole *hole = holeList.at(i);
202         node.appendChild(hole->toElement(doc));
203     }
204     return node;
205 }
206
207 int Score::update(QVector<QString> &scores)
208 {
209     for (int i = 0; i < scores.size(); i++) {
210         Hole *hole = holeList.at(i);
211         if (hole->shots() != scores[i])
212             hole->setShots(scores[i]);
213     }
214     return 0;
215 }
216
217 void Score::addHole(Hole *iHole) {
218     holeList << iHole;
219 }
220   
221 QString Score::getScore(int i) const 
222 {
223     if (i >= 0 && i < holeList.size())
224         return holeList.at(i)->shots();
225     else
226         return QString("-");
227 }
228   
229 QString Score::getTotal(int what) const
230 {
231     int tot = 0;
232
233     if (what == Total)
234         for (int i=0; i <= 17; i++)
235             tot += holeList.at(i)->shots().toInt();
236
237     if (what == TotalOut)
238         for (int i=0; i <= 8; i++)
239             tot += holeList.at(i)->shots().toInt();
240  
241     if (what == TotalIn)
242         for (int i=9; i <= 17; i++)
243             tot += holeList.at(i)->shots().toInt();
244
245     return QString("%1").arg(tot);
246 }
247
248 const QString& Score::getClubName() const
249 {
250     return club;
251 }
252
253 const QString& Score::getCourseName() const
254 {
255     return course;
256 }
257
258 const QString& Score::getDate() const
259 {
260     return date;
261 }
262
263 void Score::dump()
264 {
265     qDebug() << club << course << date ; 
266     for (int i=0; i<holeList.size(); i++)
267         holeList.at(i)->dump();
268 }
269
270 ////////////////////////////////////////////////////////////////////////
271 // Course
272 ////////////////////////////////////////////////////////////////////////
273
274 Course::Course(const QXmlAttributes &attrs) {
275     name = attrs.value("name");
276 }
277
278 Course::Course(const QDomElement node, Club * parent)
279     : club(parent)
280 {
281     name = node.attribute("name", "");
282 }
283
284 Course::Course(QString &name, 
285                QVector<QString> &par,
286                QVector<QString> &hcp)
287 {
288     this->name = name;
289   
290     for (int i = 0; i < par.size(); i++) {
291         Hole *hole = new Hole(i+1, par[i], hcp[i]);
292         holeList << hole;
293     }
294 }
295
296 Club * Course::parent()
297 {
298     return club;
299 }
300
301 void Course::setParent(Club *parent)
302 {
303     club = parent;
304 }
305
306 QDomElement Course::toElement(QDomDocument doc)
307 {
308     QDomElement node = doc.createElement("course");
309
310     node.setAttribute("name", name);
311
312     for (int i=0; i < holeList.size(); i++) {
313         Hole *hole = holeList.at(i);
314         node.appendChild(hole->toElement(doc));
315     }
316     return node;
317 }
318
319 int Course::update(QVector<QString> &par,
320                    QVector<QString> &hcp,
321                    QVector<QString> &len)
322 {
323     Q_UNUSED(len);
324     for (int i = 0; i < par.size(); i++) {
325         Hole *hole = holeList.at(i);
326         if (hole->par() != par[i])
327             hole->setPar(par[i]);
328         if (hole->hcp() != hcp[i])
329             hole->setHcp(hcp[i]);
330     }
331     return 0;
332 }
333
334 void Course::addHole(Hole *iHole) {
335     holeList << iHole;
336 }
337
338 QString Course::getPar(int i) {
339     if (i >= 0 && i < holeList.size())
340         return holeList.at(i)->par();
341     else
342         return QString("-");
343 }
344
345 QString Course::getHcp(int i) {
346     if (i >= 0 && i < holeList.size())
347         return holeList.at(i)->hcp();
348     else
349         return QString("-");
350 }
351   
352 QString& Course::getName() 
353
354     return name; 
355 }
356
357 QString Course::getTotal(int what) {
358     int tot = 0;
359
360     if (what == Total)
361         for (int i = 0; i < 18; i++)
362             tot += holeList.at(i)->par().toInt();
363
364     if (what == TotalOut)
365         for (int i = 0; i < 9; i++)
366             tot += holeList.at(i)->par().toInt();
367  
368     if (what == TotalIn)
369         for (int i = 9; i < 18; i++)
370             tot += holeList.at(i)->par().toInt();
371
372     return QString("%1").arg(tot);
373 }
374
375
376 void Course::dump() {
377     qDebug() << " " << name;
378     for (int i=0; i<holeList.size(); i++)
379         holeList.at(i)->dump();
380 }
381
382 ////////////////////////////////////////////////////////////////////////
383 // Club
384 ////////////////////////////////////////////////////////////////////////
385
386 Club::Club(const QXmlAttributes &attrs, bool readOnly) 
387     : m_readOnly(readOnly)
388 {
389     m_homeClub = false;
390     name = attrs.value("name");
391 }
392
393 Club::Club(const QDomElement node, bool readOnly) 
394     : m_readOnly(readOnly)
395 {
396     m_homeClub = false;
397     name = node.attribute("name", "");
398 }
399
400 Club::Club(QString &name, bool readOnly)
401     : m_readOnly(readOnly)
402 {
403     m_homeClub = false;
404     this->name = name;
405 }
406
407 void Club::addCourse(Course *iCourse) {
408     courseList << iCourse;
409     iCourse->setParent(this);
410 }
411
412 void Club::delCourse(Course * course) {
413     int index = courseList.indexOf(course);
414
415     if (index != -1)
416         courseList.removeAt(index);
417 }
418
419 bool Club::isReadOnly()
420 {
421     return m_readOnly;
422 }
423
424 bool Club::isEmpty()
425 {
426     bool rc = false;
427     if (courseList.count() == 0)
428         rc = true;
429     return rc;
430 }
431
432 QDomElement Club::toElement(QDomDocument doc)
433 {
434     QDomElement node = doc.createElement("club");
435
436     node.setAttribute("name", name);
437
438     for (int i=0; i < courseList.size(); i++) {
439         Course *course = courseList.at(i);
440         node.appendChild(course->toElement(doc));
441     }
442     return node;
443 }
444   
445 void Club::dump() {
446     qDebug() << name;
447     for (int i=0; i<courseList.size(); i++)
448         courseList.at(i)->dump();
449 }
450
451 QString& Club::getName() { return name; }
452
453 Course *Club::getCourse(int pos) {
454     return courseList.at(pos);
455 }
456
457 Course *Club::getCourse(const QString &courseName) 
458 {
459     QListIterator<Course *> i(courseList);
460     Course *c = 0;
461
462     while (i.hasNext()) {
463         c = i.next();
464         if (c->getName() == courseName) {
465             return c;
466         }
467     }
468     return 0;
469 }