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 //qDebug() << "index() " << row << "/" << column << "/ flag: " << flag <<"//" << parent;
173 return createIndex(row, column, flag);
176 return QModelIndex();
180 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
182 if (!index.isValid())
185 if (!course || !score)
188 int row = index.row();
189 int col = index.column();
194 if (role == Qt::TextAlignmentRole ) {
195 return Qt::AlignCenter;
198 if (index.column() > 10)
204 QColor colorHoleBg(Qt::black);
205 QColor colorHoleFg(Qt::white);
206 QColor colorBirdie(Qt::yellow);
207 QColor colorPar(Qt::green);
208 QColor colorBogey(Qt::darkGreen);
209 QColor colorDoubleBogey(Qt::cyan);
210 QColor colorBad(Qt::white);
211 QColor colorSubTotal(Qt::lightGray);
212 QColor colorTotal(Qt::gray);
214 if (role == Qt::ForegroundRole) {
215 if (row == ROW_HOLE || row == ROW_HOLE_2) {
216 QBrush brush(colorHoleFg);
220 if (role == Qt::BackgroundRole) {
222 if (row == ROW_HOLE || row == ROW_HOLE_2) {
223 QBrush brush(colorHoleBg);
227 if (row == ROW_SCORE || row == ROW_SCORE_2) {
230 if (row == ROW_SCORE) {
231 par = course->getPar(col).toInt();
232 shots = score->getScore(col).toInt();
235 par = course->getPar(col + 9).toInt();
236 shots = score->getScore(col + 9).toInt();
239 if (col == 10 && row == ROW_SCORE_2) {
241 QBrush brush(colorTotal);
246 QBrush brush(colorSubTotal);
252 QBrush brush(colorPar);
255 if (shots == (par-1)) {
257 QBrush brush(colorBirdie);
260 if (shots == (par+1)) {
262 QBrush brush(colorBogey);
265 if (shots == (par+2)) {
267 QBrush brush(colorDoubleBogey);
277 if (role == Qt::FontRole) {
278 if (row == ROW_SCORE_2 && col == 10) {
287 if (role == Qt::DisplayRole) {
292 return QString("Out");
293 if (row == ROW_HOLE_2)
294 return QString("In");
298 return course->getTotal(TotalOut);
299 if (row == ROW_PAR_2)
300 return course->getTotal(TotalIn);
303 if (row == ROW_SCORE)
304 return score->getTotal(TotalOut);
305 if (row == ROW_SCORE_2)
306 return score->getTotal(TotalIn);
309 else if (col == 10) {
311 if (row == ROW_HOLE_2)
312 return QString("Tot");
314 if (row == ROW_PAR_2)
315 return course->getTotal(Total);
316 if (row == ROW_SCORE_2)
317 return score->getTotal(Total);
327 return course->getPar(col);
329 return course->getPar(col + 9);
331 return course->getHcp(col);
333 return course->getHcp(col + 9);
335 return score->getScore(col);
337 return score->getScore(col + 9);
344 int ScoreTableModel::setItem(int row, int col, int data)
346 emit dataChanged(createIndex(row, col), createIndex(row, col));
350 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
352 if (role != Qt::DisplayRole)
355 // TODO: how to diff between the two table views (no index?)
357 if (orientation == Qt::Horizontal)
358 if (section >= 0 && section <= 8)
359 return QString("%1").arg(section+1);
360 else if (section == 9)
361 return QString(""); // was: I/O
363 return QString(""); // was: Tot
368 return QString("Par");
371 return QString("HCP");
374 return QString("Score");