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