5 #include "table-model.h"
9 QColor colorHoleBg(Qt::darkGray);
10 QColor colorHoleFg(Qt::yellow);
11 QColor colorBirdie(102, 102, 255);
12 QColor colorPar(Qt::green);
13 QColor colorBogey(Qt::darkGreen);
14 QColor colorDoubleBogey(Qt::red);
15 QColor colorBad(Qt::red);
16 QColor colorSubTotal(Qt::black);
17 QColor colorTotal(Qt::black);
19 Qt::ItemFlags ScoreTableModel::flags (const QModelIndex & index)
24 void ScoreTableModel::setMode(int m)
29 int ScoreTableModel::mode(void)
34 // Assign the 'sList' to internal 'scoreList'. Set the current score
35 // to 'currentScore', or to 's'.
36 void ScoreTableModel::setScore(QList<Score *> &sList, Score *s)
39 if (scoreList.size() > 0) {
41 currentScore = scoreList.indexOf(s);
42 if (currentScore == -1)
45 score = scoreList.at(currentScore); // NOTE: assumes non-empty list
49 void ScoreTableModel::setClub(QList<Club *> &cList)
53 if (clubList.size() > 0)
54 club = clubList.at(0);
57 course = club->getCourse(0);
60 QString ScoreTableModel::getInfoText()
65 str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
70 QString ScoreTableModel::getCountText()
72 QString str = QString("%1/%2").arg(currentScore+1, 2).arg(scoreList.count(), 2);
76 QString& ScoreTableModel::clubName(void)
79 return club->getName();
84 QString& ScoreTableModel::courseName(void)
87 return course->getName();
92 Course *ScoreTableModel::findCourse(const QString &clubName,
93 const QString &courseName)
95 QListIterator<Club *> i(clubList);
100 if (c->getName() == clubName) {
101 return c->getCourse(courseName);
107 Club *ScoreTableModel::getClub(void)
112 Course *ScoreTableModel::getCourse(void)
117 Score *ScoreTableModel::getScore(void)
122 void ScoreTableModel::first()
124 if (score && course) {
126 score = scoreList.at(currentScore);
127 course = findCourse(score->getClubName(), score->getCourseName());
128 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
132 void ScoreTableModel::last()
134 if (score && course) {
135 currentScore = scoreList.size() - 1;
136 score = scoreList.at(currentScore);
137 course = findCourse(score->getClubName(), score->getCourseName());
138 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
142 void ScoreTableModel::next()
144 if (score && course) {
145 if (currentScore < (scoreList.size() - 1)) {
147 score = scoreList.at(currentScore);
148 course = findCourse(score->getClubName(), score->getCourseName());
149 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
154 void ScoreTableModel::prev()
156 if (score && course) {
157 if (currentScore > 0) {
159 score = scoreList.at(currentScore);
160 course = findCourse(score->getClubName(), score->getCourseName());
161 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
166 int ScoreTableModel::rowCount(const QModelIndex & parent) const
171 int ScoreTableModel::columnCount(const QModelIndex & parent) const
173 return 9 + 2; // 2 for in/out and tot columns
176 QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &parent) const
178 if (hasIndex(row, column, parent)) {
179 int flag = (parent.column() > 0) ? parent.column() : 0;
180 return createIndex(row, column, flag);
183 return QModelIndex();
187 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
189 // TODO: move away from the stack
191 if (!index.isValid())
194 int row = index.row();
195 int col = index.column();
200 if (role == Qt::TextAlignmentRole ) {
201 return Qt::AlignCenter;
204 if (index.column() > 10)
210 if (role == Qt::BackgroundRole) {
211 // Hole numbers 1-18. All hole nums, in, out and tot cell but not
213 if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) {
214 QBrush brush(colorHoleBg);
217 if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) {
220 if (row == ROW_SCORE) {
221 par = course->getPar(col).toInt();
222 shots = score->getScore(col).toInt();
225 par = course->getPar(col + 9).toInt();
226 shots = score->getScore(col + 9).toInt();
229 if (col == 10 && row == ROW_SCORE_2) {
231 QBrush brush(colorTotal);
236 QBrush brush(colorSubTotal);
242 QBrush brush(colorPar);
245 if (shots == (par-1)) {
247 QBrush brush(colorBirdie);
250 if (shots == (par+1)) {
252 QBrush brush(colorBogey);
255 if (shots == (par+2)) {
257 QBrush brush(colorDoubleBogey);
260 if (shots > (par+2)) {
262 QBrush brush(colorBad);
272 if (role == Qt::FontRole) {
273 if (row == ROW_SCORE_2 && col == 10) {
282 if (role == Qt::DisplayRole) {
287 return QString("Out");
288 if (row == ROW_HOLE_2)
289 return QString("In");
292 if (score && course && row == ROW_PAR)
293 return course->getTotal(TotalOut);
294 if (score && course && row == ROW_PAR_2)
295 return course->getTotal(TotalIn);
298 if (score && row == ROW_SCORE)
299 return score->getTotal(TotalOut);
300 if (score && row == ROW_SCORE_2)
301 return score->getTotal(TotalIn);
304 else if (col == 10) {
306 if (row == ROW_HOLE_2)
307 return QString("Tot");
309 if (score && course && row == ROW_PAR_2)
310 return course->getTotal(Total);
311 if (score && row == ROW_SCORE_2)
312 return score->getTotal(Total);
323 return course->getPar(col);
326 return course->getPar(col + 9);
329 return course->getHcp(col);
332 return course->getHcp(col + 9);
335 return score->getScore(col);
338 return score->getScore(col + 9);
345 int ScoreTableModel::setItem(int row, int col, int data)
347 emit dataChanged(createIndex(row, col), createIndex(row, col));
351 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
353 // Only vertical header -- horizontal is hidden
354 if (orientation == Qt::Horizontal)
358 if (role == Qt::BackgroundRole) {
359 QColor colorHoleBg(Qt::darkGray);
360 QBrush brush(colorHoleBg);
364 if (role == Qt::DisplayRole) {
368 return QString("Par");
371 return QString("HCP");
374 return QString("Score");