2 * Copyright (C) 2009 Sakari Poussa
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 2.
11 #include "stat-model.h"
13 StatModel::StatModel(QList<Club *> &cList, QList<Score *> &sList) : clubList(cList), scoreList(sList)
18 int StatModel::rowCount(const QModelIndex & parent) const
24 int StatModel::columnCount(const QModelIndex & parent) const
30 QVariant StatModel::data(const QModelIndex & index, int role) const
35 int row = index.row();
36 int col = index.column();
38 if (col >= stat.size())
44 if (role == Qt::TextAlignmentRole ) {
45 return Qt::AlignCenter;
51 if (role == Qt::DisplayRole) {
54 return stat.at(col)->rounds();
56 return stat.at(col)->average();
58 return stat.at(col)->min();
60 return stat.at(col)->max();
62 return stat.at(col)->birdies();
64 return stat.at(col)->pars();
66 return stat.at(col)->bogeys();
68 return stat.at(col)->more();
74 QVariant StatModel::headerData(int section, Qt::Orientation orientation, int role) const
76 // Only vertical header -- horizontal is hidden
77 if (role != Qt::DisplayRole)
80 if (orientation == Qt::Horizontal) {
81 // TODO: check when no or less data than cols
84 if (section < stat.size())
85 return stat.at(section)->year();
88 if (orientation == Qt::Vertical) {
91 return QString("Rounds");
93 return QString("Average");
95 return QString("Best");
97 return QString("Worst");
99 return QString("Birdies");
101 return QString("Pars");
103 return QString("Bogeys");
105 return QString("Double+");
112 // TODO: dup code from table-model.cpp
113 Course *StatModel::findCourse(const QString &clubName,
114 const QString &courseName)
116 QListIterator<Club *> i(clubList);
119 while (i.hasNext()) {
121 if (c->getName() == clubName) {
122 return c->getCourse(courseName);
128 void StatModel::update(void)
130 QListIterator<Score *> iScore(scoreList);
131 QMultiMap<QString, Score *> yearMap;
133 // Create multi map with years as keys, scores as values
134 while (iScore.hasNext()) {
135 Score *score = iScore.next();
136 QString year = score->getDate().split("-").at(0);
137 yearMap.insert(year, score);
139 // Create uniq list of years
140 QList<QString> yearList = yearMap.uniqueKeys();
142 // For each year collect the statistics
143 QListIterator<QString> iYear(yearList);
144 while (iYear.hasNext()) {
145 QString year = iYear.next();
147 StatItem *item = new StatItem;
150 QList<Score *> scoresPerYear = yearMap.values(year);
151 QListIterator<Score *> iScoresPerYear(scoresPerYear);
153 item->setRounds(scoresPerYear.count());
155 // for each year, add score
163 while (iScoresPerYear.hasNext()) {
164 Score *s = iScoresPerYear.next();
165 int tot = s->getTotal(Total).toInt();
174 Course *c = findCourse(s->getClubName(), s->getCourseName());
176 for (int i = 0; i < 18; i++) {
177 int par = c->getPar(i).toInt();
178 int shots = s->getScore(i).toInt();
180 if (shots == (par - 1))
182 else if (shots == par)
184 else if (shots == (par + 1))
186 else if (shots >= (par + 2))
190 item->setBirdies(birdies);
192 item->setBogeys(bogeys);
195 int avg = sum / scoresPerYear.count();
196 item->setAverage(avg);