Statistics - final bits including all stats and error handling
[scorecard] / src / main-window.cpp
index e54f1fd..02080e3 100644 (file)
@@ -1,14 +1,9 @@
 #include <QtGui>
-#include <QDirModel>
-#include <QTreeView>
-#include <QListView>
-#include <QStandardItemModel>
 
 #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 appName("scorecard");
@@ -34,7 +29,7 @@ MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
 
   loadSettings();
 
-  QWidget *centralWidget = new QWidget(this);
+  centralWidget = new QWidget(this);
 
   setCentralWidget(centralWidget);
 
@@ -43,49 +38,51 @@ MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
 
   // Sort the scores based on dates
   qSort(scoreList.begin(), scoreList.end(), dateLessThan); 
+  createActions();
+  createMenus();
 
   createTableView(scoreList, clubList);
-  //createTreeView(scoreList, parent);
   createStatusBar();
 
   createLayout(centralWidget);
-
-  createActions();
-  createMenus();
 }
 
 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_HILDON
+  dataDir = "./" + dataDirName;
+#else
   if (external) {
     dataDir = mmcDir + "/" + appName + "/" + dataDirName;
   }
   else {
     dataDir = topDir + "/" + appName + "/" + dataDirName;
   }
+#endif
   scoreFile = dataDir + "/" + scoreFileName;
   clubFile = dataDir + "/" + clubFileName;
 
   QDir dir(dataDir);
   if (!dir.exists())
     if (!dir.mkpath(dataDir)) {
-      qDebug() << "Unable to create: " + dataDir;
+      qWarning() << "Unable to create: " + dataDir;
       return;
     }
-  qDebug() << "Data is at: " + dataDir;
+  qDebug() << "Data is at:" + dataDir;
 }
 
 void MainWindow::createLayout(QWidget *parent)
 {
+
   buttonLayout = new QVBoxLayout;
   //labelLayout->addStretch();
   buttonLayout->addWidget(nextButton);
@@ -99,7 +96,7 @@ void MainWindow::createLayout(QWidget *parent)
   QHBoxLayout *mainLayout = new QHBoxLayout(parent);
   mainLayout->addLayout(tableLayout);
   mainLayout->addLayout(buttonLayout);
-  setLayout(mainLayout);
+  parent->setLayout(mainLayout);
 }
 
 // Setup 'score' tab view
@@ -107,10 +104,10 @@ void MainWindow::createTableView(QList<Score *> &scoreList, QList <Club *> &club
 {
   table = new QTableView;
 
-  nextButton = new QPushButton(tr(">"));
-  prevButton = new QPushButton(tr("<"));
-  firstButton = new QPushButton(tr("<<"));
-  lastButton = new QPushButton(tr(">>"));
+  nextButton = new QPushButton(tr("Next"));
+  prevButton = new QPushButton(tr("Prev"));
+  firstButton = new QPushButton(tr("First"));
+  lastButton = new QPushButton(tr("Last"));
 
   connect(nextButton, SIGNAL(clicked()), this, SLOT(nextButtonClicked()));
   connect(prevButton, SIGNAL(clicked()), this, SLOT(prevButtonClicked()));
@@ -118,60 +115,33 @@ void MainWindow::createTableView(QList<Score *> &scoreList, QList <Club *> &club
   connect(lastButton, SIGNAL(clicked()), this, SLOT(lastButtonClicked()));
 
   scoreTableModel = new ScoreTableModel();
-  scoreTableModel->setScore(scoreList);
-  scoreTableModel->setClub(clubList);
 
   table->showGrid();
 
   table->setModel(scoreTableModel);
+  table->setSelectionMode(QAbstractItemView::NoSelection);
+
+  scoreTableModel->setScore(scoreList);
+  scoreTableModel->setClub(clubList);
 
   // Fill out all the space with the tables
   table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
   table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
+  table->verticalHeader()->setAutoFillBackground(true);
   table->horizontalHeader()->hide();
 }
 
-// When selection down in 'stat' view, this is called.
-void MainWindow::updateTreeView(const QModelIndex & index)
-{
-  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);
-  }
-}
-
 void MainWindow::createStatusBar()
 {
+#if 0
+  // TODO: use toolbar or buttons. Toolbar seems not to be ready and
+  // requires more work.
+  toolbar = addToolBar(tr("foo"));
+  toolbar->addAction(firstAct);
+  toolbar->addAction(lastAct);
+  toolbar->addAction(prevAct);
+  toolbar->addAction(nextAct);
+#endif
   updateStatusBar();
 }
 
@@ -189,35 +159,44 @@ void MainWindow::createActions()
   editCourseAct = new QAction(tr("Edit Course"), this);
   connect(editCourseAct, SIGNAL(triggered()), this, SLOT(editCourse()));
 
-#if 0
-  viewScoreAct = new QAction(tr("&View Scores"), this);
-  connect(viewScoreAct, SIGNAL(triggered()), this, SLOT(viewScore()));
+  statAct = new QAction(tr("Statistics"), this);
+  connect(statAct, SIGNAL(triggered()), this, SLOT(viewStatistics()));
 
-  viewCourseAct = new QAction(tr("&View Courses"), this);
-  connect(viewCourseAct, SIGNAL(triggered()), this, SLOT(viewCourse()));
+  nextAct = new QAction(tr( "   Next   "), this);
+  connect(nextAct, SIGNAL(triggered()), this, SLOT(nextButtonClicked()));
 
-  viewStatisticAct = new QAction(tr("&View Statistics"), this);
-  connect(viewStatisticAct, SIGNAL(triggered()), this, SLOT(viewStatistic()));
-#endif
+  prevAct = new QAction("   Prev   ", this);
+  connect(prevAct, SIGNAL(triggered()), this, SLOT(prevButtonClicked()));
+
+  firstAct = new QAction(tr("   First  "), this);
+  connect(firstAct, SIGNAL(triggered()), this, SLOT(firstButtonClicked()));
+
+  lastAct = new QAction(tr( "   Last   "), this);
+  connect(lastAct, SIGNAL(triggered()), this, SLOT(lastButtonClicked()));
 }
 
 void MainWindow::createMenus()
 {
-  menu = menuBar()->addMenu(tr("fremantle"));
-#if 0
-  menu->addAction(viewScoreAct);
-  menu->addAction(viewCourseAct);
-  menu->addAction(viewStatisticAct);
+#ifdef Q_WS_HILDON
+  menu = menuBar()->addMenu("");
+#else
+  menu = menuBar()->addMenu("Menu");
 #endif
+
   menu->addAction(newScoreAct);
   menu->addAction(newCourseAct);
   menu->addAction(editScoreAct);
   menu->addAction(editCourseAct);
+  menu->addAction(statAct);
 }
 
 void MainWindow::updateStatusBar()
 {
-  setWindowTitle(scoreTableModel->getInfoText());
+  QString title = scoreTableModel->getInfoText();
+  if (title.isEmpty())
+    title = "ScoreCard - No Scores";
+
+  setWindowTitle(title);
 }
 
 void MainWindow::firstButtonClicked()
@@ -298,6 +277,7 @@ void MainWindow::newCourse()
        }
       }
       else {
+       // New club and course
        club = new Club(clubName);
        course = new Course(courseName, par, hcp);
        club->addCourse(course);
@@ -307,7 +287,6 @@ void MainWindow::newCourse()
 
       // TODO: does this really work? No mem leaks?
       scoreTableModel->setClub(clubList);
-
     }
   }
 }
@@ -316,6 +295,11 @@ void MainWindow::editCourse()
 {
   Course *course = scoreTableModel->getCourse();
 
+  if (!course) {
+    qWarning() << "No course on edit";
+    return;
+  }
+
   CourseDialog *courseDialog = new CourseDialog(this);
   courseDialog->init(course);
 
@@ -355,12 +339,12 @@ void MainWindow::newScore()
 
     Club *club = findClub(clubName);
     if (!club) {
-      qDebug() << "Error: no such club: " << clubName;
+      qWarning() << "Error: no such club:" << clubName;
       return;
     }
     Course *course = club->getCourse(courseName);
     if (!course) {
-      qDebug() << "Error: no such course: " << courseName;
+      qWarning() << "Error: no such course:" << courseName;
       return;
     }
     scoreDialog->init(course);
@@ -417,12 +401,53 @@ void MainWindow::editScore()
   }
 }
 
+void MainWindow::viewStatistics()
+{
+  QMainWindow *win = new QMainWindow(this);
+  QString title = "Statistics";
+  win->setWindowTitle(title);
+
+  StatModel *statModel = new StatModel(clubList, scoreList);
+
+  QTableView *table = new QTableView;
+  table->showGrid();
+  table->setSelectionMode(QAbstractItemView::NoSelection);
+  table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
+  table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
+  table->verticalHeader()->setAutoFillBackground(true);
+  table->setModel(statModel);
+
+  QWidget *central = new QWidget(win);
+  win->setCentralWidget(central);
+
+  QPushButton *b1 = new QPushButton("Graphs");
+
+  QVBoxLayout *buttonLayout = new QVBoxLayout;
+  buttonLayout->addWidget(b1);
+
+  QTextEdit *textEdit = new QTextEdit;
+  //getStat(textEdit);
+
+  textEdit->setReadOnly(true);
+
+  QVBoxLayout *infoLayout = new QVBoxLayout;
+  infoLayout->addWidget(table);
+
+  QHBoxLayout *mainLayout = new QHBoxLayout(central);
+  mainLayout->addLayout(infoLayout);
+  mainLayout->addLayout(buttonLayout);
+
+  central->setLayout(mainLayout);
+
+  win->show();
+}
+
 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)
@@ -431,9 +456,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)
@@ -441,7 +466,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)
@@ -450,8 +475,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;
 
 }