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