Behave correctly if score contains unknown course
authorSakari Poussa <spoussa@gmail.com>
Fri, 28 May 2010 00:41:58 +0000 (03:41 +0300)
committerSakari Poussa <spoussa@gmail.com>
Fri, 28 May 2010 00:41:58 +0000 (03:41 +0300)
TODO
src/main-window.cpp
src/table-model.cpp

diff --git a/TODO b/TODO
index 88819d6..dca0dee 100644 (file)
--- a/TODO
+++ b/TODO
@@ -3,12 +3,13 @@ TOP Items
 TODO General:
 - New score dialog should be the same as main view (f.ex showing
   subtotals, totals, and score colors).
-- edit course table is selectable (chk others)
-- Settings
 - Use syslog
 - Year filters and maybe more, must be easy to apply
 - If course is missing the score view should show NA in par/hcp. Now
   it shows score.
 
+Settings
+  - Sort order: score, date, club
+
 TODO Debian packaging
 - install data to SD card if present
index 93c5f08..ad85c04 100644 (file)
@@ -386,6 +386,7 @@ void MainWindow::clickedList(const QModelIndex &index)
 void MainWindow::viewScore(Score * score, Course * course)
 {
     TRACE;
+    qDebug() << score << course;
     scoreWindow->setup(score, course);
     scoreWindow->show();
 }
index 562dd62..7c9c778 100644 (file)
@@ -41,204 +41,223 @@ int ScoreTableModel::columnCount(const QModelIndex &) const
 
 QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
 {
-  if (!index.isValid())
-    return QVariant();
+    if (!index.isValid())
+        return QVariant();
 
-  int row = index.row();
-  int col = index.column();
+    int row = index.row();
+    int col = index.column();
 
-  //
-  // ALIGNMENT
-  //
-  if (role == Qt::TextAlignmentRole ) {
-    return Qt::AlignCenter;
-  }
+    //
+    // ALIGNMENT
+    //
+    if (role == Qt::TextAlignmentRole ) {
+        return Qt::AlignCenter;
+    }
 
-  if (index.column() > 10)
-    return QVariant();
+    if (index.column() > 10)
+        return QVariant();
 
-  //
-  // 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;
+    //
+    // 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;
+        }
+        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 (col == (COLS+1) && row == ROW_SCORE_2) {
+                // Total score
+                QBrush brush(ScoreColor::total());
+                return brush;
+            }
+            if (col == COLS) {
+                // In and Out scores
+                QBrush brush(ScoreColor::subTotal());
+                return brush;
+            }
+            if (col < COLS) {
+                if (shots == par) {
+                    // Par
+                    QBrush brush(ScoreColor::par());
+                    return brush;
+                }
+                if (shots == (par-1)) {
+                    // Birdie
+                    QBrush brush(ScoreColor::birdie());
+                    return brush;
+                }
+                if (shots == (par+1)) {
+                    // Bogey
+                    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();
     }
-    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 (col == (COLS+1) && row == ROW_SCORE_2) {
-       // Total score
-       QBrush brush(ScoreColor::total());
-       return brush;
-      }
-      if (col == COLS) {
-       // In and Out scores
-       QBrush brush(ScoreColor::subTotal());
-       return brush;
-      }
-      if (col < COLS) {
-       if (shots == par) {
-         // Par
-         QBrush brush(ScoreColor::par());
-         return brush;
-       }
-       if (shots == (par-1)) {
-         // Birdie
-         QBrush brush(ScoreColor::birdie());
-         return brush;
-       }
-       if (shots == (par+1)) {
-         // Bogey
-         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;
-       }
-      }
+    //
+    // FONT
+    //
+    if (role == Qt::FontRole) {
+        QFont font;
+        if (col == (COLS+1) && row == ROW_SCORE_2) {
+            font.setBold(true);
+            font.setPointSize(fontSize+4);
+        }
+        else 
+            font.setPointSize(fontSize);
+        if (row == ROW_HOLE || row == ROW_HOLE_2) {
+            font.setBold(true);
+        }
+        return font;
     }
-    return QVariant();
-  }
-  //
-  // FONT
-  //
-  if (role == Qt::FontRole) {
-      QFont font;
-      if (col == (COLS+1) && row == ROW_SCORE_2) {
-          font.setBold(true);
-          font.setPointSize(fontSize+4);
-      }
-      else 
-          font.setPointSize(fontSize);
-      if (row == ROW_HOLE || row == ROW_HOLE_2) {
-          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");
-
-      // 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);
+
+    //
+    // 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 (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);
+            if (score && row == ROW_SCORE_2)
+                return score->getTotal(Total);
+            // calculate net score
+            if (score && course && row == ROW_HCP_2) {
+                int scoreTotal = score->getTotal(Total).toInt();
+                int courseTotal = course->getTotal(Total).toInt();
+                int n = scoreTotal - courseTotal;
+                return QString("+%1").arg(n);
+            }
+        }
+        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);
+                else
+                    return QString("");
+
+            case ROW_PAR_2:
+                if (course)
+                    return course->getPar(col + 9);
+                else
+                    return QString("");
+
+            case ROW_HCP: 
+                if (course)
+                    return course->getHcp(col); 
+                else
+                    return QString("");
+
+            case ROW_HCP_2:
+                if (course)
+                    return course->getHcp(col + 9);
+                else
+                    return QString("");
+
+            case ROW_SCORE:
+                if (score)
+                    return score->getScore(col);
+                else
+                    return QString("");
+              
+            case ROW_SCORE_2: 
+                if (score)
+                    return score->getScore(col + 9);
+                else
+                    return QString("");
+            }
+        }
     }
-    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);
-      if (score && row == ROW_SCORE_2)
-       return score->getTotal(Total);
-      // calculate net score
-      if (score && course && row == ROW_HCP_2) {
-          int scoreTotal = score->getTotal(Total).toInt();
-          int courseTotal = course->getTotal(Total).toInt();
-          int n = scoreTotal - courseTotal;
-          return QString("+%1").arg(n);
-      }
-    }
-    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();
+    return QVariant();
 }
 
 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
 {
-  // Only vertical header -- horizontal is hidden
-  if (orientation == Qt::Horizontal)
-    return QVariant();
+    // Only vertical header -- horizontal is hidden
+    if (orientation == Qt::Horizontal)
+        return QVariant();
 
-  if (role == Qt::FontRole) {
-      QFont font;
-      font.setPointSize(fontSize);
-      return font;
-  }
-  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");
+    if (role == Qt::FontRole) {
+        QFont font;
+        font.setPointSize(fontSize);
+        return font;
+    }
+    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();
-  }
 
-  return QVariant();
+    return QVariant();
 }
 
 //
@@ -291,18 +310,18 @@ QVariant CourseTableModel::data(const QModelIndex &index, int role) const
         }
         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();
-  }
+    //
+    // 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
     //
@@ -348,40 +367,40 @@ QVariant CourseTableModel::data(const QModelIndex &index, int role) const
             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();
+    // Only vertical header -- horizontal is hidden
+    if (orientation == Qt::Horizontal)
+        return QVariant();
 
-  if (role == Qt::FontRole) {
-      QFont font;
-      font.setPointSize(fontSize);
-      return font;
-  }
-  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");
+    if (role == Qt::FontRole) {
+        QFont font;
+        font.setPointSize(fontSize);
+        return font;
+    }
+    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();
-  }
 
-  return QVariant();
+    return QVariant();
 }