4 #include "table-model.h"
6 Qt::ItemFlags ScoreTableModel::flags ( const QModelIndex & index )
8 return Qt::NoItemFlags;
11 void ScoreTableModel::setScore(QList<Score *> &sList)
14 score = scoreList.at(currentScore); // NOTE: assumes non-empty list
17 void ScoreTableModel::setClub(QList<Club *> &cList)
20 club = clubList.at(0);
21 course = club->getCourse(0);
24 QString ScoreTableModel::getInfoText()
26 QString str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
30 QString ScoreTableModel::getCountText()
32 QString str = QString("%1/%2").arg(currentScore+1, 2).arg(scoreList.count(), 2);
36 Course *ScoreTableModel::findCourse(QString &clubName, QString &courseName)
38 QListIterator<Club *> i(clubList);
43 if (c->getName() == clubName) {
44 return c->getCourse(courseName);
50 void ScoreTableModel::first()
53 score = scoreList.at(currentScore);
54 course = findCourse(score->getClubName(), score->getCourseName());
55 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
58 void ScoreTableModel::last()
60 currentScore = scoreList.size() - 1;
61 score = scoreList.at(currentScore);
62 course = findCourse(score->getClubName(), score->getCourseName());
63 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
66 void ScoreTableModel::next()
68 if (currentScore < (scoreList.size() - 1)) {
70 score = scoreList.at(currentScore);
71 course = findCourse(score->getClubName(), score->getCourseName());
72 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
76 void ScoreTableModel::prev()
78 if (currentScore > 0) {
80 score = scoreList.at(currentScore);
81 course = findCourse(score->getClubName(), score->getCourseName());
82 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
86 int ScoreTableModel::rowCount(const QModelIndex & parent) const
91 int ScoreTableModel::columnCount(const QModelIndex & parent) const
93 return COL_COUNT + 2; // 2 for in/out and tot columns
96 QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &parent) const
98 if (hasIndex(row, column, parent)) {
99 int flag = (parent.column() > 0) ? parent.column() : 0;
100 //qDebug() << "index() " << row << "/" << column << "/ flag: " << flag <<"//" << parent;
101 return createIndex(row, column, flag);
104 return QModelIndex();
112 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
114 if (!index.isValid())
117 int row = index.row();
118 int col = index.column();
123 if (role == Qt::TextAlignmentRole ) {
124 return Qt::AlignCenter;
127 // Does this item belog to front or back nine
128 int offset = index.internalId() ? 9 : 0;
129 if (index.column() > 10)
135 if (role == Qt::BackgroundRole) {
136 int par = (course->getPar(index.column() + offset)).toInt();
137 int shots = (score->getScore(index.column() + offset)).toInt();
139 if (index.row() == ROW_SCORE) {
140 if (index.column() == 10 && offset == 9) {
142 QColor color(Qt::blue);
146 if (col == 9 && row == 2) {
148 QColor color(Qt::blue);
152 if (index.column() < 9) {
155 QColor color(Qt::yellow);
159 if (shots == (par-1)) {
161 QColor color(Qt::green);
165 if (shots == (par+1)) {
167 QColor color(Qt::red);
180 if (role == Qt::DisplayRole) {
182 if (index.column() == 9) {
183 if (index.row() == ROW_PAR)
185 return course->getTotal(TotalOut);
187 return course->getTotal(TotalIn);
188 else if (index.row() == ROW_SCORE) {
190 return score->getTotal(TotalOut);
192 return score->getTotal(TotalIn);
199 if (index.column() == 10) {
200 if (index.row() == ROW_PAR && offset == 9)
201 return course->getTotal(Total);
202 else if (index.row() == ROW_SCORE && offset == 9)
203 return score->getTotal(Total);
208 //qDebug() << "data() " << index << "/" << offset;
209 switch(index.row()) {
211 return course->getPar(index.column() + offset);
213 return course->getHcp(index.column() + offset);
215 return score->getScore(index.column() + offset);
221 int ScoreTableModel::setItem(int row, int col, int data)
223 emit dataChanged(createIndex(row, col), createIndex(row, col));
227 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
229 if (role != Qt::DisplayRole)
232 // TODO: how to diff between the two table views (no index?)
234 if (orientation == Qt::Horizontal)
235 if (section >= 0 && section <= 8)
236 return QString("%1").arg(section+1);
237 else if (section == 9)
238 return QString("I/O");
240 return QString("Tot");
244 return QString("Par");
246 return QString("HCP");
248 return QString("Score");