9885c755911f6578e9887e108e46b43f2f034d07
[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 void Hole::setHcp(QString& s) {
77   hcp = s;
78 }
79
80 QString Hole::getPar() {
81   return par;
82 }
83
84 void Hole::setPar(QString& s) {
85   par = s;
86 }
87
88 void Hole::dump() {
89   qDebug() << num << "(" << par << ") : " << shots << "/" << putts ; 
90 }
91
92 ////////////////////////////////////////////////////////////////////////
93 // Score
94 ////////////////////////////////////////////////////////////////////////
95
96 Score::Score(const QXmlAttributes &attrs) 
97 {
98   club = attrs.value("club");
99   course = attrs.value("course");
100   date = attrs.value("date");
101 }
102
103 Score::Score(QString &iClub, QString &iCourse, QString &iDate) 
104 {
105   club = iClub;
106   course = iCourse;
107   date = iDate;
108 }
109
110 Score::Score(QVector<QString> scores, QString &club, QString &course, QString &date) 
111 {
112   this->club = club;
113   this->course = course;
114   this->date = date;
115
116   for (int i = 0; i < scores.size(); i++) {
117     Hole *hole = new Hole(i+1, scores[i]);
118     holeList << hole;
119   }
120 }
121
122 Score::Score(const QDomElement node) {
123   club = node.attribute("club", "");
124   course = node.attribute("course", "");
125   date = node.attribute("date", "");
126 }
127
128 QDomElement Score::toElement(QDomDocument doc)
129 {
130   QDomElement node = doc.createElement("score");
131
132   node.setAttribute("club", club);
133   node.setAttribute("course", course);
134   node.setAttribute("date", date);
135
136   for (int i=0; i < holeList.size(); i++) {
137     Hole *hole = holeList.at(i);
138     node.appendChild(hole->toElement(doc));
139   }
140   return node;
141 }
142
143 int Score::update(QVector<QString> &scores)
144 {
145   for (int i = 0; i < scores.size(); i++) {
146     Hole *hole = holeList.at(i);
147     if (hole->getShots() != scores[i])
148       hole->setShots(scores[i]);
149   }
150   return 0;
151 }
152
153 void Score::addHole(Hole *iHole) {
154   holeList << iHole;
155 }
156   
157 QString Score::getScore(int i) const 
158 {
159   if (i >= 0 && i < holeList.size())
160     return holeList.at(i)->getShots();
161   else
162     return QString("-");
163 }
164   
165 QString Score::getTotal(int what) const
166 {
167   int tot = 0;
168
169   if (what == Total)
170     for (int i=0; i <= 17; i++)
171       tot += holeList.at(i)->getShots().toInt();
172
173   if (what == TotalOut)
174     for (int i=0; i <= 8; i++)
175       tot += holeList.at(i)->getShots().toInt();
176  
177   if (what == TotalIn)
178     for (int i=9; i <= 17; i++)
179       tot += holeList.at(i)->getShots().toInt();
180
181   return QString("%1").arg(tot);
182 }
183
184 const QString& Score::getClubName() const
185 {
186   return club;
187 }
188
189 const QString& Score::getCourseName() const
190 {
191   return course;
192 }
193
194 const QString& Score::getDate() const
195 {
196   return date;
197 }
198
199 void Score::dump()
200 {
201   qDebug() << club << course << date ; 
202   for (int i=0; i<holeList.size(); i++)
203     holeList.at(i)->dump();
204 }
205
206 ////////////////////////////////////////////////////////////////////////
207 // Course
208 ////////////////////////////////////////////////////////////////////////
209
210 Course::Course(const QXmlAttributes &attrs) {
211   name = attrs.value("name");
212 }
213
214 Course::Course(const QDomElement node) {
215   name = node.attribute("name", "");
216 }
217
218 Course::Course(QString &name, 
219                QVector<QString> &par,
220                QVector<QString> &hcp)
221 {
222   this->name = name;
223   
224   for (int i = 0; i < par.size(); i++) {
225     Hole *hole = new Hole(i+1, par[i], hcp[i]);
226     holeList << hole;
227   }
228 }
229
230 QDomElement Course::toElement(QDomDocument doc)
231 {
232   QDomElement node = doc.createElement("course");
233
234   node.setAttribute("name", name);
235
236   for (int i=0; i < holeList.size(); i++) {
237     Hole *hole = holeList.at(i);
238     node.appendChild(hole->toElement(doc));
239   }
240   return node;
241 }
242
243 int Course::update(QVector<QString> &par,
244                    QVector<QString> &hcp,
245                    QVector<QString> &len)
246 {
247   for (int i = 0; i < par.size(); i++) {
248     Hole *hole = holeList.at(i);
249     if (hole->getPar() != par[i])
250       hole->setPar(par[i]);
251     if (hole->getHcp() != hcp[i])
252       hole->setHcp(hcp[i]);
253   }
254   return 0;
255 }
256
257 void Course::addHole(Hole *iHole) {
258   holeList << iHole;
259 }
260
261 QString Course::getPar(int i) {
262   if (i >= 0 && i < holeList.size())
263     return holeList.at(i)->getPar();
264   else
265     return QString("-");
266 }
267
268 QString Course::getHcp(int i) {
269   if (i >= 0 && i < holeList.size())
270     return holeList.at(i)->getHcp();
271   else
272     return QString("-");
273 }
274   
275 QString& Course::getName() 
276
277   return name; 
278 }
279
280 QString Course::getTotal(int what) {
281   int tot = 0;
282
283   if (what == Total)
284     for (int i = 0; i < 18; i++)
285       tot += holeList.at(i)->getPar().toInt();
286
287   if (what == TotalOut)
288     for (int i = 0; i < 9; i++)
289       tot += holeList.at(i)->getPar().toInt();
290  
291   if (what == TotalIn)
292     for (int i = 9; i < 18; i++)
293       tot += holeList.at(i)->getPar().toInt();
294
295   return QString("%1").arg(tot);
296 }
297
298
299 void Course::dump() {
300   qDebug() << " " << name;
301   for (int i=0; i<holeList.size(); i++)
302     holeList.at(i)->dump();
303 }
304
305 ////////////////////////////////////////////////////////////////////////
306 // Club
307 ////////////////////////////////////////////////////////////////////////
308
309 Club::Club(const QXmlAttributes &attrs) {
310   name = attrs.value("name");
311 }
312
313 Club::Club(const QDomElement node) {
314   name = node.attribute("name", "");
315 }
316
317 Club::Club(QString &name)
318 {
319   this->name = name;
320 }
321
322 void Club::addCourse(Course *iCourse) {
323   courseList << iCourse;
324 }
325
326 QDomElement Club::toElement(QDomDocument doc)
327 {
328   QDomElement node = doc.createElement("club");
329
330   node.setAttribute("name", name);
331
332   for (int i=0; i < courseList.size(); i++) {
333     Course *course = courseList.at(i);
334     node.appendChild(course->toElement(doc));
335   }
336   return node;
337 }
338   
339 void Club::dump() {
340   qDebug() << name;
341   for (int i=0; i<courseList.size(); i++)
342     courseList.at(i)->dump();
343 }
344
345 QString& Club::getName() { return name; }
346
347 Course *Club::getCourse(int pos) {
348   return courseList.at(pos);
349 }
350
351 Course *Club::getCourse(const QString &courseName) 
352 {
353   QListIterator<Course *> i(courseList);
354   Course *c = 0;
355
356   while (i.hasNext()) {
357     c = i.next();
358     if (c->getName() == courseName) {
359       return c;
360     }
361   }
362   return 0;
363 }