Statistics - final bits including all stats and error handling
[scorecard] / src / stat-model.cpp
index 8fe6b36..f2396c7 100644 (file)
@@ -25,6 +25,9 @@ QVariant StatModel::data(const QModelIndex & index, int role) const
   int row = index.row();
   int col = index.column();
 
+  if (col >= stat.size())
+    return QVariant();
+    
   //
   // ALIGNMENT
   //
@@ -46,10 +49,13 @@ QVariant StatModel::data(const QModelIndex & index, int role) const
     case ROW_MAX: 
       return stat.at(col)->max();
     case ROW_BIRDIE: 
+      return stat.at(col)->birdies();
     case ROW_PAR: 
+      return stat.at(col)->pars();
     case ROW_BOGEY: 
+      return stat.at(col)->bogeys();
     case ROW_MORE:
-      return QVariant();
+      return stat.at(col)->more();
     }
   }
   return QVariant();
@@ -63,7 +69,10 @@ QVariant StatModel::headerData(int section, Qt::Orientation orientation, int rol
 
   if (orientation == Qt::Horizontal) {
     // TODO: check when no or less data than cols
-    return stat.at(section)->year();
+    // HERE CRASH
+
+    if (section < stat.size())
+      return stat.at(section)->year();
   }
 
   if (orientation == Qt::Vertical) {
@@ -83,34 +92,45 @@ QVariant StatModel::headerData(int section, Qt::Orientation orientation, int rol
     case ROW_BOGEY: 
       return QString("Bogeys");
     case ROW_MORE:
-      return QString("More");
+      return QString("Double+");
     }
   }
 
   return QVariant();
 }
 
+// TODO: dup code from table-model.cpp
+Course *StatModel::findCourse(const QString &clubName, 
+                             const QString &courseName)
+{
+  QListIterator<Club *> i(clubList);
+  Club *c;
+
+  while (i.hasNext()) {
+    c = i.next();
+    if (c->getName() == clubName) {
+      return c->getCourse(courseName);
+    }
+  }
+  return 0;
+}
+
 void StatModel::update(void)
 {
   QListIterator<Score *> iScore(scoreList);
   QMultiMap<QString, Score *> yearMap;
-  QStringList yearList;
 
   // Create multi map with years as keys, scores as values
-  // Create list of years
   while (iScore.hasNext()) {
     Score *score = iScore.next();
     QString year = score->getDate().split("-").at(0);
     yearMap.insert(year, score);
-    yearList << year;
   }
-
   // Create uniq list of years
-  QSet<QString> yearSet = QSet<QString>::fromList(yearList);
-
-  QSetIterator<QString> iYear(yearSet);
+  QList<QString> yearList = yearMap.uniqueKeys();
 
   // For each year collect the statistics
+  QListIterator<QString> iYear(yearList);
   while (iYear.hasNext()) {
     QString year = iYear.next();
 
@@ -126,6 +146,10 @@ void StatModel::update(void)
     int sum = 0;
     int min = 200;
     int max = 0;
+    int pars = 0;
+    int birdies = 0;
+    int bogeys = 0;
+    int more = 0;
     while (iScoresPerYear.hasNext()) {
       Score *s = iScoresPerYear.next();
       int tot = s->getTotal(Total).toInt();
@@ -136,9 +160,29 @@ void StatModel::update(void)
 
       if (tot < min)
        min = tot;
+
+      Course *c = findCourse(s->getClubName(), s->getCourseName());
+
+      for (int i = 0; i < 18; i++) {
+       int par = c->getPar(i).toInt();
+       int shots = s->getScore(i).toInt();
+       
+       if (shots == (par - 1))
+         birdies++;
+       else if (shots == par)
+         pars++;
+       else if (shots == (par + 1))
+         bogeys++;
+       else if (shots >= (par + 2))
+         more++;
+      }
     }
+    item->setBirdies(birdies);
+    item->setPars(pars);
+    item->setBogeys(bogeys);
+    item->setMore(more);
+
     int avg = sum / scoresPerYear.count();
-    
     item->setAverage(avg);
     item->setMin(min);
     item->setMax(max);