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(Score * s, Course * c, QObject *parent)
18 : QAbstractTableModel(parent)
24 int ScoreTableModel::rowCount(const QModelIndex &) const
29 int ScoreTableModel::columnCount(const QModelIndex &) const
31 return COLS + 2; // 2 for in/out and tot columns
34 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
39 int row = index.row();
40 int col = index.column();
45 if (role == Qt::TextAlignmentRole ) {
46 return Qt::AlignCenter;
49 if (index.column() > 10)
55 if (role == Qt::ForegroundRole) {
56 // Hole numbers 1-18. All hole nums, in, out and tot cell but not
58 if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
59 QBrush brush(ScoreColor::holeBg());
62 if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) {
65 if (row == ROW_SCORE) {
66 par = course->getPar(col).toInt();
67 shots = score->getScore(col).toInt();
70 par = course->getPar(col + 9).toInt();
71 shots = score->getScore(col + 9).toInt();
74 if (col == (COLS+1) && row == ROW_SCORE_2) {
76 QBrush brush(ScoreColor::total());
81 QBrush brush(ScoreColor::subTotal());
87 QBrush brush(ScoreColor::par());
90 if (shots == (par-1)) {
92 QBrush brush(ScoreColor::birdie());
95 if (shots == (par+1)) {
97 QBrush brush(ScoreColor::bogey());
100 if (shots == (par+2)) {
102 QBrush brush(ScoreColor::doubleBogey());
105 if (shots > (par+2)) {
107 QBrush brush(ScoreColor::bad());
117 if (role == Qt::FontRole) {
118 if (row == ROW_HOLE || row == ROW_HOLE_2) {
123 if (row == ROW_SCORE || row == ROW_SCORE_2) {
132 if (role == Qt::DisplayRole) {
137 return QString("Out");
138 if (row == ROW_HOLE_2)
139 return QString("In");
142 if (score && course && row == ROW_PAR)
143 return course->getTotal(TotalOut);
144 if (score && course && row == ROW_PAR_2)
145 return course->getTotal(TotalIn);
148 if (score && row == ROW_SCORE)
149 return score->getTotal(TotalOut);
150 if (score && row == ROW_SCORE_2)
151 return score->getTotal(TotalIn);
154 else if (col == (COLS+1)) {
156 if (row == ROW_HOLE_2)
157 return QString("Tot");
159 if (score && course && row == ROW_PAR_2)
160 return course->getTotal(Total);
161 if (score && row == ROW_SCORE_2)
162 return score->getTotal(Total);
173 return course->getPar(col);
176 return course->getPar(col + 9);
179 return course->getHcp(col);
182 return course->getHcp(col + 9);
185 return score->getScore(col);
188 return score->getScore(col + 9);
195 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
197 // Only vertical header -- horizontal is hidden
198 if (orientation == Qt::Horizontal)
201 if (role == Qt::DisplayRole) {
205 return QString("Hole");
208 return QString("Par");
211 return QString("HCP");
214 return QString("Score");
225 CourseTableModel::CourseTableModel(Course * c, QObject *parent)
226 : QAbstractTableModel(parent), course(c)
230 int CourseTableModel::rowCount(const QModelIndex &) const
235 int CourseTableModel::columnCount(const QModelIndex &) const
240 QVariant CourseTableModel::data(const QModelIndex &index, int role) const
242 if (!index.isValid())
245 int row = index.row();
246 int col = index.column();
251 if (role == Qt::TextAlignmentRole ) {
252 return Qt::AlignCenter;
258 if (role == Qt::FontRole) {
259 if (row == ROW_HOLE || row == ROW_HOLE_2) {
268 if (role == Qt::ForegroundRole) {
269 // Hole numbers 1-18. All hole nums, in, out and tot cell but not
271 if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
272 QBrush brush(ScoreColor::holeBg());
280 if (role == Qt::DisplayRole) {
285 return QString("Out");
286 if (row == ROW_HOLE_2)
287 return QString("In");
290 if (course && row == ROW_PAR)
291 return course->getTotal(TotalOut);
292 if (course && row == ROW_PAR_2)
293 return course->getTotal(TotalIn);
295 else if (col == (COLS+1)) {
297 if (row == ROW_HOLE_2)
298 return QString("Tot");
300 if (course && row == ROW_PAR_2)
301 return course->getTotal(Total);
312 return course->getPar(col);
315 return course->getPar(col + 9);
318 return course->getHcp(col);
321 return course->getHcp(col + 9);
328 QVariant CourseTableModel::headerData(int section, Qt::Orientation orientation, int role) const
330 // Only vertical header -- horizontal is hidden
331 if (orientation == Qt::Horizontal)
334 if (role == Qt::DisplayRole) {
338 return QString("Hole");
341 return QString("Par");
344 return QString("HCP");
347 return QString("Len");