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) {
124 if (row == ROW_HOLE || row == ROW_HOLE_2) {
129 if (row == ROW_SCORE || row == ROW_SCORE_2) {
138 if (role == Qt::DisplayRole) {
143 return QString("Out");
144 if (row == ROW_HOLE_2)
145 return QString("In");
148 if (score && course && row == ROW_PAR)
149 return course->getTotal(TotalOut);
150 if (score && course && row == ROW_PAR_2)
151 return course->getTotal(TotalIn);
154 if (score && row == ROW_SCORE)
155 return score->getTotal(TotalOut);
156 if (score && row == ROW_SCORE_2)
157 return score->getTotal(TotalIn);
160 else if (col == (COLS+1)) {
162 if (row == ROW_HOLE_2)
163 return QString("Tot");
165 if (score && course && row == ROW_PAR_2)
166 return course->getTotal(Total);
167 if (score && row == ROW_SCORE_2)
168 return score->getTotal(Total);
179 return course->getPar(col);
182 return course->getPar(col + 9);
185 return course->getHcp(col);
188 return course->getHcp(col + 9);
191 return score->getScore(col);
194 return score->getScore(col + 9);
201 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
203 // Only vertical header -- horizontal is hidden
204 if (orientation == Qt::Horizontal)
207 if (role == Qt::DisplayRole) {
211 return QString("Hole");
214 return QString("Par");
217 return QString("HCP");
220 return QString("Score");
231 CourseTableModel::CourseTableModel(QObject *parent)
232 : QAbstractTableModel(parent)
237 void CourseTableModel::set(Course *c)
242 int CourseTableModel::rowCount(const QModelIndex &) const
247 int CourseTableModel::columnCount(const QModelIndex &) const
252 QVariant CourseTableModel::data(const QModelIndex &index, int role) const
254 if (!index.isValid())
257 int row = index.row();
258 int col = index.column();
263 if (role == Qt::TextAlignmentRole ) {
264 return Qt::AlignCenter;
270 if (role == Qt::FontRole) {
271 if (row == ROW_HOLE || row == ROW_HOLE_2) {
280 if (role == Qt::ForegroundRole) {
281 // Hole numbers 1-18. All hole nums, in, out and tot cell but not
283 if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
284 QBrush brush(ScoreColor::holeBg());
292 if (role == Qt::DisplayRole) {
297 return QString("Out");
298 if (row == ROW_HOLE_2)
299 return QString("In");
302 if (course && row == ROW_PAR)
303 return course->getTotal(TotalOut);
304 if (course && row == ROW_PAR_2)
305 return course->getTotal(TotalIn);
307 else if (col == (COLS+1)) {
309 if (row == ROW_HOLE_2)
310 return QString("Tot");
312 if (course && row == ROW_PAR_2)
313 return course->getTotal(Total);
324 return course->getPar(col);
327 return course->getPar(col + 9);
330 return course->getHcp(col);
333 return course->getHcp(col + 9);
340 QVariant CourseTableModel::headerData(int section, Qt::Orientation orientation, int role) const
342 // Only vertical header -- horizontal is hidden
343 if (orientation == Qt::Horizontal)
346 if (role == Qt::DisplayRole) {
350 return QString("Hole");
353 return QString("Par");
356 return QString("HCP");
359 return QString("Len");