43fce9e29af0a8c53273795a0589ede998cf2e04
[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 8;
116 }
117  
118 int ScoreTableModel::columnCount(const QModelIndex & parent) const
119 {
120   return 9 + 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 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
136 {
137   if (!index.isValid())
138     return QVariant();
139
140   if (!course || !score)
141     return QVariant();
142
143   int row = index.row();
144   int col = index.column();
145
146   //
147   // ALIGNMENT
148   //
149   if (role == Qt::TextAlignmentRole ) {
150     return Qt::AlignCenter;
151   }
152
153   if (index.column() > 10)
154     return QVariant();
155
156   //
157   // COLORS
158   //
159   QColor colorHoleBg(Qt::black);
160   QColor colorHoleFg(Qt::white);
161   QColor colorBirdie(Qt::yellow);
162   QColor colorPar(Qt::green);
163   QColor colorBogey(Qt::darkGreen);
164   QColor colorDoubleBogey(Qt::cyan);
165   QColor colorBad(Qt::white);
166   QColor colorSubTotal(Qt::lightGray);
167   QColor colorTotal(Qt::gray);
168
169   if (role == Qt::ForegroundRole) {
170     if (row == ROW_HOLE || row == ROW_HOLE_2) {
171         QBrush brush(colorHoleFg);
172         return brush;
173     }
174   }
175   if (role == Qt::BackgroundRole) {
176     int par = course->getPar(col).toInt();
177     int shots = score->getScore(col).toInt();
178
179     // Hole numbers 1-18
180     if (row == ROW_HOLE || row == ROW_HOLE_2) {
181         QBrush brush(colorHoleBg);
182         return brush;
183     }
184
185     if (row == ROW_SCORE || row == ROW_SCORE_2) {
186       if (col == 10 && row == ROW_SCORE_2) {
187         // Total score
188         QBrush brush(colorTotal);
189         return brush;
190       }
191       if (col == 9) {
192         // In and Out scores
193         QBrush brush(colorSubTotal);
194         return brush;
195       }
196       if (col < 9) {
197         if (shots == par) {
198           // Par
199           QBrush brush(colorPar);
200           return brush;
201         }
202         if (shots == (par-1)) {
203           // Birdie
204           QBrush brush(colorBirdie);
205           return brush;
206         }
207         if (shots == (par+1)) {
208           // Bogey
209           QBrush brush(colorBogey);
210           return brush;
211         }
212         if (shots == (par+2)) {
213           // Double Bogey
214           QBrush brush(colorDoubleBogey);
215           return brush;
216         }
217       }
218     }
219     return QVariant();
220   }
221   //
222   // FONT
223   //
224   if (role == Qt::FontRole) {
225     if (row == ROW_SCORE_2 && col == 10) {
226         QFont font;
227         font.setBold(true);
228         return font;
229     }
230   }
231   //
232   // DATA
233   //
234   if (role == Qt::DisplayRole) {
235
236     if (col == 9) {
237       // In/out label
238       if (row == ROW_HOLE)
239         return QString("Out");
240       if (row == ROW_HOLE_2)
241         return QString("In");
242
243       // In/Out for par
244       if (row == ROW_PAR)
245         return course->getTotal(TotalOut);
246       if (row == ROW_PAR_2)
247         return course->getTotal(TotalIn);
248
249       // In/Out for score
250       if (row == ROW_SCORE)
251         return score->getTotal(TotalOut);
252       if (row == ROW_SCORE_2)
253         return score->getTotal(TotalIn);
254       
255     }
256     else if (col == 10) {
257       // Total label
258       if (row == ROW_HOLE_2)
259         return QString("Tot");
260       // Total score
261       if (row == ROW_PAR_2)
262         return course->getTotal(Total);
263       if (row == ROW_SCORE_2)
264         return score->getTotal(Total);
265     }
266     else {
267       // data cells
268       switch(row) {
269       case ROW_HOLE:
270         return col + 1;
271       case ROW_HOLE_2:
272         return col + 10;
273       case ROW_PAR:
274         return course->getPar(col); 
275       case ROW_PAR_2:
276         return course->getPar(col + 9); 
277       case ROW_HCP: 
278         return course->getHcp(col); 
279       case ROW_HCP_2: 
280         return course->getHcp(col + 9);
281       case ROW_SCORE: 
282         return score->getScore(col);
283       case ROW_SCORE_2: 
284         return score->getScore(col + 9);
285       }
286     }
287   }
288   return QVariant();
289 }
290
291 int ScoreTableModel::setItem(int row, int col, int data)
292 {
293   emit dataChanged(createIndex(row, col), createIndex(row, col));
294   return 1;
295 }
296
297 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
298 {
299   if (role != Qt::DisplayRole)
300          return QVariant();
301
302     // TODO: how to diff between the two table views (no index?)
303
304     if (orientation == Qt::Horizontal)
305       if (section >= 0 && section <= 8)
306         return QString("%1").arg(section+1);
307       else if (section == 9)
308         return QString(""); // was: I/O
309       else
310         return QString(""); // was: Tot
311     else {
312       switch(section) {
313       case ROW_PAR: 
314       case ROW_PAR_2: 
315         return QString("Par");
316       case ROW_HCP: 
317       case ROW_HCP_2: 
318         return QString("HCP");
319       case ROW_SCORE: 
320       case ROW_SCORE_2: 
321         return QString("Score");
322       }
323     }
324     return QVariant();
325 }
326