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.
12 #include "score-common.h"
13 #include "stat-model.h"
15 StatModel::StatModel(QList<Club *> &cList, QList<Score *> &sList) : clubList(cList), scoreList(sList)
21 int StatModel::rowCount(const QModelIndex & parent) const
27 int StatModel::columnCount(const QModelIndex & parent) const
33 QVariant StatModel::data(const QModelIndex & index, int role) const
38 int row = index.row();
39 int col = index.column();
41 if (col >= stat.size())
47 if (role == Qt::TextAlignmentRole ) {
48 return Qt::AlignCenter;
54 if (role == Qt::FontRole) {
56 font.setPointSize(fontSize);
62 if (role == Qt::DisplayRole) {
65 return stat.at(col)->rounds();
67 return stat.at(col)->average();
69 return stat.at(col)->min();
71 return stat.at(col)->max();
73 return stat.at(col)->birdies();
75 return stat.at(col)->pars();
77 return stat.at(col)->bogeys();
79 return stat.at(col)->more();
85 QVariant StatModel::headerData(int section, Qt::Orientation orientation, int role) const
87 // Only vertical header -- horizontal is hidden
88 if (role != Qt::DisplayRole)
91 if (orientation == Qt::Horizontal) {
92 // TODO: check when no or less data than cols
95 if (section < stat.size())
96 return stat.at(section)->year();
99 if (orientation == Qt::Vertical) {
102 return QString("Rounds");
104 return QString("Average");
106 return QString("Best");
108 return QString("Worst");
110 return QString("Birdies");
112 return QString("Pars");
114 return QString("Bogeys");
116 return QString("Double+");
123 // TODO: dup code from table-model.cpp
124 Course *StatModel::findCourse(const QString &clubName,
125 const QString &courseName)
127 QListIterator<Club *> i(clubList);
130 while (i.hasNext()) {
132 if (c->getName() == clubName) {
133 return c->getCourse(courseName);
139 void StatModel::update(void)
142 QListIterator<Score *> iScore(scoreList);
143 QMultiMap<QString, Score *> yearMap;
145 // Create multi map with years as keys, scores as values
146 while (iScore.hasNext()) {
147 Score *score = iScore.next();
148 QString year = score->getDate().split("-").at(0);
149 yearMap.insert(year, score);
151 // Create uniq list of years
152 QList<QString> yearList = yearMap.uniqueKeys();
154 // For each year collect the statistics
155 QListIterator<QString> iYear(yearList);
156 while (iYear.hasNext()) {
157 QString year = iYear.next();
159 StatItem *item = new StatItem;
162 QList<Score *> scoresPerYear = yearMap.values(year);
163 QListIterator<Score *> iScoresPerYear(scoresPerYear);
165 item->setRounds(scoresPerYear.count());
167 // for each year, add score
175 while (iScoresPerYear.hasNext()) {
176 Score *s = iScoresPerYear.next();
177 int tot = s->getTotal(Total).toInt();
186 Course *c = findCourse(s->getClubName(), s->getCourseName());
188 // We have a score on a course which we don't have...
191 for (int i = 0; i < 18; i++) {
192 int par = c->getPar(i).toInt();
193 int shots = s->getScore(i).toInt();
195 if (shots == (par - 1))
197 else if (shots == par)
199 else if (shots == (par + 1))
201 else if (shots >= (par + 2))
205 item->setBirdies(birdies);
207 item->setBogeys(bogeys);
210 int avg = sum / scoresPerYear.count();
211 item->setAverage(avg);