2 * Copyright (C) 2009 Sakari Poussa
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.
11 ////////////////////////////////////////////////////////////////////////
13 ////////////////////////////////////////////////////////////////////////
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");
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", "");
39 Hole::Hole(int num, QString &par, QString &hcp)
41 this->num = QString::number(num);
46 Hole::Hole(int num, QString &shots)
48 this->num = QString::number(num);
52 QDomElement Hole::toElement(QDomDocument doc)
54 QDomElement node = doc.createElement("hole");
57 node.setAttribute("num", num);
59 node.setAttribute("shots", shots);
61 node.setAttribute("putts", putts);
63 node.setAttribute("hcp", hcp);
64 if (!length.isEmpty())
65 node.setAttribute("length", length);
67 node.setAttribute("par", par);
72 QString Hole::getShots() {
76 void Hole::setShots(QString& s) {
80 QString Hole::getHcp() {
84 void Hole::setHcp(QString& s) {
88 QString Hole::getPar() {
92 void Hole::setPar(QString& s) {
97 qDebug() << num << "(" << par << ") : " << shots << "/" << putts ;
100 ////////////////////////////////////////////////////////////////////////
102 ////////////////////////////////////////////////////////////////////////
104 Score::Score(const QXmlAttributes &attrs)
106 club = attrs.value("club");
107 course = attrs.value("course");
108 date = attrs.value("date");
111 Score::Score(QString &iClub, QString &iCourse, QString &iDate)
118 Score::Score(QVector<QString> scores, QString &club, QString &course, QString &date)
121 this->course = course;
124 for (int i = 0; i < scores.size(); i++) {
125 Hole *hole = new Hole(i+1, scores[i]);
130 Score::Score(const QDomElement node) {
131 club = node.attribute("club", "");
132 course = node.attribute("course", "");
133 date = node.attribute("date", "");
136 QDomElement Score::toElement(QDomDocument doc)
138 QDomElement node = doc.createElement("score");
140 node.setAttribute("club", club);
141 node.setAttribute("course", course);
142 node.setAttribute("date", date);
144 for (int i=0; i < holeList.size(); i++) {
145 Hole *hole = holeList.at(i);
146 node.appendChild(hole->toElement(doc));
151 int Score::update(QVector<QString> &scores)
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]);
161 void Score::addHole(Hole *iHole) {
165 QString Score::getScore(int i) const
167 if (i >= 0 && i < holeList.size())
168 return holeList.at(i)->getShots();
173 QString Score::getTotal(int what) const
178 for (int i=0; i <= 17; i++)
179 tot += holeList.at(i)->getShots().toInt();
181 if (what == TotalOut)
182 for (int i=0; i <= 8; i++)
183 tot += holeList.at(i)->getShots().toInt();
186 for (int i=9; i <= 17; i++)
187 tot += holeList.at(i)->getShots().toInt();
189 return QString("%1").arg(tot);
192 const QString& Score::getClubName() const
197 const QString& Score::getCourseName() const
202 const QString& Score::getDate() const
209 qDebug() << club << course << date ;
210 for (int i=0; i<holeList.size(); i++)
211 holeList.at(i)->dump();
214 ////////////////////////////////////////////////////////////////////////
216 ////////////////////////////////////////////////////////////////////////
218 Course::Course(const QXmlAttributes &attrs) {
219 name = attrs.value("name");
222 Course::Course(const QDomElement node) {
223 name = node.attribute("name", "");
226 Course::Course(QString &name,
227 QVector<QString> &par,
228 QVector<QString> &hcp)
232 for (int i = 0; i < par.size(); i++) {
233 Hole *hole = new Hole(i+1, par[i], hcp[i]);
238 QDomElement Course::toElement(QDomDocument doc)
240 QDomElement node = doc.createElement("course");
242 node.setAttribute("name", name);
244 for (int i=0; i < holeList.size(); i++) {
245 Hole *hole = holeList.at(i);
246 node.appendChild(hole->toElement(doc));
251 int Course::update(QVector<QString> &par,
252 QVector<QString> &hcp,
253 QVector<QString> &len)
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]);
265 void Course::addHole(Hole *iHole) {
269 QString Course::getPar(int i) {
270 if (i >= 0 && i < holeList.size())
271 return holeList.at(i)->getPar();
276 QString Course::getHcp(int i) {
277 if (i >= 0 && i < holeList.size())
278 return holeList.at(i)->getHcp();
283 QString& Course::getName()
288 QString Course::getTotal(int what) {
292 for (int i = 0; i < 18; i++)
293 tot += holeList.at(i)->getPar().toInt();
295 if (what == TotalOut)
296 for (int i = 0; i < 9; i++)
297 tot += holeList.at(i)->getPar().toInt();
300 for (int i = 9; i < 18; i++)
301 tot += holeList.at(i)->getPar().toInt();
303 return QString("%1").arg(tot);
307 void Course::dump() {
308 qDebug() << " " << name;
309 for (int i=0; i<holeList.size(); i++)
310 holeList.at(i)->dump();
313 ////////////////////////////////////////////////////////////////////////
315 ////////////////////////////////////////////////////////////////////////
317 Club::Club(const QXmlAttributes &attrs) {
318 name = attrs.value("name");
321 Club::Club(const QDomElement node) {
322 name = node.attribute("name", "");
325 Club::Club(QString &name)
330 void Club::addCourse(Course *iCourse) {
331 courseList << iCourse;
334 QDomElement Club::toElement(QDomDocument doc)
336 QDomElement node = doc.createElement("club");
338 node.setAttribute("name", name);
340 for (int i=0; i < courseList.size(); i++) {
341 Course *course = courseList.at(i);
342 node.appendChild(course->toElement(doc));
349 for (int i=0; i<courseList.size(); i++)
350 courseList.at(i)->dump();
353 QString& Club::getName() { return name; }
355 Course *Club::getCourse(int pos) {
356 return courseList.at(pos);
359 Course *Club::getCourse(const QString &courseName)
361 QListIterator<Course *> i(courseList);
364 while (i.hasNext()) {
366 if (c->getName() == courseName) {