Changed the color scheme to more appealing
[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 Qt::NoItemFlags;
10 }
11
12 // Assign the 'sList' to internal 'scoreList'. Set the current score
13 // to 'currentScore', or to 's'.
14 void ScoreTableModel::setScore(QList<Score *> &sList, Score *s)
15 {
16   scoreList = sList;
17   if (scoreList.size() > 0) {
18     if (s) {
19       currentScore = scoreList.indexOf(s);
20       if (currentScore == -1)
21         currentScore = 0;
22     }
23     score = scoreList.at(currentScore); // NOTE: assumes non-empty list
24   }
25 }
26
27 void ScoreTableModel::setClub(QList<Club *> &cList)
28 {
29   clubList = cList;
30
31   if (clubList.size() > 0)
32     club = clubList.at(0);
33
34   if (club)
35     course = club->getCourse(0);
36 }
37
38 QString ScoreTableModel::getInfoText()
39 {
40   QString str("");
41
42   if (score)
43     str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
44
45   return str;
46 }
47
48 QString ScoreTableModel::getCountText()
49 {
50   QString str = QString("%1/%2").arg(currentScore+1, 2).arg(scoreList.count(), 2);
51   return str;
52 }
53
54 Course *ScoreTableModel::findCourse(const QString &clubName, 
55                                     const QString &courseName)
56 {
57   QListIterator<Club *> i(clubList);
58   Club *c;
59
60   while (i.hasNext()) {
61     c = i.next();
62     if (c->getName() == clubName) {
63       return c->getCourse(courseName);
64     }
65   }
66   return 0;
67 }
68
69 void ScoreTableModel::first()
70 {
71   if (score && course) {
72     currentScore = 0;
73     score = scoreList.at(currentScore);
74     course = findCourse(score->getClubName(), score->getCourseName());
75     emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
76   }
77 }
78
79 void ScoreTableModel::last()
80 {
81   if (score && course) {
82     currentScore = scoreList.size() - 1;
83     score = scoreList.at(currentScore);
84     course = findCourse(score->getClubName(), score->getCourseName());
85     emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
86   }
87 }
88
89 void ScoreTableModel::next()
90 {
91   if (score && course) {
92     if (currentScore < (scoreList.size() - 1)) {
93       currentScore++;
94       score = scoreList.at(currentScore);
95       course = findCourse(score->getClubName(), score->getCourseName());
96       emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
97     }
98   }
99 }
100
101 void ScoreTableModel::prev()
102 {
103   if (score && course) {
104     if (currentScore > 0) {
105       currentScore--;
106       score = scoreList.at(currentScore);
107       course = findCourse(score->getClubName(), score->getCourseName());
108       emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
109     }
110   }
111 }
112
113 int ScoreTableModel::rowCount(const QModelIndex & parent) const
114 {
115   return ROW_COUNT;
116 }
117  
118 int ScoreTableModel::columnCount(const QModelIndex & parent) const
119 {
120   return COL_COUNT + 2; // 2 for in/out and tot columns
121 }
122
123 QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &parent) const
124 {
125   if (hasIndex(row, column, parent)) {
126     int flag = (parent.column() > 0) ? parent.column() : 0;
127     //qDebug() << "index() " << row << "/" << column << "/ flag: " << flag <<"//" << parent;
128     return createIndex(row, column, flag);
129   }
130   else {
131     return QModelIndex();
132   }
133 }
134
135 #define ROW_PAR   0
136 #define ROW_HCP   1
137 #define ROW_SCORE 2
138
139 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
140 {
141   if (!index.isValid())
142     return QVariant();
143
144   if (!course || !score)
145     return QVariant();
146
147   int row = index.row();
148   int col = index.column();
149
150   //
151   // ALIGNMENT
152   //
153   if (role == Qt::TextAlignmentRole ) {
154     return Qt::AlignCenter;
155   }
156
157   // Does this item belog to front or back nine
158   int offset = index.internalId() ? 9 : 0;
159   if (index.column() > 10)
160     return QVariant();
161
162   //
163   // COLORS
164   //
165   QColor colorBirdie(Qt::yellow);
166   QColor colorPar(Qt::green);
167   QColor colorBogey(Qt::darkGreen);
168   QColor colorDoubleBogey(Qt::cyan);
169   QColor colorBad(Qt::white);
170   QColor colorSubTotal(Qt::lightGray);
171   QColor colorTotal(Qt::gray);
172
173   if (role == Qt::BackgroundRole) {
174     int par = (course->getPar(index.column() + offset)).toInt();
175     int shots = (score->getScore(index.column() + offset)).toInt();
176
177     if (index.row() == ROW_SCORE) {
178       if (index.column() == 10 && offset == 9) {
179         // Total score
180         QBrush brush(colorTotal);
181         //brush.setStyle(Qt::Dense6Pattern);
182         return brush;
183       }
184       if (col == 9 && row == 2) {
185         // In and Out scores
186         QBrush brush(colorSubTotal);
187         //brush.setStyle(Qt::Dense7Pattern);
188         return brush;
189       }
190       if (index.column() < 9) {
191         if (shots == par) {
192           // Par
193           QBrush brush(colorPar);
194           return brush;
195         }
196         if (shots == (par-1)) {
197           // Birdie
198           QBrush brush(colorBirdie);
199           return brush;
200         }
201         if (shots == (par+1)) {
202           // Bogey
203           QBrush brush(colorBogey);
204           return brush;
205         }
206         if (shots == (par+2)) {
207           // Double Bogey
208           QBrush brush(colorDoubleBogey);
209           return brush;
210         }
211       }
212     }
213     return QVariant();
214   }
215   //
216   // FONT
217   //
218   if (role == Qt::FontRole) {
219     if (index.row() == ROW_SCORE) {
220       if (index.column() == 10 && offset == 9) {
221         QFont font;
222         font.setBold(true);
223         return font;
224       }
225     }
226   }
227   //
228   // DATA
229   //
230   if (role == Qt::DisplayRole) {
231     // In/Out column
232     if (index.column() == 9) {
233       if (index.row() == ROW_PAR)
234         if (offset == 0)
235           return course->getTotal(TotalOut);
236         else 
237           return course->getTotal(TotalIn);
238       else if (index.row() == ROW_SCORE) {
239         if (offset == 0)
240           return score->getTotal(TotalOut);
241         else 
242           return score->getTotal(TotalIn);
243       }
244       else
245         return QVariant();
246     }
247
248     // Tot column
249     if (index.column() == 10) {
250       if (index.row() == ROW_PAR && offset == 9)
251         return course->getTotal(Total);
252       else if (index.row() == ROW_SCORE && offset == 9)
253         return score->getTotal(Total);
254       else
255         return QVariant();
256     }
257
258     //qDebug() << "data() " << index << "/" << offset;
259     switch(index.row()) {
260     case ROW_PAR:
261       return course->getPar(index.column() + offset); 
262     case ROW_HCP: 
263       return course->getHcp(index.column() + offset); 
264     case ROW_SCORE: 
265       return score->getScore(index.column() + offset); 
266     }
267   }
268   return QVariant();
269 }
270
271 int ScoreTableModel::setItem(int row, int col, int data)
272 {
273   emit dataChanged(createIndex(row, col), createIndex(row, col));
274   return 1;
275 }
276
277 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
278 {
279     if (role != Qt::DisplayRole)
280          return QVariant();
281
282     // TODO: how to diff between the two table views (no index?)
283
284     if (orientation == Qt::Horizontal)
285       if (section >= 0 && section <= 8)
286         return QString("%1").arg(section+1);
287       else if (section == 9)
288         return QString(""); // was: I/O
289       else
290         return QString(""); // was: Tot
291     else {
292       switch(section) {
293       case 0: 
294         return QString("Par");
295       case 1: 
296         return QString("HCP");
297       case 2: 
298         return QString("Score");
299       }
300     }
301     return QVariant();
302 }
303