- Fixed course delete crashes
[scorecard] / src / data.cpp
index 020d701..ad0813c 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * Copyright (C) 2009 Sakari Poussa
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 2.
+ */
+
 #include "data.h"
 
 ////////////////////////////////////////////////////////////////////////
@@ -65,14 +73,26 @@ QString Hole::getShots() {
   return shots;
 }
 
+void Hole::setShots(QString& s) {
+  shots = s;
+}
+
 QString Hole::getHcp() {
   return hcp;
 }
 
+void Hole::setHcp(QString& s) {
+  hcp = s;
+}
+
 QString Hole::getPar() {
   return par;
 }
 
+void Hole::setPar(QString& s) {
+  par = s;
+}
+
 void Hole::dump() {
   qDebug() << num << "(" << par << ") : " << shots << "/" << putts ; 
 }
@@ -128,18 +148,30 @@ QDomElement Score::toElement(QDomDocument doc)
   return node;
 }
 
+int Score::update(QVector<QString> &scores)
+{
+  for (int i = 0; i < scores.size(); i++) {
+    Hole *hole = holeList.at(i);
+    if (hole->getShots() != scores[i])
+      hole->setShots(scores[i]);
+  }
+  return 0;
+}
+
 void Score::addHole(Hole *iHole) {
   holeList << iHole;
 }
   
-QString Score::getScore(int i) {
+QString Score::getScore(int i) const 
+{
   if (i >= 0 && i < holeList.size())
     return holeList.at(i)->getShots();
   else
     return QString("-");
 }
   
-QString Score::getTotal(int what) {
+QString Score::getTotal(int what) const
+{
   int tot = 0;
 
   if (what == Total)
@@ -157,20 +189,24 @@ QString Score::getTotal(int what) {
   return QString("%1").arg(tot);
 }
 
-QString& Score::getClubName() {
+const QString& Score::getClubName() const
+{
   return club;
 }
 
-QString& Score::getCourseName() {
+const QString& Score::getCourseName() const
+{
   return course;
 }
 
-QString& Score::getDate() {
+const QString& Score::getDate() const
+{
   return date;
 }
 
-void Score::dump() {
-  qDebug() << club << " " << course << " " << date ; 
+void Score::dump()
+{
+  qDebug() << club << course << date ; 
   for (int i=0; i<holeList.size(); i++)
     holeList.at(i)->dump();
 }
@@ -183,8 +219,10 @@ Course::Course(const QXmlAttributes &attrs) {
   name = attrs.value("name");
 }
 
-Course::Course(const QDomElement node) {
-  name = node.attribute("name", "");
+Course::Course(const QDomElement node, Club * parent)
+    : club(parent)
+{
+    name = node.attribute("name", "");
 }
 
 Course::Course(QString &name, 
@@ -199,6 +237,16 @@ Course::Course(QString &name,
   }
 }
 
+Club * Course::parent()
+{
+    return club;
+}
+
+void Course::setParent(Club *parent)
+{
+    club = parent;
+}
+
 QDomElement Course::toElement(QDomDocument doc)
 {
   QDomElement node = doc.createElement("course");
@@ -212,6 +260,21 @@ QDomElement Course::toElement(QDomDocument doc)
   return node;
 }
 
+int Course::update(QVector<QString> &par,
+                  QVector<QString> &hcp,
+                  QVector<QString> &len)
+{
+  Q_UNUSED(len);
+  for (int i = 0; i < par.size(); i++) {
+    Hole *hole = holeList.at(i);
+    if (hole->getPar() != par[i])
+      hole->setPar(par[i]);
+    if (hole->getHcp() != hcp[i])
+      hole->setHcp(hcp[i]);
+  }
+  return 0;
+}
+
 void Course::addHole(Hole *iHole) {
   holeList << iHole;
 }
@@ -279,6 +342,22 @@ Club::Club(QString &name)
 
 void Club::addCourse(Course *iCourse) {
   courseList << iCourse;
+  iCourse->setParent(this);
+}
+
+void Club::delCourse(Course * course) {
+    int index = courseList.indexOf(course);
+
+    if (index != -1)
+        courseList.removeAt(index);
+}
+
+bool Club::isEmpty()
+{
+    bool rc = false;
+    if (courseList.count() == 0)
+        rc = true;
+    return rc;
 }
 
 QDomElement Club::toElement(QDomDocument doc)
@@ -306,7 +385,7 @@ Course *Club::getCourse(int pos) {
   return courseList.at(pos);
 }
 
-Course *Club::getCourse(QString &courseName) 
+Course *Club::getCourse(const QString &courseName) 
 {
   QListIterator<Course *> i(courseList);
   Course *c = 0;