5 #include "table-model.h"
7 Qt::ItemFlags ScoreTableModel::flags ( const QModelIndex & index )
12 void ScoreTableModel::setMode(int m)
17 int ScoreTableModel::mode(void)
22 // Assign the 'sList' to internal 'scoreList'. Set the current score
23 // to 'currentScore', or to 's'.
24 void ScoreTableModel::setScore(QList<Score *> &sList, Score *s)
27 if (scoreList.size() > 0) {
29 currentScore = scoreList.indexOf(s);
30 if (currentScore == -1)
33 score = scoreList.at(currentScore); // NOTE: assumes non-empty list
37 void ScoreTableModel::setClub(QList<Club *> &cList)
41 if (clubList.size() > 0)
42 club = clubList.at(0);
45 course = club->getCourse(0);
48 QString ScoreTableModel::getInfoText()
53 str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
58 QString ScoreTableModel::getCountText()
60 QString str = QString("%1/%2").arg(currentScore+1, 2).arg(scoreList.count(), 2);
64 QString& ScoreTableModel::clubName(void)
69 str = club->getName();
74 QString& ScoreTableModel::courseName(void)
79 str = course->getName();
84 Course *ScoreTableModel::findCourse(const QString &clubName,
85 const QString &courseName)
87 QListIterator<Club *> i(clubList);
92 if (c->getName() == clubName) {
93 return c->getCourse(courseName);
99 Club *ScoreTableModel::getClub(void)
104 Course *ScoreTableModel::getCourse(void)
109 Score *ScoreTableModel::getScore(void)
114 void ScoreTableModel::first()
116 if (score && course) {
118 score = scoreList.at(currentScore);
119 course = findCourse(score->getClubName(), score->getCourseName());
120 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
124 void ScoreTableModel::last()
126 if (score && course) {
127 currentScore = scoreList.size() - 1;
128 score = scoreList.at(currentScore);
129 course = findCourse(score->getClubName(), score->getCourseName());
130 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
134 void ScoreTableModel::next()
136 if (score && course) {
137 if (currentScore < (scoreList.size() - 1)) {
139 score = scoreList.at(currentScore);
140 course = findCourse(score->getClubName(), score->getCourseName());
141 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
146 void ScoreTableModel::prev()
148 if (score && course) {
149 if (currentScore > 0) {
151 score = scoreList.at(currentScore);
152 course = findCourse(score->getClubName(), score->getCourseName());
153 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
158 int ScoreTableModel::rowCount(const QModelIndex & parent) const
163 int ScoreTableModel::columnCount(const QModelIndex & parent) const
165 return 9 + 2; // 2 for in/out and tot columns
168 QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &parent) const
170 if (hasIndex(row, column, parent)) {
171 int flag = (parent.column() > 0) ? parent.column() : 0;
172 return createIndex(row, column, flag);
175 return QModelIndex();
179 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
181 // TODO: move away from the stack
182 QColor colorHoleBg(Qt::black);
183 QColor colorHoleFg(Qt::white);
184 QColor colorBirdie(Qt::yellow);
185 QColor colorPar(Qt::green);
186 QColor colorBogey(Qt::darkGreen);
187 QColor colorDoubleBogey(Qt::cyan);
188 QColor colorBad(Qt::white);
189 QColor colorSubTotal(Qt::lightGray);
190 QColor colorTotal(Qt::gray);
192 if (!index.isValid())
195 int row = index.row();
196 int col = index.column();
201 if (role == Qt::TextAlignmentRole ) {
202 return Qt::AlignCenter;
205 if (index.column() > 10)
211 if (role == Qt::ForegroundRole) {
212 if (row == ROW_HOLE || row == ROW_HOLE_2) {
213 QBrush brush(colorHoleFg);
217 if (role == Qt::BackgroundRole) {
219 if (row == ROW_HOLE || row == ROW_HOLE_2) {
220 QBrush brush(colorHoleBg);
224 if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) {
227 if (row == ROW_SCORE) {
228 par = course->getPar(col).toInt();
229 shots = score->getScore(col).toInt();
232 par = course->getPar(col + 9).toInt();
233 shots = score->getScore(col + 9).toInt();
236 if (col == 10 && row == ROW_SCORE_2) {
238 QBrush brush(colorTotal);
243 QBrush brush(colorSubTotal);
249 QBrush brush(colorPar);
252 if (shots == (par-1)) {
254 QBrush brush(colorBirdie);
257 if (shots == (par+1)) {
259 QBrush brush(colorBogey);
262 if (shots == (par+2)) {
264 QBrush brush(colorDoubleBogey);
274 if (role == Qt::FontRole) {
275 if (row == ROW_SCORE_2 && col == 10) {
284 if (role == Qt::DisplayRole) {
289 return QString("Out");
290 if (row == ROW_HOLE_2)
291 return QString("In");
294 if (score && course && row == ROW_PAR)
295 return course->getTotal(TotalOut);
296 if (score && course && row == ROW_PAR_2)
297 return course->getTotal(TotalIn);
300 if (score && row == ROW_SCORE)
301 return score->getTotal(TotalOut);
302 if (score && row == ROW_SCORE_2)
303 return score->getTotal(TotalIn);
306 else if (col == 10) {
308 if (row == ROW_HOLE_2)
309 return QString("Tot");
311 if (score && course && row == ROW_PAR_2)
312 return course->getTotal(Total);
313 if (score && row == ROW_SCORE_2)
314 return score->getTotal(Total);
325 return course->getPar(col);
328 return course->getPar(col + 9);
331 return course->getHcp(col);
334 return course->getHcp(col + 9);
337 return score->getScore(col);
340 return score->getScore(col + 9);
347 int ScoreTableModel::setItem(int row, int col, int data)
349 emit dataChanged(createIndex(row, col), createIndex(row, col));
353 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
355 if (role != Qt::DisplayRole)
358 // TODO: how to diff between the two table views (no index?)
360 if (orientation == Qt::Horizontal)
361 if (section >= 0 && section <= 8)
362 return QString("%1").arg(section+1);
363 else if (section == 9)
364 return QString(""); // was: I/O
366 return QString(""); // was: Tot
371 return QString("Par");
374 return QString("HCP");
377 return QString("Score");