- Refactor: score and course UI widget management into common files
[scorecard] / src / score-dialog.cpp
index 281691d..a0fd9e8 100644 (file)
 
 #include "score-dialog.h"
 #include "score-common.h"
+#include "table-model.h"
 
+////////////////////////////////////////////////////////////////////////////////
+// ScoreWindow based on QMainWindow
+////////////////////////////////////////////////////////////////////////////////
+ScoreWindow::ScoreWindow(QWidget *parent) : QMainWindow(parent)
+{
+#ifdef Q_WS_MAEMO_5
+    setAttribute(Qt::WA_Maemo5StackedWindow);
+#endif
+
+    QAction *editAction = new QAction(tr("Edit"), this);
+    connect(editAction, SIGNAL(triggered()), parent, SLOT(editScore()));
+    menuBar()->addAction(editAction);
+
+    QAction *delAction = new QAction(tr("Delete"), this);
+    connect(delAction, SIGNAL(triggered()), parent, SLOT(deleteScore()));
+    menuBar()->addAction(delAction);
+
+    model = new ScoreTableModel;
+    
+    QTableView * table = new QTableView;
+    table->showGrid();
+    table->setSelectionMode(QAbstractItemView::NoSelection);
+    table->setStyleSheet(ScoreStyle::style());
+    table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
+    table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
+    table->horizontalHeader()->hide();
+    table->setModel(model);
+    
+    QWidget *central = new QWidget(this);
+    setCentralWidget(central);
+    
+    QVBoxLayout *layout = new QVBoxLayout;
+    layout->addWidget(table);
+    
+    central->setLayout(layout);
+}
+
+ScoreWindow::~ScoreWindow()
+{
+    TRACE;
+}
+
+void ScoreWindow::setup(Score *score, Course *course)
+{
+    TRACE;
+    QString title = QString("Score: %1, %2 - %3").arg(score->getClubName()).arg(score->getCourseName()).arg(score->getDate());
+    setWindowTitle(title);
+    model->set(score, course);
+}
+
+
+////////////////////////////////////////////////////////////////////////////////
+// SelectDialog based on QDialog
+////////////////////////////////////////////////////////////////////////////////
 SelectDialog::SelectDialog(QWidget *parent) : QDialog(parent)
 {
   resize(800, 350);
@@ -82,7 +137,7 @@ void SelectDialog::init(QList<Club *> &list)
 
       QListWidgetItem *newItem = new QListWidgetItem;
 
-      QString entry = club->getName() + "," + course->getName();
+      QString entry = club->getName() + ", " + course->getName();
 
       newItem->setText(entry);
       listClub->insertItem(index, newItem);
@@ -101,7 +156,7 @@ void SelectDialog::results(QString &club,
   if (current) {
     QString tmp = current->text();
 
-    QStringList stringList = tmp.split(",");
+    QStringList stringList = tmp.split(", ");
     club = stringList[0];
     course = stringList[1];
 #ifdef Q_WS_MAEMO_5
@@ -135,119 +190,137 @@ void SelectDialog::reject(void)
 }
 
 ////////////////////////////////////////////////////////////////////////////////
+// ScoreDialog based on QDialog
 ////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////
-
 ScoreDialog::ScoreDialog(QWidget *parent) : QDialog(parent)
 {
-  resize(800, 400);
+    TRACE;
+    resize(800, 400);
 
-  QWidget *centralWidget = new QWidget(this);
+    QWidget *centralWidget = new QWidget(this);
 
-  createTable();
-  createButton();
+    createTable();
+    createButton();
+    
+    createLayout(centralWidget);
+}
 
-  createLayout(centralWidget);
+ScoreDialog::~ScoreDialog()
+{
+    //if (centralWidget)
+    //  delete centralWidget;
+    if (leftLayout)
+        delete leftLayout;
+    if (rightLayout)
+        delete rightLayout;
+    //if (mainLayout)
+    //  delete mainLayout;
+    if (table)
+        delete table;
 }
 
 void ScoreDialog::createLayout(QWidget *parent)
 {
-  leftLayout = new QVBoxLayout;
-  leftLayout->addWidget(table);
-
-  QDialogButtonBox * buttonBoxUp = new QDialogButtonBox(Qt::Vertical);
-  buttonBoxUp->addButton(pushButtonUp, QDialogButtonBox::ActionRole);
-  buttonBoxUp->addButton(pushButtonDown, QDialogButtonBox::ActionRole);
-  buttonBoxUp->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
-
-  QDialogButtonBox * buttonBoxDown = new QDialogButtonBox(Qt::Vertical);
-  buttonBoxDown->addButton(pushButtonFinish, QDialogButtonBox::ActionRole);
-
-  rightLayout = new QVBoxLayout;
-  rightLayout->addWidget(buttonBoxUp);
-  rightLayout->addStretch();
-  rightLayout->addWidget(buttonBoxDown);
-
-  QHBoxLayout *mainLayout = new QHBoxLayout(parent);
-  mainLayout->addLayout(leftLayout);
-  mainLayout->addLayout(rightLayout);
-  setLayout(mainLayout);
+    TRACE;
+    leftLayout = new QVBoxLayout;
+    leftLayout->addWidget(table);
+
+    QDialogButtonBox * buttonBoxUp = new QDialogButtonBox(Qt::Vertical);
+    buttonBoxUp->addButton(pushButtonUp, QDialogButtonBox::ActionRole);
+    buttonBoxUp->addButton(pushButtonDown, QDialogButtonBox::ActionRole);
+    buttonBoxUp->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
+
+    QDialogButtonBox * buttonBoxDown = new QDialogButtonBox(Qt::Vertical);
+    buttonBoxDown->addButton(pushButtonFinish, QDialogButtonBox::ActionRole);
+
+    rightLayout = new QVBoxLayout;
+    rightLayout->addWidget(buttonBoxUp);
+    rightLayout->addStretch();
+    rightLayout->addWidget(buttonBoxDown);
+
+    QHBoxLayout *mainLayout = new QHBoxLayout(parent);
+    mainLayout->addLayout(leftLayout);
+    mainLayout->addLayout(rightLayout);
+    setLayout(mainLayout);
 }
 
 void ScoreDialog::createTable(QWidget *parent)
 {
-  table = new QTableWidget(ROWS, COLS, parent);
+    TRACE;
+    table = new QTableWidget(ROWS, COLS, parent);
 
-  table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
-  table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
-  table->horizontalHeader()->hide();
+    table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
+    table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
+    table->horizontalHeader()->hide();
 
-  table->setStyleSheet(ScoreStyle::style());
+    table->setStyleSheet(ScoreStyle::style());
 
-  QStringList headers;
-  headers << "" << "Par" << "HCP" << "Score" << "" << "Par" << "HCP" << "Score";
-  table->setVerticalHeaderLabels(headers);
+    QStringList headers;
+    headers << "" << "Par" << "HCP" << "Score" << "" << "Par" << "HCP" << "Score";
+    table->setVerticalHeaderLabels(headers);
 }
 
 void ScoreDialog::createButton(QWidget *parent)
 {
-  Q_UNUSED(parent);
-  pushButtonUp = new QPushButton(tr("+"));
-  connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
-
-  pushButtonDown = new QPushButton(tr("-"));
-  connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
-
-  pushButtonNext = new QPushButton(tr("Next"));
-  connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
+    TRACE;
+    Q_UNUSED(parent);
+    pushButtonUp = new QPushButton(tr("+"));
+    connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
+    
+    pushButtonDown = new QPushButton(tr("-"));
+    connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
+  
+    pushButtonNext = new QPushButton(tr("Next"));
+    connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
 
-  pushButtonFinish = new QPushButton(tr("Finish"));
-  connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
+    pushButtonFinish = new QPushButton(tr("Finish"));
+    connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
 }
 
 void ScoreDialog::init(Course *course, Score *score)
 {
-  QTableWidgetItem *par, *hcp, *scoreItem, *holeNum;
-
-  for (int i = 0; i < 18; i++) {
-    par = new QTableWidgetItem(course->getPar(i));
-    hcp = new QTableWidgetItem(course->getHcp(i));
-    if (score)
-      scoreItem = new QTableWidgetItem(score->getScore(i));
-    else
-      scoreItem = new QTableWidgetItem("");
-    holeNum = new QTableWidgetItem(QString::number(i+1));
-
-    holeNum->setTextAlignment(Qt::AlignCenter);
-    holeNum->setFlags(Qt::NoItemFlags);
-    holeNum->setForeground(ScoreColor::holeBg());
-
-    par->setTextAlignment(Qt::AlignCenter);
-    par->setFlags(Qt::NoItemFlags);
-
-    hcp->setTextAlignment(Qt::AlignCenter);
-    hcp->setFlags(Qt::NoItemFlags);
-
-    scoreItem->setTextAlignment(Qt::AlignCenter);
-
-    if (i < 9) {
-      table->setItem(ROW_HOLE, i, holeNum);
-      table->setItem(ROW_PAR, i, par);
-      table->setItem(ROW_HCP, i, hcp);
-      table->setItem(ROW_SCORE, i, scoreItem);
-    }
-    else {
-      table->setItem(ROW_HOLE_2, i-9, holeNum);
-      table->setItem(ROW_PAR_2, i-9, par);
-      table->setItem(ROW_HCP_2, i-9, hcp);
-      table->setItem(ROW_SCORE_2, i-9, scoreItem);
+    TRACE;
+    QTableWidgetItem *par, *hcp, *scoreItem, *holeNum;
+
+    for (int i = 0; i < 18; i++) {
+        par = new QTableWidgetItem(course->getPar(i));
+        hcp = new QTableWidgetItem(course->getHcp(i));
+        if (score)
+            scoreItem = new QTableWidgetItem(score->getScore(i));
+        else
+            scoreItem = new QTableWidgetItem("");
+        holeNum = new QTableWidgetItem(QString::number(i+1));
+
+        holeNum->setTextAlignment(Qt::AlignCenter);
+        holeNum->setFlags(Qt::NoItemFlags);
+        holeNum->setForeground(ScoreColor::holeBg());
+        
+        par->setTextAlignment(Qt::AlignCenter);
+        par->setFlags(Qt::NoItemFlags);
+
+        hcp->setTextAlignment(Qt::AlignCenter);
+        hcp->setFlags(Qt::NoItemFlags);
+        
+        scoreItem->setTextAlignment(Qt::AlignCenter);
+
+        if (i < 9) {
+            table->setItem(ROW_HOLE, i, holeNum);
+            table->setItem(ROW_PAR, i, par);
+            table->setItem(ROW_HCP, i, hcp);
+            table->setItem(ROW_SCORE, i, scoreItem);
+        }
+        else {
+            table->setItem(ROW_HOLE_2, i-9, holeNum);
+            table->setItem(ROW_PAR_2, i-9, par);
+            table->setItem(ROW_HCP_2, i-9, hcp);
+            table->setItem(ROW_SCORE_2, i-9, scoreItem);
+        }
     }
-  }
 
-  // Set focus to 1st cell
-  table->setCurrentCell(ROW_SCORE, 0);
-  if (!score)
-    setDefaultScore(table);
+    // Set focus to 1st cell
+    table->setCurrentCell(ROW_SCORE, 0);
+    if (!score)
+        setDefaultScore(table);
 }
 
 // Set default score to par if not set
@@ -333,16 +406,17 @@ void ScoreDialog::moveToNextCell(QTableWidgetItem *item)
 
 void ScoreDialog::results(QVector<QString> &scores)
 {
-  for (int i = 0; i < 9; i++) {
-    QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
-    QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
+    TRACE;
+    for (int i = 0; i < 9; i++) {
+        QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
+        QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
 
-    if (frontItem)
-      scores[i] = frontItem->text();
+        if (frontItem)
+            scores[i] = frontItem->text();
 
-    if (backItem)
-      scores[i+9] = backItem->text();
-  }
+        if (backItem)
+            scores[i+9] = backItem->text();
+    }
 }
 
 bool ScoreDialog::validate(void)