Use common colors and style sheets for all windows and dialogs
[scorecard] / src / table-model.cpp
index c0cc2d7..6c490ed 100644 (file)
@@ -1,29 +1,67 @@
+/*
+ * 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 )
+QString empty("");
+
+Qt::ItemFlags ScoreTableModel::flags (const QModelIndex &)
+{
+  return 0;
+}
+
+void ScoreTableModel::setMode(int m)
 {
-  return Qt::NoItemFlags;
+  currentMode = m;
 }
 
-void ScoreTableModel::setScore(QList<Score *> &sList)
+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;
-  score = scoreList.at(currentScore); // NOTE: assumes non-empty list
+  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;
-  club = clubList.at(0);
-  course = club->getCourse(0);
+
+  if (clubList.size() > 0)
+    club = clubList.at(0);
+
+  if (club)
+    course = club->getCourse(0);
 }
 
 QString ScoreTableModel::getInfoText()
 {
-  QString str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
+  QString str("");
+
+  if (score)
+    str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
+
   return str;
 }
 
@@ -33,7 +71,24 @@ QString ScoreTableModel::getCountText()
   return str;
 }
 
-Course *ScoreTableModel::findCourse(QString &clubName, QString &courseName)
+QString& ScoreTableModel::clubName(void)
+{
+  if (club)
+    return club->getName();
+
+  return empty;
+}
+
+QString& ScoreTableModel::courseName(void)
+{
+  if (course)
+    return course->getName();
+
+  return empty;
+}
+
+Course *ScoreTableModel::findCourse(const QString &clubName, 
+                                   const QString &courseName)
 {
   QListIterator<Club *> i(clubList);
   Club *c;
@@ -47,57 +102,79 @@ Course *ScoreTableModel::findCourse(QString &clubName, QString &courseName)
   return 0;
 }
 
-void ScoreTableModel::first()
+Club *ScoreTableModel::getClub(void)
 {
-  currentScore = 0;
-  score = scoreList.at(currentScore);
-  course = findCourse(score->getClubName(), score->getCourseName());
-  emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
+  return club;
 }
 
-void ScoreTableModel::last()
+Course *ScoreTableModel::getCourse(void)
 {
-  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));
+  return course;
 }
 
-void ScoreTableModel::next()
+Score *ScoreTableModel::getScore(void)
+{
+  return score;
+}
+
+void ScoreTableModel::first()
 {
-  if (currentScore < (scoreList.size() - 1)) {
-    currentScore++;
+  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::prev()
+void ScoreTableModel::last()
 {
-  if (currentScore > 0) {
-    currentScore--;
+  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));
   }
 }
 
-int ScoreTableModel::rowCount(const QModelIndex & parent) const
+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()
 {
-  return ROW_COUNT;
+  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));
+    }
+  }
+}
+
+int ScoreTableModel::rowCount(const QModelIndex &) const
+{
+  return 8;
 }
  
-int ScoreTableModel::columnCount(const QModelIndex & parent) const
+int ScoreTableModel::columnCount(const QModelIndex &) const
 {
-  return COL_COUNT + 2; // 2 for in/out and tot columns
+  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;
-    //qDebug() << "index() " << row << "/" << column << "/ flag: " << flag <<"//" << parent;
     return createIndex(row, column, flag);
   }
   else {
@@ -105,12 +182,10 @@ QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &paren
   }
 }
 
-#define ROW_PAR   0
-#define ROW_HCP   1
-#define ROW_SCORE 2
-
 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
 {
+  // TODO: move away from the stack
+
   if (!index.isValid())
     return QVariant();
 
@@ -124,8 +199,6 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
     return Qt::AlignCenter;
   }
 
-  // Does this item belog to front or back nine
-  int offset = index.internalId() ? 9 : 0;
   if (index.column() > 10)
     return QVariant();
 
@@ -133,92 +206,141 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
   // COLORS
   //
   if (role == Qt::BackgroundRole) {
-    int par = (course->getPar(index.column() + offset)).toInt();
-    int shots = (score->getScore(index.column() + offset)).toInt();
+    // 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;
+      if (row == ROW_SCORE) {
+       par = course->getPar(col).toInt();
+       shots = score->getScore(col).toInt();
+      }
+      else {
+       par = course->getPar(col + 9).toInt();
+       shots = score->getScore(col + 9).toInt();
+      }
 
-    if (index.row() == ROW_SCORE) {
-      if (index.column() == 10 && offset == 9) {
+      if (col == (COLS+1) && row == ROW_SCORE_2) {
        // Total score
-       QColor color(Qt::blue);
-       QBrush brush(color);
+       QBrush brush(ScoreColor::total());
        return brush;
       }
-      if (col == 9 && row == 2) {
+      if (col == COLS) {
        // In and Out scores
-       QColor color(Qt::blue);
-       QBrush brush(color);
+       QBrush brush(ScoreColor::subTotal());
        return brush;
       }
-      if (index.column() < 9) {
+      if (col < COLS) {
        if (shots == par) {
          // Par
-         QColor color(Qt::yellow);
-         QBrush brush(color);
+         QBrush brush(ScoreColor::par());
          return brush;
        }
        if (shots == (par-1)) {
          // Birdie
-         QColor color(Qt::green);
-         QBrush brush(color);
+         QBrush brush(ScoreColor::birdie());
          return brush;
        }
        if (shots == (par+1)) {
          // Bogey
-         QColor color(Qt::red);
-         QBrush brush(color);
+         QBrush brush(ScoreColor::bogey());
+         return brush;
+       }
+       if (shots == (par+2)) {
+         // Double Bogey
+         QBrush brush(ScoreColor::doubleBogey());
+         return brush;
+       }
+       if (shots > (par+2)) {
+         // Very bad
+         QBrush brush(ScoreColor::bad());
          return brush;
        }
       }
     }
     return QVariant();
   }
-
-
   //
-  // DATA
+  // FONT
   //
-  if (role == Qt::DisplayRole) {
-    // In/Out column
-    if (index.column() == 9) {
-      if (index.row() == ROW_PAR)
-       if (offset == 0)
-         return course->getTotal(TotalOut);
-       else 
-         return course->getTotal(TotalIn);
-      else if (index.row() == ROW_SCORE) {
-       if (offset == 0)
-         return score->getTotal(TotalOut);
-       else 
-         return score->getTotal(TotalIn);
-      }
-      else
-       return QVariant();
+  if (role == Qt::FontRole) {
+    if (row == ROW_SCORE_2 && col == (COLS+1)) {
+       QFont font;
+       font.setBold(true);
+       return font;
     }
+  }
+  //
+  // 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");
 
-    // Tot column
-    if (index.column() == 10) {
-      if (index.row() == ROW_PAR && offset == 9)
+      // In/Out for par
+      if (score && course && row == ROW_PAR)
+       return course->getTotal(TotalOut);
+      if (score && course && row == ROW_PAR_2)
+       return course->getTotal(TotalIn);
+
+      // In/Out for score
+      if (score && row == ROW_SCORE)
+       return score->getTotal(TotalOut);
+      if (score && row == ROW_SCORE_2)
+       return score->getTotal(TotalIn);
+      
+    }
+    else if (col == (COLS+1)) {
+      // Total label
+      if (row == ROW_HOLE_2)
+       return QString("Tot");
+      // Total score
+      if (score && course && row == ROW_PAR_2)
        return course->getTotal(Total);
-      else if (index.row() == ROW_SCORE && offset == 9)
+      if (score && row == ROW_SCORE_2)
        return score->getTotal(Total);
-      else
-       return QVariant();
     }
-
-    //qDebug() << "data() " << index << "/" << offset;
-    switch(index.row()) {
-    case ROW_PAR:
-      return course->getPar(index.column() + offset); 
-    case ROW_HCP: 
-      return course->getHcp(index.column() + offset); 
-    case ROW_SCORE: 
-      return score->getScore(index.column() + offset); 
+    else {
+      // data cells
+      switch(row) {
+      case ROW_HOLE:
+       return col + 1;
+      case ROW_HOLE_2:
+       return col + 10;
+      case ROW_PAR:
+       if (score && course)
+         return course->getPar(col); 
+      case ROW_PAR_2:
+       if (score && course)
+         return course->getPar(col + 9); 
+      case ROW_HCP: 
+       if (score && course)
+         return course->getHcp(col); 
+      case ROW_HCP_2:
+       if (score && course)
+         return course->getHcp(col + 9);
+      case ROW_SCORE:
+       if (score)
+         return score->getScore(col);
+      case ROW_SCORE_2: 
+       if (score)
+         return score->getScore(col + 9);
+      }
     }
   }
   return QVariant();
 }
 
-int ScoreTableModel::setItem(int row, int col, int data)
+int ScoreTableModel::setItem(int row, int col, int)
 {
   emit dataChanged(createIndex(row, col), createIndex(row, col));
   return 1;
@@ -226,28 +348,25 @@ int ScoreTableModel::setItem(int row, int col, int data)
 
 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
 {
-    if (role != Qt::DisplayRole)
-         return QVariant();
-
-    // TODO: how to diff between the two table views (no index?)
+  // Only vertical header -- horizontal is hidden
+  if (orientation == Qt::Horizontal)
+    return QVariant();
 
-    if (orientation == Qt::Horizontal)
-      if (section >= 0 && section <= 8)
-       return QString("%1").arg(section+1);
-      else if (section == 9)
-       return QString("I/O");
-      else
-       return QString("Tot");
-    else {
-      switch(section) {
-      case 0: 
-       return QString("Par");
-      case 1: 
-       return QString("HCP");
-      case 2: 
-       return QString("Score");
-      }
+  if (role == Qt::DisplayRole) {
+    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");
     }
     return QVariant();
+  }
+
+  return QVariant();
 }
-                                      
+