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::darkGray);
183 QColor colorHoleFg(Qt::yellow);
184 QColor colorBirdie(Qt::yellow);
185 QColor colorPar(Qt::green);
186 QColor colorBogey(Qt::darkGreen);
187 QColor colorDoubleBogey(Qt::red);
188 QColor colorBad(Qt::red);
189 QColor colorSubTotal(Qt::black);
190 QColor colorTotal(Qt::black);
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::BackgroundRole) {
213 if (row == ROW_HOLE || 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");