98bbf18da4ea45f026a823bfc27f5fea89fc0d66
[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   //if (role == Qt::ForegroundRole) {
157     int par = (course->getPar(index.column() + offset)).toInt();
158     int shots = (score->getScore(index.column() + offset)).toInt();
159
160     if (index.row() == ROW_SCORE) {
161       if (index.column() == 10 && offset == 9) {
162         // Total score
163         QColor color(Qt::blue);
164         QBrush brush(color);
165         brush.setStyle(Qt::Dense6Pattern);
166         return brush;
167       }
168       if (col == 9 && row == 2) {
169         // In and Out scores
170         QColor color(Qt::blue);
171         QBrush brush(color);
172         brush.setStyle(Qt::Dense7Pattern);
173         return brush;
174       }
175       if (index.column() < 9) {
176         if (shots == par) {
177           // Par
178           QColor color(Qt::yellow);
179           QBrush brush(color);
180           return brush;
181         }
182         if (shots == (par-1)) {
183           // Birdie
184           QColor color(Qt::green);
185           QBrush brush(color);
186           return brush;
187         }
188         if (shots == (par+1)) {
189           // Bogey
190           QColor color(Qt::red);
191           QBrush brush(color);
192           return brush;
193         }
194       }
195     }
196     return QVariant();
197   }
198
199
200   //
201   // DATA
202   //
203   if (role == Qt::DisplayRole) {
204     // In/Out column
205     if (index.column() == 9) {
206       if (index.row() == ROW_PAR)
207         if (offset == 0)
208           return course->getTotal(TotalOut);
209         else 
210           return course->getTotal(TotalIn);
211       else if (index.row() == ROW_SCORE) {
212         if (offset == 0)
213           return score->getTotal(TotalOut);
214         else 
215           return score->getTotal(TotalIn);
216       }
217       else
218         return QVariant();
219     }
220
221     // Tot column
222     if (index.column() == 10) {
223       if (index.row() == ROW_PAR && offset == 9)
224         return course->getTotal(Total);
225       else if (index.row() == ROW_SCORE && offset == 9)
226         return score->getTotal(Total);
227       else
228         return QVariant();
229     }
230
231     //qDebug() << "data() " << index << "/" << offset;
232     switch(index.row()) {
233     case ROW_PAR:
234       return course->getPar(index.column() + offset); 
235     case ROW_HCP: 
236       return course->getHcp(index.column() + offset); 
237     case ROW_SCORE: 
238       return score->getScore(index.column() + offset); 
239     }
240   }
241   return QVariant();
242 }
243
244 int ScoreTableModel::setItem(int row, int col, int data)
245 {
246   emit dataChanged(createIndex(row, col), createIndex(row, col));
247   return 1;
248 }
249
250 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
251 {
252     if (role != Qt::DisplayRole)
253          return QVariant();
254
255     // TODO: how to diff between the two table views (no index?)
256
257     if (orientation == Qt::Horizontal)
258       if (section >= 0 && section <= 8)
259         return QString("%1").arg(section+1);
260       else if (section == 9)
261         return QString(""); // was: I/O
262       else
263         return QString(""); // was: Tot
264     else {
265       switch(section) {
266       case 0: 
267         return QString("Par");
268       case 1: 
269         return QString("HCP");
270       case 2: 
271         return QString("Score");
272       }
273     }
274     return QVariant();
275 }
276