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