- Fixed course delete crashes
[scorecard] / src / main-window.cpp
index 42c6b44..deda9ef 100644 (file)
@@ -29,6 +29,8 @@ QString scoreFile;
 QString clubFileName("club.xml");
 QString clubFile;
 QString logFile("/tmp/scorecard.log");
+QString titleScores("ScoreCard - Scores");
+QString titleCourses("ScoreCard - Courses");
 
 bool dateLessThan(const Score *s1, const Score *s2)
 {
@@ -85,6 +87,7 @@ 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()
 {
     QModelIndex index = selectionModel->currentIndex();
@@ -122,7 +125,6 @@ MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
   createMenus();
 
   createListView(scoreList, clubList);
-  updateTitleBar();
 
   createLayoutList(centralWidget);
 }
@@ -173,7 +175,7 @@ void MainWindow::createLayoutList(QWidget *parent)
 void MainWindow::createListView(QList<Score *> &scoreList, 
                                 QList <Club *> &clubList)
 {
-    list = new QListView;
+    list = new QListView(this);
 
     scoreListModel = new ScoreListModel(scoreList, clubList);
     courseListModel = new CourseListModel(clubList);
@@ -181,25 +183,27 @@ void MainWindow::createListView(QList<Score *> &scoreList,
     list->setStyleSheet(ScoreStyle::style());
 
     list->setSelectionMode(QAbstractItemView::SingleSelection);
+    list->setProperty("FingerScrolling", true);
 
     // Initial view
     listScores();
+
+    connect(list, SIGNAL(clicked(QModelIndex)),
+            this, SLOT(clickedList(QModelIndex)));
 }
 
 void MainWindow::listScores()
 {
     list->setModel(scoreListModel);
     selectionModel = list->selectionModel();
-    connect(selectionModel, SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)),
-            this, SLOT(scoreSelectionChanged(const QItemSelection &, const QItemSelection &)));
+    updateTitleBar(titleScores);
 }
 
 void MainWindow::listCourses()
 {
     list->setModel(courseListModel);
     selectionModel = list->selectionModel();
-    connect(selectionModel, SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)),
-            this, SLOT(courseSelectionChanged(const QItemSelection &, const QItemSelection &)));
+    updateTitleBar(titleCourses);
 }
 
 void MainWindow::createActions()
@@ -241,10 +245,9 @@ void MainWindow::createMenus()
     menu->addActions(filterGroup->actions());
 }
 
-void MainWindow::updateTitleBar()
+void MainWindow::updateTitleBar(QString & msg)
 {
-    QString title("ScoreCard");
-    setWindowTitle(title);
+    setWindowTitle(msg);
 }
 
 void MainWindow::showNote(QString msg)
@@ -256,42 +259,31 @@ void MainWindow::showNote(QString msg)
 #endif
 }
 
-void MainWindow::scoreSelectionChanged(const QItemSelection &selected,
-                                       const QItemSelection &deselected)
+void MainWindow::clickedList(const QModelIndex &index)
 {
-    QModelIndexList list = selected.indexes();
-
-    int row = list.at(0).row();
+    int row = index.row();
 
-    Score * score = scoreList.at(row);
-    Course * course = findCourse(score->getClubName(), score->getCourseName());
-    viewScore(score, course);
-}
-
-void MainWindow::courseSelectionChanged(const QItemSelection &selected,
-                                        const QItemSelection &deselected)
-{
-    QModelIndexList indexes = selected.indexes();
-
-    int row = indexes.at(0).row();
-
-    QString str = courseListModel->data(indexes.at(0), Qt::DisplayRole).toString();
-
-    QStringList strList = str.split(",");
+    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;
+        if (strList.count() != 2) {
+            showNote(QString("Invalid course selection"));
+            return;
+        }
+        Course * course = findCourse(strList.at(0), strList.at(1));
+        viewCourse(course);
     }
-    Course * course = findCourse(strList.at(0), strList.at(1));
-    viewCourse(course);
 }
 
-void MainWindow::changeCurrent(const QModelIndex &current,
-                               const QModelIndex &previous)
-{
-    qDebug() << "Current: " << current;
-}
 
 void MainWindow::newCourse()
 {
@@ -340,8 +332,7 @@ void MainWindow::newCourse()
             }
             // Save it
             saveClubFile(clubFile, clubList);
-            // TODO: update on live
-            //courseListModel->update(courseList);
+            courseListModel->update(clubList);
             list->update();
         }
     }
@@ -503,8 +494,7 @@ void MainWindow::editScore()
     }
 }
 
-void MainWindow::viewScore(Score * score, 
-                           Course * 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());