19e52f0af5270ac9a8c9e19ec00ac664877fdf5c
[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 void Course::setParent(Club *parent)
246 {
247     club = parent;
248 }
249
250 QDomElement Course::toElement(QDomDocument doc)
251 {
252   QDomElement node = doc.createElement("course");
253
254   node.setAttribute("name", name);
255
256   for (int i=0; i < holeList.size(); i++) {
257     Hole *hole = holeList.at(i);
258     node.appendChild(hole->toElement(doc));
259   }
260   return node;
261 }
262
263 int Course::update(QVector<QString> &par,
264                    QVector<QString> &hcp,
265                    QVector<QString> &len)
266 {
267   Q_UNUSED(len);
268   for (int i = 0; i < par.size(); i++) {
269     Hole *hole = holeList.at(i);
270     if (hole->getPar() != par[i])
271       hole->setPar(par[i]);
272     if (hole->getHcp() != hcp[i])
273       hole->setHcp(hcp[i]);
274   }
275   return 0;
276 }
277
278 void Course::addHole(Hole *iHole) {
279   holeList << iHole;
280 }
281
282 QString Course::getPar(int i) {
283   if (i >= 0 && i < holeList.size())
284     return holeList.at(i)->getPar();
285   else
286     return QString("-");
287 }
288
289 QString Course::getHcp(int i) {
290   if (i >= 0 && i < holeList.size())
291     return holeList.at(i)->getHcp();
292   else
293     return QString("-");
294 }
295   
296 QString& Course::getName() 
297
298   return name; 
299 }
300
301 QString Course::getTotal(int what) {
302   int tot = 0;
303
304   if (what == Total)
305     for (int i = 0; i < 18; i++)
306       tot += holeList.at(i)->getPar().toInt();
307
308   if (what == TotalOut)
309     for (int i = 0; i < 9; i++)
310       tot += holeList.at(i)->getPar().toInt();
311  
312   if (what == TotalIn)
313     for (int i = 9; i < 18; i++)
314       tot += holeList.at(i)->getPar().toInt();
315
316   return QString("%1").arg(tot);
317 }
318
319
320 void Course::dump() {
321   qDebug() << " " << name;
322   for (int i=0; i<holeList.size(); i++)
323     holeList.at(i)->dump();
324 }
325
326 ////////////////////////////////////////////////////////////////////////
327 // Club
328 ////////////////////////////////////////////////////////////////////////
329
330 Club::Club(const QXmlAttributes &attrs, bool readOnly) 
331   : m_readOnly(readOnly)
332 {
333     name = attrs.value("name");
334 }
335
336 Club::Club(const QDomElement node, bool readOnly) 
337   : m_readOnly(readOnly)
338 {
339     name = node.attribute("name", "");
340 }
341
342 Club::Club(QString &name, bool readOnly)
343   : m_readOnly(readOnly)
344 {
345     this->name = name;
346 }
347
348 void Club::addCourse(Course *iCourse) {
349   courseList << iCourse;
350   iCourse->setParent(this);
351 }
352
353 void Club::delCourse(Course * course) {
354     int index = courseList.indexOf(course);
355
356     if (index != -1)
357         courseList.removeAt(index);
358 }
359
360 bool Club::isReadOnly()
361 {
362     return m_readOnly;
363 }
364
365 bool Club::isEmpty()
366 {
367     bool rc = false;
368     if (courseList.count() == 0)
369         rc = true;
370     return rc;
371 }
372
373 QDomElement Club::toElement(QDomDocument doc)
374 {
375   QDomElement node = doc.createElement("club");
376
377   node.setAttribute("name", name);
378
379   for (int i=0; i < courseList.size(); i++) {
380     Course *course = courseList.at(i);
381     node.appendChild(course->toElement(doc));
382   }
383   return node;
384 }
385   
386 void Club::dump() {
387   qDebug() << name;
388   for (int i=0; i<courseList.size(); i++)
389     courseList.at(i)->dump();
390 }
391
392 QString& Club::getName() { return name; }
393
394 Course *Club::getCourse(int pos) {
395   return courseList.at(pos);
396 }
397
398 Course *Club::getCourse(const QString &courseName) 
399 {
400   QListIterator<Course *> i(courseList);
401   Course *c = 0;
402
403   while (i.hasNext()) {
404     c = i.next();
405     if (c->getName() == courseName) {
406       return c;
407     }
408   }
409   return 0;
410 }