Merge branch 'master' of /opt/src/sb1/qt/scorecard
[scorecard] / src / table-model.cpp
index 077ca02..8cadfd2 100644 (file)
+/*
+ * 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 <QColor>
 #include <QBrush>
 #include <QFont>
 #include "table-model.h"
+#include "score-common.h"
 
-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<Score *> &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<Club *> &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)
-{
-  QString str("");
-
-  if (club)
-    str = club->getName();
-
-  return str;
-}
-
-QString& ScoreTableModel::courseName(void)
-{
-  QString str("");
-
-  if (course)
-    str = course->getName();
-
-  return str;
-}
-
-Course *ScoreTableModel::findCourse(const QString &clubName, 
-                                   const QString &courseName)
-{
-  QListIterator<Club *> 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));
-    }
-  }
-}
+QString empty("");
 
-void ScoreTableModel::prev()
+ScoreTableModel::ScoreTableModel(Score * s, Course * c, QObject *parent) 
+    : QAbstractTableModel(parent)
 {
-  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
-{
-  return 9 + 2; // 2 for in/out and tot columns
-}
-
-QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &parent) const
+int ScoreTableModel::columnCount(const QModelIndex &) const
 {
-  if (hasIndex(row, column, parent)) {
-    int flag = (parent.column() > 0) ? parent.column() : 0;
-    //qDebug() << "index() " << row << "/" << column << "/ flag: " << flag <<"//" << parent;
-    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
-  QColor colorHoleBg(Qt::black);
-  QColor colorHoleFg(Qt::white);
-  QColor colorBirdie(Qt::yellow);
-  QColor colorPar(Qt::green);
-  QColor colorBogey(Qt::darkGreen);
-  QColor colorDoubleBogey(Qt::cyan);
-  QColor colorBad(Qt::white);
-  QColor colorSubTotal(Qt::lightGray);
-  QColor colorTotal(Qt::gray);
-
   if (!index.isValid())
     return QVariant();
 
@@ -210,18 +53,12 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
   // COLORS
   //
   if (role == Qt::ForegroundRole) {
-    if (row == ROW_HOLE || row == ROW_HOLE_2) {
-       QBrush brush(colorHoleFg);
-       return brush;
-    }
-  }
-  if (role == Qt::BackgroundRole) {
-    // Hole numbers 1-18
-    if (row == ROW_HOLE || row == ROW_HOLE_2) {
-       QBrush brush(colorHoleBg);
-       return brush;
+    // 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;
     }
-
     if (score && course && (row == ROW_SCORE || row == ROW_SCORE_2)) {
       int par;
       int shots;
@@ -234,35 +71,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(ScoreColor::bad());
          return brush;
        }
       }
@@ -273,18 +115,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");
@@ -304,7 +151,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");
@@ -345,39 +192,162 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
   return QVariant();
 }
 
-int ScoreTableModel::setItem(int row, int col, int data)
+QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
 {
-  emit dataChanged(createIndex(row, col), createIndex(row, col));
-  return 1;
+  // 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_SCORE: 
+    case ROW_SCORE_2: 
+      return QString("Score");
+    }
+    return QVariant();
+  }
+
+  return QVariant();
 }
 
-QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
+//
+// CourseTableModel
+//
+CourseTableModel::CourseTableModel(Course * c, QObject *parent) 
+    : QAbstractTableModel(parent), course(c)
 {
-  if (role != Qt::DisplayRole)
-         return QVariant();
+}
 
-    // TODO: how to diff between the two table views (no index?)
+int CourseTableModel::rowCount(const QModelIndex &) const
+{
+    return ROWS;
+}
+int CourseTableModel::columnCount(const QModelIndex &) const
+{
+    return COLS + 2;
+}
 
-    if (orientation == Qt::Horizontal)
-      if (section >= 0 && section <= 8)
-       return QString("%1").arg(section+1);
-      else if (section == 9)
-       return QString(""); // was: I/O
-      else
-       return QString(""); // was: Tot
-    else {
-      switch(section) {
-      case ROW_PAR: 
-      case ROW_PAR_2: 
-       return QString("Par");
-      case ROW_HCP: 
-      case ROW_HCP_2: 
-       return QString("HCP");
-      case ROW_SCORE: 
-      case ROW_SCORE_2: 
-       return QString("Score");
+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();
+  }
+  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();
+}