- Fixed course delete crashes
[scorecard] / src / main-window.cpp
index c235605..deda9ef 100644 (file)
@@ -1,19 +1,27 @@
+/*
+ * Copyright (C) 2009 Sakari Poussa
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 2.
+ */
+
 #include <QtGui>
-#include <QDirModel>
-#include <QTreeView>
-#include <QListView>
-#include <QStandardItemModel>
+#ifdef Q_WS_MAEMO_5
+#include <QMaemo5InformationBox>
+#endif
 
+#include "score-common.h"
 #include "main-window.h"
 #include "score-dialog.h"
 #include "course-dialog.h"
-#include "tree-widget.h"
-#include "xml-parser.h"
+#include "stat-model.h"
 #include "xml-dom-parser.h"
 
-QString topDir("/opt/scorecard");
-QString mmcDir("/media/mmc1/scorecard");
-QString dataDirName("/data/");
+QString appName("scorecard");
+QString topDir("/opt");
+QString mmcDir("/media/mmc1");
+QString dataDirName("data");
 QString dataDir;
 QString imgDir(topDir + "/pixmaps");
 QString scoreFileName("score.xml");
@@ -21,342 +29,584 @@ QString scoreFile;
 QString clubFileName("club.xml");
 QString clubFile;
 QString logFile("/tmp/scorecard.log");
+QString titleScores("ScoreCard - Scores");
+QString titleCourses("ScoreCard - Courses");
 
-MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
+bool dateLessThan(const Score *s1, const Score *s2)
 {
-  resize(800, 480);
+  return (*s1) < (*s2);
+}
 
-  loadSettings();
+bool dateMoreThan(const Score *s1, const Score *s2)
+{
+  return (*s1) > (*s2);
+}
+// Find score based on club and course name
+Score *MainWindow::findScore(QString & clubName, QString & courseName)
+{
+    QListIterator<Score *> i(scoreList);
+    Score * s;
+
+    while (i.hasNext()) {
+        s = i.next();
+        if ((s->getClubName() == clubName) &&
+            (s->getCourseName() == courseName))
+            return s;
+    }
+    return 0;
+}
 
-  QWidget *centralWidget = new QWidget(this);
+// Find club based on name
+Club *MainWindow::findClub(QString &name)
+{
+    QListIterator<Club *> i(clubList);
+    Club *c;
 
-  // TODO: move to proper function
-  tableViewFront = new QTableView(centralWidget);
-  tableViewBack = new QTableView(centralWidget);
+    while (i.hasNext()) {
+        c = i.next();
+        if (c->getName() == name)
+            return c;
+    }
+    return 0;
+}
 
-  setCentralWidget(centralWidget);
+// Find course based on club & course name
+Course *MainWindow::findCourse(const QString &clubName, 
+                               const QString &courseName)
+{
+    QListIterator<Club *> i(clubList);
+    Club *c;
+
+    while (i.hasNext()) {
+        c = i.next();
+        if (c->getName() == clubName) {
+            return c->getCourse(courseName);
+        }
+    }
+    return 0;
+}
 
-  loadScoreFile(scoreFile, scoreList);
-  loadClubFile(clubFile, clubList);
+// 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()
+{
+    QModelIndex index = selectionModel->currentIndex();
+    const QAbstractItemModel *model = selectionModel->model();
+    QString str = model->data(index, Qt::DisplayRole).toString();
+
+    QStringList strList = str.split(",");
+    if (strList.count() != 2) {
+        showNote(tr("Invalid course selection"));
+        return 0;
+    }
+    return findCourse(strList[0], strList[1]);
+}
+
+MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
+{
+  resize(800, 480);
 
-#ifdef TEST
-  saveScoreFile(scoreFileWr, scoreList);
-  saveClubFile(clubFileWr, clubList);
+#ifdef Q_WS_MAEMO_5
+  setAttribute(Qt::WA_Maemo5StackedWindow);
 #endif
 
-  createTableView(scoreList, clubList);
-  //createTreeView(scoreList, parent);
-  createStatusBar();
+  loadSettings();
 
-  createLayout(centralWidget);
+  centralWidget = new QWidget(this);
 
+  setCentralWidget(centralWidget);
+
+  loadScoreFile(scoreFile, scoreList);
+  loadClubFile(clubFile, clubList);
+
+  // Sort the scores based on dates
+  qSort(scoreList.begin(), scoreList.end(), dateMoreThan); 
   createActions();
   createMenus();
+
+  createListView(scoreList, clubList);
+
+  createLayoutList(centralWidget);
 }
 
 void MainWindow::loadSettings(void)
 {
   bool external = false;
 
-#ifndef Q_WS_HILDON
-  topDir = ".";
-#endif
-
   QDir mmc(mmcDir);
   if (mmc.exists())
     external = true;
 
+  // TODO: make via user option, automatic will never work
+  external = false;
+
+#ifndef Q_WS_MAEMO_5
+  dataDir = "./" + dataDirName;
+#else
   if (external) {
-    dataDir = mmcDir + dataDirName;
+    dataDir = mmcDir + "/" + appName + "/" + dataDirName;
   }
   else {
-    dataDir = topDir + dataDirName;
+    dataDir = topDir + "/" + appName + "/" + dataDirName;
   }
-  scoreFile = dataDir + scoreFileName;
-  clubFile = dataDir + clubFileName;
+#endif
+  scoreFile = dataDir + "/" + scoreFileName;
+  clubFile = dataDir + "/" + clubFileName;
 
   QDir dir(dataDir);
   if (!dir.exists())
-    if (!dir.mkdir(dataDir)) {
-      // TODO: mkdir does not work...
-      qDebug() << "Unable to create: " + dataDir;
+    if (!dir.mkpath(dataDir)) {
+      qWarning() << "Unable to create: " + dataDir;
       return;
     }
-  qDebug() << "Data is at: " + dataDir;
+  qDebug() << "Data is at:" + dataDir;
 }
 
-void MainWindow::createLayout(QWidget *parent)
+void MainWindow::createLayoutList(QWidget *parent)
 {
-  buttonLayout = new QVBoxLayout;
-  //labelLayout->addStretch();
-  buttonLayout->addWidget(nextButton);
-  buttonLayout->addWidget(prevButton);
-  buttonLayout->addWidget(lastButton);
-  buttonLayout->addWidget(firstButton);
-
-  tableLayout = new QVBoxLayout;
-  tableLayout->addWidget(tableViewFront);
-  tableLayout->addWidget(tableViewBack);
-
-  QHBoxLayout *mainLayout = new QHBoxLayout(parent);
-  mainLayout->addLayout(tableLayout);
-  mainLayout->addLayout(buttonLayout);
-  setLayout(mainLayout);
+    QVBoxLayout * tableLayout = new QVBoxLayout;
+    tableLayout->addWidget(list);
+
+    QHBoxLayout *mainLayout = new QHBoxLayout(parent);
+    mainLayout->addLayout(tableLayout);
+    parent->setLayout(mainLayout);
 }
 
-// Setup 'score' tab view
-void MainWindow::createTableView(QList<Score *> &scoreList, QList <Club *> &clubList)
+void MainWindow::createListView(QList<Score *> &scoreList, 
+                                QList <Club *> &clubList)
 {
-  nextButton = new QPushButton(tr(">"));
-  prevButton = new QPushButton(tr("<"));
-  firstButton = new QPushButton(tr("<<"));
-  lastButton = new QPushButton(tr(">>"));
-
-  connect(nextButton, SIGNAL(clicked()), this, SLOT(nextButtonClicked()));
-  connect(prevButton, SIGNAL(clicked()), this, SLOT(prevButtonClicked()));
-  connect(firstButton, SIGNAL(clicked()), this, SLOT(firstButtonClicked()));
-  connect(lastButton, SIGNAL(clicked()), this, SLOT(lastButtonClicked()));
-
-  scoreTableModel = new ScoreTableModel();
-  scoreTableModel->setScore(scoreList);
-  scoreTableModel->setClub(clubList);
-
-  tableViewFront->showGrid();
-  tableViewBack->showGrid();
-
-  tableViewFront->setModel(scoreTableModel);
-  tableViewBack->setModel(scoreTableModel);
-
-  // Flag model items which one belongs to front and back nines
-  tableViewFront->setRootIndex(scoreTableModel->index(0, 0));
-  tableViewBack->setRootIndex(scoreTableModel->index(0, 1));
-
-  // Fill out all the space with the tables
-  tableViewFront->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
-  tableViewFront->verticalHeader()->setResizeMode(QHeaderView::Stretch);
-  tableViewBack->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
-  tableViewBack->verticalHeader()->setResizeMode(QHeaderView::Stretch);
+    list = new QListView(this);
+
+    scoreListModel = new ScoreListModel(scoreList, clubList);
+    courseListModel = new CourseListModel(clubList);
+
+    list->setStyleSheet(ScoreStyle::style());
+
+    list->setSelectionMode(QAbstractItemView::SingleSelection);
+    list->setProperty("FingerScrolling", true);
+
+    // Initial view
+    listScores();
+
+    connect(list, SIGNAL(clicked(QModelIndex)),
+            this, SLOT(clickedList(QModelIndex)));
 }
 
-// When selection down in 'stat' view, this is called.
-void MainWindow::updateTreeView(const QModelIndex & index)
+void MainWindow::listScores()
 {
-  QString scope("Scope");
-  QString count("Rounds");
-  QString scoreAvg("Score (avg.)");
-  QString scoreBest("Score (best)");
-  QString score("Total");
-  QString scoreIn("Total in");
-  QString scoreOut("Total out");
-
-  QVariant str = scoreTreeModel->data(index, Qt::DisplayRole);
-  QVariant type = scoreTreeModel->data(index, ScoreTreeModel::Type);
-
-  qDebug() << "update(" << index.row() << "/" << index.column() << "):" << str << type;
-
-  tableModel->setData(tableModel->index(0, 0, QModelIndex()), scope);
-  tableModel->setData(tableModel->index(0, 1, QModelIndex()), str);
-
-  if (type == TreeItem::TypeDate) {
-    tableModel->setData(tableModel->index(1, 0, QModelIndex()), count);
-    tableModel->setData(tableModel->index(2, 0, QModelIndex()), scoreAvg);
-    tableModel->setData(tableModel->index(3, 0, QModelIndex()), scoreBest);
-  }    
-  else if (type == TreeItem::TypeScore) {
-    QVariant value = scoreTreeModel->data(index, ScoreTreeModel::Total);
-    tableModel->setData(tableModel->index(1, 0, QModelIndex()), score);
-    tableModel->setData(tableModel->index(1, 1, QModelIndex()), value);
-
-    value = scoreTreeModel->data(index, ScoreTreeModel::TotalOut);
-    tableModel->setData(tableModel->index(2, 0, QModelIndex()), scoreOut);
-    tableModel->setData(tableModel->index(2, 1, QModelIndex()), value);
-
-    value = scoreTreeModel->data(index, ScoreTreeModel::TotalIn);
-    tableModel->setData(tableModel->index(3, 0, QModelIndex()), scoreIn);
-    tableModel->setData(tableModel->index(3, 1, QModelIndex()), value);
-  }
+    list->setModel(scoreListModel);
+    selectionModel = list->selectionModel();
+    updateTitleBar(titleScores);
 }
 
-void MainWindow::createStatusBar()
+void MainWindow::listCourses()
 {
-  updateStatusBar();
+    list->setModel(courseListModel);
+    selectionModel = list->selectionModel();
+    updateTitleBar(titleCourses);
 }
 
 void MainWindow::createActions()
 {
-  newScoreAct = new QAction(tr("&New Score"), this);
-  connect(newScoreAct, SIGNAL(triggered()), this, SLOT(newScore()));
+    newScoreAction = new QAction(tr("New Score"), this);
+    connect(newScoreAction, SIGNAL(triggered()), this, SLOT(newScore()));
 
-  newCourseAct = new QAction(tr("&New Course"), this);
-  connect(newCourseAct, SIGNAL(triggered()), this, SLOT(newCourse()));
+    newCourseAction = new QAction(tr("New Course"), this);
+    connect(newCourseAction, SIGNAL(triggered()), this, SLOT(newCourse()));
 
-#if 0
-  viewScoreAct = new QAction(tr("&View Scores"), this);
-  connect(viewScoreAct, SIGNAL(triggered()), this, SLOT(viewScore()));
+    statAction = new QAction(tr("Statistics"), this);
+    connect(statAction, SIGNAL(triggered()), this, SLOT(viewStatistics()));
 
-  viewCourseAct = new QAction(tr("&View Courses"), this);
-  connect(viewCourseAct, SIGNAL(triggered()), this, SLOT(viewCourse()));
+    // Maemo5 style menu filters
+    filterGroup = new QActionGroup(this);
+    filterGroup->setExclusive(true);
 
-  viewStatisticAct = new QAction(tr("&View Statistics"), this);
-  connect(viewStatisticAct, SIGNAL(triggered()), this, SLOT(viewStatistic()));
-#endif
+    listScoreAction = new QAction(tr("Scores"), filterGroup);
+    listScoreAction->setCheckable(true);
+    listScoreAction->setChecked(true);
+    connect(listScoreAction, SIGNAL(triggered()), this, SLOT(listScores()));
+
+    listCourseAction = new QAction(tr("Courses"), filterGroup);
+    listCourseAction->setCheckable(true);
+    connect(listCourseAction, SIGNAL(triggered()), this, SLOT(listCourses()));
 }
 
 void MainWindow::createMenus()
 {
-  menu = menuBar()->addMenu(tr("fremantle"));
-#if 0
-  menu->addAction(viewScoreAct);
-  menu->addAction(viewCourseAct);
-  menu->addAction(viewStatisticAct);
+#ifdef Q_WS_MAEMO_5
+    menu = menuBar()->addMenu("");
+#else
+    menu = menuBar()->addMenu("Menu");
 #endif
-  menu->addAction(newScoreAct);
-  menu->addAction(newCourseAct);
-}
 
-void MainWindow::updateStatusBar()
-{
-  setWindowTitle(scoreTableModel->getInfoText());
+    menu->addAction(newScoreAction);
+    menu->addAction(newCourseAction);
+    menu->addAction(statAction);
+    menu->addActions(filterGroup->actions());
 }
 
-void MainWindow::firstButtonClicked()
+void MainWindow::updateTitleBar(QString & msg)
 {
-  scoreTableModel->first();
-  updateStatusBar();
+    setWindowTitle(msg);
 }
 
-void MainWindow::lastButtonClicked()
+void MainWindow::showNote(QString msg)
 {
-  scoreTableModel->last();
-  updateStatusBar();
+#ifdef Q_WS_MAEMO_5
+    QMaemo5InformationBox::information(this, 
+                                       msg,
+                                       QMaemo5InformationBox::DefaultTimeout);
+#endif
 }
 
-void MainWindow::nextButtonClicked()
+void MainWindow::clickedList(const QModelIndex &index)
 {
-  scoreTableModel->next();
-  updateStatusBar();
+    int row = index.row();
+
+    const QAbstractItemModel *m = index.model();
+    if (m == scoreListModel) {
+        if (row < scoreList.count()) {
+            Score * score = scoreList.at(row);
+            Course * course = findCourse(score->getClubName(), score->getCourseName());
+            viewScore(score, course);
+        }
+    }
+    else if (m == courseListModel) {
+        QString str = courseListModel->data(index, Qt::DisplayRole).toString();
+        QStringList strList = str.split(",");
+
+        if (strList.count() != 2) {
+            showNote(QString("Invalid course selection"));
+            return;
+        }
+        Course * course = findCourse(strList.at(0), strList.at(1));
+        viewCourse(course);
+    }
 }
 
-void MainWindow::prevButtonClicked()
+
+void MainWindow::newCourse()
 {
-  scoreTableModel->prev();
-  updateStatusBar();
+    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) {
+                    qDebug() << "Error: 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();
+        }
+    }
 }
 
-// FIXME: dup code from table-model.cpp
-Club *MainWindow::findClub(QString &name)
+void MainWindow::deleteCourse()
 {
-  QListIterator<Club *> i(clubList);
-  Club *c;
+    Course * course = findCourse();
+    Club * club = course->parent();
 
-  while (i.hasNext()) {
-    c = i.next();
-    if (c->getName() == name)
-      return c;
-  }
-  return 0;
+    // 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::newCourse()
+void MainWindow::editCourse()
 {
-  CourseSelectDialog *selectDialog = new CourseSelectDialog(this);
-
-  int result = selectDialog->exec();
-  if (result) {
-    QString clubName;
-    QString courseName;
-    QString date;
+    Course *course = findCourse();
 
-    selectDialog->results(clubName, courseName);
+    if (!course) {
+        showNote(tr("No course on edit"));
+        return;
+    }
 
     CourseDialog *courseDialog = new CourseDialog(this);
+    courseDialog->init(course);
 
-    QString title = "New Course : " + clubName + "," + courseName;
+    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 *course = 0;
-      Club *club = findClub(clubName);
-      if (club) {
-       course = club->getCourse(courseName);
-       if (course) {
-         qDebug() << "Error: club/course already in the database";
-         return;
-       }
-       else {
-         course = new Course(courseName, par, hcp);
-         club->addCourse(course);
-       }
-      }
-      else {
-       club = new Club(clubName);
-       course = new Course(courseName, par, hcp);
-       club->addCourse(course);
-       clubList << club;
-      }
-      saveClubFile(clubFile, clubList);
-
-      // TODO: does this really work? No mem leaks?
-      scoreTableModel->setClub(clubList);
-
+        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);
     }
-  }
 }
 
 void MainWindow::newScore()
 {
-  SelectDialog *selectDialog = new SelectDialog(this);
+    SelectDialog *selectDialog = new SelectDialog(this);
 
-  selectDialog->init(clubList);
+    selectDialog->init(clubList);
 
-  int result = selectDialog->exec();
-  if (result) {
-    QString clubName;
-    QString courseName;
-    QString date;
+    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::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());
+
+    if (!course || !score) {
+        qDebug() << "No score/course to edit";
+        return;
+    }
 
-    selectDialog->results(clubName, courseName, date);
+    QString date = score->getDate();
 
-    ScoreDialog *scoreDialog = new ScoreDialog(this);
-    QString title = "New Score : " + courseName + ", " + date;
+    ScoreDialog *scoreDialog = new ScoreDialog;
+  
+    QString title = "Edit Score: " + course->getName() + ", " + date;
     scoreDialog->setWindowTitle(title);
 
-    Club *club = findClub(clubName);
-    if (!club) {
-      qDebug() << "Error: no such club: " << clubName;
-      return;
-    }
-    Course *course = club->getCourse(courseName);
-    if (!course) {
-      qDebug() << "Error: no such course: " << courseName;
-      return;
+    scoreDialog->init(course, score);
+
+    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);
+        // Update the model
+        scoreListModel->update(scoreList);
     }
+}
 
-    scoreDialog->init(course);
+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
 
-    result = scoreDialog->exec();
+    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::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(scoreWin);
+    scoreWin->setCentralWidget(central);
+    
+    QVBoxLayout *layout = new QVBoxLayout;
+    layout->addWidget(table);
+    
+    central->setLayout(layout);
+    scoreWin->show();
+}
 
-    if (result) {
-      QVector<QString> scores(18);
+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
 
-      scoreDialog->results(scores);
-      Score *score = new Score(scores, clubName, courseName, date);
-      scoreList << score;
+    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);
+
+    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);
+    
+    QVBoxLayout *layout = new QVBoxLayout;
+    layout->addWidget(table);
+    
+    central->setLayout(layout);
+    courseWin->show();
+}
 
-      saveScoreFile(scoreFile, scoreList);
+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
 
-      // TODO: does this really work? No mem leaks?
-      scoreTableModel->setScore(scoreList);
-      lastButtonClicked();
-    }
-  }
+  StatModel *model = new StatModel(clubList, scoreList);
+
+  QTableView *table = new QTableView;
+  table->showGrid();
+  table->setSelectionMode(QAbstractItemView::NoSelection);
+  table->setStyleSheet(ScoreStyle::headerView());
+  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);
+
+  QTextEdit *textEdit = new QTextEdit;
+
+  textEdit->setReadOnly(true);
+
+  QVBoxLayout *infoLayout = new QVBoxLayout;
+  infoLayout->addWidget(table);
+
+  QHBoxLayout *mainLayout = new QHBoxLayout(central);
+  mainLayout->addLayout(infoLayout);
+  central->setLayout(mainLayout);
+
+  win->show();
 }
 
 void MainWindow::loadScoreFile(QString &fileName, QList<Score *> &list)
@@ -364,7 +614,7 @@ void MainWindow::loadScoreFile(QString &fileName, QList<Score *> &list)
   ScoreXmlHandler handler(list);
 
   if (handler.parse(fileName))
-    qDebug() << "File loaded: " + fileName +" entries : " + list.size();
+    qDebug() << "File loaded:" << fileName << " entries:" << list.size();
 }
 
 void MainWindow::saveScoreFile(QString &fileName, QList<Score *> &list)
@@ -373,9 +623,9 @@ void MainWindow::saveScoreFile(QString &fileName, QList<Score *> &list)
 
   if (handler.save(fileName))
     // TODO: banner
-    qDebug() << "File saved : " << fileName << " entries : " << list.size();
+    qDebug() << "File saved:" << fileName << " entries:" << list.size();
   else
-    qDebug() << "Unable to save : " << fileName;
+    qWarning() << "Unable to save:" << fileName;
 }
 
 void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list)
@@ -383,7 +633,7 @@ void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list)
   ClubXmlHandler handler(list);
 
   if (handler.parse(fileName))
-    qDebug() << "File loaded: " + fileName +" entries : " + list.size();
+    qDebug() << "File loaded:" << fileName << " entries:" << list.size();
 }
 
 void MainWindow::saveClubFile(QString &fileName, QList<Club *> &list)
@@ -392,8 +642,8 @@ void MainWindow::saveClubFile(QString &fileName, QList<Club *> &list)
 
   if (handler.save(fileName))
     // TODO: banner
-    qDebug() << "File saved : " << fileName << " entries : " << list.size();
+    qDebug() << "File saved:" << fileName << " entries:" << list.size();
   else
-    qDebug() << "Unable to save : " << fileName;
+    qWarning() << "Unable to save:" << fileName;
 
 }