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 if (scoreList.size() > 0)
15 score = scoreList.at(currentScore); // NOTE: assumes non-empty list
18 void ScoreTableModel::setClub(QList<Club *> &cList)
22 if (clubList.size() > 0)
23 club = clubList.at(0);
26 course = club->getCourse(0);
29 QString ScoreTableModel::getInfoText()
34 str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
39 QString ScoreTableModel::getCountText()
41 QString str = QString("%1/%2").arg(currentScore+1, 2).arg(scoreList.count(), 2);
45 Course *ScoreTableModel::findCourse(QString &clubName, QString &courseName)
47 QListIterator<Club *> i(clubList);
52 if (c->getName() == clubName) {
53 return c->getCourse(courseName);
59 void ScoreTableModel::first()
61 if (score && course) {
63 score = scoreList.at(currentScore);
64 course = findCourse(score->getClubName(), score->getCourseName());
65 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
69 void ScoreTableModel::last()
71 if (score && course) {
72 currentScore = scoreList.size() - 1;
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::next()
81 if (score && course) {
82 if (currentScore < (scoreList.size() - 1)) {
84 score = scoreList.at(currentScore);
85 course = findCourse(score->getClubName(), score->getCourseName());
86 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
91 void ScoreTableModel::prev()
93 if (score && course) {
94 if (currentScore > 0) {
96 score = scoreList.at(currentScore);
97 course = findCourse(score->getClubName(), score->getCourseName());
98 emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
103 int ScoreTableModel::rowCount(const QModelIndex & parent) const
108 int ScoreTableModel::columnCount(const QModelIndex & parent) const
110 return COL_COUNT + 2; // 2 for in/out and tot columns
113 QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &parent) const
115 if (hasIndex(row, column, parent)) {
116 int flag = (parent.column() > 0) ? parent.column() : 0;
117 //qDebug() << "index() " << row << "/" << column << "/ flag: " << flag <<"//" << parent;
118 return createIndex(row, column, flag);
121 return QModelIndex();
129 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
131 if (!index.isValid())
134 if (!course || !score)
137 int row = index.row();
138 int col = index.column();
143 if (role == Qt::TextAlignmentRole ) {
144 return Qt::AlignCenter;
147 // Does this item belog to front or back nine
148 int offset = index.internalId() ? 9 : 0;
149 if (index.column() > 10)
155 if (role == Qt::BackgroundRole) {
156 int par = (course->getPar(index.column() + offset)).toInt();
157 int shots = (score->getScore(index.column() + offset)).toInt();
159 if (index.row() == ROW_SCORE) {
160 if (index.column() == 10 && offset == 9) {
162 QColor color(Qt::blue);
166 if (col == 9 && row == 2) {
168 QColor color(Qt::blue);
172 if (index.column() < 9) {
175 QColor color(Qt::yellow);
179 if (shots == (par-1)) {
181 QColor color(Qt::green);
185 if (shots == (par+1)) {
187 QColor color(Qt::red);
200 if (role == Qt::DisplayRole) {
202 if (index.column() == 9) {
203 if (index.row() == ROW_PAR)
205 return course->getTotal(TotalOut);
207 return course->getTotal(TotalIn);
208 else if (index.row() == ROW_SCORE) {
210 return score->getTotal(TotalOut);
212 return score->getTotal(TotalIn);
219 if (index.column() == 10) {
220 if (index.row() == ROW_PAR && offset == 9)
221 return course->getTotal(Total);
222 else if (index.row() == ROW_SCORE && offset == 9)
223 return score->getTotal(Total);
228 //qDebug() << "data() " << index << "/" << offset;
229 switch(index.row()) {
231 return course->getPar(index.column() + offset);
233 return course->getHcp(index.column() + offset);
235 return score->getScore(index.column() + offset);
241 int ScoreTableModel::setItem(int row, int col, int data)
243 emit dataChanged(createIndex(row, col), createIndex(row, col));
247 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
249 if (role != Qt::DisplayRole)
252 // TODO: how to diff between the two table views (no index?)
254 if (orientation == Qt::Horizontal)
255 if (section >= 0 && section <= 8)
256 return QString("%1").arg(section+1);
257 else if (section == 9)
258 return QString("I/O");
260 return QString("Tot");
264 return QString("Par");
266 return QString("HCP");
268 return QString("Score");