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(26, 26, 26);
17 QColor colorHoleFg(Qt::yellow);
18 QColor colorBirdie(138, 226, 52);
19 QColor colorPar(Qt::black);
20 #ifdef SC_ORANGE_THEME
21 QColor colorBogey(0xfc, 0xaf, 0x3e);
22 QColor colorDoubleBogey(0xf5, 0x79, 0x00);
23 QColor colorBad(0xce, 0x5c, 0x00);
26 QColor colorBogey(0x72, 0x9f, 0xcf);
27 QColor colorDoubleBogey(0x34, 0x65, 0xa4);
28 QColor colorBad(0x20, 0x4a, 0x87);
30 QColor colorSubTotal(Qt::black);
31 QColor colorTotal(Qt::black);
33 Qt::ItemFlags ScoreTableModel::flags (const QModelIndex & index)
38 void ScoreTableModel::setMode(int m)
43 int ScoreTableModel::mode(void)
48 // Assign the 'sList' to internal 'scoreList'. Set the current score
49 // to 'currentScore', or to 's'.
50 void ScoreTableModel::setScore(QList<Score *> &sList, Score *s)
53 if (scoreList.size() > 0) {
55 currentScore = scoreList.indexOf(s);
56 if (currentScore == -1)
59 score = scoreList.at(currentScore); // NOTE: assumes non-empty list
63 void ScoreTableModel::setClub(QList<Club *> &cList)
67 if (clubList.size() > 0)
68 club = clubList.at(0);
71 course = club->getCourse(0);
74 QString ScoreTableModel::getInfoText()
79 str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
84 QString ScoreTableModel::getCountText()
86 QString str = QString("%1/%2").arg(currentScore+1, 2).arg(scoreList.count(), 2);
90 QString& ScoreTableModel::clubName(void)
93 return club->getName();
98 QString& ScoreTableModel::courseName(void)
101 return course->getName();
106 Course *ScoreTableModel::findCourse(const QString &clubName,
107 const QString &courseName)
109 QListIterator<Club *> i(clubList);
112 while (i.hasNext()) {
114 if (c->getName() == clubName) {
115 return c->getCourse(courseName);
121 Club *ScoreTableModel::getClub(void)
126 Course *ScoreTableModel::getCourse(void)
131 Score *ScoreTableModel::getScore(void)
136 void ScoreTableModel::first()
138 if (score && course) {
140 score = scoreList.at(currentScore);
141 course = findCourse(score->getClubName(), score->getCourseName());
142 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
146 void ScoreTableModel::last()
148 if (score && course) {
149 currentScore = scoreList.size() - 1;
150 score = scoreList.at(currentScore);
151 course = findCourse(score->getClubName(), score->getCourseName());
152 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
156 void ScoreTableModel::next()
158 if (score && course) {
159 if (currentScore < (scoreList.size() - 1)) {
161 score = scoreList.at(currentScore);
162 course = findCourse(score->getClubName(), score->getCourseName());
163 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
168 void ScoreTableModel::prev()
170 if (score && course) {
171 if (currentScore > 0) {
173 score = scoreList.at(currentScore);
174 course = findCourse(score->getClubName(), score->getCourseName());
175 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
180 int ScoreTableModel::rowCount(const QModelIndex & parent) const
185 int ScoreTableModel::columnCount(const QModelIndex & parent) const
187 return 9 + 2; // 2 for in/out and tot columns
190 QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &parent) const
192 if (hasIndex(row, column, parent)) {
193 int flag = (parent.column() > 0) ? parent.column() : 0;
194 return createIndex(row, column, flag);
197 return QModelIndex();
201 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
203 // TODO: move away from the stack
205 if (!index.isValid())
208 int row = index.row();
209 int col = index.column();
214 if (role == Qt::TextAlignmentRole ) {
215 return Qt::AlignCenter;
218 if (index.column() > 10)
224 if (role == Qt::BackgroundRole) {
225 // Hole numbers 1-18. All hole nums, in, out and tot cell but not
227 if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
228 QBrush brush(colorHoleBg);
231 if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) {
234 if (row == ROW_SCORE) {
235 par = course->getPar(col).toInt();
236 shots = score->getScore(col).toInt();
239 par = course->getPar(col + 9).toInt();
240 shots = score->getScore(col + 9).toInt();
243 if (col == 10 && row == ROW_SCORE_2) {
245 QBrush brush(colorTotal);
250 QBrush brush(colorSubTotal);
256 QBrush brush(colorPar);
259 if (shots == (par-1)) {
261 QBrush brush(colorBirdie);
264 if (shots == (par+1)) {
266 QBrush brush(colorBogey);
269 if (shots == (par+2)) {
271 QBrush brush(colorDoubleBogey);
274 if (shots > (par+2)) {
276 QBrush brush(colorBad);
286 if (role == Qt::FontRole) {
287 if (row == ROW_SCORE_2 && col == 10) {
296 if (role == Qt::DisplayRole) {
301 return QString("Out");
302 if (row == ROW_HOLE_2)
303 return QString("In");
306 if (score && course && row == ROW_PAR)
307 return course->getTotal(TotalOut);
308 if (score && course && row == ROW_PAR_2)
309 return course->getTotal(TotalIn);
312 if (score && row == ROW_SCORE)
313 return score->getTotal(TotalOut);
314 if (score && row == ROW_SCORE_2)
315 return score->getTotal(TotalIn);
318 else if (col == 10) {
320 if (row == ROW_HOLE_2)
321 return QString("Tot");
323 if (score && course && row == ROW_PAR_2)
324 return course->getTotal(Total);
325 if (score && row == ROW_SCORE_2)
326 return score->getTotal(Total);
337 return course->getPar(col);
340 return course->getPar(col + 9);
343 return course->getHcp(col);
346 return course->getHcp(col + 9);
349 return score->getScore(col);
352 return score->getScore(col + 9);
359 int ScoreTableModel::setItem(int row, int col, int data)
361 emit dataChanged(createIndex(row, col), createIndex(row, col));
365 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
367 // Only vertical header -- horizontal is hidden
368 if (orientation == Qt::Horizontal)
372 if (role == Qt::BackgroundRole) {
373 QColor colorHoleBg(Qt::darkGray);
374 QBrush brush(colorHoleBg);
378 if (role == Qt::DisplayRole) {
382 return QString("Par");
385 return QString("HCP");
388 return QString("Score");