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 COL_COUNT + 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();
139 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
141 if (!index.isValid())
144 if (!course || !score)
147 int row = index.row();
148 int col = index.column();
153 if (role == Qt::TextAlignmentRole ) {
154 return Qt::AlignCenter;
157 // Does this item belog to front or back nine
158 int offset = index.internalId() ? 9 : 0;
159 if (index.column() > 10)
165 QColor colorBirdie(Qt::yellow);
166 QColor colorPar(Qt::green);
167 QColor colorBogey(Qt::darkGreen);
168 QColor colorDoubleBogey(Qt::cyan);
169 QColor colorBad(Qt::white);
170 QColor colorSubTotal(Qt::lightGray);
171 QColor colorTotal(Qt::gray);
173 if (role == Qt::BackgroundRole) {
174 int par = (course->getPar(index.column() + offset)).toInt();
175 int shots = (score->getScore(index.column() + offset)).toInt();
177 if (index.row() == ROW_SCORE) {
178 if (index.column() == 10 && offset == 9) {
180 QBrush brush(colorTotal);
181 //brush.setStyle(Qt::Dense6Pattern);
184 if (col == 9 && row == 2) {
186 QBrush brush(colorSubTotal);
187 //brush.setStyle(Qt::Dense7Pattern);
190 if (index.column() < 9) {
193 QBrush brush(colorPar);
196 if (shots == (par-1)) {
198 QBrush brush(colorBirdie);
201 if (shots == (par+1)) {
203 QBrush brush(colorBogey);
206 if (shots == (par+2)) {
208 QBrush brush(colorDoubleBogey);
218 if (role == Qt::FontRole) {
219 if (index.row() == ROW_SCORE) {
220 if (index.column() == 10 && offset == 9) {
230 if (role == Qt::DisplayRole) {
232 if (index.column() == 9) {
233 if (index.row() == ROW_PAR)
235 return course->getTotal(TotalOut);
237 return course->getTotal(TotalIn);
238 else if (index.row() == ROW_SCORE) {
240 return score->getTotal(TotalOut);
242 return score->getTotal(TotalIn);
249 if (index.column() == 10) {
250 if (index.row() == ROW_PAR && offset == 9)
251 return course->getTotal(Total);
252 else if (index.row() == ROW_SCORE && offset == 9)
253 return score->getTotal(Total);
258 //qDebug() << "data() " << index << "/" << offset;
259 switch(index.row()) {
261 return course->getPar(index.column() + offset);
263 return course->getHcp(index.column() + offset);
265 return score->getScore(index.column() + offset);
271 int ScoreTableModel::setItem(int row, int col, int data)
273 emit dataChanged(createIndex(row, col), createIndex(row, col));
277 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
279 if (role != Qt::DisplayRole)
282 // TODO: how to diff between the two table views (no index?)
284 if (orientation == Qt::Horizontal)
285 if (section >= 0 && section <= 8)
286 return QString("%1").arg(section+1);
287 else if (section == 9)
288 return QString(""); // was: I/O
290 return QString(""); // was: Tot
294 return QString("Par");
296 return QString("HCP");
298 return QString("Score");