270e04bec3c298e7ecaffeed8c5ee0312200dec0
[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) {
223   name = node.attribute("name", "");
224 }
225
226 Course::Course(QString &name, 
227                QVector<QString> &par,
228                QVector<QString> &hcp)
229 {
230   this->name = name;
231   
232   for (int i = 0; i < par.size(); i++) {
233     Hole *hole = new Hole(i+1, par[i], hcp[i]);
234     holeList << hole;
235   }
236 }
237
238 QDomElement Course::toElement(QDomDocument doc)
239 {
240   QDomElement node = doc.createElement("course");
241
242   node.setAttribute("name", name);
243
244   for (int i=0; i < holeList.size(); i++) {
245     Hole *hole = holeList.at(i);
246     node.appendChild(hole->toElement(doc));
247   }
248   return node;
249 }
250
251 int Course::update(QVector<QString> &par,
252                    QVector<QString> &hcp,
253                    QVector<QString> &len)
254 {
255   for (int i = 0; i < par.size(); i++) {
256     Hole *hole = holeList.at(i);
257     if (hole->getPar() != par[i])
258       hole->setPar(par[i]);
259     if (hole->getHcp() != hcp[i])
260       hole->setHcp(hcp[i]);
261   }
262   return 0;
263 }
264
265 void Course::addHole(Hole *iHole) {
266   holeList << iHole;
267 }
268
269 QString Course::getPar(int i) {
270   if (i >= 0 && i < holeList.size())
271     return holeList.at(i)->getPar();
272   else
273     return QString("-");
274 }
275
276 QString Course::getHcp(int i) {
277   if (i >= 0 && i < holeList.size())
278     return holeList.at(i)->getHcp();
279   else
280     return QString("-");
281 }
282   
283 QString& Course::getName() 
284
285   return name; 
286 }
287
288 QString Course::getTotal(int what) {
289   int tot = 0;
290
291   if (what == Total)
292     for (int i = 0; i < 18; i++)
293       tot += holeList.at(i)->getPar().toInt();
294
295   if (what == TotalOut)
296     for (int i = 0; i < 9; i++)
297       tot += holeList.at(i)->getPar().toInt();
298  
299   if (what == TotalIn)
300     for (int i = 9; i < 18; i++)
301       tot += holeList.at(i)->getPar().toInt();
302
303   return QString("%1").arg(tot);
304 }
305
306
307 void Course::dump() {
308   qDebug() << " " << name;
309   for (int i=0; i<holeList.size(); i++)
310     holeList.at(i)->dump();
311 }
312
313 ////////////////////////////////////////////////////////////////////////
314 // Club
315 ////////////////////////////////////////////////////////////////////////
316
317 Club::Club(const QXmlAttributes &attrs) {
318   name = attrs.value("name");
319 }
320
321 Club::Club(const QDomElement node) {
322   name = node.attribute("name", "");
323 }
324
325 Club::Club(QString &name)
326 {
327   this->name = name;
328 }
329
330 void Club::addCourse(Course *iCourse) {
331   courseList << iCourse;
332 }
333
334 QDomElement Club::toElement(QDomDocument doc)
335 {
336   QDomElement node = doc.createElement("club");
337
338   node.setAttribute("name", name);
339
340   for (int i=0; i < courseList.size(); i++) {
341     Course *course = courseList.at(i);
342     node.appendChild(course->toElement(doc));
343   }
344   return node;
345 }
346   
347 void Club::dump() {
348   qDebug() << name;
349   for (int i=0; i<courseList.size(); i++)
350     courseList.at(i)->dump();
351 }
352
353 QString& Club::getName() { return name; }
354
355 Course *Club::getCourse(int pos) {
356   return courseList.at(pos);
357 }
358
359 Course *Club::getCourse(const QString &courseName) 
360 {
361   QListIterator<Course *> i(courseList);
362   Course *c = 0;
363
364   while (i.hasNext()) {
365     c = i.next();
366     if (c->getName() == courseName) {
367       return c;
368     }
369   }
370   return 0;
371 }