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)
25 void ScoreTableModel::set(Score * s, Course * c, int h)
32 int ScoreTableModel::rowCount(const QModelIndex &) const
37 int ScoreTableModel::columnCount(const QModelIndex &) const
39 return COLS + 2; // 2 for in/out and tot columns
42 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
47 int row = index.row();
48 int col = index.column();
53 if (role == Qt::TextAlignmentRole ) {
54 return Qt::AlignCenter;
57 if (index.column() > 10)
63 if (role == Qt::ForegroundRole) {
64 // Hole numbers 1-18. All hole nums, in, out and tot cell but not
66 if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
67 QBrush brush(ScoreColor::holeBg());
70 if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) {
73 if (row == ROW_SCORE) {
74 par = course->getPar(col).toInt();
75 shots = score->getScore(col).toInt();
78 par = course->getPar(col + 9).toInt();
79 shots = score->getScore(col + 9).toInt();
82 if (col == (COLS+1) && row == ROW_SCORE_2) {
84 QBrush brush(ScoreColor::total());
89 QBrush brush(ScoreColor::subTotal());
95 QBrush brush(ScoreColor::par());
98 if (shots == (par-1)) {
100 QBrush brush(ScoreColor::birdie());
103 if (shots == (par+1)) {
105 QBrush brush(ScoreColor::bogey());
108 if (shots == (par+2)) {
110 QBrush brush(ScoreColor::doubleBogey());
113 if (shots > (par+2)) {
115 QBrush brush(ScoreColor::bad());
125 if (role == Qt::FontRole) {
127 if (col == (COLS+1) && row == ROW_SCORE_2) {
129 font.setPointSize(fontSize+4);
132 font.setPointSize(fontSize);
133 if (row == ROW_HOLE || row == ROW_HOLE_2) {
142 if (role == Qt::DisplayRole) {
147 return QString("Out");
148 if (row == ROW_HOLE_2)
149 return QString("In");
152 if (score && course && row == ROW_PAR)
153 return course->getTotal(TotalOut);
154 if (score && course && row == ROW_PAR_2)
155 return course->getTotal(TotalIn);
158 if (score && row == ROW_SCORE)
159 return score->getTotal(TotalOut);
160 if (score && row == ROW_SCORE_2)
161 return score->getTotal(TotalIn);
164 else if (col == (COLS+1)) {
166 if (row == ROW_HOLE_2)
167 return QString("Tot");
169 if (score && course && row == ROW_PAR_2)
170 return course->getTotal(Total);
171 if (score && row == ROW_SCORE_2)
172 return score->getTotal(Total);
173 // calculate net score
174 if (score && course && row == ROW_HCP_2) {
175 int scoreTotal = score->getTotal(Total).toInt();
176 int courseTotal = course->getTotal(Total).toInt();
177 int n = scoreTotal - courseTotal;
178 return QString("+%1").arg(n);
192 return course->getPar(col);
198 return course->getPar(col + 9);
204 return course->getHcp(col);
210 return course->getHcp(col + 9);
216 return score->getScore(col);
222 return score->getScore(col + 9);
231 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
233 // Only vertical header -- horizontal is hidden
234 if (orientation == Qt::Horizontal)
237 if (role == Qt::FontRole) {
239 font.setPointSize(fontSize);
242 if (role == Qt::DisplayRole) {
246 return QString("Hole");
249 return QString("Par");
252 return QString("HCP");
255 return QString("Score");
266 CourseTableModel::CourseTableModel(QObject *parent)
267 : QAbstractTableModel(parent)
272 void CourseTableModel::set(Course *c)
277 int CourseTableModel::rowCount(const QModelIndex &) const
282 int CourseTableModel::columnCount(const QModelIndex &) const
287 QVariant CourseTableModel::data(const QModelIndex &index, int role) const
289 if (!index.isValid())
292 int row = index.row();
293 int col = index.column();
298 if (role == Qt::TextAlignmentRole ) {
299 return Qt::AlignCenter;
305 if (role == Qt::FontRole) {
307 font.setPointSize(fontSize);
308 if (row == ROW_HOLE || row == ROW_HOLE_2) {
316 if (role == Qt::ForegroundRole) {
317 // Hole numbers 1-18. All hole nums, in, out and tot cell but not
319 if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
320 QBrush brush(ScoreColor::holeBg());
328 if (role == Qt::DisplayRole) {
333 return QString("Out");
334 if (row == ROW_HOLE_2)
335 return QString("In");
338 if (course && row == ROW_PAR)
339 return course->getTotal(TotalOut);
340 if (course && row == ROW_PAR_2)
341 return course->getTotal(TotalIn);
343 else if (col == (COLS+1)) {
345 if (row == ROW_HOLE_2)
346 return QString("Tot");
348 if (course && row == ROW_PAR_2)
349 return course->getTotal(Total);
360 return course->getPar(col);
363 return course->getPar(col + 9);
366 return course->getHcp(col);
369 return course->getHcp(col + 9);
376 QVariant CourseTableModel::headerData(int section, Qt::Orientation orientation, int role) const
378 // Only vertical header -- horizontal is hidden
379 if (orientation == Qt::Horizontal)
382 if (role == Qt::FontRole) {
384 font.setPointSize(fontSize);
387 if (role == Qt::DisplayRole) {
391 return QString("Hole");
394 return QString("Par");
397 return QString("HCP");
400 return QString("Len");