Sort scores based on dates not the entry date.
[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) const 
136 {
137   if (i >= 0 && i < holeList.size())
138     return holeList.at(i)->getShots();
139   else
140     return QString("-");
141 }
142   
143 QString Score::getTotal(int what) const
144 {
145   int tot = 0;
146
147   if (what == Total)
148     for (int i=0; i <= 17; i++)
149       tot += holeList.at(i)->getShots().toInt();
150
151   if (what == TotalOut)
152     for (int i=0; i <= 8; i++)
153       tot += holeList.at(i)->getShots().toInt();
154  
155   if (what == TotalIn)
156     for (int i=9; i <= 17; i++)
157       tot += holeList.at(i)->getShots().toInt();
158
159   return QString("%1").arg(tot);
160 }
161
162 const QString& Score::getClubName() const
163 {
164   return club;
165 }
166
167 const QString& Score::getCourseName() const
168 {
169   return course;
170 }
171
172 const QString& Score::getDate() const
173 {
174   return date;
175 }
176
177 void Score::dump()
178 {
179   qDebug() << club << " " << course << " " << date ; 
180   for (int i=0; i<holeList.size(); i++)
181     holeList.at(i)->dump();
182 }
183
184 ////////////////////////////////////////////////////////////////////////
185 // Course
186 ////////////////////////////////////////////////////////////////////////
187
188 Course::Course(const QXmlAttributes &attrs) {
189   name = attrs.value("name");
190 }
191
192 Course::Course(const QDomElement node) {
193   name = node.attribute("name", "");
194 }
195
196 Course::Course(QString &name, 
197                QVector<QString> &par,
198                QVector<QString> &hcp)
199 {
200   this->name = name;
201   
202   for (int i = 0; i < par.size(); i++) {
203     Hole *hole = new Hole(i+1, par[i], hcp[i]);
204     holeList << hole;
205   }
206 }
207
208 QDomElement Course::toElement(QDomDocument doc)
209 {
210   QDomElement node = doc.createElement("course");
211
212   node.setAttribute("name", name);
213
214   for (int i=0; i < holeList.size(); i++) {
215     Hole *hole = holeList.at(i);
216     node.appendChild(hole->toElement(doc));
217   }
218   return node;
219 }
220
221 void Course::addHole(Hole *iHole) {
222   holeList << iHole;
223 }
224
225 QString Course::getPar(int i) {
226   if (i >= 0 && i < holeList.size())
227     return holeList.at(i)->getPar();
228   else
229     return QString("-");
230 }
231
232 QString Course::getHcp(int i) {
233   if (i >= 0 && i < holeList.size())
234     return holeList.at(i)->getHcp();
235   else
236     return QString("-");
237 }
238   
239 QString& Course::getName() 
240
241   return name; 
242 }
243
244 QString Course::getTotal(int what) {
245   int tot = 0;
246
247   if (what == Total)
248     for (int i = 0; i < 18; i++)
249       tot += holeList.at(i)->getPar().toInt();
250
251   if (what == TotalOut)
252     for (int i = 0; i < 9; i++)
253       tot += holeList.at(i)->getPar().toInt();
254  
255   if (what == TotalIn)
256     for (int i = 9; i < 18; i++)
257       tot += holeList.at(i)->getPar().toInt();
258
259   return QString("%1").arg(tot);
260 }
261
262
263 void Course::dump() {
264   qDebug() << " " << name;
265   for (int i=0; i<holeList.size(); i++)
266     holeList.at(i)->dump();
267 }
268
269 ////////////////////////////////////////////////////////////////////////
270 // Club
271 ////////////////////////////////////////////////////////////////////////
272
273 Club::Club(const QXmlAttributes &attrs) {
274   name = attrs.value("name");
275 }
276
277 Club::Club(const QDomElement node) {
278   name = node.attribute("name", "");
279 }
280
281 Club::Club(QString &name)
282 {
283   this->name = name;
284 }
285
286 void Club::addCourse(Course *iCourse) {
287   courseList << iCourse;
288 }
289
290 QDomElement Club::toElement(QDomDocument doc)
291 {
292   QDomElement node = doc.createElement("club");
293
294   node.setAttribute("name", name);
295
296   for (int i=0; i < courseList.size(); i++) {
297     Course *course = courseList.at(i);
298     node.appendChild(course->toElement(doc));
299   }
300   return node;
301 }
302   
303 void Club::dump() {
304   qDebug() << name;
305   for (int i=0; i<courseList.size(); i++)
306     courseList.at(i)->dump();
307 }
308
309 QString& Club::getName() { return name; }
310
311 Course *Club::getCourse(int pos) {
312   return courseList.at(pos);
313 }
314
315 Course *Club::getCourse(const QString &courseName) 
316 {
317   QListIterator<Course *> i(courseList);
318   Course *c = 0;
319
320   while (i.hasNext()) {
321     c = i.next();
322     if (c->getName() == courseName) {
323       return c;
324     }
325   }
326   return 0;
327 }