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"
15 Qt::ItemFlags ScoreTableModel::flags (const QModelIndex &)
20 void ScoreTableModel::setMode(int m)
25 int ScoreTableModel::mode(void)
30 // Assign the 'sList' to internal 'scoreList'. Set the current score
31 // to 'currentScore', or to 's'.
32 void ScoreTableModel::setScore(QList<Score *> &sList, Score *s)
35 if (scoreList.size() > 0) {
37 currentScore = scoreList.indexOf(s);
38 if (currentScore == -1)
41 score = scoreList.at(currentScore); // NOTE: assumes non-empty list
45 void ScoreTableModel::setClub(QList<Club *> &cList)
49 if (clubList.size() > 0)
50 club = clubList.at(0);
53 course = club->getCourse(0);
56 QString ScoreTableModel::getInfoText()
61 str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
66 QString ScoreTableModel::getCountText()
68 QString str = QString("%1/%2").arg(currentScore+1, 2).arg(scoreList.count(), 2);
72 QString& ScoreTableModel::clubName(void)
75 return club->getName();
80 QString& ScoreTableModel::courseName(void)
83 return course->getName();
88 Course *ScoreTableModel::findCourse(const QString &clubName,
89 const QString &courseName)
91 QListIterator<Club *> i(clubList);
96 if (c->getName() == clubName) {
97 return c->getCourse(courseName);
103 Club *ScoreTableModel::getClub(void)
108 Course *ScoreTableModel::getCourse(void)
113 Score *ScoreTableModel::getScore(void)
118 void ScoreTableModel::first()
120 if (score && course) {
122 score = scoreList.at(currentScore);
123 course = findCourse(score->getClubName(), score->getCourseName());
124 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
128 void ScoreTableModel::last()
130 if (score && course) {
131 currentScore = scoreList.size() - 1;
132 score = scoreList.at(currentScore);
133 course = findCourse(score->getClubName(), score->getCourseName());
134 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
138 void ScoreTableModel::next()
140 if (score && course) {
141 if (currentScore < (scoreList.size() - 1)) {
143 score = scoreList.at(currentScore);
144 course = findCourse(score->getClubName(), score->getCourseName());
145 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
150 void ScoreTableModel::prev()
152 if (score && course) {
153 if (currentScore > 0) {
155 score = scoreList.at(currentScore);
156 course = findCourse(score->getClubName(), score->getCourseName());
157 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
162 int ScoreTableModel::rowCount(const QModelIndex &) const
167 int ScoreTableModel::columnCount(const QModelIndex &) const
169 return 9 + 2; // 2 for in/out and tot columns
172 QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &parent) const
174 if (hasIndex(row, column, parent)) {
175 int flag = (parent.column() > 0) ? parent.column() : 0;
176 return createIndex(row, column, flag);
179 return QModelIndex();
183 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
185 // TODO: move away from the stack
187 if (!index.isValid())
190 int row = index.row();
191 int col = index.column();
196 if (role == Qt::TextAlignmentRole ) {
197 return Qt::AlignCenter;
200 if (index.column() > 10)
206 if (role == Qt::BackgroundRole) {
207 // Hole numbers 1-18. All hole nums, in, out and tot cell but not
209 if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
210 QBrush brush(colorHoleBg);
213 if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) {
216 if (row == ROW_SCORE) {
217 par = course->getPar(col).toInt();
218 shots = score->getScore(col).toInt();
221 par = course->getPar(col + 9).toInt();
222 shots = score->getScore(col + 9).toInt();
225 if (col == (COLS+1) && row == ROW_SCORE_2) {
227 QBrush brush(colorTotal);
232 QBrush brush(colorSubTotal);
238 QBrush brush(colorPar);
241 if (shots == (par-1)) {
243 QBrush brush(colorBirdie);
246 if (shots == (par+1)) {
248 QBrush brush(colorBogey);
251 if (shots == (par+2)) {
253 QBrush brush(colorDoubleBogey);
256 if (shots > (par+2)) {
258 QBrush brush(colorBad);
268 if (role == Qt::FontRole) {
269 if (row == ROW_SCORE_2 && col == (COLS+1)) {
278 if (role == Qt::DisplayRole) {
283 return QString("Out");
284 if (row == ROW_HOLE_2)
285 return QString("In");
288 if (score && course && row == ROW_PAR)
289 return course->getTotal(TotalOut);
290 if (score && course && row == ROW_PAR_2)
291 return course->getTotal(TotalIn);
294 if (score && row == ROW_SCORE)
295 return score->getTotal(TotalOut);
296 if (score && row == ROW_SCORE_2)
297 return score->getTotal(TotalIn);
300 else if (col == (COLS+1)) {
302 if (row == ROW_HOLE_2)
303 return QString("Tot");
305 if (score && course && row == ROW_PAR_2)
306 return course->getTotal(Total);
307 if (score && row == ROW_SCORE_2)
308 return score->getTotal(Total);
319 return course->getPar(col);
322 return course->getPar(col + 9);
325 return course->getHcp(col);
328 return course->getHcp(col + 9);
331 return score->getScore(col);
334 return score->getScore(col + 9);
341 int ScoreTableModel::setItem(int row, int col, int)
343 emit dataChanged(createIndex(row, col), createIndex(row, col));
347 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
349 // Only vertical header -- horizontal is hidden
350 if (orientation == Qt::Horizontal)
353 if (role == Qt::DisplayRole) {
357 return QString("Par");
360 return QString("HCP");
363 return QString("Score");