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 ////////////////////////////////////////////////////////////////////////
14 Hole::Hole(int num, QString &par, QString &hcp)
16 this->m_num = QString::number(num);
21 Hole::Hole(int num, QString &shots)
23 this->m_num = QString::number(num);
24 this->m_shots = shots;
27 // XML to internal converions
28 Hole::Hole(const QDomElement node)
30 m_num = node.attribute("num", "");
31 m_hcp = node.attribute("hcp", "");
32 m_length = node.attribute("length", "");
33 m_par = node.attribute("par", "");
34 m_shots = node.attribute("shots", "");
35 m_putts = node.attribute("putts", "");
36 m_greenInRegulation = node.attribute("green-in-regulation", "");
37 m_fairwayHit = node.attribute("fairway-hit", "");
38 m_sandSave = node.attribute("sand-save", "");
39 m_penalty = node.attribute("penalty", "");
42 // Internal to XML conversion
43 QDomElement Hole::toElement(QDomDocument doc)
45 QDomElement node = doc.createElement("hole");
48 node.setAttribute("num", m_num);
50 node.setAttribute("hcp", m_hcp);
51 if (!m_length.isEmpty())
52 node.setAttribute("length", m_length);
54 node.setAttribute("par", m_par);
55 if (!m_shots.isEmpty())
56 node.setAttribute("shots", m_shots);
57 if (!m_putts.isEmpty())
58 node.setAttribute("putts", m_putts);
59 if (!m_greenInRegulation.isEmpty())
60 node.setAttribute("green-in-regulation", m_greenInRegulation);
61 if (!m_fairwayHit.isEmpty())
62 node.setAttribute("fairway-hit", m_fairwayHit);
63 if (!m_sandSave.isEmpty())
64 node.setAttribute("sand-save", m_sandSave);
65 if (!m_penalty.isEmpty())
66 node.setAttribute("penalty", m_penalty);
76 void Hole::setHcp(QString& s)
86 void Hole::setPar(QString& s)
96 void Hole::setShots(QString& s)
101 QString Hole::putts()
106 void Hole::setPutts(QString& s)
111 QString Hole::greenInRegulation()
113 return m_greenInRegulation;
116 void Hole::setGreenInRegulation(QString& s)
118 m_greenInRegulation = s;
121 QString Hole::fairwayHit()
126 void Hole::setFairwayHit(QString& s)
131 QString Hole::sandSave()
136 void Hole::setSandSave(QString& s)
141 QString Hole::penalty()
146 void Hole::setPenalty(QString& s)
153 qDebug() << m_num << "(" << m_par << ") : " << m_shots << m_putts << m_greenInRegulation << m_fairwayHit << m_sandSave << m_penalty;
156 ////////////////////////////////////////////////////////////////////////
158 ////////////////////////////////////////////////////////////////////////
160 Score::Score(const QXmlAttributes &attrs)
162 club = attrs.value("club");
163 course = attrs.value("course");
164 date = attrs.value("date");
167 Score::Score(QString &iClub, QString &iCourse, QString &iDate)
174 Score::Score(QVector<QString> scores, QString &club, QString &course, QString &date)
177 this->course = course;
180 for (int i = 0; i < scores.size(); i++) {
181 Hole *hole = new Hole(i+1, scores[i]);
186 Score::Score(const QDomElement node) {
187 club = node.attribute("club", "");
188 course = node.attribute("course", "");
189 date = node.attribute("date", "");
192 QDomElement Score::toElement(QDomDocument doc)
194 QDomElement node = doc.createElement("score");
196 node.setAttribute("club", club);
197 node.setAttribute("course", course);
198 node.setAttribute("date", date);
200 for (int i=0; i < holeList.size(); i++) {
201 Hole *hole = holeList.at(i);
202 node.appendChild(hole->toElement(doc));
207 int Score::update(QVector<QString> &scores)
209 for (int i = 0; i < scores.size(); i++) {
210 Hole *hole = holeList.at(i);
211 if (hole->shots() != scores[i])
212 hole->setShots(scores[i]);
217 void Score::addHole(Hole *iHole) {
221 QString Score::getScore(int i) const
223 if (i >= 0 && i < holeList.size())
224 return holeList.at(i)->shots();
229 QString Score::getTotal(int what) const
234 for (int i=0; i <= 17; i++)
235 tot += holeList.at(i)->shots().toInt();
237 if (what == TotalOut)
238 for (int i=0; i <= 8; i++)
239 tot += holeList.at(i)->shots().toInt();
242 for (int i=9; i <= 17; i++)
243 tot += holeList.at(i)->shots().toInt();
245 return QString("%1").arg(tot);
248 const QString& Score::getClubName() const
253 const QString& Score::getCourseName() const
258 const QString& Score::getDate() const
265 qDebug() << club << course << date ;
266 for (int i=0; i<holeList.size(); i++)
267 holeList.at(i)->dump();
270 ////////////////////////////////////////////////////////////////////////
272 ////////////////////////////////////////////////////////////////////////
274 Course::Course(const QXmlAttributes &attrs) {
275 name = attrs.value("name");
278 Course::Course(const QDomElement node, Club * parent)
281 name = node.attribute("name", "");
284 Course::Course(QString &name,
285 QVector<QString> &par,
286 QVector<QString> &hcp)
290 for (int i = 0; i < par.size(); i++) {
291 Hole *hole = new Hole(i+1, par[i], hcp[i]);
296 Club * Course::parent()
301 void Course::setParent(Club *parent)
306 QDomElement Course::toElement(QDomDocument doc)
308 QDomElement node = doc.createElement("course");
310 node.setAttribute("name", name);
312 for (int i=0; i < holeList.size(); i++) {
313 Hole *hole = holeList.at(i);
314 node.appendChild(hole->toElement(doc));
319 int Course::update(QVector<QString> &par,
320 QVector<QString> &hcp,
321 QVector<QString> &len)
324 for (int i = 0; i < par.size(); i++) {
325 Hole *hole = holeList.at(i);
326 if (hole->par() != par[i])
327 hole->setPar(par[i]);
328 if (hole->hcp() != hcp[i])
329 hole->setHcp(hcp[i]);
334 void Course::addHole(Hole *iHole) {
338 QString Course::getPar(int i) {
339 if (i >= 0 && i < holeList.size())
340 return holeList.at(i)->par();
345 QString Course::getHcp(int i) {
346 if (i >= 0 && i < holeList.size())
347 return holeList.at(i)->hcp();
352 QString& Course::getName()
357 QString Course::getTotal(int what) {
361 for (int i = 0; i < 18; i++)
362 tot += holeList.at(i)->par().toInt();
364 if (what == TotalOut)
365 for (int i = 0; i < 9; i++)
366 tot += holeList.at(i)->par().toInt();
369 for (int i = 9; i < 18; i++)
370 tot += holeList.at(i)->par().toInt();
372 return QString("%1").arg(tot);
376 void Course::dump() {
377 qDebug() << " " << name;
378 for (int i=0; i<holeList.size(); i++)
379 holeList.at(i)->dump();
382 ////////////////////////////////////////////////////////////////////////
384 ////////////////////////////////////////////////////////////////////////
386 Club::Club(const QXmlAttributes &attrs, bool readOnly)
387 : m_readOnly(readOnly)
390 name = attrs.value("name");
393 Club::Club(const QDomElement node, bool readOnly)
394 : m_readOnly(readOnly)
397 name = node.attribute("name", "");
400 Club::Club(QString &name, bool readOnly)
401 : m_readOnly(readOnly)
407 void Club::addCourse(Course *iCourse) {
408 courseList << iCourse;
409 iCourse->setParent(this);
412 void Club::delCourse(Course * course) {
413 int index = courseList.indexOf(course);
416 courseList.removeAt(index);
419 bool Club::isReadOnly()
427 if (courseList.count() == 0)
432 QDomElement Club::toElement(QDomDocument doc)
434 QDomElement node = doc.createElement("club");
436 node.setAttribute("name", name);
438 for (int i=0; i < courseList.size(); i++) {
439 Course *course = courseList.at(i);
440 node.appendChild(course->toElement(doc));
447 for (int i=0; i<courseList.size(); i++)
448 courseList.at(i)->dump();
451 QString& Club::getName() { return name; }
453 Course *Club::getCourse(int pos) {
454 return courseList.at(pos);
457 Course *Club::getCourse(const QString &courseName)
459 QListIterator<Course *> i(courseList);
462 while (i.hasNext()) {
464 if (c->getName() == courseName) {