More robust handling of cases where no data files
authorSakari Poussa <sakari.poussa@nokia.com>
Sun, 18 Oct 2009 20:04:02 +0000 (23:04 +0300)
committerSakari Poussa <sakari.poussa@nokia.com>
Sun, 18 Oct 2009 20:04:02 +0000 (23:04 +0300)
data/club.xml
data/score.xml
src/main-window.cpp
src/table-model.cpp
src/table-model.h

index 9f3974b..50e8be9 100644 (file)
    <hole num="18" par="4" hcp="9" />
   </course>
  </club>
+ <club name="crash" >
+  <course name="1" >
+   <hole num="1" par="4" hcp="9" />
+   <hole num="2" par="4" hcp="9" />
+   <hole num="3" par="4" hcp="9" />
+   <hole num="4" par="4" hcp="9" />
+   <hole num="5" par="4" hcp="9" />
+   <hole num="6" par="4" hcp="9" />
+   <hole num="7" par="4" hcp="9" />
+   <hole num="8" par="4" hcp="9" />
+   <hole num="9" par="4" hcp="9" />
+   <hole num="10" par="4" hcp="9" />
+   <hole num="11" par="4" hcp="9" />
+   <hole num="12" par="4" hcp="9" />
+   <hole num="13" par="4" hcp="9" />
+   <hole num="14" par="4" hcp="9" />
+   <hole num="15" par="4" hcp="9" />
+   <hole num="16" par="4" hcp="9" />
+   <hole num="17" par="4" hcp="9" />
+   <hole num="18" par="4" hcp="9" />
+  </course>
+ </club>
+ <club name="foo" >
+  <course name="bar" >
+   <hole num="1" par="4" hcp="9" />
+   <hole num="2" par="4" hcp="9" />
+   <hole num="3" par="4" hcp="9" />
+   <hole num="4" par="4" hcp="9" />
+   <hole num="5" par="4" hcp="9" />
+   <hole num="6" par="4" hcp="9" />
+   <hole num="7" par="4" hcp="9" />
+   <hole num="8" par="4" hcp="9" />
+   <hole num="9" par="4" hcp="9" />
+   <hole num="10" par="4" hcp="9" />
+   <hole num="11" par="4" hcp="9" />
+   <hole num="12" par="4" hcp="9" />
+   <hole num="13" par="4" hcp="9" />
+   <hole num="14" par="4" hcp="9" />
+   <hole num="15" par="4" hcp="9" />
+   <hole num="16" par="4" hcp="9" />
+   <hole num="17" par="4" hcp="9" />
+   <hole num="18" par="4" hcp="9" />
+  </course>
+ </club>
+ <club name="ggg" >
+  <course name="ggg" >
+   <hole num="1" par="4" hcp="9" />
+   <hole num="2" par="4" hcp="9" />
+   <hole num="3" par="4" hcp="9" />
+   <hole num="4" par="4" hcp="9" />
+   <hole num="5" par="4" hcp="9" />
+   <hole num="6" par="4" hcp="9" />
+   <hole num="7" par="4" hcp="9" />
+   <hole num="8" par="4" hcp="9" />
+   <hole num="9" par="4" hcp="9" />
+   <hole num="10" par="4" hcp="9" />
+   <hole num="11" par="4" hcp="9" />
+   <hole num="12" par="4" hcp="9" />
+   <hole num="13" par="4" hcp="9" />
+   <hole num="14" par="4" hcp="9" />
+   <hole num="15" par="4" hcp="9" />
+   <hole num="16" par="4" hcp="9" />
+   <hole num="17" par="4" hcp="9" />
+   <hole num="18" par="4" hcp="9" />
+  </course>
+ </club>
 </data>
index df2b7e8..4dfe26a 100644 (file)
   <hole num="17" shots="4" />
   <hole num="18" shots="4" />
  </score>
+ <score club="crash" course="1" date="2009-10-15" >
+  <hole num="1" shots="4" />
+  <hole num="2" shots="4" />
+  <hole num="3" shots="4" />
+  <hole num="4" shots="4" />
+  <hole num="5" shots="4" />
+  <hole num="6" shots="4" />
+  <hole num="7" shots="4" />
+  <hole num="8" shots="4" />
+  <hole num="9" shots="4" />
+  <hole num="10" shots="4" />
+  <hole num="11" shots="4" />
+  <hole num="12" shots="4" />
+  <hole num="13" shots="4" />
+  <hole num="14" shots="4" />
+  <hole num="15" shots="4" />
+  <hole num="16" shots="4" />
+  <hole num="17" shots="4" />
+  <hole num="18" shots="4" />
+ </score>
+ <score club="crash" course="1" date="2009-10-15" >
+  <hole num="1" shots="4" />
+  <hole num="2" shots="4" />
+  <hole num="3" shots="4" />
+  <hole num="4" shots="4" />
+  <hole num="5" shots="4" />
+  <hole num="6" shots="4" />
+  <hole num="7" shots="4" />
+  <hole num="8" shots="4" />
+  <hole num="9" shots="4" />
+  <hole num="10" shots="4" />
+  <hole num="11" shots="4" />
+  <hole num="12" shots="4" />
+  <hole num="13" shots="4" />
+  <hole num="14" shots="4" />
+  <hole num="15" shots="4" />
+  <hole num="16" shots="4" />
+  <hole num="17" shots="4" />
+  <hole num="18" shots="4" />
+ </score>
+ <score club="foo" course="bar" date="2009-10-15" >
+  <hole num="1" shots="4" />
+  <hole num="2" shots="4" />
+  <hole num="3" shots="4" />
+  <hole num="4" shots="4" />
+  <hole num="5" shots="4" />
+  <hole num="6" shots="4" />
+  <hole num="7" shots="4" />
+  <hole num="8" shots="4" />
+  <hole num="9" shots="4" />
+  <hole num="10" shots="4" />
+  <hole num="11" shots="4" />
+  <hole num="12" shots="4" />
+  <hole num="13" shots="4" />
+  <hole num="14" shots="4" />
+  <hole num="15" shots="4" />
+  <hole num="16" shots="4" />
+  <hole num="17" shots="4" />
+  <hole num="18" shots="4" />
+ </score>
+ <score club="ggg" course="ggg" date="2009-10-15" >
+  <hole num="1" shots="4" />
+  <hole num="2" shots="4" />
+  <hole num="3" shots="4" />
+  <hole num="4" shots="4" />
+  <hole num="5" shots="4" />
+  <hole num="6" shots="4" />
+  <hole num="7" shots="4" />
+  <hole num="8" shots="4" />
+  <hole num="9" shots="4" />
+  <hole num="10" shots="4" />
+  <hole num="11" shots="4" />
+  <hole num="12" shots="4" />
+  <hole num="13" shots="4" />
+  <hole num="14" shots="4" />
+  <hole num="15" shots="4" />
+  <hole num="16" shots="4" />
+  <hole num="17" shots="4" />
+  <hole num="18" shots="4" />
+ </score>
 </data>
index 3f59c10..a1e3488 100644 (file)
@@ -229,8 +229,6 @@ void MainWindow::newCourse()
     QString date;
 
     selectDialog->results(clubName, courseName);
-    qDebug() << "club=" << clubName;
-    qDebug() << "course=" << courseName;
 
     CourseDialog *courseDialog = new CourseDialog(this);
 
@@ -265,6 +263,10 @@ void MainWindow::newCourse()
        clubList << club;
       }
       saveClubFile(clubFile, clubList);
+
+      // TODO: does this really work? No mem leaks?
+      scoreTableModel->setClub(clubList);
+
     }
   }
 }
@@ -323,8 +325,8 @@ void MainWindow::loadScoreFile(QString &fileName, QList<Score *> &list)
   ScoreXmlHandler handler(list);
 
   if (handler.parse(fileName))
-    qDebug() << "Score loaded : " << fileName << 
-      " entries : " << list.size();
+    qDebug() << "Score loaded : " << fileName;
+  qDebug() << "Score entries : " << list.size();
 }
 
 void MainWindow::saveScoreFile(QString &fileName, QList<Score *> &list)
@@ -341,8 +343,8 @@ void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list)
   ClubXmlHandler handler(list);
 
   if (handler.parse(fileName))
-    qDebug() << "Club loaded: " << fileName << 
-      " entries : " << list.size();
+    qDebug() << "Club loaded: " << fileName;
+  qDebug() << "Club entries : " << list.size();
 }
 
 void MainWindow::saveClubFile(QString &fileName, QList<Club *> &list)
index c0cc2d7..e973bfb 100644 (file)
@@ -11,19 +11,28 @@ Qt::ItemFlags ScoreTableModel::flags ( const QModelIndex & index )
 void ScoreTableModel::setScore(QList<Score *> &sList)
 {
   scoreList = sList;
-  score = scoreList.at(currentScore); // NOTE: assumes non-empty list
+  if (scoreList.size() > 0)
+    score = scoreList.at(currentScore); // NOTE: assumes non-empty list
 }
 
 void ScoreTableModel::setClub(QList<Club *> &cList)
 {
   clubList = cList;
-  club = clubList.at(0);
-  course = club->getCourse(0);
+
+  if (clubList.size() > 0)
+    club = clubList.at(0);
+
+  if (club)
+    course = club->getCourse(0);
 }
 
 QString ScoreTableModel::getInfoText()
 {
-  QString str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
+  QString str("");
+
+  if (score)
+    str = QString("%1, %2 / [%3/%4]").arg(score->getCourseName()).arg(score->getDate()).arg(currentScore+1).arg(scoreList.count());
+
   return str;
 }
 
@@ -49,37 +58,45 @@ Course *ScoreTableModel::findCourse(QString &clubName, QString &courseName)
 
 void ScoreTableModel::first()
 {
-  currentScore = 0;
-  score = scoreList.at(currentScore);
-  course = findCourse(score->getClubName(), score->getCourseName());
-  emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
+  if (score && course) {
+    currentScore = 0;
+    score = scoreList.at(currentScore);
+    course = findCourse(score->getClubName(), score->getCourseName());
+    emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
+  }
 }
 
 void ScoreTableModel::last()
 {
-  currentScore = scoreList.size() - 1;
-  score = scoreList.at(currentScore);
-  course = findCourse(score->getClubName(), score->getCourseName());
-  emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
+  if (score && course) {
+    currentScore = scoreList.size() - 1;
+    score = scoreList.at(currentScore);
+    course = findCourse(score->getClubName(), score->getCourseName());
+    emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
+  }
 }
 
 void ScoreTableModel::next()
 {
-  if (currentScore < (scoreList.size() - 1)) {
-    currentScore++;
-    score = scoreList.at(currentScore);
-    course = findCourse(score->getClubName(), score->getCourseName());
-    emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
+  if (score && course) {
+    if (currentScore < (scoreList.size() - 1)) {
+      currentScore++;
+      score = scoreList.at(currentScore);
+      course = findCourse(score->getClubName(), score->getCourseName());
+      emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
+    }
   }
 }
 
 void ScoreTableModel::prev()
 {
-  if (currentScore > 0) {
-    currentScore--;
-    score = scoreList.at(currentScore);
-    course = findCourse(score->getClubName(), score->getCourseName());
-    emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
+  if (score && course) {
+    if (currentScore > 0) {
+      currentScore--;
+      score = scoreList.at(currentScore);
+      course = findCourse(score->getClubName(), score->getCourseName());
+      emit dataChanged(createIndex(0, 0), createIndex(ROW_COUNT-1, COL_COUNT-1));
+    }
   }
 }
 
@@ -114,6 +131,9 @@ QVariant ScoreTableModel::data(const QModelIndex &index, int role) const
   if (!index.isValid())
     return QVariant();
 
+  if (!course || !score)
+    return QVariant();
+
   int row = index.row();
   int col = index.column();
 
index 89c0ab1..bb4f44a 100644 (file)
@@ -17,6 +17,9 @@ public:
   ScoreTableModel(QObject *parent = 0) : QAbstractTableModel(parent) 
   {
     currentScore = 0;
+    score = 0;
+    club = 0;
+    course = 0;
   }
   Qt::ItemFlags flags ( const QModelIndex & index );
   void setScore(QList<Score *> &sList);