3 #include "stat-model.h"
5 StatModel::StatModel(QList<Club *> &cList, QList<Score *> &sList) : clubList(cList), scoreList(sList)
10 int StatModel::rowCount(const QModelIndex & parent) const
15 int StatModel::columnCount(const QModelIndex & parent) const
20 QVariant StatModel::data(const QModelIndex & index, int role) const
25 int row = index.row();
26 int col = index.column();
31 if (role == Qt::TextAlignmentRole ) {
32 return Qt::AlignCenter;
38 if (role == Qt::DisplayRole) {
41 return stat.at(col)->rounds();
43 return stat.at(col)->average();
45 return stat.at(col)->min();
47 return stat.at(col)->max();
58 QVariant StatModel::headerData(int section, Qt::Orientation orientation, int role) const
60 // Only vertical header -- horizontal is hidden
61 if (role != Qt::DisplayRole)
64 if (orientation == Qt::Horizontal) {
65 // TODO: check when no or less data than cols
66 return stat.at(section)->year();
69 if (orientation == Qt::Vertical) {
72 return QString("Rounds");
74 return QString("Average");
76 return QString("Best");
78 return QString("Worst");
80 return QString("Birdies");
82 return QString("Pars");
84 return QString("Bogeys");
86 return QString("More");
93 void StatModel::update(void)
95 QListIterator<Score *> iScore(scoreList);
96 QMultiMap<QString, Score *> yearMap;
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);
108 // Create uniq list of years
109 QSet<QString> yearSet = QSet<QString>::fromList(yearList);
111 QSetIterator<QString> iYear(yearSet);
113 // For each year collect the statistics
114 while (iYear.hasNext()) {
115 QString year = iYear.next();
117 StatItem *item = new StatItem;
120 QList<Score *> scoresPerYear = yearMap.values(year);
121 QListIterator<Score *> iScoresPerYear(scoresPerYear);
123 item->setRounds(scoresPerYear.count());
125 // for each year, add score
129 while (iScoresPerYear.hasNext()) {
130 Score *s = iScoresPerYear.next();
131 int tot = s->getTotal(Total).toInt();
140 int avg = sum / scoresPerYear.count();
142 item->setAverage(avg);