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