5 #include "table-model.h"
7 Qt::ItemFlags ScoreTableModel::flags ( const QModelIndex & index )
9 return Qt::NoItemFlags;
12 // Assign the 'sList' to internal 'scoreList'. Set the current score
13 // to 'currentScore', or to 's'.
14 void ScoreTableModel::setScore(QList<Score *> &sList, Score *s)
17 if (scoreList.size() > 0) {
19 currentScore = scoreList.indexOf(s);
20 if (currentScore == -1)
23 score = scoreList.at(currentScore); // NOTE: assumes non-empty list
27 void ScoreTableModel::setClub(QList<Club *> &cList)
31 if (clubList.size() > 0)
32 club = clubList.at(0);
35 course = club->getCourse(0);
38 QString ScoreTableModel::getInfoText()
43 str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
48 QString ScoreTableModel::getCountText()
50 QString str = QString("%1/%2").arg(currentScore+1, 2).arg(scoreList.count(), 2);
54 Course *ScoreTableModel::findCourse(const QString &clubName,
55 const QString &courseName)
57 QListIterator<Club *> i(clubList);
62 if (c->getName() == clubName) {
63 return c->getCourse(courseName);
69 void ScoreTableModel::first()
71 if (score && course) {
73 score = scoreList.at(currentScore);
74 course = findCourse(score->getClubName(), score->getCourseName());
75 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
79 void ScoreTableModel::last()
81 if (score && course) {
82 currentScore = scoreList.size() - 1;
83 score = scoreList.at(currentScore);
84 course = findCourse(score->getClubName(), score->getCourseName());
85 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
89 void ScoreTableModel::next()
91 if (score && course) {
92 if (currentScore < (scoreList.size() - 1)) {
94 score = scoreList.at(currentScore);
95 course = findCourse(score->getClubName(), score->getCourseName());
96 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
101 void ScoreTableModel::prev()
103 if (score && course) {
104 if (currentScore > 0) {
106 score = scoreList.at(currentScore);
107 course = findCourse(score->getClubName(), score->getCourseName());
108 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
113 int ScoreTableModel::rowCount(const QModelIndex & parent) const
118 int ScoreTableModel::columnCount(const QModelIndex & parent) const
120 return 9 + 2; // 2 for in/out and tot columns
123 QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &parent) const
125 if (hasIndex(row, column, parent)) {
126 int flag = (parent.column() > 0) ? parent.column() : 0;
127 //qDebug() << "index() " << row << "/" << column << "/ flag: " << flag <<"//" << parent;
128 return createIndex(row, column, flag);
131 return QModelIndex();
135 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
137 if (!index.isValid())
140 if (!course || !score)
143 int row = index.row();
144 int col = index.column();
149 if (role == Qt::TextAlignmentRole ) {
150 return Qt::AlignCenter;
153 if (index.column() > 10)
159 QColor colorHoleBg(Qt::black);
160 QColor colorHoleFg(Qt::white);
161 QColor colorBirdie(Qt::yellow);
162 QColor colorPar(Qt::green);
163 QColor colorBogey(Qt::darkGreen);
164 QColor colorDoubleBogey(Qt::cyan);
165 QColor colorBad(Qt::white);
166 QColor colorSubTotal(Qt::lightGray);
167 QColor colorTotal(Qt::gray);
169 if (role == Qt::ForegroundRole) {
170 if (row == ROW_HOLE || row == ROW_HOLE_2) {
171 QBrush brush(colorHoleFg);
175 if (role == Qt::BackgroundRole) {
176 int par = course->getPar(col).toInt();
177 int shots = score->getScore(col).toInt();
180 if (row == ROW_HOLE || row == ROW_HOLE_2) {
181 QBrush brush(colorHoleBg);
185 if (row == ROW_SCORE || row == ROW_SCORE_2) {
186 if (col == 10 && row == ROW_SCORE_2) {
188 QBrush brush(colorTotal);
193 QBrush brush(colorSubTotal);
199 QBrush brush(colorPar);
202 if (shots == (par-1)) {
204 QBrush brush(colorBirdie);
207 if (shots == (par+1)) {
209 QBrush brush(colorBogey);
212 if (shots == (par+2)) {
214 QBrush brush(colorDoubleBogey);
224 if (role == Qt::FontRole) {
225 if (row == ROW_SCORE_2 && col == 10) {
234 if (role == Qt::DisplayRole) {
239 return QString("Out");
240 if (row == ROW_HOLE_2)
241 return QString("In");
245 return course->getTotal(TotalOut);
246 if (row == ROW_PAR_2)
247 return course->getTotal(TotalIn);
250 if (row == ROW_SCORE)
251 return score->getTotal(TotalOut);
252 if (row == ROW_SCORE_2)
253 return score->getTotal(TotalIn);
256 else if (col == 10) {
258 if (row == ROW_HOLE_2)
259 return QString("Tot");
261 if (row == ROW_PAR_2)
262 return course->getTotal(Total);
263 if (row == ROW_SCORE_2)
264 return score->getTotal(Total);
274 return course->getPar(col);
276 return course->getPar(col + 9);
278 return course->getHcp(col);
280 return course->getHcp(col + 9);
282 return score->getScore(col);
284 return score->getScore(col + 9);
291 int ScoreTableModel::setItem(int row, int col, int data)
293 emit dataChanged(createIndex(row, col), createIndex(row, col));
297 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
299 if (role != Qt::DisplayRole)
302 // TODO: how to diff between the two table views (no index?)
304 if (orientation == Qt::Horizontal)
305 if (section >= 0 && section <= 8)
306 return QString("%1").arg(section+1);
307 else if (section == 9)
308 return QString(""); // was: I/O
310 return QString(""); // was: Tot
315 return QString("Par");
318 return QString("HCP");
321 return QString("Score");