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 "table-model.h"
13 #include "score-common.h"
17 Qt::ItemFlags ScoreTableModel::flags (const QModelIndex &)
22 void ScoreTableModel::setMode(int m)
27 int ScoreTableModel::mode(void)
32 // Assign the 'sList' to internal 'scoreList'. Set the current score
33 // to 'currentScore', or to 's'.
34 void ScoreTableModel::setScore(QList<Score *> &sList, Score *s)
37 if (scoreList.size() > 0) {
39 currentScore = scoreList.indexOf(s);
40 if (currentScore == -1)
43 score = scoreList.at(currentScore); // NOTE: assumes non-empty list
44 course = findCourse(score->getClubName(), score->getCourseName());
48 void ScoreTableModel::setClub(QList<Club *> &cList)
52 if (clubList.size() > 0)
53 club = clubList.at(0);
56 course = club->getCourse(0);
59 QString ScoreTableModel::getInfoText()
64 str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
69 QString ScoreTableModel::getCountText()
71 QString str = QString("%1/%2").arg(currentScore+1, 2).arg(scoreList.count(), 2);
75 QString& ScoreTableModel::clubName(void)
78 return club->getName();
83 QString& ScoreTableModel::courseName(void)
86 return course->getName();
91 Course *ScoreTableModel::findCourse(const QString &clubName,
92 const QString &courseName)
94 QListIterator<Club *> i(clubList);
99 if (c->getName() == clubName) {
100 return c->getCourse(courseName);
106 Club *ScoreTableModel::getClub(void)
111 Course *ScoreTableModel::getCourse(void)
116 Score *ScoreTableModel::getScore(void)
121 void ScoreTableModel::first()
123 if (score && course) {
125 score = scoreList.at(currentScore);
126 course = findCourse(score->getClubName(), score->getCourseName());
127 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
131 void ScoreTableModel::last()
133 if (score && course) {
134 currentScore = scoreList.size() - 1;
135 score = scoreList.at(currentScore);
136 course = findCourse(score->getClubName(), score->getCourseName());
137 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
141 void ScoreTableModel::next()
143 if (score && course) {
144 if (currentScore < (scoreList.size() - 1)) {
146 score = scoreList.at(currentScore);
147 course = findCourse(score->getClubName(), score->getCourseName());
148 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
153 void ScoreTableModel::prev()
155 if (score && course) {
156 if (currentScore > 0) {
158 score = scoreList.at(currentScore);
159 course = findCourse(score->getClubName(), score->getCourseName());
160 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
165 int ScoreTableModel::rowCount(const QModelIndex &) const
170 int ScoreTableModel::columnCount(const QModelIndex &) const
172 return 9 + 2; // 2 for in/out and tot columns
175 QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &parent) const
177 if (hasIndex(row, column, parent)) {
178 int flag = (parent.column() > 0) ? parent.column() : 0;
179 return createIndex(row, column, flag);
182 return QModelIndex();
186 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
188 // TODO: move away from the stack
190 if (!index.isValid())
193 int row = index.row();
194 int col = index.column();
199 if (role == Qt::TextAlignmentRole ) {
200 return Qt::AlignCenter;
203 if (index.column() > 10)
209 if (role == Qt::BackgroundRole) {
210 // Hole numbers 1-18. All hole nums, in, out and tot cell but not
212 if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
213 QBrush brush(ScoreColor::holeBg());
216 if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) {
219 if (row == ROW_SCORE) {
220 par = course->getPar(col).toInt();
221 shots = score->getScore(col).toInt();
224 par = course->getPar(col + 9).toInt();
225 shots = score->getScore(col + 9).toInt();
228 if (col == (COLS+1) && row == ROW_SCORE_2) {
230 QBrush brush(ScoreColor::total());
235 QBrush brush(ScoreColor::subTotal());
241 QBrush brush(ScoreColor::par());
244 if (shots == (par-1)) {
246 QBrush brush(ScoreColor::birdie());
249 if (shots == (par+1)) {
251 QBrush brush(ScoreColor::bogey());
254 if (shots == (par+2)) {
256 QBrush brush(ScoreColor::doubleBogey());
259 if (shots > (par+2)) {
261 QBrush brush(ScoreColor::bad());
271 if (role == Qt::FontRole) {
272 if (row == ROW_SCORE_2 && col == (COLS+1)) {
281 if (role == Qt::DisplayRole) {
286 return QString("Out");
287 if (row == ROW_HOLE_2)
288 return QString("In");
291 if (score && course && row == ROW_PAR)
292 return course->getTotal(TotalOut);
293 if (score && course && row == ROW_PAR_2)
294 return course->getTotal(TotalIn);
297 if (score && row == ROW_SCORE)
298 return score->getTotal(TotalOut);
299 if (score && row == ROW_SCORE_2)
300 return score->getTotal(TotalIn);
303 else if (col == (COLS+1)) {
305 if (row == ROW_HOLE_2)
306 return QString("Tot");
308 if (score && course && row == ROW_PAR_2)
309 return course->getTotal(Total);
310 if (score && row == ROW_SCORE_2)
311 return score->getTotal(Total);
322 return course->getPar(col);
325 return course->getPar(col + 9);
328 return course->getHcp(col);
331 return course->getHcp(col + 9);
334 return score->getScore(col);
337 return score->getScore(col + 9);
344 int ScoreTableModel::setItem(int row, int col, int)
346 emit dataChanged(createIndex(row, col), createIndex(row, col));
350 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
352 // Only vertical header -- horizontal is hidden
353 if (orientation == Qt::Horizontal)
356 if (role == Qt::DisplayRole) {
360 return QString("Par");
363 return QString("HCP");
366 return QString("Score");