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)
20 int StatModel::rowCount(const QModelIndex & parent) const
26 int StatModel::columnCount(const QModelIndex & parent) const
32 QVariant StatModel::data(const QModelIndex & index, int role) const
37 int row = index.row();
38 int col = index.column();
40 if (col >= stat.size())
46 if (role == Qt::TextAlignmentRole ) {
47 return Qt::AlignCenter;
53 if (role == Qt::FontRole) {
55 font.setPointSize(fontSize);
61 if (role == Qt::DisplayRole) {
64 return stat.at(col)->rounds();
66 return stat.at(col)->average();
68 return stat.at(col)->min();
70 return stat.at(col)->max();
72 return stat.at(col)->birdies();
74 return stat.at(col)->pars();
76 return stat.at(col)->bogeys();
78 return stat.at(col)->more();
84 QVariant StatModel::headerData(int section, Qt::Orientation orientation, int role) const
86 // Only vertical header -- horizontal is hidden
87 if (role != Qt::DisplayRole)
90 if (orientation == Qt::Horizontal) {
91 // TODO: check when no or less data than cols
94 if (section < stat.size())
95 return stat.at(section)->year();
98 if (orientation == Qt::Vertical) {
101 return QString("Rounds");
103 return QString("Average");
105 return QString("Best");
107 return QString("Worst");
109 return QString("Birdies");
111 return QString("Pars");
113 return QString("Bogeys");
115 return QString("Double+");
122 // TODO: dup code from table-model.cpp
123 Course *StatModel::findCourse(const QString &clubName,
124 const QString &courseName)
126 QListIterator<Club *> i(clubList);
129 while (i.hasNext()) {
131 if (c->getName() == clubName) {
132 return c->getCourse(courseName);
138 void StatModel::update(void)
140 QListIterator<Score *> iScore(scoreList);
141 QMultiMap<QString, Score *> yearMap;
143 // Create multi map with years as keys, scores as values
144 while (iScore.hasNext()) {
145 Score *score = iScore.next();
146 QString year = score->getDate().split("-").at(0);
147 yearMap.insert(year, score);
149 // Create uniq list of years
150 QList<QString> yearList = yearMap.uniqueKeys();
152 // For each year collect the statistics
153 QListIterator<QString> iYear(yearList);
154 while (iYear.hasNext()) {
155 QString year = iYear.next();
157 StatItem *item = new StatItem;
160 QList<Score *> scoresPerYear = yearMap.values(year);
161 QListIterator<Score *> iScoresPerYear(scoresPerYear);
163 item->setRounds(scoresPerYear.count());
165 // for each year, add score
173 while (iScoresPerYear.hasNext()) {
174 Score *s = iScoresPerYear.next();
175 int tot = s->getTotal(Total).toInt();
184 Course *c = findCourse(s->getClubName(), s->getCourseName());
186 for (int i = 0; i < 18; i++) {
187 int par = c->getPar(i).toInt();
188 int shots = s->getScore(i).toInt();
190 if (shots == (par - 1))
192 else if (shots == par)
194 else if (shots == (par + 1))
196 else if (shots >= (par + 2))
200 item->setBirdies(birdies);
202 item->setBogeys(bogeys);
205 int avg = sum / scoresPerYear.count();
206 item->setAverage(avg);