X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Ftable-model.cpp;h=30d2909d8ca4d3d4e10ff99c3eeef85cc3b023a5;hb=ca97d376ad8ea6345638e9988b4a6ec473bb29d5;hp=529b43d97636c6d3674714a0c85d4decd89079dc;hpb=ee2b485238302c6a7dab52bfbc128a50d1a5d2f9;p=scorecard diff --git a/src/table-model.cpp b/src/table-model.cpp index 529b43d..30d2909 100644 --- a/src/table-model.cpp +++ b/src/table-model.cpp @@ -1,193 +1,44 @@ +/* + * Copyright (C) 2009 Sakari Poussa + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2. + */ #include #include #include #include "table-model.h" +#include "score-common.h" QString empty(""); -QColor colorHoleBg(Qt::darkGray); -QColor colorHoleFg(Qt::yellow); -QColor colorBirdie(102, 102, 255); -QColor colorPar(Qt::green); -QColor colorBogey(Qt::darkGreen); -QColor colorDoubleBogey(Qt::red); -QColor colorBad(Qt::red); -QColor colorSubTotal(Qt::black); -QColor colorTotal(Qt::black); - -Qt::ItemFlags ScoreTableModel::flags (const QModelIndex & index) -{ - return 0; -} - -void ScoreTableModel::setMode(int m) -{ - currentMode = m; -} - -int ScoreTableModel::mode(void) -{ - return currentMode; -} - -// Assign the 'sList' to internal 'scoreList'. Set the current score -// to 'currentScore', or to 's'. -void ScoreTableModel::setScore(QList &sList, Score *s) -{ - scoreList = sList; - if (scoreList.size() > 0) { - if (s) { - currentScore = scoreList.indexOf(s); - if (currentScore == -1) - currentScore = 0; - } - score = scoreList.at(currentScore); // NOTE: assumes non-empty list - } -} - -void ScoreTableModel::setClub(QList &cList) -{ - clubList = cList; - - if (clubList.size() > 0) - club = clubList.at(0); - - if (club) - course = club->getCourse(0); -} - -QString ScoreTableModel::getInfoText() -{ - QString str(""); - - if (score) - str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count()); - - return str; -} - -QString ScoreTableModel::getCountText() -{ - QString str = QString("%1/%2").arg(currentScore+1, 2).arg(scoreList.count(), 2); - return str; -} - -QString& ScoreTableModel::clubName(void) +ScoreTableModel::ScoreTableModel(QObject *parent) + : QAbstractTableModel(parent) { - if (club) - return club->getName(); - - return empty; + score = 0; + course = 0; } -QString& ScoreTableModel::courseName(void) +void ScoreTableModel::set(Score * s, Course * c) { - if (course) - return course->getName(); - - return empty; -} - -Course *ScoreTableModel::findCourse(const QString &clubName, - const QString &courseName) -{ - QListIterator i(clubList); - Club *c; - - while (i.hasNext()) { - c = i.next(); - if (c->getName() == clubName) { - return c->getCourse(courseName); - } - } - return 0; -} - -Club *ScoreTableModel::getClub(void) -{ - return club; -} - -Course *ScoreTableModel::getCourse(void) -{ - return course; -} - -Score *ScoreTableModel::getScore(void) -{ - return score; -} - -void ScoreTableModel::first() -{ - if (score && course) { - currentScore = 0; - score = scoreList.at(currentScore); - course = findCourse(score->getClubName(), score->getCourseName()); - emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1)); - } -} - -void ScoreTableModel::last() -{ - if (score && course) { - currentScore = scoreList.size() - 1; - score = scoreList.at(currentScore); - course = findCourse(score->getClubName(), score->getCourseName()); - emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1)); - } -} - -void ScoreTableModel::next() -{ - if (score && course) { - if (currentScore < (scoreList.size() - 1)) { - currentScore++; - score = scoreList.at(currentScore); - course = findCourse(score->getClubName(), score->getCourseName()); - emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1)); - } - } -} - -void ScoreTableModel::prev() -{ - if (score && course) { - if (currentScore > 0) { - currentScore--; - score = scoreList.at(currentScore); - course = findCourse(score->getClubName(), score->getCourseName()); - emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1)); - } - } + score = s; + course = c; } -int ScoreTableModel::rowCount(const QModelIndex & parent) const +int ScoreTableModel::rowCount(const QModelIndex &) const { - return 8; + return ROWS; } -int ScoreTableModel::columnCount(const QModelIndex & parent) const +int ScoreTableModel::columnCount(const QModelIndex &) const { - return 9 + 2; // 2 for in/out and tot columns -} - -QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &parent) const -{ - if (hasIndex(row, column, parent)) { - int flag = (parent.column() > 0) ? parent.column() : 0; - return createIndex(row, column, flag); - } - else { - return QModelIndex(); - } + return COLS + 2; // 2 for in/out and tot columns } QVariant ScoreTableModel::data(const QModelIndex &index, int role) const { - // TODO: move away from the stack - if (!index.isValid()) return QVariant(); @@ -207,12 +58,12 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const // // COLORS // - if (role == Qt::BackgroundRole) { + if (role == Qt::ForegroundRole) { // Hole numbers 1-18. All hole nums, in, out and tot cell but not // the empty cell. if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) { - QBrush brush(colorHoleBg); - return brush; + QBrush brush(ScoreColor::holeBg()); + return brush; } if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) { int par; @@ -226,40 +77,40 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const shots = score->getScore(col + 9).toInt(); } - if (col == 10 && row == ROW_SCORE_2) { + if (col == (COLS+1) && row == ROW_SCORE_2) { // Total score - QBrush brush(colorTotal); + QBrush brush(ScoreColor::total()); return brush; } - if (col == 9) { + if (col == COLS) { // In and Out scores - QBrush brush(colorSubTotal); + QBrush brush(ScoreColor::subTotal()); return brush; } - if (col < 9) { + if (col < COLS) { if (shots == par) { // Par - QBrush brush(colorPar); + QBrush brush(ScoreColor::par()); return brush; } if (shots == (par-1)) { // Birdie - QBrush brush(colorBirdie); + QBrush brush(ScoreColor::birdie()); return brush; } if (shots == (par+1)) { // Bogey - QBrush brush(colorBogey); + QBrush brush(ScoreColor::bogey()); return brush; } if (shots == (par+2)) { // Double Bogey - QBrush brush(colorDoubleBogey); + QBrush brush(ScoreColor::doubleBogey()); return brush; } if (shots > (par+2)) { // Very bad - QBrush brush(colorBad); + QBrush brush(ScoreColor::bad()); return brush; } } @@ -270,18 +121,23 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const // FONT // if (role == Qt::FontRole) { - if (row == ROW_SCORE_2 && col == 10) { - QFont font; - font.setBold(true); - return font; - } + if (row == ROW_HOLE || row == ROW_HOLE_2) { + QFont font; + font.setBold(true); + return font; + } + if (row == ROW_SCORE || row == ROW_SCORE_2) { + QFont font; + font.setBold(true); + return font; + } } // - // DATA + // NUMBERS // if (role == Qt::DisplayRole) { - if (col == 9) { + if (col == COLS) { // In/out label if (row == ROW_HOLE) return QString("Out"); @@ -301,7 +157,7 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const return score->getTotal(TotalIn); } - else if (col == 10) { + else if (col == (COLS+1)) { // Total label if (row == ROW_HOLE_2) return QString("Tot"); @@ -342,27 +198,17 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const return QVariant(); } -int ScoreTableModel::setItem(int row, int col, int data) -{ - emit dataChanged(createIndex(row, col), createIndex(row, col)); - return 1; -} - QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const { // Only vertical header -- horizontal is hidden if (orientation == Qt::Horizontal) return QVariant(); -#if 1 - if (role == Qt::BackgroundRole) { - QColor colorHoleBg(Qt::darkGray); - QBrush brush(colorHoleBg); - return brush; - } -#endif if (role == Qt::DisplayRole) { switch(section) { + case ROW_HOLE: + case ROW_HOLE_2: + return QString("Hole"); case ROW_PAR: case ROW_PAR_2: return QString("Par"); @@ -379,3 +225,141 @@ QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, i return QVariant(); } +// +// CourseTableModel +// +CourseTableModel::CourseTableModel(QObject *parent) + : QAbstractTableModel(parent) +{ + course = 0; +} + +void CourseTableModel::set(Course *c) +{ + course = c; +} + +int CourseTableModel::rowCount(const QModelIndex &) const +{ + return ROWS; +} + +int CourseTableModel::columnCount(const QModelIndex &) const +{ + return COLS + 2; +} + +QVariant CourseTableModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + int row = index.row(); + int col = index.column(); + + // + // ALIGNMENT + // + if (role == Qt::TextAlignmentRole ) { + return Qt::AlignCenter; + } + + // + // FONT + // + if (role == Qt::FontRole) { + if (row == ROW_HOLE || row == ROW_HOLE_2) { + QFont font; + font.setBold(true); + return font; + } + } + // + // COLORS + // + if (role == Qt::ForegroundRole) { + // Hole numbers 1-18. All hole nums, in, out and tot cell but not + // the empty cell. + if ((row == ROW_HOLE && col != 10) || row == ROW_HOLE_2) { + QBrush brush(ScoreColor::holeBg()); + return brush; + } + return QVariant(); + } + // + // NUMBERS + // + if (role == Qt::DisplayRole) { + + if (col == COLS) { + // In/out label + if (row == ROW_HOLE) + return QString("Out"); + if (row == ROW_HOLE_2) + return QString("In"); + + // In/Out for par + if (course && row == ROW_PAR) + return course->getTotal(TotalOut); + if (course && row == ROW_PAR_2) + return course->getTotal(TotalIn); + } + else if (col == (COLS+1)) { + // Total label + if (row == ROW_HOLE_2) + return QString("Tot"); + // Total score + if (course && row == ROW_PAR_2) + return course->getTotal(Total); + } + else { + // data cells + switch(row) { + case ROW_HOLE: + return col + 1; + case ROW_HOLE_2: + return col + 10; + case ROW_PAR: + if (course) + return course->getPar(col); + case ROW_PAR_2: + if (course) + return course->getPar(col + 9); + case ROW_HCP: + if (course) + return course->getHcp(col); + case ROW_HCP_2: + if (course) + return course->getHcp(col + 9); + } + } + } + return QVariant(); +} + +QVariant CourseTableModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + // Only vertical header -- horizontal is hidden + if (orientation == Qt::Horizontal) + return QVariant(); + + if (role == Qt::DisplayRole) { + switch(section) { + case ROW_HOLE: + case ROW_HOLE_2: + return QString("Hole"); + case ROW_PAR: + case ROW_PAR_2: + return QString("Par"); + case ROW_HCP: + case ROW_HCP_2: + return QString("HCP"); + case ROW_LEN: + case ROW_LEN_2: + return QString("Len"); + } + return QVariant(); + } + + return QVariant(); +}