- Add and flush default courses when settings change
[scorecard] / src / table-model.cpp
1 /*
2  * Copyright (C) 2009 Sakari Poussa
3  *
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.
7  */
8
9 #include <QColor>
10 #include <QBrush>
11 #include <QFont>
12 #include "table-model.h"
13 #include "score-common.h"
14
15 QString empty("");
16
17 ScoreTableModel::ScoreTableModel(Score * s, Course * c, QObject *parent) 
18     : QAbstractTableModel(parent)
19 {
20     score = s;
21     course = c;
22 }
23
24 int ScoreTableModel::rowCount(const QModelIndex &) const
25 {
26     return ROWS;
27 }
28  
29 int ScoreTableModel::columnCount(const QModelIndex &) const
30 {
31     return COLS + 2; // 2 for in/out and tot columns
32 }
33
34 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
35 {
36   if (!index.isValid())
37     return QVariant();
38
39   int row = index.row();
40   int col = index.column();
41
42   //
43   // ALIGNMENT
44   //
45   if (role == Qt::TextAlignmentRole ) {
46     return Qt::AlignCenter;
47   }
48
49   if (index.column() > 10)
50     return QVariant();
51
52   //
53   // COLORS
54   //
55   if (role == Qt::ForegroundRole) {
56     // Hole numbers 1-18. All hole nums, in, out and tot cell but not
57     // the empty cell.
58     if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
59       QBrush brush(ScoreColor::holeBg());
60       return brush;
61     }
62     if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) {
63       int par;
64       int shots;
65       if (row == ROW_SCORE) {
66         par = course->getPar(col).toInt();
67         shots = score->getScore(col).toInt();
68       }
69       else {
70         par = course->getPar(col + 9).toInt();
71         shots = score->getScore(col + 9).toInt();
72       }
73
74       if (col == (COLS+1) && row == ROW_SCORE_2) {
75         // Total score
76         QBrush brush(ScoreColor::total());
77         return brush;
78       }
79       if (col == COLS) {
80         // In and Out scores
81         QBrush brush(ScoreColor::subTotal());
82         return brush;
83       }
84       if (col < COLS) {
85         if (shots == par) {
86           // Par
87           QBrush brush(ScoreColor::par());
88           return brush;
89         }
90         if (shots == (par-1)) {
91           // Birdie
92           QBrush brush(ScoreColor::birdie());
93           return brush;
94         }
95         if (shots == (par+1)) {
96           // Bogey
97           QBrush brush(ScoreColor::bogey());
98           return brush;
99         }
100         if (shots == (par+2)) {
101           // Double Bogey
102           QBrush brush(ScoreColor::doubleBogey());
103           return brush;
104         }
105         if (shots > (par+2)) {
106           // Very bad
107           QBrush brush(ScoreColor::bad());
108           return brush;
109         }
110       }
111     }
112     return QVariant();
113   }
114   //
115   // FONT
116   //
117   if (role == Qt::FontRole) {
118       if (row == ROW_HOLE || row == ROW_HOLE_2) {
119           QFont font;
120           font.setBold(true);
121           return font;
122       }
123       if (row == ROW_SCORE || row == ROW_SCORE_2) {
124           QFont font;
125           font.setBold(true);
126           return font;
127       }
128   }
129   //
130   // NUMBERS
131   //
132   if (role == Qt::DisplayRole) {
133
134     if (col == COLS) {
135       // In/out label
136       if (row == ROW_HOLE)
137         return QString("Out");
138       if (row == ROW_HOLE_2)
139         return QString("In");
140
141       // In/Out for par
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);
146
147       // In/Out for score
148       if (score && row == ROW_SCORE)
149         return score->getTotal(TotalOut);
150       if (score && row == ROW_SCORE_2)
151         return score->getTotal(TotalIn);
152       
153     }
154     else if (col == (COLS+1)) {
155       // Total label
156       if (row == ROW_HOLE_2)
157         return QString("Tot");
158       // Total score
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);
163     }
164     else {
165       // data cells
166       switch(row) {
167       case ROW_HOLE:
168         return col + 1;
169       case ROW_HOLE_2:
170         return col + 10;
171       case ROW_PAR:
172         if (score && course)
173           return course->getPar(col); 
174       case ROW_PAR_2:
175         if (score && course)
176           return course->getPar(col + 9); 
177       case ROW_HCP: 
178         if (score && course)
179           return course->getHcp(col); 
180       case ROW_HCP_2:
181         if (score && course)
182           return course->getHcp(col + 9);
183       case ROW_SCORE:
184         if (score)
185           return score->getScore(col);
186       case ROW_SCORE_2: 
187         if (score)
188           return score->getScore(col + 9);
189       }
190     }
191   }
192   return QVariant();
193 }
194
195 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
196 {
197   // Only vertical header -- horizontal is hidden
198   if (orientation == Qt::Horizontal)
199     return QVariant();
200
201   if (role == Qt::DisplayRole) {
202     switch(section) {
203     case ROW_HOLE: 
204     case ROW_HOLE_2: 
205       return QString("Hole");
206     case ROW_PAR: 
207     case ROW_PAR_2: 
208       return QString("Par");
209     case ROW_HCP: 
210     case ROW_HCP_2: 
211       return QString("HCP");
212     case ROW_SCORE: 
213     case ROW_SCORE_2: 
214       return QString("Score");
215     }
216     return QVariant();
217   }
218
219   return QVariant();
220 }
221
222 //
223 // CourseTableModel
224 //
225 CourseTableModel::CourseTableModel(Course * c, QObject *parent) 
226     : QAbstractTableModel(parent), course(c)
227 {
228 }
229
230 int CourseTableModel::rowCount(const QModelIndex &) const
231 {
232     return ROWS;
233 }
234  
235 int CourseTableModel::columnCount(const QModelIndex &) const
236 {
237     return COLS + 2;
238 }
239
240 QVariant CourseTableModel::data(const QModelIndex &index, int role) const
241 {
242     if (!index.isValid())
243         return QVariant();
244
245     int row = index.row();
246     int col = index.column();
247
248     //
249     // ALIGNMENT
250     //
251     if (role == Qt::TextAlignmentRole ) {
252         return Qt::AlignCenter;
253     }
254
255     //
256     // FONT
257     //
258     if (role == Qt::FontRole) {
259         if (row == ROW_HOLE || row == ROW_HOLE_2) {
260             QFont font;
261             font.setBold(true);
262             return font;
263         }
264     }
265   //
266   // COLORS
267   //
268   if (role == Qt::ForegroundRole) {
269       // Hole numbers 1-18. All hole nums, in, out and tot cell but not
270       // the empty cell.
271       if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
272           QBrush brush(ScoreColor::holeBg());
273           return brush;
274       }
275       return QVariant();
276   }
277     //
278     // NUMBERS
279     //
280     if (role == Qt::DisplayRole) {
281
282         if (col == COLS) {
283             // In/out label
284             if (row == ROW_HOLE)
285                 return QString("Out");
286             if (row == ROW_HOLE_2)
287                 return QString("In");
288
289             // In/Out for par
290             if (course && row == ROW_PAR)
291                 return course->getTotal(TotalOut);
292             if (course && row == ROW_PAR_2)
293                 return course->getTotal(TotalIn);
294         }
295         else if (col == (COLS+1)) {
296             // Total label
297             if (row == ROW_HOLE_2)
298                 return QString("Tot");
299             // Total score
300             if (course && row == ROW_PAR_2)
301                 return course->getTotal(Total);
302         }
303         else {
304             // data cells
305             switch(row) {
306             case ROW_HOLE:
307                 return col + 1;
308             case ROW_HOLE_2:
309                 return col + 10;
310             case ROW_PAR:
311                 if (course)
312                     return course->getPar(col); 
313             case ROW_PAR_2:
314                 if (course)
315                     return course->getPar(col + 9); 
316             case ROW_HCP: 
317                 if (course)
318                     return course->getHcp(col); 
319             case ROW_HCP_2:
320                 if (course)
321                     return course->getHcp(col + 9);
322       }
323     }
324   }
325   return QVariant();
326 }
327
328 QVariant CourseTableModel::headerData(int section, Qt::Orientation orientation, int role) const
329 {
330   // Only vertical header -- horizontal is hidden
331   if (orientation == Qt::Horizontal)
332     return QVariant();
333
334   if (role == Qt::DisplayRole) {
335     switch(section) {
336     case ROW_HOLE: 
337     case ROW_HOLE_2: 
338       return QString("Hole");
339     case ROW_PAR: 
340     case ROW_PAR_2: 
341       return QString("Par");
342     case ROW_HCP: 
343     case ROW_HCP_2: 
344       return QString("HCP");
345     case ROW_LEN: 
346     case ROW_LEN_2: 
347       return QString("Len");
348     }
349     return QVariant();
350   }
351
352   return QVariant();
353 }