- Refactor: score and course UI widget management into common files
authorSakari Poussa <spoussa@gmail.com>
Sun, 16 May 2010 20:28:55 +0000 (23:28 +0300)
committerSakari Poussa <spoussa@gmail.com>
Sun, 16 May 2010 20:28:55 +0000 (23:28 +0300)
- Score and course windows are created once - show/hide to manage
- Fix delete and edit score/course crashes

12 files changed:
data/score.xml
scorecard.pro
src/course-dialog.cpp
src/course-dialog.h
src/list-model.cpp
src/main-window.cpp
src/main-window.h
src/score-common.h
src/score-dialog.cpp
src/score-dialog.h
src/table-model.cpp
src/table-model.h

index 438d6d7..ecc9381 100644 (file)
@@ -1,7 +1,7 @@
 <data>
  <score club="Master" course="Forest" date="2009-08-14">
   <hole num="1" shots="6"/>
-  <hole num="2" shots="5"/>
+  <hole num="2" shots="6"/>
   <hole num="3" shots="5"/>
   <hole num="4" shots="4"/>
   <hole num="5" shots="5"/>
   <hole num="18" shots="6"/>
  </score>
  <score club="Master" course="Master" date="2009-06-26">
-  <hole num="1" shots="5"/>
+  <hole num="1" shots="6"/>
   <hole num="2" shots="7"/>
   <hole num="3" shots="6"/>
   <hole num="4" shots="6"/>
index e8504b1..709e7de 100644 (file)
@@ -10,6 +10,8 @@ CONFIG  += qt debug
 TEMPLATE = app
 QT      += xml maemo5
 
+DEFINES += WANT_DEBUG
+
 RESOURCES = scorecard.qrc
 
 HEADERS  = \
index 5abbd7b..9010ef1 100644 (file)
 
 #include "course-dialog.h"
 #include "score-common.h"
+#include "table-model.h"
 
+////////////////////////////////////////////////////////////////////////////////
+// CourseWindow based on QMainWindow
+////////////////////////////////////////////////////////////////////////////////
+CourseWindow::CourseWindow(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(editCourse()));
+    menuBar()->addAction(editAction);
+
+    QAction *delAction = new QAction(tr("Delete"), this);
+    connect(delAction, SIGNAL(triggered()), parent, SLOT(deleteCourse()));
+    menuBar()->addAction(delAction);
+
+    model = new CourseTableModel;
+    
+    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);
+}
+
+void CourseWindow::setup(Course *course)
+{
+    QString title = QString("Course: %1, Par - %2").arg(course->getName()).arg(course->getTotal(Total));
+    setWindowTitle(title);
+
+    model->set(course);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// CourseSelectDialog based on QDialog
+////////////////////////////////////////////////////////////////////////////////
 CourseSelectDialog::CourseSelectDialog(QWidget *parent) : QDialog(parent)
 {
   QWidget *centralWidget = new QWidget(this);
@@ -79,9 +128,8 @@ void CourseSelectDialog::next(void)
 }
 
 ////////////////////////////////////////////////////////////////////////////////
+// CourseDialog based on QDialog
 ////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////
-
 CourseDialog::CourseDialog(QWidget *parent) : QDialog(parent)
 {
   resize(800, 400);
index b6a54ef..35e7c8d 100644 (file)
 #include <QDebug>
 
 #include "data.h"
+#include "table-model.h"
 
 QT_BEGIN_NAMESPACE
 class QTableWidget;
 class QTableWidgetItem;
 QT_END_NAMESPACE
 
+class CourseWindow: public QMainWindow
+{
+    Q_OBJECT;
+
+public:
+    CourseWindow(QWidget *parent = 0);
+
+    void setup(Course *course);
+
+private:
+    CourseTableModel *model;
+};
+
 class CourseSelectDialog: public QDialog
 {
   Q_OBJECT
index 5c3e8be..7db6434 100644 (file)
@@ -109,7 +109,7 @@ void CourseListModel::updateList()
         QListIterator<Course *> j(courseList);
         while (j.hasNext()) {
             Course *course = j.next();
-            QString *str = new QString(club->getName() + "," + course->getName());
+            QString *str = new QString(club->getName() + ", " + course->getName());
             clubAndCourseList << str;
         }
     }
index c22be4d..d499d06 100644 (file)
@@ -51,6 +51,7 @@ bool dateMoreThan(const Score *s1, const Score *s2)
 // Find score based on club and course name
 Score *MainWindow::findScore(QString & clubName, QString & courseName)
 {
+    TRACE;
     QListIterator<Score *> i(scoreList);
     Score * s;
 
@@ -66,6 +67,7 @@ Score *MainWindow::findScore(QString & clubName, QString & courseName)
 // Find club based on name
 Club *MainWindow::findClub(QString &name)
 {
+    TRACE;
     QListIterator<Club *> i(clubList);
     Club *c;
 
@@ -81,6 +83,7 @@ Club *MainWindow::findClub(QString &name)
 Course *MainWindow::findCourse(const QString &clubName, 
                                const QString &courseName)
 {
+    TRACE;
     QListIterator<Club *> i(clubList);
     Club *c;
 
@@ -95,13 +98,18 @@ Course *MainWindow::findCourse(const QString &clubName,
 
 // Find course based on current selection on the list
 // TODO: make sure this is only called when course list is the model...
-Course *MainWindow::findCourse()
+Course *MainWindow::currentCourse()
 {
+    TRACE;
     QModelIndex index = selectionModel->currentIndex();
+
+    if (!index.isValid())
+        return 0;
+
     const QAbstractItemModel *model = selectionModel->model();
     QString str = model->data(index, Qt::DisplayRole).toString();
 
-    QStringList strList = str.split(",");
+    QStringList strList = str.split(", ");
     if (strList.count() != 2) {
         showNote(tr("Invalid course selection"));
         return 0;
@@ -109,8 +117,22 @@ Course *MainWindow::findCourse()
     return findCourse(strList[0], strList[1]);
 }
 
+// Find score based on current selection on the list
+// TODO: make sure this is only called when score list is the model...
+Score *MainWindow::currentScore()
+{
+    TRACE;
+    QModelIndex index = selectionModel->currentIndex();
+
+    if (!index.isValid())
+        return 0;
+
+    return scoreList.at(index.row());
+}
+
 void MainWindow::flushReadOnlyItems()
 {
+    TRACE;
     QMutableListIterator<Club *> i(clubList);
     Club *c;
 
@@ -151,10 +173,14 @@ MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
   createListView(scoreList, clubList);
 
   createLayoutList(centralWidget);
+
+  scoreWindow = new ScoreWindow(this);
+  courseWindow = new CourseWindow(this);
 }
 
 void MainWindow::loadSettings(void)
 {
+    TRACE;
   bool external = false;
 
   QDir mmc(mmcDir);
@@ -201,6 +227,7 @@ void MainWindow::loadSettings(void)
 
 void MainWindow::saveSettings(void)
 {
+    TRACE;
     settings.beginGroup(settingsGroup);
     if (conf.hcp.isValid())
         settings.setValue(settingsHcp, conf.hcp);
@@ -213,6 +240,7 @@ void MainWindow::saveSettings(void)
 
 void MainWindow::createLayoutList(QWidget *parent)
 {
+    TRACE;
     QVBoxLayout * tableLayout = new QVBoxLayout;
     tableLayout->addWidget(list);
 
@@ -224,6 +252,7 @@ void MainWindow::createLayoutList(QWidget *parent)
 void MainWindow::createListView(QList<Score *> &scoreList, 
                                 QList <Club *> &clubList)
 {
+    TRACE;
     list = new QListView(this);
 
     scoreListModel = new ScoreListModel(scoreList, clubList);
@@ -243,6 +272,7 @@ void MainWindow::createListView(QList<Score *> &scoreList,
 
 void MainWindow::listScores()
 {
+    TRACE;
     list->setModel(scoreListModel);
     selectionModel = list->selectionModel();
     updateTitleBar(titleScores);
@@ -250,6 +280,7 @@ void MainWindow::listScores()
 
 void MainWindow::listCourses()
 {
+    TRACE;
     list->setModel(courseListModel);
     selectionModel = list->selectionModel();
     updateTitleBar(titleCourses);
@@ -257,6 +288,7 @@ void MainWindow::listCourses()
 
 void MainWindow::createActions()
 {
+    TRACE;
     newScoreAction = new QAction(tr("New Score"), this);
     connect(newScoreAction, SIGNAL(triggered()), this, SLOT(newScore()));
 
@@ -285,6 +317,7 @@ void MainWindow::createActions()
 
 void MainWindow::createMenus()
 {
+    TRACE;
 #ifdef Q_WS_MAEMO_5
     menu = menuBar()->addMenu("");
 #else
@@ -300,6 +333,7 @@ void MainWindow::createMenus()
 
 void MainWindow::updateTitleBar(QString & msg)
 {
+    TRACE;
     setWindowTitle(msg);
 }
 
@@ -314,6 +348,7 @@ void MainWindow::showNote(QString msg)
 
 void MainWindow::clickedList(const QModelIndex &index)
 {
+    TRACE;
     int row = index.row();
 
     const QAbstractItemModel *m = index.model();
@@ -326,7 +361,7 @@ void MainWindow::clickedList(const QModelIndex &index)
     }
     else if (m == courseListModel) {
         QString str = courseListModel->data(index, Qt::DisplayRole).toString();
-        QStringList strList = str.split(",");
+        QStringList strList = str.split(", ");
 
         if (strList.count() != 2) {
             showNote(QString("Invalid course selection"));
@@ -337,118 +372,16 @@ void MainWindow::clickedList(const QModelIndex &index)
     }
 }
 
-
-void MainWindow::newCourse()
-{
-    CourseSelectDialog *selectDialog = new CourseSelectDialog(this);
-
-    int result = selectDialog->exec();
-    if (result) {
-        QString clubName;
-        QString courseName;
-        QString date;
-
-        selectDialog->results(clubName, courseName);
-
-        CourseDialog *courseDialog = new CourseDialog(this);
-        courseDialog->init();
-        QString title = "New Course: " + clubName + "," + courseName;
-        courseDialog->setWindowTitle(title);
-
-        int result = courseDialog->exec();
-        if (result) {
-            QVector<QString> par(18);
-            QVector<QString> hcp(18);
-            QVector<QString> len(18);
-
-            courseDialog->results(par, hcp, len);
-
-            Course *course = 0;
-            Club *club = findClub(clubName);
-            if (club) {
-                course = club->getCourse(courseName);
-                if (course) {
-                    showNote(tr("Club/course already in the database"));
-                    return;
-                }
-                else {
-                    course = new Course(courseName, par, hcp);
-                    club->addCourse(course);
-                }
-            }
-            else {
-                // New club and course
-                club = new Club(clubName);
-                course = new Course(courseName, par, hcp);
-                club->addCourse(course);
-                clubList << club;
-            }
-            // Save it
-            saveClubFile(clubFile, clubList);
-            courseListModel->update(clubList);
-            list->update();
-        }
-    }
-}
-
-void MainWindow::deleteCourse()
-{
-    Course * course = findCourse();
-    Club * club = course->parent();
-
-    // Can not delete course if it has scores -- check
-    if (findScore(club->getName(), course->getName()) != 0) {
-        showNote(tr("Can not delete course, delete scores on the course first"));
-        return;
-    }
-    // Close the window
-    if (courseWin)
-        courseWin->close();
-
-    club->delCourse(course);
-
-    if (club->isEmpty()) {
-        int index = clubList.indexOf(club);
-        if (index != -1)
-            clubList.removeAt(index);
-    }
-
-    // Save it
-    saveClubFile(clubFile, clubList);
-    courseListModel->update(clubList);
-    list->update();
-}
-
-void MainWindow::editCourse()
+void MainWindow::viewScore(Score * score, Course * course)
 {
-    Course *course = findCourse();
-
-    if (!course) {
-        showNote(tr("No course on edit"));
-        return;
-    }
-
-    CourseDialog *courseDialog = new CourseDialog(this);
-    courseDialog->init(course);
-
-    QString title = "Edit Course: " + course->getName();
-    courseDialog->setWindowTitle(title);
-  
-    int result = courseDialog->exec();
-    if (result) {
-        QVector<QString> par(18);
-        QVector<QString> hcp(18);
-        QVector<QString> len(18);
-    
-        courseDialog->results(par, hcp, len);
-    
-        course->update(par, hcp, len);
-        saveClubFile(clubFile, clubList);
-    }
+    TRACE;
+    scoreWindow->setup(score, course);
+    scoreWindow->show();
 }
 
 void MainWindow::newScore()
 {
+    TRACE;
     SelectDialog *selectDialog = new SelectDialog(this);
 
     selectDialog->init(clubList);
@@ -494,24 +427,12 @@ void MainWindow::newScore()
     }
 }
 
-void MainWindow::deleteScore()
-{
-    if (scoreWin)
-        scoreWin->close();
-
-    QModelIndex index = selectionModel->currentIndex();
-    scoreList.removeAt(index.row());
-    // Save it
-    saveScoreFile(scoreFile, scoreList);
-    scoreListModel->update(scoreList);
-    list->update();
-}
-
 void MainWindow::editScore()
 {
-    QModelIndex index = selectionModel->currentIndex();
-    Score * score = scoreList.at(index.row());
+    TRACE;
     Course * course = 0;
+    Score *score = currentScore();
+
     if (score) 
         course = findCourse(score->getClubName(), score->getCourseName());
 
@@ -522,15 +443,13 @@ void MainWindow::editScore()
 
     QString date = score->getDate();
 
-    ScoreDialog *scoreDialog = new ScoreDialog;
+    ScoreDialog *scoreDialog = new ScoreDialog(this);
+    scoreDialog->init(course, score);
   
     QString title = "Edit Score: " + course->getName() + ", " + date;
     scoreDialog->setWindowTitle(title);
 
-    scoreDialog->init(course, score);
-
     int result = scoreDialog->exec();
-
     if (result) {
         QVector<QString> scores(18);
 
@@ -542,90 +461,160 @@ void MainWindow::editScore()
         qSort(scoreList.begin(), scoreList.end(), dateMoreThan); 
         // Save it
         saveScoreFile(scoreFile, scoreList);
-        // Update the model
-        scoreListModel->update(scoreList);
     }
+    if (scoreDialog)
+        delete scoreDialog;
 }
 
-void MainWindow::viewScore(Score * score, Course * course)
+void MainWindow::deleteScore()
 {
-    scoreWin = new QMainWindow(this);
-    QString title = QString("Score: %1, %2 - %3").arg(score->getClubName()).arg(score->getCourseName()).arg(score->getDate());
-    scoreWin->setWindowTitle(title);
-#ifdef Q_WS_MAEMO_5
-    scoreWin->setAttribute(Qt::WA_Maemo5StackedWindow);
-#endif
-
-    QAction *editAction = new QAction(tr("Edit"), scoreWin);
-    connect(editAction, SIGNAL(triggered()), this, SLOT(editScore()));
-    scoreWin->menuBar()->addAction(editAction);
+    TRACE;
+    if (scoreWindow)
+        scoreWindow->close();
 
-    QAction *delAction = new QAction(tr("Delete"), scoreWin);
-    connect(delAction, SIGNAL(triggered()), this, SLOT(deleteScore()));
-    scoreWin->menuBar()->addAction(delAction);
-
-    ScoreTableModel *model = new ScoreTableModel(score, course);
-    
-    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(scoreWin);
-    scoreWin->setCentralWidget(central);
-    
-    QVBoxLayout *layout = new QVBoxLayout;
-    layout->addWidget(table);
+    QModelIndex index = selectionModel->currentIndex();
+    if (!index.isValid()) {
+        qDebug() << "Invalid index";
+        return;
+    }
     
-    central->setLayout(layout);
-    scoreWin->show();
+    scoreList.removeAt(index.row());
+    // Save it
+    saveScoreFile(scoreFile, scoreList);
+    scoreListModel->update(scoreList);
+    list->update();
 }
 
 void MainWindow::viewCourse(Course * course)
 {
-    courseWin = new QMainWindow(this);
-    QString title = QString("Course: %1, Par - %2").arg(course->getName()).arg(course->getTotal(Total));
-    courseWin->setWindowTitle(title);
-#ifdef Q_WS_MAEMO_5
-    courseWin->setAttribute(Qt::WA_Maemo5StackedWindow);
-#endif
+    TRACE;
+    courseWindow->setup(course);
+    courseWindow->show();
+}
 
-    QAction *editAction = new QAction(tr("Edit"), courseWin);
-    connect(editAction, SIGNAL(triggered()), this, SLOT(editCourse()));
-    courseWin->menuBar()->addAction(editAction);
+void MainWindow::newCourse()
+{
+    TRACE;
+    CourseSelectDialog *selectDialog = new CourseSelectDialog(this);
 
-    QAction *delAction = new QAction(tr("Delete"), courseWin);
-    connect(delAction, SIGNAL(triggered()), this, SLOT(deleteCourse()));
-    courseWin->menuBar()->addAction(delAction);
+    int result = selectDialog->exec();
+    if (result) {
+        QString clubName;
+        QString courseName;
+        QString date;
 
-    CourseTableModel *model = new CourseTableModel(course);
-    
-    QTableView * table = new QTableView;
-    table->showGrid();
-    table->setSelectionMode(QAbstractItemView::NoSelection);
-    //table->setStyleSheet(ScoreStyle::headerView());
-    table->setStyleSheet(ScoreStyle::style());
-    table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
-    table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
-    table->horizontalHeader()->hide();
-    table->setModel(model);
-    
-    QWidget *central = new QWidget(courseWin);
-    courseWin->setCentralWidget(central);
+        selectDialog->results(clubName, courseName);
+
+        CourseDialog *courseDialog = new CourseDialog(this);
+        courseDialog->init();
+        QString title = "New Course: " + clubName + "," + courseName;
+        courseDialog->setWindowTitle(title);
+
+        int result = courseDialog->exec();
+        if (result) {
+            QVector<QString> par(18);
+            QVector<QString> hcp(18);
+            QVector<QString> len(18);
+
+            courseDialog->results(par, hcp, len);
+
+            Course *course = 0;
+            Club *club = findClub(clubName);
+            if (club) {
+                course = club->getCourse(courseName);
+                if (course) {
+                    showNote(tr("Club/course already in the database"));
+                    return;
+                }
+                else {
+                    course = new Course(courseName, par, hcp);
+                    club->addCourse(course);
+                }
+            }
+            else {
+                // New club and course
+                club = new Club(clubName);
+                course = new Course(courseName, par, hcp);
+                club->addCourse(course);
+                clubList << club;
+            }
+            // Save it
+            saveClubFile(clubFile, clubList);
+            courseListModel->update(clubList);
+            list->update();
+        }
+    }
+}
+
+void MainWindow::editCourse()
+{
+    TRACE;
+    Course *course = currentCourse();
+
+    if (!course) {
+        showNote(tr("No course on edit"));
+        return;
+    }
+
+    CourseDialog *courseDialog = new CourseDialog(this);
+    courseDialog->init(course);
+
+    QString title = "Edit Course: " + course->getName();
+    courseDialog->setWindowTitle(title);
+  
+    int result = courseDialog->exec();
+    if (result) {
+        QVector<QString> par(18);
+        QVector<QString> hcp(18);
+        QVector<QString> len(18);
     
-    QVBoxLayout *layout = new QVBoxLayout;
-    layout->addWidget(table);
+        courseDialog->results(par, hcp, len);
     
-    central->setLayout(layout);
-    courseWin->show();
+        course->update(par, hcp, len);
+        saveClubFile(clubFile, clubList);
+    }
+    if (courseDialog)
+        delete courseDialog;
+}
+
+void MainWindow::deleteCourse()
+{
+    TRACE;
+    Club *club = 0;
+    Course * course = currentCourse();
+
+    if (!course) {
+        qDebug() << "Invalid course for deletion";
+        return;
+    }
+    club = course->parent();
+
+    // Can not delete course if it has scores -- check
+    if (findScore(club->getName(), course->getName()) != 0) {
+        showNote(tr("Can not delete course, delete scores on the course first"));
+        return;
+    }
+    // Close the window
+    if (courseWindow)
+        courseWindow->close();
+
+    club->delCourse(course);
+
+    if (club->isEmpty()) {
+        int index = clubList.indexOf(club);
+        if (index != -1)
+            clubList.removeAt(index);
+    }
+
+    // Save it
+    saveClubFile(clubFile, clubList);
+    courseListModel->update(clubList);
+    list->update();
 }
 
 void MainWindow::viewStatistics()
 {
+    TRACE;
   QMainWindow *win = new QMainWindow(this);
   QString title = "Statistics";
   win->setWindowTitle(title);
@@ -663,6 +652,7 @@ void MainWindow::viewStatistics()
 
 void MainWindow::viewSettings()
 {
+    TRACE;
     SettingsDialog *dlg = new SettingsDialog(this);
 
     dlg->init(conf, clubList);
index d587a77..b4ff965 100644 (file)
@@ -21,6 +21,8 @@
 
 #include "data.h"
 #include "score-common.h"
+#include "score-dialog.h"
+#include "course-dialog.h"
 #include "table-model.h"
 #include "list-model.h"
 
@@ -62,7 +64,8 @@ private:
   void saveSettings(void);
   Club * findClub(QString &name);
   Course * findCourse(const QString &clubName, const QString &courseName);
-  Course * findCourse();
+  Course * currentCourse();
+  Score * currentScore();
   Score * findScore(QString & clubName, QString & courseName);
   void flushReadOnlyItems();
                                
@@ -107,6 +110,6 @@ private:
   void createActions();
 
   // Windows
-  QMainWindow *scoreWin;
-  QMainWindow *courseWin;
+  ScoreWindow *scoreWindow;
+  CourseWindow *courseWindow;
 };
index 58dbc8c..e94bf22 100644 (file)
@@ -28,9 +28,16 @@ QColor colorBad(0x72, 0x9f, 0xcf);
 QColor colorSubTotal(Qt::black);
 QColor colorTotal(Qt::black);
 #endif
-
 #include <QColor>
 
+#ifndef WANT_DEBUG
+#define TRACE
+#else
+#include <QDebug>
+#define TRACE qDebug()<<QString("[%1] %2(): %3").arg(__FILE__).arg(__func__).arg(__LINE__);
+#endif
+
+
 class AppSettings {
 public:
     QVariant hcp;
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)
index dee7d52..2e35bfb 100644 (file)
@@ -10,6 +10,7 @@
 #define SCORE_DIALOG_H
 
 #include <QDialog>
+#include <QMainWindow>
 #include <QTableWidget>
 #include <QListWidget>
 #include <QComboBox>
 #endif
 
 #include "data.h"
+#include "table-model.h"
 
-class SelectDialog: public QDialog
+class ScoreWindow: public QMainWindow
 {
-  Q_OBJECT
+    Q_OBJECT;
+
+public:
+    ScoreWindow(QWidget *parent = 0);
+    ~ScoreWindow();
 
- public:
-  SelectDialog(QWidget *parent = 0);
+    void setup(Score * score, Course * course);
 
-  void results(QString &club, QString &course, QString &date);
-  void init(QList<Club *> &list);
+private:
+    ScoreTableModel *model;
+};
+
+
+class SelectDialog: public QDialog
+{
+    Q_OBJECT;
+
+public:
+    SelectDialog(QWidget *parent = 0);
+    
+    void results(QString &club, QString &course, QString &date);
+    void init(QList<Club *> &list);
 
 private slots:
-  void next(void);
+    void next(void);
 
- private:
+private:
 
-  bool validate(void);
-  void reject(void);
+    bool validate(void);
+    void reject(void);
 
-  void comboBoxCourseUpdate(void);
-  void createLayout(QWidget *parent = 0);
+    void comboBoxCourseUpdate(void);
+    void createLayout(QWidget *parent = 0);
 
-  // Widgets
-  QListWidget  *listClub;
+    // Widgets
+    QListWidget  *listClub;
 #ifdef Q_WS_MAEMO_5
-  QMaemo5ValueButton *dateButton;
+    QMaemo5ValueButton *dateButton;
 #else
-  QLineEdit    *lineEditDate;
-  QDateEdit    *date;
+    QLineEdit    *lineEditDate;
+    QDateEdit    *date;
 #endif
-  QLabel       *labelClub;
-  QLabel       *labelCourse;
-  QPushButton  *pushButtonNext;
+    QLabel       *labelClub;
+    QLabel       *labelCourse;
+    QPushButton  *pushButtonNext;
 
   // Layouts
-  QVBoxLayout *leftLayout;
-  QVBoxLayout *rightLayout;
+    QVBoxLayout *leftLayout;
+    QVBoxLayout *rightLayout;
 
-  QList<Club *> clubList;
+    QList<Club *> clubList;
 };
 
 class ScoreDialog: public QDialog
@@ -70,6 +87,7 @@ class ScoreDialog: public QDialog
 
 public:
   ScoreDialog(QWidget *parent = 0);
+  ~ScoreDialog();
   void init(Course *course, Score *score = 0);
   void results(QVector<QString> &scores);
   bool validate(void);
index 8cadfd2..30d2909 100644 (file)
 
 QString empty("");
 
-ScoreTableModel::ScoreTableModel(Score * s, Course * c, QObject *parent) 
+ScoreTableModel::ScoreTableModel(QObject *parent) 
     : QAbstractTableModel(parent)
 {
+    score = 0;
+    course = 0;
+}
+
+void ScoreTableModel::set(Score * s, Course * c)
+{
     score = s;
     course = c;
 }
@@ -222,9 +228,15 @@ QVariant ScoreTableModel::headerData(int section, Qt::Orientation orientation, i
 //
 // CourseTableModel
 //
-CourseTableModel::CourseTableModel(Course * c, QObject *parent) 
-    : QAbstractTableModel(parent), course(c)
+CourseTableModel::CourseTableModel(QObject *parent) 
+    : QAbstractTableModel(parent)
 {
+    course = 0;
+}
+
+void CourseTableModel::set(Course *c)
+{
+    course = c;
 }
 
 int CourseTableModel::rowCount(const QModelIndex &) const
index c6f16c4..dc3832b 100644 (file)
@@ -5,6 +5,8 @@
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, version 2.
  */
+#ifndef TABLE_MODEL_H
+#define TABLE_MODEL_H
 
 #include <QStringList>
 #include <QAbstractTableModel>
@@ -19,8 +21,9 @@ class ScoreTableModel : public QAbstractTableModel
     Q_OBJECT
 
 public:
-    ScoreTableModel(Score *, Course *, QObject *parent = 0);
+    ScoreTableModel(QObject *parent = 0);
 
+    void set(Score *, Course *);
     int rowCount(const QModelIndex & parent) const;
     int columnCount(const QModelIndex & parent) const;
     QVariant data(const QModelIndex & index, int role) const;
@@ -52,8 +55,9 @@ class CourseTableModel : public QAbstractTableModel
     Q_OBJECT
 
 public:
-    CourseTableModel(Course *, QObject *parent = 0);
+    CourseTableModel(QObject *parent = 0);
 
+    void set(Course *);
     int rowCount(const QModelIndex & parent) const;
     int columnCount(const QModelIndex & parent) const;
     QVariant data(const QModelIndex & index, int role) const;
@@ -78,3 +82,4 @@ private:
     Club *club;
     Course *course;
 };
+#endif