Maemo5 date picker button
[scorecard] / src / main-window.cpp
index e54f1fd..49d973a 100644 (file)
@@ -1,14 +1,18 @@
+/*
+ * 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>
 
+#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 appName("scorecard");
@@ -34,7 +38,7 @@ MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
 
   loadSettings();
 
-  QWidget *centralWidget = new QWidget(this);
+  centralWidget = new QWidget(this);
 
   setCentralWidget(centralWidget);
 
@@ -43,49 +47,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();
+  updateTitleBar();
 
   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_MAEMO_5
+  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 +105,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 +113,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,61 +124,22 @@ 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->setStyleSheet(ScoreColor::styleSheet());
+
   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->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()
-{
-  updateStatusBar();
+  //table->verticalHeader()->setStyleSheet("color : gray }");
 }
 
 void MainWindow::createActions()
@@ -189,59 +156,68 @@ 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_MAEMO_5
+  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()
+void MainWindow::updateTitleBar()
 {
-  setWindowTitle(scoreTableModel->getInfoText());
+  QString title = scoreTableModel->getInfoText();
+  if (title.isEmpty())
+    title = "ScoreCard - No Scores";
+
+  setWindowTitle(title);
 }
 
 void MainWindow::firstButtonClicked()
 {
   scoreTableModel->first();
-  updateStatusBar();
+  updateTitleBar();
 }
 
 void MainWindow::lastButtonClicked()
 {
   scoreTableModel->last();
-  updateStatusBar();
+  updateTitleBar();
 }
 
 void MainWindow::nextButtonClicked()
 {
   scoreTableModel->next();
-  updateStatusBar();
+  updateTitleBar();
 }
 
 void MainWindow::prevButtonClicked()
 {
   scoreTableModel->prev();
-  updateStatusBar();
+  updateTitleBar();
 }
 
 // FIXME: dup code from table-model.cpp
@@ -298,6 +274,7 @@ void MainWindow::newCourse()
        }
       }
       else {
+       // New club and course
        club = new Club(clubName);
        course = new Course(courseName, par, hcp);
        club->addCourse(course);
@@ -307,7 +284,6 @@ void MainWindow::newCourse()
 
       // TODO: does this really work? No mem leaks?
       scoreTableModel->setClub(clubList);
-
     }
   }
 }
@@ -316,6 +292,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 +336,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);
@@ -379,7 +360,7 @@ void MainWindow::newScore()
 
       // TODO: does this really work? No mem leaks?
       scoreTableModel->setScore(scoreList, score);
-      updateStatusBar();
+      updateTitleBar();
     }
   }
 }
@@ -388,6 +369,12 @@ void MainWindow::editScore()
 {
   Course *course = scoreTableModel->getCourse();
   Score *score = scoreTableModel->getScore();
+
+  if (!course || !score) {
+    qDebug() << "No score/course to edit";
+    return;
+  }
+
   QString date = score->getDate();
 
   ScoreDialog *scoreDialog = new ScoreDialog(this);
@@ -413,16 +400,57 @@ void MainWindow::editScore()
 
     // TODO: does this really work? No mem leaks?
     scoreTableModel->setScore(scoreList, score);
-    updateStatusBar();
+    updateTitleBar();
   }
 }
 
+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 +459,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 +469,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 +478,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;
 
 }