5 #include "table-model.h"
7 Qt::ItemFlags ScoreTableModel::flags ( const QModelIndex & index )
9 Qt::ItemFlags flags = QAbstractItemModel::flags(index);
10 qDebug() << "flags " << currentMode;
15 void ScoreTableModel::setMode(int m)
20 int ScoreTableModel::mode(void)
25 // Assign the 'sList' to internal 'scoreList'. Set the current score
26 // to 'currentScore', or to 's'.
27 void ScoreTableModel::setScore(QList<Score *> &sList, Score *s)
30 if (scoreList.size() > 0) {
32 currentScore = scoreList.indexOf(s);
33 if (currentScore == -1)
36 score = scoreList.at(currentScore); // NOTE: assumes non-empty list
40 void ScoreTableModel::setClub(QList<Club *> &cList)
44 if (clubList.size() > 0)
45 club = clubList.at(0);
48 course = club->getCourse(0);
51 QString ScoreTableModel::getInfoText()
56 str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
61 QString ScoreTableModel::getCountText()
63 QString str = QString("%1/%2").arg(currentScore+1, 2).arg(scoreList.count(), 2);
67 QString& ScoreTableModel::clubName(void)
72 str = club->getName();
77 QString& ScoreTableModel::courseName(void)
82 str = course->getName();
87 Course *ScoreTableModel::findCourse(const QString &clubName,
88 const QString &courseName)
90 QListIterator<Club *> i(clubList);
95 if (c->getName() == clubName) {
96 return c->getCourse(courseName);
102 Club *ScoreTableModel::getClub(void)
107 Course *ScoreTableModel::getCourse(void)
112 Score *ScoreTableModel::getScore(void)
117 void ScoreTableModel::first()
119 if (score && course) {
121 score = scoreList.at(currentScore);
122 course = findCourse(score->getClubName(), score->getCourseName());
123 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
127 void ScoreTableModel::last()
129 if (score && course) {
130 currentScore = scoreList.size() - 1;
131 score = scoreList.at(currentScore);
132 course = findCourse(score->getClubName(), score->getCourseName());
133 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
137 void ScoreTableModel::next()
139 if (score && course) {
140 if (currentScore < (scoreList.size() - 1)) {
142 score = scoreList.at(currentScore);
143 course = findCourse(score->getClubName(), score->getCourseName());
144 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
149 void ScoreTableModel::prev()
151 if (score && course) {
152 if (currentScore > 0) {
154 score = scoreList.at(currentScore);
155 course = findCourse(score->getClubName(), score->getCourseName());
156 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
161 int ScoreTableModel::rowCount(const QModelIndex & parent) const
166 int ScoreTableModel::columnCount(const QModelIndex & parent) const
168 return 9 + 2; // 2 for in/out and tot columns
171 QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &parent) const
173 if (hasIndex(row, column, parent)) {
174 int flag = (parent.column() > 0) ? parent.column() : 0;
175 //qDebug() << "index() " << row << "/" << column << "/ flag: " << flag <<"//" << parent;
176 return createIndex(row, column, flag);
179 return QModelIndex();
183 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
185 if (!index.isValid())
188 if (!course || !score)
191 int row = index.row();
192 int col = index.column();
197 if (role == Qt::TextAlignmentRole ) {
198 return Qt::AlignCenter;
201 if (index.column() > 10)
207 QColor colorHoleBg(Qt::black);
208 QColor colorHoleFg(Qt::white);
209 QColor colorBirdie(Qt::yellow);
210 QColor colorPar(Qt::green);
211 QColor colorBogey(Qt::darkGreen);
212 QColor colorDoubleBogey(Qt::cyan);
213 QColor colorBad(Qt::white);
214 QColor colorSubTotal(Qt::lightGray);
215 QColor colorTotal(Qt::gray);
217 if (role == Qt::ForegroundRole) {
218 if (row == ROW_HOLE || row == ROW_HOLE_2) {
219 QBrush brush(colorHoleFg);
223 if (role == Qt::BackgroundRole) {
224 int par = course->getPar(col).toInt();
225 int shots = score->getScore(col).toInt();
228 if (row == ROW_HOLE || row == ROW_HOLE_2) {
229 QBrush brush(colorHoleBg);
233 if (row == ROW_SCORE || row == ROW_SCORE_2) {
234 if (col == 10 && row == ROW_SCORE_2) {
236 QBrush brush(colorTotal);
241 QBrush brush(colorSubTotal);
247 QBrush brush(colorPar);
250 if (shots == (par-1)) {
252 QBrush brush(colorBirdie);
255 if (shots == (par+1)) {
257 QBrush brush(colorBogey);
260 if (shots == (par+2)) {
262 QBrush brush(colorDoubleBogey);
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");
293 return course->getTotal(TotalOut);
294 if (row == ROW_PAR_2)
295 return course->getTotal(TotalIn);
298 if (row == ROW_SCORE)
299 return score->getTotal(TotalOut);
300 if (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 (row == ROW_PAR_2)
310 return course->getTotal(Total);
311 if (row == ROW_SCORE_2)
312 return score->getTotal(Total);
322 return course->getPar(col);
324 return course->getPar(col + 9);
326 return course->getHcp(col);
328 return course->getHcp(col + 9);
330 return score->getScore(col);
332 return score->getScore(col + 9);
339 int ScoreTableModel::setItem(int row, int col, int data)
341 emit dataChanged(createIndex(row, col), createIndex(row, col));
345 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
347 if (role != Qt::DisplayRole)
350 // TODO: how to diff between the two table views (no index?)
352 if (orientation == Qt::Horizontal)
353 if (section >= 0 && section <= 8)
354 return QString("%1").arg(section+1);
355 else if (section == 9)
356 return QString(""); // was: I/O
358 return QString(""); // was: Tot
363 return QString("Par");
366 return QString("HCP");
369 return QString("Score");