Statistics - new files
[scorecard] / src / stat-model.cpp
1 #include <QVariant>
2
3 #include "stat-model.h"
4
5 StatModel::StatModel(QList<Club *> &cList, QList<Score *> &sList) : clubList(cList), scoreList(sList)
6 {
7   update();
8 }
9
10 int StatModel::rowCount(const QModelIndex & parent) const
11 {
12   return ROWS;
13 }
14
15 int StatModel::columnCount(const QModelIndex & parent) const
16 {
17   return COLS;
18 }
19
20 QVariant StatModel::data(const QModelIndex & index, int role) const
21 {
22   if (!index.isValid())
23     return QVariant();
24
25   int row = index.row();
26   int col = index.column();
27
28   //
29   // ALIGNMENT
30   //
31   if (role == Qt::TextAlignmentRole ) {
32     return Qt::AlignCenter;
33   }
34
35   //
36   // NUMBERS
37   //
38   if (role == Qt::DisplayRole) {
39     switch (row) {
40     case ROW_ROUNDS: 
41       return stat.at(col)->rounds();
42     case ROW_AVERAGE: 
43       return stat.at(col)->average();
44     case ROW_MIN: 
45       return stat.at(col)->min();
46     case ROW_MAX: 
47       return stat.at(col)->max();
48     case ROW_BIRDIE: 
49     case ROW_PAR: 
50     case ROW_BOGEY: 
51     case ROW_MORE:
52       return QVariant();
53     }
54   }
55   return QVariant();
56 }
57
58 QVariant StatModel::headerData(int section, Qt::Orientation orientation, int role) const
59 {
60   // Only vertical header -- horizontal is hidden
61   if (role != Qt::DisplayRole)
62     return QVariant();
63
64   if (orientation == Qt::Horizontal) {
65     // TODO: check when no or less data than cols
66     return stat.at(section)->year();
67   }
68
69   if (orientation == Qt::Vertical) {
70     switch(section) {
71     case ROW_ROUNDS: 
72       return QString("Rounds");
73     case ROW_AVERAGE: 
74       return QString("Average");
75     case ROW_MIN: 
76       return QString("Best");
77     case ROW_MAX: 
78       return QString("Worst");
79     case ROW_BIRDIE: 
80       return QString("Birdies");
81     case ROW_PAR: 
82       return QString("Pars");
83     case ROW_BOGEY: 
84       return QString("Bogeys");
85     case ROW_MORE:
86       return QString("More");
87     }
88   }
89
90   return QVariant();
91 }
92
93 void StatModel::update(void)
94 {
95   QListIterator<Score *> iScore(scoreList);
96   QMultiMap<QString, Score *> yearMap;
97   QStringList yearList;
98
99   // Create multi map with years as keys, scores as values
100   // Create list of years
101   while (iScore.hasNext()) {
102     Score *score = iScore.next();
103     QString year = score->getDate().split("-").at(0);
104     yearMap.insert(year, score);
105     yearList << year;
106   }
107
108   // Create uniq list of years
109   QSet<QString> yearSet = QSet<QString>::fromList(yearList);
110
111   QSetIterator<QString> iYear(yearSet);
112
113   // For each year collect the statistics
114   while (iYear.hasNext()) {
115     QString year = iYear.next();
116
117     StatItem *item = new StatItem;
118     item->setYear(year);
119
120     QList<Score *> scoresPerYear = yearMap.values(year);
121     QListIterator<Score *> iScoresPerYear(scoresPerYear);
122     
123     item->setRounds(scoresPerYear.count());
124
125     // for each year, add score
126     int sum = 0;
127     int min = 200;
128     int max = 0;
129     while (iScoresPerYear.hasNext()) {
130       Score *s = iScoresPerYear.next();
131       int tot = s->getTotal(Total).toInt();
132       sum += tot;
133
134       if (tot > max)
135         max = tot;
136
137       if (tot < min)
138         min = tot;
139     }
140     int avg = sum / scoresPerYear.count();
141     
142     item->setAverage(avg);
143     item->setMin(min);
144     item->setMax(max);
145
146     stat << item;
147   }
148 }