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 // TODO: move away from the stack
183 QColor colorHoleBg(Qt::black);
184 QColor colorHoleFg(Qt::white);
185 QColor colorBirdie(Qt::yellow);
186 QColor colorPar(Qt::green);
187 QColor colorBogey(Qt::darkGreen);
188 QColor colorDoubleBogey(Qt::cyan);
189 QColor colorBad(Qt::white);
190 QColor colorSubTotal(Qt::lightGray);
191 QColor colorTotal(Qt::gray);
193 if (!index.isValid())
196 int row = index.row();
197 int col = index.column();
202 if (role == Qt::TextAlignmentRole ) {
203 return Qt::AlignCenter;
206 if (index.column() > 10)
212 if (role == Qt::ForegroundRole) {
213 if (row == ROW_HOLE || row == ROW_HOLE_2) {
214 QBrush brush(colorHoleFg);
218 if (role == Qt::BackgroundRole) {
220 if (row == ROW_HOLE || row == ROW_HOLE_2) {
221 QBrush brush(colorHoleBg);
225 if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) {
228 if (row == ROW_SCORE) {
229 par = course->getPar(col).toInt();
230 shots = score->getScore(col).toInt();
233 par = course->getPar(col + 9).toInt();
234 shots = score->getScore(col + 9).toInt();
237 if (col == 10 && row == ROW_SCORE_2) {
239 QBrush brush(colorTotal);
244 QBrush brush(colorSubTotal);
250 QBrush brush(colorPar);
253 if (shots == (par-1)) {
255 QBrush brush(colorBirdie);
258 if (shots == (par+1)) {
260 QBrush brush(colorBogey);
263 if (shots == (par+2)) {
265 QBrush brush(colorDoubleBogey);
275 if (role == Qt::FontRole) {
276 if (row == ROW_SCORE_2 && col == 10) {
285 if (role == Qt::DisplayRole) {
290 return QString("Out");
291 if (row == ROW_HOLE_2)
292 return QString("In");
295 if (score && course && row == ROW_PAR)
296 return course->getTotal(TotalOut);
297 if (score && course && row == ROW_PAR_2)
298 return course->getTotal(TotalIn);
301 if (score && row == ROW_SCORE)
302 return score->getTotal(TotalOut);
303 if (score && row == ROW_SCORE_2)
304 return score->getTotal(TotalIn);
307 else if (col == 10) {
309 if (row == ROW_HOLE_2)
310 return QString("Tot");
312 if (score && course && row == ROW_PAR_2)
313 return course->getTotal(Total);
314 if (score && row == ROW_SCORE_2)
315 return score->getTotal(Total);
326 return course->getPar(col);
329 return course->getPar(col + 9);
332 return course->getHcp(col);
335 return course->getHcp(col + 9);
338 return score->getScore(col);
341 return score->getScore(col + 9);
348 int ScoreTableModel::setItem(int row, int col, int data)
350 emit dataChanged(createIndex(row, col), createIndex(row, col));
354 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
356 if (role != Qt::DisplayRole)
359 // TODO: how to diff between the two table views (no index?)
361 if (orientation == Qt::Horizontal)
362 if (section >= 0 && section <= 8)
363 return QString("%1").arg(section+1);
364 else if (section == 9)
365 return QString(""); // was: I/O
367 return QString(""); // was: Tot
372 return QString("Par");
375 return QString("HCP");
378 return QString("Score");