e2f3d8d2348e9a36ac8e5cbbb748a886da2a73ff
[scorecard] / src / table-model.cpp
1
2 #include <QColor>
3 #include <QBrush>
4 #include <QFont>
5 #include "table-model.h"
6
7 Qt::ItemFlags ScoreTableModel::flags ( const QModelIndex & index )
8 {
9   return 0;
10 }
11
12 void ScoreTableModel::setMode(int m)
13 {
14   currentMode = m;
15 }
16
17 int ScoreTableModel::mode(void)
18 {
19   return currentMode;
20 }
21
22 // Assign the 'sList' to internal 'scoreList'. Set the current score
23 // to 'currentScore', or to 's'.
24 void ScoreTableModel::setScore(QList<Score *> &sList, Score *s)
25 {
26   scoreList = sList;
27   if (scoreList.size() > 0) {
28     if (s) {
29       currentScore = scoreList.indexOf(s);
30       if (currentScore == -1)
31         currentScore = 0;
32     }
33     score = scoreList.at(currentScore); // NOTE: assumes non-empty list
34   }
35 }
36
37 void ScoreTableModel::setClub(QList<Club *> &cList)
38 {
39   clubList = cList;
40
41   if (clubList.size() > 0)
42     club = clubList.at(0);
43
44   if (club)
45     course = club->getCourse(0);
46 }
47
48 QString ScoreTableModel::getInfoText()
49 {
50   QString str("");
51
52   if (score)
53     str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
54
55   return str;
56 }
57
58 QString ScoreTableModel::getCountText()
59 {
60   QString str = QString("%1/%2").arg(currentScore+1, 2).arg(scoreList.count(), 2);
61   return str;
62 }
63
64 QString& ScoreTableModel::clubName(void)
65 {
66   QString str("");
67
68   if (club)
69     str = club->getName();
70
71   return str;
72 }
73
74 QString& ScoreTableModel::courseName(void)
75 {
76   QString str("");
77
78   if (course)
79     str = course->getName();
80
81   return str;
82 }
83
84 Course *ScoreTableModel::findCourse(const QString &clubName, 
85                                     const QString &courseName)
86 {
87   QListIterator<Club *> i(clubList);
88   Club *c;
89
90   while (i.hasNext()) {
91     c = i.next();
92     if (c->getName() == clubName) {
93       return c->getCourse(courseName);
94     }
95   }
96   return 0;
97 }
98
99 Club *ScoreTableModel::getClub(void)
100 {
101   return club;
102 }
103
104 Course *ScoreTableModel::getCourse(void)
105 {
106   return course;
107 }
108
109 Score *ScoreTableModel::getScore(void)
110 {
111   return score;
112 }
113
114 void ScoreTableModel::first()
115 {
116   if (score && course) {
117     currentScore = 0;
118     score = scoreList.at(currentScore);
119     course = findCourse(score->getClubName(), score->getCourseName());
120     emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
121   }
122 }
123
124 void ScoreTableModel::last()
125 {
126   if (score && course) {
127     currentScore = scoreList.size() - 1;
128     score = scoreList.at(currentScore);
129     course = findCourse(score->getClubName(), score->getCourseName());
130     emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
131   }
132 }
133
134 void ScoreTableModel::next()
135 {
136   if (score && course) {
137     if (currentScore < (scoreList.size() - 1)) {
138       currentScore++;
139       score = scoreList.at(currentScore);
140       course = findCourse(score->getClubName(), score->getCourseName());
141       emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
142     }
143   }
144 }
145
146 void ScoreTableModel::prev()
147 {
148   if (score && course) {
149     if (currentScore > 0) {
150       currentScore--;
151       score = scoreList.at(currentScore);
152       course = findCourse(score->getClubName(), score->getCourseName());
153       emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
154     }
155   }
156 }
157
158 int ScoreTableModel::rowCount(const QModelIndex & parent) const
159 {
160   return 8;
161 }
162  
163 int ScoreTableModel::columnCount(const QModelIndex & parent) const
164 {
165   return 9 + 2; // 2 for in/out and tot columns
166 }
167
168 QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &parent) const
169 {
170   if (hasIndex(row, column, parent)) {
171     int flag = (parent.column() > 0) ? parent.column() : 0;
172     return createIndex(row, column, flag);
173   }
174   else {
175     return QModelIndex();
176   }
177 }
178
179 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
180 {
181   // TODO: move away from the stack
182   QColor colorHoleBg(Qt::black);
183   QColor colorHoleFg(Qt::white);
184   QColor colorBirdie(Qt::yellow);
185   QColor colorPar(Qt::green);
186   QColor colorBogey(Qt::darkGreen);
187   QColor colorDoubleBogey(Qt::cyan);
188   QColor colorBad(Qt::white);
189   QColor colorSubTotal(Qt::lightGray);
190   QColor colorTotal(Qt::gray);
191
192   if (!index.isValid())
193     return QVariant();
194
195   int row = index.row();
196   int col = index.column();
197
198   //
199   // ALIGNMENT
200   //
201   if (role == Qt::TextAlignmentRole ) {
202     return Qt::AlignCenter;
203   }
204
205   if (index.column() > 10)
206     return QVariant();
207
208   //
209   // COLORS
210   //
211   if (role == Qt::ForegroundRole) {
212     if (row == ROW_HOLE || row == ROW_HOLE_2) {
213         QBrush brush(colorHoleFg);
214         return brush;
215     }
216   }
217   if (role == Qt::BackgroundRole) {
218     // Hole numbers 1-18
219     if (row == ROW_HOLE || row == ROW_HOLE_2) {
220         QBrush brush(colorHoleBg);
221         return brush;
222     }
223
224     if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) {
225       int par;
226       int shots;
227       if (row == ROW_SCORE) {
228         par = course->getPar(col).toInt();
229         shots = score->getScore(col).toInt();
230       }
231       else {
232         par = course->getPar(col + 9).toInt();
233         shots = score->getScore(col + 9).toInt();
234       }
235
236       if (col == 10 && row == ROW_SCORE_2) {
237         // Total score
238         QBrush brush(colorTotal);
239         return brush;
240       }
241       if (col == 9) {
242         // In and Out scores
243         QBrush brush(colorSubTotal);
244         return brush;
245       }
246       if (col < 9) {
247         if (shots == par) {
248           // Par
249           QBrush brush(colorPar);
250           return brush;
251         }
252         if (shots == (par-1)) {
253           // Birdie
254           QBrush brush(colorBirdie);
255           return brush;
256         }
257         if (shots == (par+1)) {
258           // Bogey
259           QBrush brush(colorBogey);
260           return brush;
261         }
262         if (shots == (par+2)) {
263           // Double Bogey
264           QBrush brush(colorDoubleBogey);
265           return brush;
266         }
267       }
268     }
269     return QVariant();
270   }
271   //
272   // FONT
273   //
274   if (role == Qt::FontRole) {
275     if (row == ROW_SCORE_2 && col == 10) {
276         QFont font;
277         font.setBold(true);
278         return font;
279     }
280   }
281   //
282   // DATA
283   //
284   if (role == Qt::DisplayRole) {
285
286     if (col == 9) {
287       // In/out label
288       if (row == ROW_HOLE)
289         return QString("Out");
290       if (row == ROW_HOLE_2)
291         return QString("In");
292
293       // In/Out for par
294       if (score && course && row == ROW_PAR)
295         return course->getTotal(TotalOut);
296       if (score && course && row == ROW_PAR_2)
297         return course->getTotal(TotalIn);
298
299       // In/Out for score
300       if (score && row == ROW_SCORE)
301         return score->getTotal(TotalOut);
302       if (score && row == ROW_SCORE_2)
303         return score->getTotal(TotalIn);
304       
305     }
306     else if (col == 10) {
307       // Total label
308       if (row == ROW_HOLE_2)
309         return QString("Tot");
310       // Total score
311       if (score && course && row == ROW_PAR_2)
312         return course->getTotal(Total);
313       if (score && row == ROW_SCORE_2)
314         return score->getTotal(Total);
315     }
316     else {
317       // data cells
318       switch(row) {
319       case ROW_HOLE:
320         return col + 1;
321       case ROW_HOLE_2:
322         return col + 10;
323       case ROW_PAR:
324         if (score && course)
325           return course->getPar(col); 
326       case ROW_PAR_2:
327         if (score && course)
328           return course->getPar(col + 9); 
329       case ROW_HCP: 
330         if (score && course)
331           return course->getHcp(col); 
332       case ROW_HCP_2:
333         if (score && course)
334           return course->getHcp(col + 9);
335       case ROW_SCORE:
336         if (score)
337           return score->getScore(col);
338       case ROW_SCORE_2: 
339         if (score)
340           return score->getScore(col + 9);
341       }
342     }
343   }
344   return QVariant();
345 }
346
347 int ScoreTableModel::setItem(int row, int col, int data)
348 {
349   emit dataChanged(createIndex(row, col), createIndex(row, col));
350   return 1;
351 }
352
353 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
354 {
355   if (role != Qt::DisplayRole)
356          return QVariant();
357
358     // TODO: how to diff between the two table views (no index?)
359
360     if (orientation == Qt::Horizontal)
361       if (section >= 0 && section <= 8)
362         return QString("%1").arg(section+1);
363       else if (section == 9)
364         return QString(""); // was: I/O
365       else
366         return QString(""); // was: Tot
367     else {
368       switch(section) {
369       case ROW_PAR: 
370       case ROW_PAR_2: 
371         return QString("Par");
372       case ROW_HCP: 
373       case ROW_HCP_2: 
374         return QString("HCP");
375       case ROW_SCORE: 
376       case ROW_SCORE_2: 
377         return QString("Score");
378       }
379     }
380     return QVariant();
381 }
382