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
47 void ScoreTableModel::setClub(QList<Club *> &cList)
51 if (clubList.size() > 0)
52 club = clubList.at(0);
55 course = club->getCourse(0);
58 QString ScoreTableModel::getInfoText()
63 str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
68 QString ScoreTableModel::getCountText()
70 QString str = QString("%1/%2").arg(currentScore+1, 2).arg(scoreList.count(), 2);
74 QString& ScoreTableModel::clubName(void)
77 return club->getName();
82 QString& ScoreTableModel::courseName(void)
85 return course->getName();
90 Course *ScoreTableModel::findCourse(const QString &clubName,
91 const QString &courseName)
93 QListIterator<Club *> i(clubList);
98 if (c->getName() == clubName) {
99 return c->getCourse(courseName);
105 Club *ScoreTableModel::getClub(void)
110 Course *ScoreTableModel::getCourse(void)
115 Score *ScoreTableModel::getScore(void)
120 void ScoreTableModel::first()
122 if (score && course) {
124 score = scoreList.at(currentScore);
125 course = findCourse(score->getClubName(), score->getCourseName());
126 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
130 void ScoreTableModel::last()
132 if (score && course) {
133 currentScore = scoreList.size() - 1;
134 score = scoreList.at(currentScore);
135 course = findCourse(score->getClubName(), score->getCourseName());
136 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
140 void ScoreTableModel::next()
142 if (score && course) {
143 if (currentScore < (scoreList.size() - 1)) {
145 score = scoreList.at(currentScore);
146 course = findCourse(score->getClubName(), score->getCourseName());
147 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
152 void ScoreTableModel::prev()
154 if (score && course) {
155 if (currentScore > 0) {
157 score = scoreList.at(currentScore);
158 course = findCourse(score->getClubName(), score->getCourseName());
159 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
164 int ScoreTableModel::rowCount(const QModelIndex &) const
169 int ScoreTableModel::columnCount(const QModelIndex &) const
171 return 9 + 2; // 2 for in/out and tot columns
174 QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &parent) const
176 if (hasIndex(row, column, parent)) {
177 int flag = (parent.column() > 0) ? parent.column() : 0;
178 return createIndex(row, column, flag);
181 return QModelIndex();
185 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
187 // TODO: move away from the stack
189 if (!index.isValid())
192 int row = index.row();
193 int col = index.column();
198 if (role == Qt::TextAlignmentRole ) {
199 return Qt::AlignCenter;
202 if (index.column() > 10)
208 if (role == Qt::BackgroundRole) {
209 // Hole numbers 1-18. All hole nums, in, out and tot cell but not
211 if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
212 QBrush brush(ScoreColor::holeBg());
215 if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) {
218 if (row == ROW_SCORE) {
219 par = course->getPar(col).toInt();
220 shots = score->getScore(col).toInt();
223 par = course->getPar(col + 9).toInt();
224 shots = score->getScore(col + 9).toInt();
227 if (col == (COLS+1) && row == ROW_SCORE_2) {
229 QBrush brush(ScoreColor::total());
234 QBrush brush(ScoreColor::subTotal());
240 QBrush brush(ScoreColor::par());
243 if (shots == (par-1)) {
245 QBrush brush(ScoreColor::birdie());
248 if (shots == (par+1)) {
250 QBrush brush(ScoreColor::bogey());
253 if (shots == (par+2)) {
255 QBrush brush(ScoreColor::doubleBogey());
258 if (shots > (par+2)) {
260 QBrush brush(ScoreColor::bad());
270 if (role == Qt::FontRole) {
271 if (row == ROW_SCORE_2 && col == (COLS+1)) {
280 if (role == Qt::DisplayRole) {
285 return QString("Out");
286 if (row == ROW_HOLE_2)
287 return QString("In");
290 if (score && course && row == ROW_PAR)
291 return course->getTotal(TotalOut);
292 if (score && course && row == ROW_PAR_2)
293 return course->getTotal(TotalIn);
296 if (score && row == ROW_SCORE)
297 return score->getTotal(TotalOut);
298 if (score && row == ROW_SCORE_2)
299 return score->getTotal(TotalIn);
302 else if (col == (COLS+1)) {
304 if (row == ROW_HOLE_2)
305 return QString("Tot");
307 if (score && course && row == ROW_PAR_2)
308 return course->getTotal(Total);
309 if (score && row == ROW_SCORE_2)
310 return score->getTotal(Total);
321 return course->getPar(col);
324 return course->getPar(col + 9);
327 return course->getHcp(col);
330 return course->getHcp(col + 9);
333 return score->getScore(col);
336 return score->getScore(col + 9);
343 int ScoreTableModel::setItem(int row, int col, int)
345 emit dataChanged(createIndex(row, col), createIndex(row, col));
349 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
351 // Only vertical header -- horizontal is hidden
352 if (orientation == Qt::Horizontal)
355 if (role == Qt::DisplayRole) {
359 return QString("Par");
362 return QString("HCP");
365 return QString("Score");