Corner case handling. Copyright update
[scorecard] / src / main-window.cpp
index 34958fa..98351e2 100644 (file)
@@ -12,9 +12,9 @@
 #include "xml-dom-parser.h"
 
 QString appName("scorecard");
-QString topDir("/opt/scorecard");
-QString mmcDir("/media/mmc1/scorecard");
-QString dataDirName("/data/");
+QString topDir("/opt");
+QString mmcDir("/media/mmc1");
+QString dataDirName("data");
 QString dataDir;
 QString imgDir(topDir + "/pixmaps");
 QString scoreFileName("score.xml");
@@ -44,49 +44,52 @@ 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 + 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)
 {
+
   buttonLayout = new QVBoxLayout;
   //labelLayout->addStretch();
   buttonLayout->addWidget(nextButton);
@@ -100,7 +103,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
@@ -108,10 +111,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()));
@@ -119,16 +122,20 @@ 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);
+  QItemSelectionModel selectionModel();
+  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();
 }
 
@@ -173,6 +180,15 @@ void MainWindow::updateTreeView(const QModelIndex & index)
 
 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();
 }
 
@@ -187,6 +203,9 @@ void MainWindow::createActions()
   editScoreAct = new QAction(tr("Edit Score"), this);
   connect(editScoreAct, SIGNAL(triggered()), this, SLOT(editScore()));
 
+  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()));
@@ -197,11 +216,23 @@ void MainWindow::createActions()
   viewStatisticAct = new QAction(tr("&View Statistics"), this);
   connect(viewStatisticAct, SIGNAL(triggered()), this, SLOT(viewStatistic()));
 #endif
+
+  nextAct = new QAction(tr( "   Next   "), this);
+  connect(nextAct, SIGNAL(triggered()), this, SLOT(nextButtonClicked()));
+
+  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"));
+  menu = menuBar()->addMenu("");
 #if 0
   menu->addAction(viewScoreAct);
   menu->addAction(viewCourseAct);
@@ -210,6 +241,7 @@ void MainWindow::createMenus()
   menu->addAction(newScoreAct);
   menu->addAction(newCourseAct);
   menu->addAction(editScoreAct);
+  menu->addAction(editCourseAct);
 }
 
 void MainWindow::updateStatusBar()
@@ -268,6 +300,7 @@ void MainWindow::newCourse()
     selectDialog->results(clubName, courseName);
 
     CourseDialog *courseDialog = new CourseDialog(this);
+    courseDialog->init();
 
     QString title = "New Course: " + clubName + "," + courseName;
     courseDialog->setWindowTitle(title);
@@ -294,6 +327,7 @@ void MainWindow::newCourse()
        }
       }
       else {
+       // New club and course
        club = new Club(clubName);
        course = new Course(courseName, par, hcp);
        club->addCourse(course);
@@ -303,11 +337,38 @@ void MainWindow::newCourse()
 
       // TODO: does this really work? No mem leaks?
       scoreTableModel->setClub(clubList);
-
     }
   }
 }
 
+void MainWindow::editCourse()
+{
+  Course *course = scoreTableModel->getCourse();
+
+  if (!course) {
+    qWarning() << "No course on edit";
+    return;
+  }
+
+  CourseDialog *courseDialog = new CourseDialog(this);
+  courseDialog->init(course);
+
+  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->update(par, hcp, len);
+    saveClubFile(clubFile, clubList);
+  }
+}
+
 void MainWindow::newScore()
 {
   SelectDialog *selectDialog = new SelectDialog(this);
@@ -328,19 +389,16 @@ 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);
-
     result = scoreDialog->exec();
-
     if (result) {
       QVector<QString> scores(18);
 
@@ -398,7 +456,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)
@@ -407,9 +465,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)
@@ -417,7 +475,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)
@@ -426,8 +484,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;
 
 }