new colors and menu settings to adapt new qt libs
[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::darkGray);
183   QColor colorHoleFg(Qt::yellow);
184   QColor colorBirdie(Qt::yellow);
185   QColor colorPar(Qt::green);
186   QColor colorBogey(Qt::darkGreen);
187   QColor colorDoubleBogey(Qt::red);
188   QColor colorBad(Qt::red);
189   QColor colorSubTotal(Qt::black);
190   QColor colorTotal(Qt::black);
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::BackgroundRole) {
212     // Hole numbers 1-18
213     if (row == ROW_HOLE || row == ROW_HOLE_2) {
214         QBrush brush(colorHoleBg);
215         return brush;
216     }
217     if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) {
218       int par;
219       int shots;
220       if (row == ROW_SCORE) {
221         par = course->getPar(col).toInt();
222         shots = score->getScore(col).toInt();
223       }
224       else {
225         par = course->getPar(col + 9).toInt();
226         shots = score->getScore(col + 9).toInt();
227       }
228
229       if (col == 10 && row == ROW_SCORE_2) {
230         // Total score
231         QBrush brush(colorTotal);
232         return brush;
233       }
234       if (col == 9) {
235         // In and Out scores
236         QBrush brush(colorSubTotal);
237         return brush;
238       }
239       if (col < 9) {
240         if (shots == par) {
241           // Par
242           QBrush brush(colorPar);
243           return brush;
244         }
245         if (shots == (par-1)) {
246           // Birdie
247           QBrush brush(colorBirdie);
248           return brush;
249         }
250         if (shots == (par+1)) {
251           // Bogey
252           QBrush brush(colorBogey);
253           return brush;
254         }
255         if (shots == (par+2)) {
256           // Double Bogey
257           QBrush brush(colorDoubleBogey);
258           return brush;
259         }
260         if (shots > (par+2)) {
261           // Very bad
262           QBrush brush(colorBad);
263           return brush;
264         }
265       }
266     }
267     return QVariant();
268   }
269   //
270   // FONT
271   //
272   if (role == Qt::FontRole) {
273     if (row == ROW_SCORE_2 && col == 10) {
274         QFont font;
275         font.setBold(true);
276         return font;
277     }
278   }
279   //
280   // DATA
281   //
282   if (role == Qt::DisplayRole) {
283
284     if (col == 9) {
285       // In/out label
286       if (row == ROW_HOLE)
287         return QString("Out");
288       if (row == ROW_HOLE_2)
289         return QString("In");
290
291       // In/Out for par
292       if (score && course && row == ROW_PAR)
293         return course->getTotal(TotalOut);
294       if (score && course && row == ROW_PAR_2)
295         return course->getTotal(TotalIn);
296
297       // In/Out for score
298       if (score && row == ROW_SCORE)
299         return score->getTotal(TotalOut);
300       if (score && row == ROW_SCORE_2)
301         return score->getTotal(TotalIn);
302       
303     }
304     else if (col == 10) {
305       // Total label
306       if (row == ROW_HOLE_2)
307         return QString("Tot");
308       // Total score
309       if (score && course && row == ROW_PAR_2)
310         return course->getTotal(Total);
311       if (score && row == ROW_SCORE_2)
312         return score->getTotal(Total);
313     }
314     else {
315       // data cells
316       switch(row) {
317       case ROW_HOLE:
318         return col + 1;
319       case ROW_HOLE_2:
320         return col + 10;
321       case ROW_PAR:
322         if (score && course)
323           return course->getPar(col); 
324       case ROW_PAR_2:
325         if (score && course)
326           return course->getPar(col + 9); 
327       case ROW_HCP: 
328         if (score && course)
329           return course->getHcp(col); 
330       case ROW_HCP_2:
331         if (score && course)
332           return course->getHcp(col + 9);
333       case ROW_SCORE:
334         if (score)
335           return score->getScore(col);
336       case ROW_SCORE_2: 
337         if (score)
338           return score->getScore(col + 9);
339       }
340     }
341   }
342   return QVariant();
343 }
344
345 int ScoreTableModel::setItem(int row, int col, int data)
346 {
347   emit dataChanged(createIndex(row, col), createIndex(row, col));
348   return 1;
349 }
350
351 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
352 {
353   // Only vertical header -- horizontal is hidden
354   if (orientation == Qt::Horizontal)
355     return QVariant();
356
357 #if 1
358   if (role == Qt::BackgroundRole) {
359     QColor colorHoleBg(Qt::darkGray);
360     QBrush brush(colorHoleBg);
361     return brush;
362   }
363 #endif
364   if (role == Qt::DisplayRole) {
365     switch(section) {
366     case ROW_PAR: 
367     case ROW_PAR_2: 
368       return QString("Par");
369     case ROW_HCP: 
370     case ROW_HCP_2: 
371       return QString("HCP");
372     case ROW_SCORE: 
373     case ROW_SCORE_2: 
374       return QString("Score");
375     }
376     return QVariant();
377   }
378
379   return QVariant();
380 }
381