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();
28 if (col >= stat.size())
34 if (role == Qt::TextAlignmentRole ) {
35 return Qt::AlignCenter;
41 if (role == Qt::DisplayRole) {
44 return stat.at(col)->rounds();
46 return stat.at(col)->average();
48 return stat.at(col)->min();
50 return stat.at(col)->max();
52 return stat.at(col)->birdies();
54 return stat.at(col)->pars();
56 return stat.at(col)->bogeys();
58 return stat.at(col)->more();
64 QVariant StatModel::headerData(int section, Qt::Orientation orientation, int role) const
66 // Only vertical header -- horizontal is hidden
67 if (role != Qt::DisplayRole)
70 if (orientation == Qt::Horizontal) {
71 // TODO: check when no or less data than cols
74 if (section < stat.size())
75 return stat.at(section)->year();
78 if (orientation == Qt::Vertical) {
81 return QString("Rounds");
83 return QString("Average");
85 return QString("Best");
87 return QString("Worst");
89 return QString("Birdies");
91 return QString("Pars");
93 return QString("Bogeys");
95 return QString("Double+");
102 // TODO: dup code from table-model.cpp
103 Course *StatModel::findCourse(const QString &clubName,
104 const QString &courseName)
106 QListIterator<Club *> i(clubList);
109 while (i.hasNext()) {
111 if (c->getName() == clubName) {
112 return c->getCourse(courseName);
118 void StatModel::update(void)
120 QListIterator<Score *> iScore(scoreList);
121 QMultiMap<QString, Score *> yearMap;
123 // Create multi map with years as keys, scores as values
124 while (iScore.hasNext()) {
125 Score *score = iScore.next();
126 QString year = score->getDate().split("-").at(0);
127 yearMap.insert(year, score);
129 // Create uniq list of years
130 QList<QString> yearList = yearMap.uniqueKeys();
132 // For each year collect the statistics
133 QListIterator<QString> iYear(yearList);
134 while (iYear.hasNext()) {
135 QString year = iYear.next();
137 StatItem *item = new StatItem;
140 QList<Score *> scoresPerYear = yearMap.values(year);
141 QListIterator<Score *> iScoresPerYear(scoresPerYear);
143 item->setRounds(scoresPerYear.count());
145 // for each year, add score
153 while (iScoresPerYear.hasNext()) {
154 Score *s = iScoresPerYear.next();
155 int tot = s->getTotal(Total).toInt();
164 Course *c = findCourse(s->getClubName(), s->getCourseName());
166 for (int i = 0; i < 18; i++) {
167 int par = c->getPar(i).toInt();
168 int shots = s->getScore(i).toInt();
170 if (shots == (par - 1))
172 else if (shots == par)
174 else if (shots == (par + 1))
176 else if (shots >= (par + 2))
180 item->setBirdies(birdies);
182 item->setBogeys(bogeys);
185 int avg = sum / scoresPerYear.count();
186 item->setAverage(avg);