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
23 int StatModel::columnCount(const QModelIndex & parent) const
28 QVariant StatModel::data(const QModelIndex & index, int role) const
33 int row = index.row();
34 int col = index.column();
36 if (col >= stat.size())
42 if (role == Qt::TextAlignmentRole ) {
43 return Qt::AlignCenter;
49 if (role == Qt::DisplayRole) {
52 return stat.at(col)->rounds();
54 return stat.at(col)->average();
56 return stat.at(col)->min();
58 return stat.at(col)->max();
60 return stat.at(col)->birdies();
62 return stat.at(col)->pars();
64 return stat.at(col)->bogeys();
66 return stat.at(col)->more();
72 QVariant StatModel::headerData(int section, Qt::Orientation orientation, int role) const
74 // Only vertical header -- horizontal is hidden
75 if (role != Qt::DisplayRole)
78 if (orientation == Qt::Horizontal) {
79 // TODO: check when no or less data than cols
82 if (section < stat.size())
83 return stat.at(section)->year();
86 if (orientation == Qt::Vertical) {
89 return QString("Rounds");
91 return QString("Average");
93 return QString("Best");
95 return QString("Worst");
97 return QString("Birdies");
99 return QString("Pars");
101 return QString("Bogeys");
103 return QString("Double+");
110 // TODO: dup code from table-model.cpp
111 Course *StatModel::findCourse(const QString &clubName,
112 const QString &courseName)
114 QListIterator<Club *> i(clubList);
117 while (i.hasNext()) {
119 if (c->getName() == clubName) {
120 return c->getCourse(courseName);
126 void StatModel::update(void)
128 QListIterator<Score *> iScore(scoreList);
129 QMultiMap<QString, Score *> yearMap;
131 // Create multi map with years as keys, scores as values
132 while (iScore.hasNext()) {
133 Score *score = iScore.next();
134 QString year = score->getDate().split("-").at(0);
135 yearMap.insert(year, score);
137 // Create uniq list of years
138 QList<QString> yearList = yearMap.uniqueKeys();
140 // For each year collect the statistics
141 QListIterator<QString> iYear(yearList);
142 while (iYear.hasNext()) {
143 QString year = iYear.next();
145 StatItem *item = new StatItem;
148 QList<Score *> scoresPerYear = yearMap.values(year);
149 QListIterator<Score *> iScoresPerYear(scoresPerYear);
151 item->setRounds(scoresPerYear.count());
153 // for each year, add score
161 while (iScoresPerYear.hasNext()) {
162 Score *s = iScoresPerYear.next();
163 int tot = s->getTotal(Total).toInt();
172 Course *c = findCourse(s->getClubName(), s->getCourseName());
174 for (int i = 0; i < 18; i++) {
175 int par = c->getPar(i).toInt();
176 int shots = s->getScore(i).toInt();
178 if (shots == (par - 1))
180 else if (shots == par)
182 else if (shots == (par + 1))
184 else if (shots >= (par + 2))
188 item->setBirdies(birdies);
190 item->setBogeys(bogeys);
193 int avg = sum / scoresPerYear.count();
194 item->setAverage(avg);