TODO list update. Initial screen for pro mode
[scorecard] / src / main-window.cpp
index 33c4e20..08e61fd 100644 (file)
@@ -24,6 +24,7 @@ QString topDir("/opt");
 QString mmcDir("/media/mmc1");
 QString dataDirName("data");
 QString dataDir;
+QString userDataDir;
 QString imgDir(topDir + "/pixmaps");
 QString scoreFileName("score.xml");
 QString scoreFile;
@@ -37,16 +38,28 @@ QString titleCourses("ScoreCard - Courses");
 
 bool dateLessThan(const Score *s1, const Score *s2)
 {
-  return (*s1) < (*s2);
+    return (*s1) < (*s2);
 }
 
 bool dateMoreThan(const Score *s1, const Score *s2)
 {
-  return (*s1) > (*s2);
+    return (*s1) > (*s2);
 }
+
+bool scoreMoreThan(const Score *s1, const Score *s2)
+{
+    return s1->getTotal(Total).toInt() > s2->getTotal(Total).toInt();
+}
+
+bool scoreLessThan(const Score *s1, const Score *s2)
+{
+    return s1->getTotal(Total).toInt() < s2->getTotal(Total).toInt();
+}
+
 // Find score based on club and course name
 Score *MainWindow::findScore(QString & clubName, QString & courseName)
 {
+    TRACE;
     QListIterator<Score *> i(scoreList);
     Score * s;
 
@@ -62,6 +75,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;
 
@@ -77,6 +91,7 @@ Club *MainWindow::findClub(QString &name)
 Course *MainWindow::findCourse(const QString &clubName, 
                                const QString &courseName)
 {
+    TRACE;
     QListIterator<Club *> i(clubList);
     Club *c;
 
@@ -91,13 +106,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;
@@ -105,90 +125,195 @@ 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;
+
+    while (i.hasNext()) {
+        c = i.next();
+        if (c->isReadOnly()) {
+            qDebug() << "Del:" << c->getName();
+            i.remove();
+        }
+    }
+}
+
+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);
+    }
+}
+
+void MainWindow::sortScoreList()
+{
+    if (conf.sortOrder == "Date")
+        qSort(scoreList.begin(), scoreList.end(), dateMoreThan); 
+    else if (conf.sortOrder == "Score")
+        qSort(scoreList.begin(), scoreList.end(), scoreLessThan); 
+}
+
 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);
+    markHomeClub();
 
-  loadScoreFile(scoreFile, scoreList);
-  loadClubFile(masterFile, clubList, true);
-  loadClubFile(clubFile, clubList);
+    // Sort the scores based on settings
+    sortScoreList();
 
-  // Sort the scores based on dates
-  qSort(scoreList.begin(), scoreList.end(), dateMoreThan); 
-  createActions();
-  createMenus();
+    createActions();
+    createMenus();
 
-  createListView(scoreList, clubList);
+    createListView(scoreList, clubList);
 
-  createLayoutList(centralWidget);
+    createLayoutList(centralWidget);
+
+    scoreWindow = new ScoreWindow(this);
+    courseWindow = new CourseWindow(this);
 }
 
 void MainWindow::loadSettings(void)
 {
-  bool external = false;
+    TRACE;
+    bool external = false;
 
-  QDir mmc(mmcDir);
-  if (mmc.exists())
-    external = true;
+    QDir mmc(mmcDir);
+    if (mmc.exists())
+        external = true;
 
-  // TODO: make via user option, automatic will never work
-  external = false;
+    // TODO: make via user option, automatic will never work
+    external = false;
 
 #ifndef Q_WS_MAEMO_5
-  dataDir = "./" + dataDirName;
+    dataDir = "./" + dataDirName;
 #else
-  if (external) {
-    dataDir = mmcDir + "/" + appName + "/" + dataDirName;
-  }
-  else {
-    dataDir = topDir + "/" + appName + "/" + dataDirName;
-  }
+    if (external) {
+        dataDir = mmcDir + "/" + appName + "/" + dataDirName;
+    }
+    else {
+        dataDir = topDir + "/" + appName + "/" + dataDirName;
+    }
 #endif
-  scoreFile = dataDir + "/" + scoreFileName;
-  clubFile = dataDir + "/" + clubFileName;
-  masterFile = dataDir + "/" + masterFileName;
-
-  QDir dir(dataDir);
-  if (!dir.exists())
-    if (!dir.mkpath(dataDir)) {
-      qWarning() << "Unable to create: " + dataDir;
-      return;
+
+    // Use MyDoc directory to get automatic backup/restore 
+    userDataDir = QDir::homePath() + "/MyDocs/." + appName;
+    QDir dir(userDataDir);
+    if (!dir.exists())
+        if (!dir.mkpath(userDataDir)) {
+            qWarning() << "Unable to create: " + userDataDir;
+            return;
+        }
+
+    masterFile = dataDir + "/" + masterFileName;
+
+    // Store user data files under $HOME so they are not lost in
+    // re-flash
+    scoreFile = userDataDir + "/" + scoreFileName;
+    clubFile = userDataDir + "/" + clubFileName;
+
+    // Start of 0.19 migration
+    // Copy existing user data to new location
+    // 0.18 and earlier: score.xml and club.xml are in /opt/scorecard/data
+    // 0.19 and later: score.xml and club.xml are in /home/user/MyDocs/.scorecard
+    QString scoreFileOld = dataDir + "/" + scoreFileName;
+    QString clubFileOld = dataDir + "/" + clubFileName;
+
+    QFile file1(scoreFileOld);
+    QFile file2(clubFileOld);
+    QDir move;
+    if (file1.exists()) {
+        move.rename(scoreFileOld, scoreFile);
+        qDebug() << "Moved: " << scoreFileOld << "->" << scoreFile;
+    }
+    if (file2.exists()) {
+        move.rename(clubFileOld, clubFile);
+        qDebug() << "Moved: " << clubFileOld << "->" << clubFile;
     }
-  qDebug() << "Data is at:" + dataDir;
+    // End of 0.19 migration
+
+    qDebug() << "User data is at:" + userDataDir;
+
+    settings.beginGroup(settingsGroup);
+    conf.hcp = settings.value(settingsHcp);
+    conf.homeClub = settings.value(settingsHomeClub);
+    conf.sortOrder = settings.value(settingsSortOrder);
+    conf.userMode = settings.value(settingsUserMode);
+    conf.defaultCourses = settings.value(settingsDefaultCourses);
+    settings.endGroup();
+
+    // Use default courses if no settings for that
+    if (!conf.defaultCourses.isValid())
+        conf.defaultCourses = "Yes";
+
+    // Use date sort order if no settings for that
+    if (!conf.sortOrder.isValid())
+        conf.sortOrder = "Date";
 
-  settings.beginGroup("Settings");
-  conf.hcp = settings.value("hcp");
-  conf.homeClub = settings.value("home-club");
-  conf.defaultCourses = settings.value("default-courses");
-  settings.endGroup();
-  qDebug() << "Settings: " << conf.hcp << conf.homeClub << conf.defaultCourses;
+    // Use basic mode if no settings for that
+    if (!conf.userMode.isValid())
+        conf.userMode = "Basic";
+
+    qDebug() << "Settings: " << conf.hcp << conf.homeClub << conf.sortOrder << conf.userMode << conf.defaultCourses;
 }
 
 void MainWindow::saveSettings(void)
 {
-    settings.beginGroup("Settings");
+    TRACE;
+    settings.beginGroup(settingsGroup);
     if (conf.hcp.isValid())
-        settings.setValue("hcp", conf.hcp);
+        settings.setValue(settingsHcp, conf.hcp);
     if (conf.homeClub.isValid())
-        settings.setValue("home-club", conf.homeClub);
+        settings.setValue(settingsHomeClub, conf.homeClub);
+    if (conf.sortOrder.isValid())
+        settings.setValue(settingsSortOrder, conf.sortOrder);
+    if (conf.userMode.isValid())
+        settings.setValue(settingsUserMode, conf.userMode);
     if (conf.defaultCourses.isValid())
-        settings.setValue("default-courses", conf.defaultCourses);
+        settings.setValue(settingsDefaultCourses, conf.defaultCourses);
     settings.endGroup();
 }
 
-
 void MainWindow::createLayoutList(QWidget *parent)
 {
+    TRACE;
     QVBoxLayout * tableLayout = new QVBoxLayout;
     tableLayout->addWidget(list);
 
@@ -200,12 +325,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);
@@ -219,6 +345,7 @@ void MainWindow::createListView(QList<Score *> &scoreList,
 
 void MainWindow::listScores()
 {
+    TRACE;
     list->setModel(scoreListModel);
     selectionModel = list->selectionModel();
     updateTitleBar(titleScores);
@@ -226,6 +353,7 @@ void MainWindow::listScores()
 
 void MainWindow::listCourses()
 {
+    TRACE;
     list->setModel(courseListModel);
     selectionModel = list->selectionModel();
     updateTitleBar(titleCourses);
@@ -233,6 +361,7 @@ void MainWindow::listCourses()
 
 void MainWindow::createActions()
 {
+    TRACE;
     newScoreAction = new QAction(tr("New Score"), this);
     connect(newScoreAction, SIGNAL(triggered()), this, SLOT(newScore()));
 
@@ -261,6 +390,7 @@ void MainWindow::createActions()
 
 void MainWindow::createMenus()
 {
+    TRACE;
 #ifdef Q_WS_MAEMO_5
     menu = menuBar()->addMenu("");
 #else
@@ -276,6 +406,7 @@ void MainWindow::createMenus()
 
 void MainWindow::updateTitleBar(QString & msg)
 {
+    TRACE;
     setWindowTitle(msg);
 }
 
@@ -290,6 +421,7 @@ void MainWindow::showNote(QString msg)
 
 void MainWindow::clickedList(const QModelIndex &index)
 {
+    TRACE;
     int row = index.row();
 
     const QAbstractItemModel *m = index.model();
@@ -302,7 +434,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"));
@@ -313,9 +445,137 @@ void MainWindow::clickedList(const QModelIndex &index)
     }
 }
 
+void MainWindow::viewScore(Score * score, Course * course)
+{
+    TRACE;
+    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;
+        if (conf.userMode == "Basic")
+            scoreDialog = (ScoreDialog *)new ScoreDialog18(this);
+        else
+            scoreDialog = (ScoreDialog *)new ScoreDialogSingle(this);
+
+        QString title = "New Score: " + courseName + ", " + date;
+        scoreDialog->setWindowTitle(title);
+
+        qDebug() << clubName << courseName;
+
+        Club *club = findClub(clubName);
+        if (!club) {
+            showNote(tr("No 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 settings
+            sortScoreList();
+            // 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) {
+        showNote(tr("No score or course to edit"));
+        return;
+    }
+
+    QString date = score->getDate();
+
+    ScoreDialog18 *scoreDialog = new ScoreDialog18(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();
@@ -344,7 +604,7 @@ void MainWindow::newCourse()
             if (club) {
                 course = club->getCourse(courseName);
                 if (course) {
-                    qDebug() << "Error: club/course already in the database";
+                    showNote(tr("Club/course already in the database"));
                     return;
                 }
                 else {
@@ -367,37 +627,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"));
@@ -421,273 +654,150 @@ 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);
-
-    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);
+    TRACE;
+    Club *club = 0;
+    Course * course = currentCourse();
 
-        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();
+    club = course->parent();
 
-    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());
-
-    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);
-
-    QAction *delAction = new QAction(tr("Delete"), courseWin);
-    connect(delAction, SIGNAL(triggered()), this, SLOT(deleteCourse()));
-    courseWin->menuBar()->addAction(delAction);
+    StatModel *model = new StatModel(clubList, scoreList);
 
-    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
-
-  StatModel *model = new StatModel(clubList, scoreList);
 
-  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);
+    QWidget *central = new QWidget(win);
+    win->setCentralWidget(central);
 
-  QWidget *central = new QWidget(win);
-  win->setCentralWidget(central);
+    QTextEdit *textEdit = new QTextEdit;
 
-  QTextEdit *textEdit = new QTextEdit;
+    textEdit->setReadOnly(true);
 
-  textEdit->setReadOnly(true);
+    QVBoxLayout *infoLayout = new QVBoxLayout;
+    infoLayout->addWidget(table);
 
-  QVBoxLayout *infoLayout = new QVBoxLayout;
-  infoLayout->addWidget(table);
+    QHBoxLayout *mainLayout = new QHBoxLayout(central);
+    mainLayout->addLayout(infoLayout);
+    central->setLayout(mainLayout);
 
-  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);
 
     int result = dlg->exec();
     if (result) {
-        QString a, b, c;
-
+        QString oldValue = conf.defaultCourses.toString();
         dlg->results(conf);
-        //qDebug() <<  << b << c;
+        QString newValue = conf.defaultCourses.toString();
         saveSettings();
+
+        // Reload club list, or drop r/o courses from list
+        if (oldValue == "Yes" && newValue == "No") {
+            flushReadOnlyItems();
+            courseListModel->update(clubList);
+            list->update();
+        }
+        else if ((oldValue == "No" || oldValue == "") && newValue == "Yes") {
+            loadClubFile(masterFile, clubList, true);
+            courseListModel->update(clubList);
+            list->update();
+        }
+        // TODO: do these only if needed
+        markHomeClub();
+        sortScoreList();
+        scoreListModel->update(scoreList);
+        list->update();
     }
 }
 
 void MainWindow::loadScoreFile(QString &fileName, QList<Score *> &list)
 {
-  ScoreXmlHandler handler(list);
+    ScoreXmlHandler handler(list);
 
-  if (handler.parse(fileName))
-    qDebug() << "File loaded:" << fileName << " entries:" << list.size();
+    if (handler.parse(fileName))
+        qDebug() << "File loaded:" << fileName << " entries:" << list.size();
 }
 
 void MainWindow::saveScoreFile(QString &fileName, QList<Score *> &list)
 {
-  ScoreXmlHandler handler(list);
+    ScoreXmlHandler handler(list);
 
-  if (handler.save(fileName))
-    // TODO: banner
-    qDebug() << "File saved:" << fileName << " entries:" << list.size();
-  else
-    qWarning() << "Unable to save:" << fileName;
+    if (handler.save(fileName))
+        // TODO: banner
+        qDebug() << "File saved:" << fileName << " entries:" << list.size();
+    else
+        qWarning() << "Unable to save:" << fileName;
 }
 
 void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list, bool readOnly)
 {
-  ClubXmlHandler handler(list);
+    ClubXmlHandler handler(list);
 
-  if (handler.parse(fileName, readOnly))
-    qDebug() << "File loaded:" << fileName << " entries:" << list.size();
+    if (handler.parse(fileName, readOnly))
+        qDebug() << "File loaded:" << fileName << " entries:" << list.size();
 }
 
 void MainWindow::saveClubFile(QString &fileName, QList<Club *> &list)
 {
-  ClubXmlHandler handler(list);
-
-  if (handler.save(fileName))
-    // TODO: banner
-    qDebug() << "File saved:" << fileName << " entries:" << list.size();
-  else
-    qWarning() << "Unable to save:" << fileName;
+    ClubXmlHandler handler(list);
 
+    if (handler.save(fileName))
+        // TODO: banner
+        qDebug() << "File saved:" << fileName << " entries:" << list.size();
+    else
+        qWarning() << "Unable to save:" << fileName;
 }