Behave correctly if score contains unknown course
[scorecard] / src / main-window.cpp
index c22be4d..ad85c04 100644 (file)
@@ -34,10 +34,6 @@ QString masterFile;
 QString logFile("/tmp/scorecard.log");
 QString titleScores("ScoreCard - Scores");
 QString titleCourses("ScoreCard - Courses");
-QString settingsGroup("Settings");
-QString settingsHcp("hcp");
-QString settingsHomeClub("home-club");
-QString settingsDefaultCourses("default-courses");
 
 bool dateLessThan(const Score *s1, const Score *s2)
 {
@@ -51,6 +47,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 +63,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 +79,7 @@ Club *MainWindow::findClub(QString &name)
 Course *MainWindow::findCourse(const QString &clubName, 
                                const QString &courseName)
 {
+    TRACE;
     QListIterator<Club *> i(clubList);
     Club *c;
 
@@ -95,13 +94,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 +113,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;
 
@@ -123,38 +141,57 @@ void MainWindow::flushReadOnlyItems()
     }
 }
 
+void MainWindow::markHomeClub()
+{
+    TRACE;
+    QListIterator<Club *> i(clubList);
+    Club *c;
+
+    while (i.hasNext()) {
+        c = i.next();
+        if (c->getName() == conf.homeClub)
+            c->setHomeClub(true);
+        else
+            c->setHomeClub(false);
+    }
+}
 
 MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
 {
-  resize(800, 480);
+    resize(800, 480);
 
 #ifdef Q_WS_MAEMO_5
-  setAttribute(Qt::WA_Maemo5StackedWindow);
+    setAttribute(Qt::WA_Maemo5StackedWindow);
 #endif
 
-  loadSettings();
+    loadSettings();
 
-  centralWidget = new QWidget(this);
+    centralWidget = new QWidget(this);
 
-  setCentralWidget(centralWidget);
+    setCentralWidget(centralWidget);
 
-  loadScoreFile(scoreFile, scoreList);
-  if (conf.defaultCourses == "Yes")
-      loadClubFile(masterFile, clubList, true);
-  loadClubFile(clubFile, clubList);
+    loadScoreFile(scoreFile, scoreList);
+    if (conf.defaultCourses == "Yes")
+        loadClubFile(masterFile, clubList, true);
+    loadClubFile(clubFile, clubList);
+    markHomeClub();
 
-  // Sort the scores based on dates
-  qSort(scoreList.begin(), scoreList.end(), dateMoreThan); 
-  createActions();
-  createMenus();
+    // Sort the scores based on dates
+    qSort(scoreList.begin(), scoreList.end(), dateMoreThan); 
+    createActions();
+    createMenus();
 
-  createListView(scoreList, clubList);
+    createListView(scoreList, clubList);
 
-  createLayoutList(centralWidget);
+    createLayoutList(centralWidget);
+
+    scoreWindow = new ScoreWindow(this);
+    courseWindow = new CourseWindow(this);
 }
 
 void MainWindow::loadSettings(void)
 {
+    TRACE;
   bool external = false;
 
   QDir mmc(mmcDir);
@@ -201,6 +238,7 @@ void MainWindow::loadSettings(void)
 
 void MainWindow::saveSettings(void)
 {
+    TRACE;
     settings.beginGroup(settingsGroup);
     if (conf.hcp.isValid())
         settings.setValue(settingsHcp, conf.hcp);
@@ -213,6 +251,7 @@ void MainWindow::saveSettings(void)
 
 void MainWindow::createLayoutList(QWidget *parent)
 {
+    TRACE;
     QVBoxLayout * tableLayout = new QVBoxLayout;
     tableLayout->addWidget(list);
 
@@ -224,12 +263,13 @@ void MainWindow::createLayoutList(QWidget *parent)
 void MainWindow::createListView(QList<Score *> &scoreList, 
                                 QList <Club *> &clubList)
 {
+    TRACE;
     list = new QListView(this);
 
     scoreListModel = new ScoreListModel(scoreList, clubList);
     courseListModel = new CourseListModel(clubList);
 
-    list->setStyleSheet(ScoreStyle::style());
+    list->setStyleSheet(defaultStyleSheet);
 
     list->setSelectionMode(QAbstractItemView::SingleSelection);
     list->setProperty("FingerScrolling", true);
@@ -243,6 +283,7 @@ void MainWindow::createListView(QList<Score *> &scoreList,
 
 void MainWindow::listScores()
 {
+    TRACE;
     list->setModel(scoreListModel);
     selectionModel = list->selectionModel();
     updateTitleBar(titleScores);
@@ -250,6 +291,7 @@ void MainWindow::listScores()
 
 void MainWindow::listCourses()
 {
+    TRACE;
     list->setModel(courseListModel);
     selectionModel = list->selectionModel();
     updateTitleBar(titleCourses);
@@ -257,6 +299,7 @@ void MainWindow::listCourses()
 
 void MainWindow::createActions()
 {
+    TRACE;
     newScoreAction = new QAction(tr("New Score"), this);
     connect(newScoreAction, SIGNAL(triggered()), this, SLOT(newScore()));
 
@@ -285,6 +328,7 @@ void MainWindow::createActions()
 
 void MainWindow::createMenus()
 {
+    TRACE;
 #ifdef Q_WS_MAEMO_5
     menu = menuBar()->addMenu("");
 #else
@@ -300,6 +344,7 @@ void MainWindow::createMenus()
 
 void MainWindow::updateTitleBar(QString & msg)
 {
+    TRACE;
     setWindowTitle(msg);
 }
 
@@ -314,6 +359,7 @@ void MainWindow::showNote(QString msg)
 
 void MainWindow::clickedList(const QModelIndex &index)
 {
+    TRACE;
     int row = index.row();
 
     const QAbstractItemModel *m = index.model();
@@ -326,7 +372,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,9 +383,130 @@ void MainWindow::clickedList(const QModelIndex &index)
     }
 }
 
+void MainWindow::viewScore(Score * score, Course * course)
+{
+    TRACE;
+    qDebug() << score << course;
+    scoreWindow->setup(score, course);
+    scoreWindow->show();
+}
+
+void MainWindow::newScore()
+{
+    TRACE;
+    SelectDialog *selectDialog = new SelectDialog(this);
+
+    selectDialog->init(clubList);
+
+    int result = selectDialog->exec();
+    if (result) {
+        QString clubName;
+        QString courseName;
+        QString date;
+
+        selectDialog->results(clubName, courseName, date);
+
+        ScoreDialog *scoreDialog = new ScoreDialog(this);
+        QString title = "New Score: " + courseName + ", " + date;
+        scoreDialog->setWindowTitle(title);
+
+        Club *club = findClub(clubName);
+        if (!club) {
+            showNote(tr("Error: no such club"));
+            return;
+        }
+        Course *course = club->getCourse(courseName);
+        if (!course) {
+            showNote(tr("Error: no such course:"));
+            return;
+        }
+        scoreDialog->init(course);
+        result = scoreDialog->exec();
+        if (result) {
+            QVector<QString> scores(18);
+
+            scoreDialog->results(scores);
+            Score *score = new Score(scores, clubName, courseName, date);
+            scoreList << score;
+
+            // Sort the scores based on dates
+            qSort(scoreList.begin(), scoreList.end(), dateMoreThan); 
+            // Save it
+            saveScoreFile(scoreFile, scoreList);
+            scoreListModel->update(scoreList);
+            list->update();
+        }
+    }
+}
+
+void MainWindow::editScore()
+{
+    TRACE;
+    Course * course = 0;
+    Score *score = currentScore();
+
+    if (score) 
+        course = findCourse(score->getClubName(), score->getCourseName());
+
+    if (!course || !score) {
+        qDebug() << "No score/course to edit";
+        return;
+    }
+
+    QString date = score->getDate();
+
+    ScoreDialog *scoreDialog = new ScoreDialog(this);
+    scoreDialog->init(course, score);
+  
+    QString title = "Edit Score: " + course->getName() + ", " + date;
+    scoreDialog->setWindowTitle(title);
+
+    int result = scoreDialog->exec();
+    if (result) {
+        QVector<QString> scores(18);
+
+        scoreDialog->results(scores);
+    
+        score->update(scores);
+
+        // Sort the scores based on dates
+        qSort(scoreList.begin(), scoreList.end(), dateMoreThan); 
+        // Save it
+        saveScoreFile(scoreFile, scoreList);
+    }
+    if (scoreDialog)
+        delete scoreDialog;
+}
+
+void MainWindow::deleteScore()
+{
+    TRACE;
+    if (scoreWindow)
+        scoreWindow->close();
+
+    QModelIndex index = selectionModel->currentIndex();
+    if (!index.isValid()) {
+        qDebug() << "Invalid index";
+        return;
+    }
+    
+    scoreList.removeAt(index.row());
+    // Save it
+    saveScoreFile(scoreFile, scoreList);
+    scoreListModel->update(scoreList);
+    list->update();
+}
+
+void MainWindow::viewCourse(Course * course)
+{
+    TRACE;
+    courseWindow->setup(course);
+    courseWindow->show();
+}
 
 void MainWindow::newCourse()
 {
+    TRACE;
     CourseSelectDialog *selectDialog = new CourseSelectDialog(this);
 
     int result = selectDialog->exec();
@@ -391,37 +558,10 @@ void MainWindow::newCourse()
     }
 }
 
-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()
 {
-    Course *course = findCourse();
+    TRACE;
+    Course *course = currentCourse();
 
     if (!course) {
         showNote(tr("No course on edit"));
@@ -445,224 +585,86 @@ void MainWindow::editCourse()
         course->update(par, hcp, len);
         saveClubFile(clubFile, clubList);
     }
+    if (courseDialog)
+        delete courseDialog;
 }
 
-void MainWindow::newScore()
+void MainWindow::deleteCourse()
 {
-    SelectDialog *selectDialog = new SelectDialog(this);
+    TRACE;
+    Club *club = 0;
+    Course * course = currentCourse();
 
-    selectDialog->init(clubList);
-
-    int result = selectDialog->exec();
-    if (result) {
-        QString clubName;
-        QString courseName;
-        QString date;
-
-        selectDialog->results(clubName, courseName, date);
-
-        ScoreDialog *scoreDialog = new ScoreDialog(this);
-        QString title = "New Score: " + courseName + ", " + date;
-        scoreDialog->setWindowTitle(title);
-
-        Club *club = findClub(clubName);
-        if (!club) {
-            showNote(tr("Error: no such club"));
-            return;
-        }
-        Course *course = club->getCourse(courseName);
-        if (!course) {
-            showNote(tr("Error: no such course:"));
-            return;
-        }
-        scoreDialog->init(course);
-        result = scoreDialog->exec();
-        if (result) {
-            QVector<QString> scores(18);
-
-            scoreDialog->results(scores);
-            Score *score = new Score(scores, clubName, courseName, date);
-            scoreList << score;
-
-            // Sort the scores based on dates
-            qSort(scoreList.begin(), scoreList.end(), dateMoreThan); 
-            // Save it
-            saveScoreFile(scoreFile, scoreList);
-            scoreListModel->update(scoreList);
-            list->update();
-        }
+    if (!course) {
+        qDebug() << "Invalid course for deletion";
+        return;
     }
-}
-
-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());
-    Course * course = 0;
-    if (score) 
-        course = findCourse(score->getClubName(), score->getCourseName());
+    club = course->parent();
 
-    if (!course || !score) {
-        qDebug() << "No score/course to edit";
+    // 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();
 
-    QString date = score->getDate();
-
-    ScoreDialog *scoreDialog = new ScoreDialog;
-  
-    QString title = "Edit Score: " + course->getName() + ", " + date;
-    scoreDialog->setWindowTitle(title);
-
-    scoreDialog->init(course, score);
-
-    int result = scoreDialog->exec();
-
-    if (result) {
-        QVector<QString> scores(18);
-
-        scoreDialog->results(scores);
-    
-        score->update(scores);
+    club->delCourse(course);
 
-        // Sort the scores based on dates
-        qSort(scoreList.begin(), scoreList.end(), dateMoreThan); 
-        // Save it
-        saveScoreFile(scoreFile, scoreList);
-        // Update the model
-        scoreListModel->update(scoreList);
+    if (club->isEmpty()) {
+        int index = clubList.indexOf(club);
+        if (index != -1)
+            clubList.removeAt(index);
     }
-}
 
-void MainWindow::viewScore(Score * score, Course * course)
-{
-    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);
-
-    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);
-    
-    central->setLayout(layout);
-    scoreWin->show();
+    // Save it
+    saveClubFile(clubFile, clubList);
+    courseListModel->update(clubList);
+    list->update();
 }
 
-void MainWindow::viewCourse(Course * course)
+void MainWindow::viewStatistics()
 {
-    courseWin = new QMainWindow(this);
-    QString title = QString("Course: %1, Par - %2").arg(course->getName()).arg(course->getTotal(Total));
-    courseWin->setWindowTitle(title);
+    TRACE;
+    QMainWindow *win = new QMainWindow(this);
+    QString title = "Statistics";
+    win->setWindowTitle(title);
 #ifdef Q_WS_MAEMO_5
-    courseWin->setAttribute(Qt::WA_Maemo5StackedWindow);
+    win->setAttribute(Qt::WA_Maemo5StackedWindow);
 #endif
 
-    QAction *editAction = new QAction(tr("Edit"), courseWin);
-    connect(editAction, SIGNAL(triggered()), this, SLOT(editCourse()));
-    courseWin->menuBar()->addAction(editAction);
+    StatModel *model = new StatModel(clubList, scoreList);
 
-    QAction *delAction = new QAction(tr("Delete"), courseWin);
-    connect(delAction, SIGNAL(triggered()), this, SLOT(deleteCourse()));
-    courseWin->menuBar()->addAction(delAction);
-
-    CourseTableModel *model = new CourseTableModel(course);
-    
-    QTableView * table = new QTableView;
+    QTableView *table = new QTableView;
     table->showGrid();
     table->setSelectionMode(QAbstractItemView::NoSelection);
-    //table->setStyleSheet(ScoreStyle::headerView());
-    table->setStyleSheet(ScoreStyle::style());
+    table->setStyleSheet(statStyleSheet);
     table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
     table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
-    table->horizontalHeader()->hide();
+    table->verticalHeader()->setAutoFillBackground(true);
     table->setModel(model);
-    
-    QWidget *central = new QWidget(courseWin);
-    courseWin->setCentralWidget(central);
-    
-    QVBoxLayout *layout = new QVBoxLayout;
-    layout->addWidget(table);
-    
-    central->setLayout(layout);
-    courseWin->show();
-}
 
-void MainWindow::viewStatistics()
-{
-  QMainWindow *win = new QMainWindow(this);
-  QString title = "Statistics";
-  win->setWindowTitle(title);
-#ifdef Q_WS_MAEMO_5
-  win->setAttribute(Qt::WA_Maemo5StackedWindow);
-#endif
+    QWidget *central = new QWidget(win);
+    win->setCentralWidget(central);
 
-  StatModel *model = new StatModel(clubList, scoreList);
+    QTextEdit *textEdit = new QTextEdit;
 
-  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->verticalHeader()->setAutoFillBackground(true);
-  table->setModel(model);
+    textEdit->setReadOnly(true);
 
-  QWidget *central = new QWidget(win);
-  win->setCentralWidget(central);
+    QVBoxLayout *infoLayout = new QVBoxLayout;
+    infoLayout->addWidget(table);
 
-  QTextEdit *textEdit = new QTextEdit;
+    QHBoxLayout *mainLayout = new QHBoxLayout(central);
+    mainLayout->addLayout(infoLayout);
+    central->setLayout(mainLayout);
 
-  textEdit->setReadOnly(true);
-
-  QVBoxLayout *infoLayout = new QVBoxLayout;
-  infoLayout->addWidget(table);
-
-  QHBoxLayout *mainLayout = new QHBoxLayout(central);
-  mainLayout->addLayout(infoLayout);
-  central->setLayout(mainLayout);
-
-  win->show();
+    win->show();
 }
 
 void MainWindow::viewSettings()
 {
+    TRACE;
     SettingsDialog *dlg = new SettingsDialog(this);
 
     dlg->init(conf, clubList);
@@ -674,8 +676,6 @@ void MainWindow::viewSettings()
         QString newValue = conf.defaultCourses.toString();
         saveSettings();
 
-        qDebug() << "Settings:" << oldValue << "->" << newValue;
-
         // Reload club list, or drop r/o courses from list
         if (oldValue == "Yes" && newValue == "No") {
             flushReadOnlyItems();
@@ -687,6 +687,7 @@ void MainWindow::viewSettings()
             courseListModel->update(clubList);
             list->update();
         }
+        markHomeClub();
     }
 }