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 //if (role == Qt::ForegroundRole) {
157 int par = (course->getPar(index.column() + offset)).toInt();
158 int shots = (score->getScore(index.column() + offset)).toInt();
160 if (index.row() == ROW_SCORE) {
161 if (index.column() == 10 && offset == 9) {
163 QColor color(Qt::blue);
165 brush.setStyle(Qt::Dense6Pattern);
168 if (col == 9 && row == 2) {
170 QColor color(Qt::blue);
172 brush.setStyle(Qt::Dense7Pattern);
175 if (index.column() < 9) {
178 QColor color(Qt::yellow);
182 if (shots == (par-1)) {
184 QColor color(Qt::green);
188 if (shots == (par+1)) {
190 QColor color(Qt::red);
203 if (role == Qt::DisplayRole) {
205 if (index.column() == 9) {
206 if (index.row() == ROW_PAR)
208 return course->getTotal(TotalOut);
210 return course->getTotal(TotalIn);
211 else if (index.row() == ROW_SCORE) {
213 return score->getTotal(TotalOut);
215 return score->getTotal(TotalIn);
222 if (index.column() == 10) {
223 if (index.row() == ROW_PAR && offset == 9)
224 return course->getTotal(Total);
225 else if (index.row() == ROW_SCORE && offset == 9)
226 return score->getTotal(Total);
231 //qDebug() << "data() " << index << "/" << offset;
232 switch(index.row()) {
234 return course->getPar(index.column() + offset);
236 return course->getHcp(index.column() + offset);
238 return score->getScore(index.column() + offset);
244 int ScoreTableModel::setItem(int row, int col, int data)
246 emit dataChanged(createIndex(row, col), createIndex(row, col));
250 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
252 if (role != Qt::DisplayRole)
255 // TODO: how to diff between the two table views (no index?)
257 if (orientation == Qt::Horizontal)
258 if (section >= 0 && section <= 8)
259 return QString("%1").arg(section+1);
260 else if (section == 9)
261 return QString(""); // was: I/O
263 return QString(""); // was: Tot
267 return QString("Par");
269 return QString("HCP");
271 return QString("Score");