30d2909d8ca4d3d4e10ff99c3eeef85cc3b023a5
[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       if (row == ROW_HOLE || row == ROW_HOLE_2) {
125           QFont font;
126           font.setBold(true);
127           return font;
128       }
129       if (row == ROW_SCORE || row == ROW_SCORE_2) {
130           QFont font;
131           font.setBold(true);
132           return font;
133       }
134   }
135   //
136   // NUMBERS
137   //
138   if (role == Qt::DisplayRole) {
139
140     if (col == COLS) {
141       // In/out label
142       if (row == ROW_HOLE)
143         return QString("Out");
144       if (row == ROW_HOLE_2)
145         return QString("In");
146
147       // In/Out for par
148       if (score && course && row == ROW_PAR)
149         return course->getTotal(TotalOut);
150       if (score && course && row == ROW_PAR_2)
151         return course->getTotal(TotalIn);
152
153       // In/Out for score
154       if (score && row == ROW_SCORE)
155         return score->getTotal(TotalOut);
156       if (score && row == ROW_SCORE_2)
157         return score->getTotal(TotalIn);
158       
159     }
160     else if (col == (COLS+1)) {
161       // Total label
162       if (row == ROW_HOLE_2)
163         return QString("Tot");
164       // Total score
165       if (score && course && row == ROW_PAR_2)
166         return course->getTotal(Total);
167       if (score && row == ROW_SCORE_2)
168         return score->getTotal(Total);
169     }
170     else {
171       // data cells
172       switch(row) {
173       case ROW_HOLE:
174         return col + 1;
175       case ROW_HOLE_2:
176         return col + 10;
177       case ROW_PAR:
178         if (score && course)
179           return course->getPar(col); 
180       case ROW_PAR_2:
181         if (score && course)
182           return course->getPar(col + 9); 
183       case ROW_HCP: 
184         if (score && course)
185           return course->getHcp(col); 
186       case ROW_HCP_2:
187         if (score && course)
188           return course->getHcp(col + 9);
189       case ROW_SCORE:
190         if (score)
191           return score->getScore(col);
192       case ROW_SCORE_2: 
193         if (score)
194           return score->getScore(col + 9);
195       }
196     }
197   }
198   return QVariant();
199 }
200
201 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
202 {
203   // Only vertical header -- horizontal is hidden
204   if (orientation == Qt::Horizontal)
205     return QVariant();
206
207   if (role == Qt::DisplayRole) {
208     switch(section) {
209     case ROW_HOLE: 
210     case ROW_HOLE_2: 
211       return QString("Hole");
212     case ROW_PAR: 
213     case ROW_PAR_2: 
214       return QString("Par");
215     case ROW_HCP: 
216     case ROW_HCP_2: 
217       return QString("HCP");
218     case ROW_SCORE: 
219     case ROW_SCORE_2: 
220       return QString("Score");
221     }
222     return QVariant();
223   }
224
225   return QVariant();
226 }
227
228 //
229 // CourseTableModel
230 //
231 CourseTableModel::CourseTableModel(QObject *parent) 
232     : QAbstractTableModel(parent)
233 {
234     course = 0;
235 }
236
237 void CourseTableModel::set(Course *c)
238 {
239     course = c;
240 }
241
242 int CourseTableModel::rowCount(const QModelIndex &) const
243 {
244     return ROWS;
245 }
246  
247 int CourseTableModel::columnCount(const QModelIndex &) const
248 {
249     return COLS + 2;
250 }
251
252 QVariant CourseTableModel::data(const QModelIndex &index, int role) const
253 {
254     if (!index.isValid())
255         return QVariant();
256
257     int row = index.row();
258     int col = index.column();
259
260     //
261     // ALIGNMENT
262     //
263     if (role == Qt::TextAlignmentRole ) {
264         return Qt::AlignCenter;
265     }
266
267     //
268     // FONT
269     //
270     if (role == Qt::FontRole) {
271         if (row == ROW_HOLE || row == ROW_HOLE_2) {
272             QFont font;
273             font.setBold(true);
274             return font;
275         }
276     }
277   //
278   // COLORS
279   //
280   if (role == Qt::ForegroundRole) {
281       // Hole numbers 1-18. All hole nums, in, out and tot cell but not
282       // the empty cell.
283       if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
284           QBrush brush(ScoreColor::holeBg());
285           return brush;
286       }
287       return QVariant();
288   }
289     //
290     // NUMBERS
291     //
292     if (role == Qt::DisplayRole) {
293
294         if (col == COLS) {
295             // In/out label
296             if (row == ROW_HOLE)
297                 return QString("Out");
298             if (row == ROW_HOLE_2)
299                 return QString("In");
300
301             // In/Out for par
302             if (course && row == ROW_PAR)
303                 return course->getTotal(TotalOut);
304             if (course && row == ROW_PAR_2)
305                 return course->getTotal(TotalIn);
306         }
307         else if (col == (COLS+1)) {
308             // Total label
309             if (row == ROW_HOLE_2)
310                 return QString("Tot");
311             // Total score
312             if (course && row == ROW_PAR_2)
313                 return course->getTotal(Total);
314         }
315         else {
316             // data cells
317             switch(row) {
318             case ROW_HOLE:
319                 return col + 1;
320             case ROW_HOLE_2:
321                 return col + 10;
322             case ROW_PAR:
323                 if (course)
324                     return course->getPar(col); 
325             case ROW_PAR_2:
326                 if (course)
327                     return course->getPar(col + 9); 
328             case ROW_HCP: 
329                 if (course)
330                     return course->getHcp(col); 
331             case ROW_HCP_2:
332                 if (course)
333                     return course->getHcp(col + 9);
334       }
335     }
336   }
337   return QVariant();
338 }
339
340 QVariant CourseTableModel::headerData(int section, Qt::Orientation orientation, int role) const
341 {
342   // Only vertical header -- horizontal is hidden
343   if (orientation == Qt::Horizontal)
344     return QVariant();
345
346   if (role == Qt::DisplayRole) {
347     switch(section) {
348     case ROW_HOLE: 
349     case ROW_HOLE_2: 
350       return QString("Hole");
351     case ROW_PAR: 
352     case ROW_PAR_2: 
353       return QString("Par");
354     case ROW_HCP: 
355     case ROW_HCP_2: 
356       return QString("HCP");
357     case ROW_LEN: 
358     case ROW_LEN_2: 
359       return QString("Len");
360     }
361     return QVariant();
362   }
363
364   return QVariant();
365 }