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 font.setPointSize(fontSize);
128 if (row == ROW_HOLE || row == ROW_HOLE_2) {
137 if (role == Qt::DisplayRole) {
142 return QString("Out");
143 if (row == ROW_HOLE_2)
144 return QString("In");
147 if (score && course && row == ROW_PAR)
148 return course->getTotal(TotalOut);
149 if (score && course && row == ROW_PAR_2)
150 return course->getTotal(TotalIn);
153 if (score && row == ROW_SCORE)
154 return score->getTotal(TotalOut);
155 if (score && row == ROW_SCORE_2)
156 return score->getTotal(TotalIn);
159 else if (col == (COLS+1)) {
161 if (row == ROW_HOLE_2)
162 return QString("Tot");
164 if (score && course && row == ROW_PAR_2)
165 return course->getTotal(Total);
166 if (score && row == ROW_SCORE_2)
167 return score->getTotal(Total);
168 // calculate net score
169 if (score && course && row == ROW_HCP_2) {
170 int scoreTotal = score->getTotal(Total).toInt();
171 int courseTotal = course->getTotal(Total).toInt();
172 int n = scoreTotal - courseTotal;
173 return QString("+%1").arg(n);
185 return course->getPar(col);
188 return course->getPar(col + 9);
191 return course->getHcp(col);
194 return course->getHcp(col + 9);
197 return score->getScore(col);
200 return score->getScore(col + 9);
207 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
209 // Only vertical header -- horizontal is hidden
210 if (orientation == Qt::Horizontal)
213 if (role == Qt::FontRole) {
215 font.setPointSize(fontSize);
218 if (role == Qt::DisplayRole) {
222 return QString("Hole");
225 return QString("Par");
228 return QString("HCP");
231 return QString("Score");
242 CourseTableModel::CourseTableModel(QObject *parent)
243 : QAbstractTableModel(parent)
248 void CourseTableModel::set(Course *c)
253 int CourseTableModel::rowCount(const QModelIndex &) const
258 int CourseTableModel::columnCount(const QModelIndex &) const
263 QVariant CourseTableModel::data(const QModelIndex &index, int role) const
265 if (!index.isValid())
268 int row = index.row();
269 int col = index.column();
274 if (role == Qt::TextAlignmentRole ) {
275 return Qt::AlignCenter;
281 if (role == Qt::FontRole) {
283 font.setPointSize(fontSize);
284 if (row == ROW_HOLE || row == ROW_HOLE_2) {
292 if (role == Qt::ForegroundRole) {
293 // Hole numbers 1-18. All hole nums, in, out and tot cell but not
295 if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
296 QBrush brush(ScoreColor::holeBg());
304 if (role == Qt::DisplayRole) {
309 return QString("Out");
310 if (row == ROW_HOLE_2)
311 return QString("In");
314 if (course && row == ROW_PAR)
315 return course->getTotal(TotalOut);
316 if (course && row == ROW_PAR_2)
317 return course->getTotal(TotalIn);
319 else if (col == (COLS+1)) {
321 if (row == ROW_HOLE_2)
322 return QString("Tot");
324 if (course && row == ROW_PAR_2)
325 return course->getTotal(Total);
336 return course->getPar(col);
339 return course->getPar(col + 9);
342 return course->getHcp(col);
345 return course->getHcp(col + 9);
352 QVariant CourseTableModel::headerData(int section, Qt::Orientation orientation, int role) const
354 // Only vertical header -- horizontal is hidden
355 if (orientation == Qt::Horizontal)
358 if (role == Qt::FontRole) {
360 font.setPointSize(fontSize);
363 if (role == Qt::DisplayRole) {
367 return QString("Hole");
370 return QString("Par");
373 return QString("HCP");
376 return QString("Len");