Merge two table views into one in main window
authorSakari Poussa <sakari.poussa@nokia.com>
Thu, 22 Oct 2009 19:57:38 +0000 (22:57 +0300)
committerSakari Poussa <sakari.poussa@nokia.com>
Thu, 22 Oct 2009 19:57:38 +0000 (22:57 +0300)
TODO
src/main-window.cpp
src/main-window.h
src/table-model.cpp
src/table-model.h

diff --git a/TODO b/TODO
index 7830464..f9cd1d8 100644 (file)
--- a/TODO
+++ b/TODO
@@ -2,9 +2,10 @@ TODO General:
 
 for 0.1
 - New score - first entry does not update
+  - Seems to be bug in the Qt/Maemo
 
 for 0.2
-- Date picker button
+- Date picker button (widget is missing)
 - Course view - sort menu buttons
 - Edit course
 - Edit score
index 6ca8d3a..46d7328 100644 (file)
@@ -37,8 +37,7 @@ MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
   QWidget *centralWidget = new QWidget(this);
 
   // TODO: move to proper function
-  tableViewFront = new QTableView(centralWidget);
-  tableViewBack = new QTableView(centralWidget);
+  table = new QTableView(centralWidget);
 
   setCentralWidget(centralWidget);
 
@@ -99,8 +98,7 @@ void MainWindow::createLayout(QWidget *parent)
   buttonLayout->addWidget(firstButton);
 
   tableLayout = new QVBoxLayout;
-  tableLayout->addWidget(tableViewFront);
-  tableLayout->addWidget(tableViewBack);
+  tableLayout->addWidget(table);
 
   QHBoxLayout *mainLayout = new QHBoxLayout(parent);
   mainLayout->addLayout(tableLayout);
@@ -125,21 +123,17 @@ void MainWindow::createTableView(QList<Score *> &scoreList, QList <Club *> &club
   scoreTableModel->setScore(scoreList);
   scoreTableModel->setClub(clubList);
 
-  tableViewFront->showGrid();
-  tableViewBack->showGrid();
+  table->showGrid();
 
-  tableViewFront->setModel(scoreTableModel);
-  tableViewBack->setModel(scoreTableModel);
+  table->setModel(scoreTableModel);
 
   // Flag model items which one belongs to front and back nines
-  tableViewFront->setRootIndex(scoreTableModel->index(0, 0));
-  tableViewBack->setRootIndex(scoreTableModel->index(0, 1));
+  table->setRootIndex(scoreTableModel->index(0, 1));
 
   // Fill out all the space with the tables
-  tableViewFront->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
-  tableViewFront->verticalHeader()->setResizeMode(QHeaderView::Stretch);
-  tableViewBack->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
-  tableViewBack->verticalHeader()->setResizeMode(QHeaderView::Stretch);
+  table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
+  table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
+  table->horizontalHeader()->hide();
 }
 
 // When selection down in 'stat' view, this is called.
index d01907b..815a64c 100644 (file)
@@ -54,8 +54,7 @@ private:
   QAbstractItemModel *tableModel;
   QItemSelectionModel *selectionModel;
 
-  QTableView *tableViewFront;
-  QTableView *tableViewBack;
+  QTableView *table;
 
   // Layouts
   QVBoxLayout *tableLayout;
index 5cbff29..43fce9e 100644 (file)
@@ -112,12 +112,12 @@ void ScoreTableModel::prev()
 
 int ScoreTableModel::rowCount(const QModelIndex & parent) const
 {
-  return ROW_COUNT;
+  return 8;
 }
  
 int ScoreTableModel::columnCount(const QModelIndex & parent) 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
@@ -132,10 +132,6 @@ 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
 {
   if (!index.isValid())
@@ -154,14 +150,14 @@ 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();
 
   //
   // COLORS
   //
+  QColor colorHoleBg(Qt::black);
+  QColor colorHoleFg(Qt::white);
   QColor colorBirdie(Qt::yellow);
   QColor colorPar(Qt::green);
   QColor colorBogey(Qt::darkGreen);
@@ -170,24 +166,34 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
   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(index.column() + offset)).toInt();
-    int shots = (score->getScore(index.column() + offset)).toInt();
+    int par = course->getPar(col).toInt();
+    int shots = score->getScore(col).toInt();
 
-    if (index.row() == ROW_SCORE) {
-      if (index.column() == 10 && offset == 9) {
+    // Hole numbers 1-18
+    if (row == ROW_HOLE || row == ROW_HOLE_2) {
+       QBrush brush(colorHoleBg);
+       return brush;
+    }
+
+    if (row == ROW_SCORE || row == ROW_SCORE_2) {
+      if (col == 10 && row == ROW_SCORE_2) {
        // Total score
        QBrush brush(colorTotal);
-       //brush.setStyle(Qt::Dense6Pattern);
        return brush;
       }
-      if (col == 9 && row == 2) {
+      if (col == 9) {
        // In and Out scores
        QBrush brush(colorSubTotal);
-       //brush.setStyle(Qt::Dense7Pattern);
        return brush;
       }
-      if (index.column() < 9) {
+      if (col < 9) {
        if (shots == par) {
          // Par
          QBrush brush(colorPar);
@@ -216,53 +222,67 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
   // FONT
   //
   if (role == Qt::FontRole) {
-    if (index.row() == ROW_SCORE) {
-      if (index.column() == 10 && offset == 9) {
+    if (row == ROW_SCORE_2 && col == 10) {
        QFont font;
        font.setBold(true);
        return font;
-      }
     }
   }
   //
   // DATA
   //
   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();
-    }
 
-    // Tot column
-    if (index.column() == 10) {
-      if (index.row() == ROW_PAR && offset == 9)
+    if (col == 9) {
+      // In/out label
+      if (row == ROW_HOLE)
+       return QString("Out");
+      if (row == ROW_HOLE_2)
+       return QString("In");
+
+      // In/Out for par
+      if (row == ROW_PAR)
+       return course->getTotal(TotalOut);
+      if (row == ROW_PAR_2)
+       return course->getTotal(TotalIn);
+
+      // In/Out for score
+      if (row == ROW_SCORE)
+       return score->getTotal(TotalOut);
+      if (row == ROW_SCORE_2)
+       return score->getTotal(TotalIn);
+      
+    }
+    else if (col == 10) {
+      // Total label
+      if (row == ROW_HOLE_2)
+       return QString("Tot");
+      // Total score
+      if (row == ROW_PAR_2)
        return course->getTotal(Total);
-      else if (index.row() == ROW_SCORE && offset == 9)
+      if (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:
+       return course->getPar(col); 
+      case ROW_PAR_2:
+       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);
+      case ROW_SCORE_2: 
+       return score->getScore(col + 9);
+      }
     }
   }
   return QVariant();
@@ -276,7 +296,7 @@ int ScoreTableModel::setItem(int row, int col, int data)
 
 QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, int role) const
 {
-    if (role != Qt::DisplayRole)
+  if (role != Qt::DisplayRole)
          return QVariant();
 
     // TODO: how to diff between the two table views (no index?)
@@ -290,14 +310,17 @@ QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, i
        return QString(""); // was: Tot
     else {
       switch(section) {
-      case 0: 
+      case ROW_PAR: 
+      case ROW_PAR_2: 
        return QString("Par");
-      case 1: 
+      case ROW_HCP: 
+      case ROW_HCP_2: 
        return QString("HCP");
-      case 2: 
+      case ROW_SCORE: 
+      case ROW_SCORE_2: 
        return QString("Score");
       }
     }
     return QVariant();
 }
-                                      
+
index 5837843..f0a7e31 100644 (file)
@@ -42,6 +42,10 @@ public:
   void last();
 
  private:
+  enum { ROWS = 8, COLS = 9 };
+  enum { ROW_HOLE = 0, ROW_PAR = 1, ROW_HCP = 2, ROW_SCORE = 3, 
+        ROW_HOLE_2 = 4, ROW_PAR_2 = 5, ROW_HCP_2 = 6, ROW_SCORE_2 = 7};
+
   QList<Score *> scoreList;
   QList<Club *> clubList;