Sort scores based on dates not the entry date.
authorSakari Poussa <sakari.poussa@nokia.com>
Wed, 21 Oct 2009 18:46:53 +0000 (21:46 +0300)
committerSakari Poussa <sakari.poussa@nokia.com>
Wed, 21 Oct 2009 18:46:53 +0000 (21:46 +0300)
Show new score in the main view (not last anymore)

data/score.xml
scorecard.pro
src/data.cpp
src/data.h
src/main-window.cpp
src/score-dialog.cpp
src/score-dialog.h
src/table-model.cpp
src/table-model.h

index 4eb3fad..63b0c8d 100644 (file)
 <hole num="17" shots="5"/>
 <hole num="18" shots="4"/>
 </score>
-<score club="Master" course="Forest" date="2008-10-11">
+<score club="Master" course="Forest" date="2004-10-11">
 <hole num="1" shots="6"/>
 <hole num="2" shots="4"/>
 <hole num="3" shots="8"/>
index d29d7fd..830f8a3 100644 (file)
@@ -1,4 +1,4 @@
-TARGET = bin/scorecard
+TARGET = scorecard
 DESTDIR = bin
 MOC_DIR = moc
 OBJECTS_DIR = obj
index 020d701..1b0a846 100644 (file)
@@ -132,14 +132,16 @@ void Score::addHole(Hole *iHole) {
   holeList << iHole;
 }
   
-QString Score::getScore(int i) {
+QString Score::getScore(int i) const 
+{
   if (i >= 0 && i < holeList.size())
     return holeList.at(i)->getShots();
   else
     return QString("-");
 }
   
-QString Score::getTotal(int what) {
+QString Score::getTotal(int what) const
+{
   int tot = 0;
 
   if (what == Total)
@@ -157,19 +159,23 @@ QString Score::getTotal(int what) {
   return QString("%1").arg(tot);
 }
 
-QString& Score::getClubName() {
+const QString& Score::getClubName() const
+{
   return club;
 }
 
-QString& Score::getCourseName() {
+const QString& Score::getCourseName() const
+{
   return course;
 }
 
-QString& Score::getDate() {
+const QString& Score::getDate() const
+{
   return date;
 }
 
-void Score::dump() {
+void Score::dump()
+{
   qDebug() << club << " " << course << " " << date ; 
   for (int i=0; i<holeList.size(); i++)
     holeList.at(i)->dump();
@@ -306,7 +312,7 @@ Course *Club::getCourse(int pos) {
   return courseList.at(pos);
 }
 
-Course *Club::getCourse(QString &courseName) 
+Course *Club::getCourse(const QString &courseName) 
 {
   QListIterator<Course *> i(courseList);
   Course *c = 0;
index 8936e21..e71bb50 100644 (file)
@@ -34,19 +34,23 @@ class Score {
   Score(const QDomElement node);
   Score(QVector<QString> scores, QString &club, QString &course, QString &date);
 
+  bool operator< (const Score& val) const 
+  { 
+    return date < val.getDate();
+  }
+
   QDomElement toElement(QDomDocument doc);
   void addHole(Hole *iHole);
-  QString getScore(int i);
-  QString getTotal(int what);
-  QString& getClubName();
-  QString& getCourseName();
-  QString& getDate();
+  QString getScore(int i) const;
+  QString getTotal(int what) const;
+  const QString& getClubName() const;
+  const QString& getCourseName() const;
+  const QString& getDate() const;
   void dump();
 
  private:
   QList <Hole *> holeList;
   QString club, course, date;
-
 };
 
 class Course {
@@ -80,7 +84,7 @@ class Club {
   void dump();
   QString& getName();
   Course *getCourse(int pos);
-  Course *getCourse(QString &courseName);
+  Course *getCourse(const QString &courseName);
 
   QList <Course *> getCourseList() { return courseList; } // HACK: fixme
 
index c235605..6ca8d3a 100644 (file)
@@ -11,6 +11,7 @@
 #include "xml-parser.h"
 #include "xml-dom-parser.h"
 
+QString appName("scorecard");
 QString topDir("/opt/scorecard");
 QString mmcDir("/media/mmc1/scorecard");
 QString dataDirName("/data/");
@@ -22,6 +23,11 @@ QString clubFileName("club.xml");
 QString clubFile;
 QString logFile("/tmp/scorecard.log");
 
+bool dateLessThan(const Score *s1, const Score *s2)
+{
+  return (*s1) < (*s2);
+}
+
 MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
 {
   resize(800, 480);
@@ -39,10 +45,8 @@ MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
   loadScoreFile(scoreFile, scoreList);
   loadClubFile(clubFile, clubList);
 
-#ifdef TEST
-  saveScoreFile(scoreFileWr, scoreList);
-  saveClubFile(clubFileWr, clubList);
-#endif
+  // Sort the scores based on dates
+  qSort(scoreList.begin(), scoreList.end(), dateLessThan); 
 
   createTableView(scoreList, clubList);
   //createTreeView(scoreList, parent);
@@ -350,11 +354,14 @@ void MainWindow::newScore()
       Score *score = new Score(scores, clubName, courseName, date);
       scoreList << score;
 
+      // Sort the scores based on dates
+      qSort(scoreList.begin(), scoreList.end(), dateLessThan); 
+      // Save it
       saveScoreFile(scoreFile, scoreList);
 
       // TODO: does this really work? No mem leaks?
-      scoreTableModel->setScore(scoreList);
-      lastButtonClicked();
+      scoreTableModel->setScore(scoreList, score);
+      updateStatusBar();
     }
   }
 }
@@ -364,7 +371,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)
@@ -383,7 +390,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)
index 357e688..8fc9494 100644 (file)
@@ -20,10 +20,13 @@ void SelectDialog::createLayout(QWidget *parent)
   QDate today(QDate::currentDate());
   lineEditDate->setText(today.toString("yyyy-MM-dd"));
 
+  date = new QDateEdit(parent);
+
   connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
 
   leftLayout = new QVBoxLayout;
   leftLayout->addWidget(listClub);
+  leftLayout->addWidget(date);
   leftLayout->addWidget(lineEditDate);
 
   rightLayout = new QVBoxLayout;
index 8b3defd..f595b3a 100644 (file)
@@ -7,6 +7,7 @@
 #include <QComboBox>
 #include <QLabel>
 #include <QPushButton>
+#include <QDateEdit>
 #include <QLayout>
 
 #include "data.h"
@@ -35,6 +36,7 @@ private slots:
   // Widgets
   QListWidget  *listClub;
   QLineEdit    *lineEditDate;
+  QDateEdit    *date;
   QLabel       *labelClub;
   QLabel       *labelCourse;
   QPushButton  *pushButtonNext;
index 98bbf18..8a10cfa 100644 (file)
@@ -8,11 +8,19 @@ Qt::ItemFlags ScoreTableModel::flags ( const QModelIndex & index )
   return Qt::NoItemFlags;
 }
 
-void ScoreTableModel::setScore(QList<Score *> &sList)
+// Assign the 'sList' to internal 'scoreList'. Set the current score
+// to 'currentScore', or to 's'.
+void ScoreTableModel::setScore(QList<Score *> &sList, Score *s)
 {
   scoreList = sList;
-  if (scoreList.size() > 0)
+  if (scoreList.size() > 0) {
+    if (s) {
+      currentScore = scoreList.indexOf(s);
+      if (currentScore == -1)
+       currentScore = 0;
+    }
     score = scoreList.at(currentScore); // NOTE: assumes non-empty list
+  }
 }
 
 void ScoreTableModel::setClub(QList<Club *> &cList)
@@ -42,7 +50,8 @@ QString ScoreTableModel::getCountText()
   return str;
 }
 
-Course *ScoreTableModel::findCourse(QString &clubName, QString &courseName)
+Course *ScoreTableModel::findCourse(const QString &clubName, 
+                                   const QString &courseName)
 {
   QListIterator<Club *> i(clubList);
   Club *c;
index bb4f44a..5837843 100644 (file)
@@ -22,9 +22,9 @@ public:
     course = 0;
   }
   Qt::ItemFlags flags ( const QModelIndex & index );
-  void setScore(QList<Score *> &sList);
+  void setScore(QList<Score *> &sList, Score *score = 0);
   void setClub(QList<Club *> &cList);
-  Course *findCourse(QString &clubName, QString &courseName);
+  Course *findCourse(const QString &clubName, const QString &courseName);
   int rowCount(const QModelIndex & parent) const;
   int columnCount(const QModelIndex & parent) const;
   QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;