Don't try to use the ext MMC yet
[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     //qDebug() << "index() " << row << "/" << column << "/ flag: " << flag <<"//" << parent;
173     return createIndex(row, column, flag);
174   }
175   else {
176     return QModelIndex();
177   }
178 }
179
180 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
181 {
182   // TODO: move away from the stack
183   QColor colorHoleBg(Qt::black);
184   QColor colorHoleFg(Qt::white);
185   QColor colorBirdie(Qt::yellow);
186   QColor colorPar(Qt::green);
187   QColor colorBogey(Qt::darkGreen);
188   QColor colorDoubleBogey(Qt::cyan);
189   QColor colorBad(Qt::white);
190   QColor colorSubTotal(Qt::lightGray);
191   QColor colorTotal(Qt::gray);
192
193   if (!index.isValid())
194     return QVariant();
195
196   int row = index.row();
197   int col = index.column();
198
199   //
200   // ALIGNMENT
201   //
202   if (role == Qt::TextAlignmentRole ) {
203     return Qt::AlignCenter;
204   }
205
206   if (index.column() > 10)
207     return QVariant();
208
209   //
210   // COLORS
211   //
212   if (role == Qt::ForegroundRole) {
213     if (row == ROW_HOLE || row == ROW_HOLE_2) {
214         QBrush brush(colorHoleFg);
215         return brush;
216     }
217   }
218   if (role == Qt::BackgroundRole) {
219     // Hole numbers 1-18
220     if (row == ROW_HOLE || row == ROW_HOLE_2) {
221         QBrush brush(colorHoleBg);
222         return brush;
223     }
224
225     if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) {
226       int par;
227       int shots;
228       if (row == ROW_SCORE) {
229         par = course->getPar(col).toInt();
230         shots = score->getScore(col).toInt();
231       }
232       else {
233         par = course->getPar(col + 9).toInt();
234         shots = score->getScore(col + 9).toInt();
235       }
236
237       if (col == 10 && row == ROW_SCORE_2) {
238         // Total score
239         QBrush brush(colorTotal);
240         return brush;
241       }
242       if (col == 9) {
243         // In and Out scores
244         QBrush brush(colorSubTotal);
245         return brush;
246       }
247       if (col < 9) {
248         if (shots == par) {
249           // Par
250           QBrush brush(colorPar);
251           return brush;
252         }
253         if (shots == (par-1)) {
254           // Birdie
255           QBrush brush(colorBirdie);
256           return brush;
257         }
258         if (shots == (par+1)) {
259           // Bogey
260           QBrush brush(colorBogey);
261           return brush;
262         }
263         if (shots == (par+2)) {
264           // Double Bogey
265           QBrush brush(colorDoubleBogey);
266           return brush;
267         }
268       }
269     }
270     return QVariant();
271   }
272   //
273   // FONT
274   //
275   if (role == Qt::FontRole) {
276     if (row == ROW_SCORE_2 && col == 10) {
277         QFont font;
278         font.setBold(true);
279         return font;
280     }
281   }
282   //
283   // DATA
284   //
285   if (role == Qt::DisplayRole) {
286
287     if (col == 9) {
288       // In/out label
289       if (row == ROW_HOLE)
290         return QString("Out");
291       if (row == ROW_HOLE_2)
292         return QString("In");
293
294       // In/Out for par
295       if (score && course && row == ROW_PAR)
296         return course->getTotal(TotalOut);
297       if (score && course && row == ROW_PAR_2)
298         return course->getTotal(TotalIn);
299
300       // In/Out for score
301       if (score && row == ROW_SCORE)
302         return score->getTotal(TotalOut);
303       if (score && row == ROW_SCORE_2)
304         return score->getTotal(TotalIn);
305       
306     }
307     else if (col == 10) {
308       // Total label
309       if (row == ROW_HOLE_2)
310         return QString("Tot");
311       // Total score
312       if (score && course && row == ROW_PAR_2)
313         return course->getTotal(Total);
314       if (score && row == ROW_SCORE_2)
315         return score->getTotal(Total);
316     }
317     else {
318       // data cells
319       switch(row) {
320       case ROW_HOLE:
321         return col + 1;
322       case ROW_HOLE_2:
323         return col + 10;
324       case ROW_PAR:
325         if (score && course)
326           return course->getPar(col); 
327       case ROW_PAR_2:
328         if (score && course)
329           return course->getPar(col + 9); 
330       case ROW_HCP: 
331         if (score && course)
332           return course->getHcp(col); 
333       case ROW_HCP_2:
334         if (score && course)
335           return course->getHcp(col + 9);
336       case ROW_SCORE:
337         if (score)
338           return score->getScore(col);
339       case ROW_SCORE_2: 
340         if (score)
341           return score->getScore(col + 9);
342       }
343     }
344   }
345   return QVariant();
346 }
347
348 int ScoreTableModel::setItem(int row, int col, int data)
349 {
350   emit dataChanged(createIndex(row, col), createIndex(row, col));
351   return 1;
352 }
353
354 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
355 {
356   if (role != Qt::DisplayRole)
357          return QVariant();
358
359     // TODO: how to diff between the two table views (no index?)
360
361     if (orientation == Qt::Horizontal)
362       if (section >= 0 && section <= 8)
363         return QString("%1").arg(section+1);
364       else if (section == 9)
365         return QString(""); // was: I/O
366       else
367         return QString(""); // was: Tot
368     else {
369       switch(section) {
370       case ROW_PAR: 
371       case ROW_PAR_2: 
372         return QString("Par");
373       case ROW_HCP: 
374       case ROW_HCP_2: 
375         return QString("HCP");
376       case ROW_SCORE: 
377       case ROW_SCORE_2: 
378         return QString("Score");
379       }
380     }
381     return QVariant();
382 }
383