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 ScoreTableModel::ScoreTableModel(QObject *parent)
18 : QAbstractTableModel(parent)
24 void ScoreTableModel::set(Score * s, Course * c)
30 int ScoreTableModel::rowCount(const QModelIndex &) const
35 int ScoreTableModel::columnCount(const QModelIndex &) const
37 return COLS + 2; // 2 for in/out and tot columns
40 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
45 int row = index.row();
46 int col = index.column();
51 if (role == Qt::TextAlignmentRole ) {
52 return Qt::AlignCenter;
55 if (index.column() > 10)
61 if (role == Qt::ForegroundRole) {
62 // Hole numbers 1-18. All hole nums, in, out and tot cell but not
64 if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
65 QBrush brush(ScoreColor::holeBg());
68 if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) {
71 if (row == ROW_SCORE) {
72 par = course->getPar(col).toInt();
73 shots = score->getScore(col).toInt();
76 par = course->getPar(col + 9).toInt();
77 shots = score->getScore(col + 9).toInt();
80 if (col == (COLS+1) && row == ROW_SCORE_2) {
82 QBrush brush(ScoreColor::total());
87 QBrush brush(ScoreColor::subTotal());
93 QBrush brush(ScoreColor::par());
96 if (shots == (par-1)) {
98 QBrush brush(ScoreColor::birdie());
101 if (shots == (par+1)) {
103 QBrush brush(ScoreColor::bogey());
106 if (shots == (par+2)) {
108 QBrush brush(ScoreColor::doubleBogey());
111 if (shots > (par+2)) {
113 QBrush brush(ScoreColor::bad());
123 if (role == Qt::FontRole) {
126 font.setPointSize(26);
133 if (role == Qt::DisplayRole) {
138 return QString("Out");
139 if (row == ROW_HOLE_2)
140 return QString("In");
143 if (score && course && row == ROW_PAR)
144 return course->getTotal(TotalOut);
145 if (score && course && row == ROW_PAR_2)
146 return course->getTotal(TotalIn);
149 if (score && row == ROW_SCORE)
150 return score->getTotal(TotalOut);
151 if (score && row == ROW_SCORE_2)
152 return score->getTotal(TotalIn);
155 else if (col == (COLS+1)) {
157 if (row == ROW_HOLE_2)
158 return QString("Tot");
160 if (score && course && row == ROW_PAR_2)
161 return course->getTotal(Total);
162 if (score && row == ROW_SCORE_2)
163 return score->getTotal(Total);
174 return course->getPar(col);
177 return course->getPar(col + 9);
180 return course->getHcp(col);
183 return course->getHcp(col + 9);
186 return score->getScore(col);
189 return score->getScore(col + 9);
196 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
198 // Only vertical header -- horizontal is hidden
199 if (orientation == Qt::Horizontal)
202 if (role == Qt::DisplayRole) {
206 return QString("Hole");
209 return QString("Par");
212 return QString("HCP");
215 return QString("Score");
226 CourseTableModel::CourseTableModel(QObject *parent)
227 : QAbstractTableModel(parent)
232 void CourseTableModel::set(Course *c)
237 int CourseTableModel::rowCount(const QModelIndex &) const
242 int CourseTableModel::columnCount(const QModelIndex &) const
247 QVariant CourseTableModel::data(const QModelIndex &index, int role) const
249 if (!index.isValid())
252 int row = index.row();
253 int col = index.column();
258 if (role == Qt::TextAlignmentRole ) {
259 return Qt::AlignCenter;
265 if (role == Qt::FontRole) {
266 if (row == ROW_HOLE || row == ROW_HOLE_2) {
275 if (role == Qt::ForegroundRole) {
276 // Hole numbers 1-18. All hole nums, in, out and tot cell but not
278 if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
279 QBrush brush(ScoreColor::holeBg());
287 if (role == Qt::DisplayRole) {
292 return QString("Out");
293 if (row == ROW_HOLE_2)
294 return QString("In");
297 if (course && row == ROW_PAR)
298 return course->getTotal(TotalOut);
299 if (course && row == ROW_PAR_2)
300 return course->getTotal(TotalIn);
302 else if (col == (COLS+1)) {
304 if (row == ROW_HOLE_2)
305 return QString("Tot");
307 if (course && row == ROW_PAR_2)
308 return course->getTotal(Total);
319 return course->getPar(col);
322 return course->getPar(col + 9);
325 return course->getHcp(col);
328 return course->getHcp(col + 9);
335 QVariant CourseTableModel::headerData(int section, Qt::Orientation orientation, int role) const
337 // Only vertical header -- horizontal is hidden
338 if (orientation == Qt::Horizontal)
341 if (role == Qt::DisplayRole) {
345 return QString("Hole");
348 return QString("Par");
351 return QString("HCP");
354 return QString("Len");