Deleted extra files, removed unused .cpp files for build
[scorecard] / src / table-model.cpp
index 43fce9e..529b43d 100644 (file)
@@ -4,9 +4,31 @@
 #include <QFont>
 #include "table-model.h"
 
-Qt::ItemFlags ScoreTableModel::flags ( const QModelIndex & index )
+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 Qt::NoItemFlags;
+  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
@@ -51,6 +73,22 @@ QString ScoreTableModel::getCountText()
   return str;
 }
 
+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)
 {
@@ -66,6 +104,21 @@ Course *ScoreTableModel::findCourse(const QString &clubName,
   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) {
@@ -124,7 +177,6 @@ QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &paren
 {
   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 {
@@ -134,10 +186,9 @@ QModelIndex ScoreTableModel::index(int row, int column, const QModelIndex &paren
 
 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
 {
-  if (!index.isValid())
-    return QVariant();
+  // TODO: move away from the stack
 
-  if (!course || !score)
+  if (!index.isValid())
     return QVariant();
 
   int row = index.row();
@@ -156,33 +207,25 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
   //
   // COLORS
   //
-  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 (role == Qt::ForegroundRole) {
-    if (row == ROW_HOLE || row == ROW_HOLE_2) {
-       QBrush brush(colorHoleFg);
-       return brush;
-    }
-  }
   if (role == Qt::BackgroundRole) {
-    int par = course->getPar(col).toInt();
-    int shots = score->getScore(col).toInt();
-
-    // Hole numbers 1-18
-    if (row == ROW_HOLE || row == ROW_HOLE_2) {
+    // 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;
     }
+    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 (row == ROW_SCORE || row == ROW_SCORE_2) {
       if (col == 10 && row == ROW_SCORE_2) {
        // Total score
        QBrush brush(colorTotal);
@@ -214,6 +257,11 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
          QBrush brush(colorDoubleBogey);
          return brush;
        }
+       if (shots > (par+2)) {
+         // Very bad
+         QBrush brush(colorBad);
+         return brush;
+       }
       }
     }
     return QVariant();
@@ -241,15 +289,15 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
        return QString("In");
 
       // In/Out for par
-      if (row == ROW_PAR)
+      if (score && course && row == ROW_PAR)
        return course->getTotal(TotalOut);
-      if (row == ROW_PAR_2)
+      if (score && course && row == ROW_PAR_2)
        return course->getTotal(TotalIn);
 
       // In/Out for score
-      if (row == ROW_SCORE)
+      if (score && row == ROW_SCORE)
        return score->getTotal(TotalOut);
-      if (row == ROW_SCORE_2)
+      if (score && row == ROW_SCORE_2)
        return score->getTotal(TotalIn);
       
     }
@@ -258,9 +306,9 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
       if (row == ROW_HOLE_2)
        return QString("Tot");
       // Total score
-      if (row == ROW_PAR_2)
+      if (score && course && row == ROW_PAR_2)
        return course->getTotal(Total);
-      if (row == ROW_SCORE_2)
+      if (score && row == ROW_SCORE_2)
        return score->getTotal(Total);
     }
     else {
@@ -271,17 +319,23 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
       case ROW_HOLE_2:
        return col + 10;
       case ROW_PAR:
-       return course->getPar(col); 
+       if (score && course)
+         return course->getPar(col); 
       case ROW_PAR_2:
-       return course->getPar(col + 9); 
+       if (score && course)
+         return course->getPar(col + 9); 
       case ROW_HCP: 
-       return course->getHcp(col); 
-      case ROW_HCP_2: 
-       return course->getHcp(col + 9);
-      case ROW_SCORE: 
-       return score->getScore(col);
+       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: 
-       return score->getScore(col + 9);
+       if (score)
+         return score->getScore(col + 9);
       }
     }
   }
@@ -296,31 +350,32 @@ 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?)
-
-    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");
-      }
+  // 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_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();
 }