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