Statistics - new files
authorSakari Poussa <spoussa@gmail.com>
Tue, 24 Nov 2009 19:51:58 +0000 (21:51 +0200)
committerSakari Poussa <spoussa@gmail.com>
Tue, 24 Nov 2009 19:51:58 +0000 (21:51 +0200)
src/stat-model.cpp [new file with mode: 0644]
src/stat-model.h [new file with mode: 0644]

diff --git a/src/stat-model.cpp b/src/stat-model.cpp
new file mode 100644 (file)
index 0000000..8fe6b36
--- /dev/null
@@ -0,0 +1,148 @@
+#include <QVariant>
+
+#include "stat-model.h"
+
+StatModel::StatModel(QList<Club *> &cList, QList<Score *> &sList) : clubList(cList), scoreList(sList)
+{
+  update();
+}
+
+int StatModel::rowCount(const QModelIndex & parent) const
+{
+  return ROWS;
+}
+
+int StatModel::columnCount(const QModelIndex & parent) const
+{
+  return COLS;
+}
+
+QVariant StatModel::data(const QModelIndex & index, int role) const
+{
+  if (!index.isValid())
+    return QVariant();
+
+  int row = index.row();
+  int col = index.column();
+
+  //
+  // ALIGNMENT
+  //
+  if (role == Qt::TextAlignmentRole ) {
+    return Qt::AlignCenter;
+  }
+
+  //
+  // NUMBERS
+  //
+  if (role == Qt::DisplayRole) {
+    switch (row) {
+    case ROW_ROUNDS: 
+      return stat.at(col)->rounds();
+    case ROW_AVERAGE: 
+      return stat.at(col)->average();
+    case ROW_MIN: 
+      return stat.at(col)->min();
+    case ROW_MAX: 
+      return stat.at(col)->max();
+    case ROW_BIRDIE: 
+    case ROW_PAR: 
+    case ROW_BOGEY: 
+    case ROW_MORE:
+      return QVariant();
+    }
+  }
+  return QVariant();
+}
+
+QVariant StatModel::headerData(int section, Qt::Orientation orientation, int role) const
+{
+  // Only vertical header -- horizontal is hidden
+  if (role != Qt::DisplayRole)
+    return QVariant();
+
+  if (orientation == Qt::Horizontal) {
+    // TODO: check when no or less data than cols
+    return stat.at(section)->year();
+  }
+
+  if (orientation == Qt::Vertical) {
+    switch(section) {
+    case ROW_ROUNDS: 
+      return QString("Rounds");
+    case ROW_AVERAGE: 
+      return QString("Average");
+    case ROW_MIN: 
+      return QString("Best");
+    case ROW_MAX: 
+      return QString("Worst");
+    case ROW_BIRDIE: 
+      return QString("Birdies");
+    case ROW_PAR: 
+      return QString("Pars");
+    case ROW_BOGEY: 
+      return QString("Bogeys");
+    case ROW_MORE:
+      return QString("More");
+    }
+  }
+
+  return QVariant();
+}
+
+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);
+
+  // For each year collect the statistics
+  while (iYear.hasNext()) {
+    QString year = iYear.next();
+
+    StatItem *item = new StatItem;
+    item->setYear(year);
+
+    QList<Score *> scoresPerYear = yearMap.values(year);
+    QListIterator<Score *> iScoresPerYear(scoresPerYear);
+    
+    item->setRounds(scoresPerYear.count());
+
+    // for each year, add score
+    int sum = 0;
+    int min = 200;
+    int max = 0;
+    while (iScoresPerYear.hasNext()) {
+      Score *s = iScoresPerYear.next();
+      int tot = s->getTotal(Total).toInt();
+      sum += tot;
+
+      if (tot > max)
+       max = tot;
+
+      if (tot < min)
+       min = tot;
+    }
+    int avg = sum / scoresPerYear.count();
+    
+    item->setAverage(avg);
+    item->setMin(min);
+    item->setMax(max);
+
+    stat << item;
+  }
+}
diff --git a/src/stat-model.h b/src/stat-model.h
new file mode 100644 (file)
index 0000000..d6a77a6
--- /dev/null
@@ -0,0 +1,76 @@
+#include <QAbstractTableModel>
+
+#include "data.h"
+
+class StatItem
+{
+ public:
+  StatItem() {}
+
+  void setYear(QString &year) { m_year = year.toInt(); }
+  int year() { return m_year; }
+
+  void setRounds(int n) { m_rounds = n; }
+  int rounds() { return m_rounds; }
+
+  void setAverage(int n) { m_average = n; }
+  int average() { return m_average; }
+
+  void setMin(int n) { m_min = n; }
+  int min() { return m_min; }
+
+  void setMax(int n) { m_max = n; }
+  int max() { return m_max; }
+
+ private:
+
+  int m_year;
+  int m_rounds;
+  int m_average;
+  int m_min;
+  int m_max;
+  int m_birdies;
+  int m_pars;
+  int m_bogeys;
+  int m_more;
+};
+
+class StatModel : public QAbstractTableModel
+{
+  Q_OBJECT
+
+public:
+  StatModel(QList<Club *> &clubList, QList<Score *> &scoreList);
+
+  int rowCount(const QModelIndex & parent) const;
+  int columnCount(const QModelIndex & parent) const;
+  QVariant data(const QModelIndex & index, int role) const;
+  QVariant headerData(int section, Qt::Orientation orientation, int role) const;
+
+
+ private:
+  
+  enum { ROWS = 8, COLS = 4 };
+  enum { ROW_ROUNDS = 0, 
+        ROW_AVERAGE, 
+        ROW_MIN, 
+        ROW_MAX, 
+        ROW_BIRDIE, 
+        ROW_PAR, 
+        ROW_BOGEY, 
+        ROW_MORE
+  };
+  enum { COL_TOTAL = 0, 
+        COL_1ST_YEAR, // Latest year
+        COL_2ND_YEAR, // One before latest
+        COL_3RD_YEAR  // Two before latest
+  };
+
+
+  QList<Club *> &clubList;
+  QList<Score *> &scoreList;
+
+  QList<StatItem *> stat;
+
+  void update();
+};