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"
16 QColor colorHoleBg(Qt::darkGray);
17 QColor colorHoleFg(Qt::yellow);
18 QColor colorBirdie(102, 102, 255);
19 QColor colorPar(Qt::green);
20 QColor colorBogey(Qt::darkGreen);
21 QColor colorDoubleBogey(Qt::red);
22 QColor colorBad(Qt::red);
23 QColor colorSubTotal(Qt::black);
24 QColor colorTotal(Qt::black);
26 Qt::ItemFlags ScoreTableModel::flags (const QModelIndex & index)
31 void ScoreTableModel::setMode(int m)
36 int ScoreTableModel::mode(void)
41 // Assign the 'sList' to internal 'scoreList'. Set the current score
42 // to 'currentScore', or to 's'.
43 void ScoreTableModel::setScore(QList<Score *> &sList, Score *s)
46 if (scoreList.size() > 0) {
48 currentScore = scoreList.indexOf(s);
49 if (currentScore == -1)
52 score = scoreList.at(currentScore); // NOTE: assumes non-empty list
56 void ScoreTableModel::setClub(QList<Club *> &cList)
60 if (clubList.size() > 0)
61 club = clubList.at(0);
64 course = club->getCourse(0);
67 QString ScoreTableModel::getInfoText()
72 str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
77 QString ScoreTableModel::getCountText()
79 QString str = QString("%1/%2").arg(currentScore+1, 2).arg(scoreList.count(), 2);
83 QString& ScoreTableModel::clubName(void)
86 return club->getName();
91 QString& ScoreTableModel::courseName(void)
94 return course->getName();
99 Course *ScoreTableModel::findCourse(const QString &clubName,
100 const QString &courseName)
102 QListIterator<Club *> i(clubList);
105 while (i.hasNext()) {
107 if (c->getName() == clubName) {
108 return c->getCourse(courseName);
114 Club *ScoreTableModel::getClub(void)
119 Course *ScoreTableModel::getCourse(void)
124 Score *ScoreTableModel::getScore(void)
129 void ScoreTableModel::first()
131 if (score && course) {
133 score = scoreList.at(currentScore);
134 course = findCourse(score->getClubName(), score->getCourseName());
135 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
139 void ScoreTableModel::last()
141 if (score && course) {
142 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));
149 void ScoreTableModel::next()
151 if (score && course) {
152 if (currentScore < (scoreList.size() - 1)) {
154 score = scoreList.at(currentScore);
155 course = findCourse(score->getClubName(), score->getCourseName());
156 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
161 void ScoreTableModel::prev()
163 if (score && course) {
164 if (currentScore > 0) {
166 score = scoreList.at(currentScore);
167 course = findCourse(score->getClubName(), score->getCourseName());
168 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
173 int ScoreTableModel::rowCount(const QModelIndex & parent) const
178 int ScoreTableModel::columnCount(const QModelIndex & parent) const
180 return 9 + 2; // 2 for in/out and tot columns
183 QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &parent) const
185 if (hasIndex(row, column, parent)) {
186 int flag = (parent.column() > 0) ? parent.column() : 0;
187 return createIndex(row, column, flag);
190 return QModelIndex();
194 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
196 // TODO: move away from the stack
198 if (!index.isValid())
201 int row = index.row();
202 int col = index.column();
207 if (role == Qt::TextAlignmentRole ) {
208 return Qt::AlignCenter;
211 if (index.column() > 10)
217 if (role == Qt::BackgroundRole) {
218 // Hole numbers 1-18. All hole nums, in, out and tot cell but not
220 if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
221 QBrush brush(colorHoleBg);
224 if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) {
227 if (row == ROW_SCORE) {
228 par = course->getPar(col).toInt();
229 shots = score->getScore(col).toInt();
232 par = course->getPar(col + 9).toInt();
233 shots = score->getScore(col + 9).toInt();
236 if (col == 10 && row == ROW_SCORE_2) {
238 QBrush brush(colorTotal);
243 QBrush brush(colorSubTotal);
249 QBrush brush(colorPar);
252 if (shots == (par-1)) {
254 QBrush brush(colorBirdie);
257 if (shots == (par+1)) {
259 QBrush brush(colorBogey);
262 if (shots == (par+2)) {
264 QBrush brush(colorDoubleBogey);
267 if (shots > (par+2)) {
269 QBrush brush(colorBad);
279 if (role == Qt::FontRole) {
280 if (row == ROW_SCORE_2 && col == 10) {
289 if (role == Qt::DisplayRole) {
294 return QString("Out");
295 if (row == ROW_HOLE_2)
296 return QString("In");
299 if (score && course && row == ROW_PAR)
300 return course->getTotal(TotalOut);
301 if (score && course && row == ROW_PAR_2)
302 return course->getTotal(TotalIn);
305 if (score && row == ROW_SCORE)
306 return score->getTotal(TotalOut);
307 if (score && row == ROW_SCORE_2)
308 return score->getTotal(TotalIn);
311 else if (col == 10) {
313 if (row == ROW_HOLE_2)
314 return QString("Tot");
316 if (score && course && row == ROW_PAR_2)
317 return course->getTotal(Total);
318 if (score && row == ROW_SCORE_2)
319 return score->getTotal(Total);
330 return course->getPar(col);
333 return course->getPar(col + 9);
336 return course->getHcp(col);
339 return course->getHcp(col + 9);
342 return score->getScore(col);
345 return score->getScore(col + 9);
352 int ScoreTableModel::setItem(int row, int col, int data)
354 emit dataChanged(createIndex(row, col), createIndex(row, col));
358 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
360 // Only vertical header -- horizontal is hidden
361 if (orientation == Qt::Horizontal)
365 if (role == Qt::BackgroundRole) {
366 QColor colorHoleBg(Qt::darkGray);
367 QBrush brush(colorHoleBg);
371 if (role == Qt::DisplayRole) {
375 return QString("Par");
378 return QString("HCP");
381 return QString("Score");