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