From 1362d51e8c026dd31200a9934a13e017b19a327b Mon Sep 17 00:00:00 2001 From: Sakari Poussa Date: Wed, 21 Oct 2009 21:46:53 +0300 Subject: [PATCH] Sort scores based on dates not the entry date. Show new score in the main view (not last anymore) --- data/score.xml | 2 +- scorecard.pro | 2 +- src/data.cpp | 20 +++++++++++++------- src/data.h | 18 +++++++++++------- src/main-window.cpp | 23 +++++++++++++++-------- src/score-dialog.cpp | 3 +++ src/score-dialog.h | 2 ++ src/table-model.cpp | 15 ++++++++++++--- src/table-model.h | 4 ++-- 9 files changed, 60 insertions(+), 29 deletions(-) diff --git a/data/score.xml b/data/score.xml index 4eb3fad..63b0c8d 100644 --- a/data/score.xml +++ b/data/score.xml @@ -659,7 +659,7 @@ - + diff --git a/scorecard.pro b/scorecard.pro index d29d7fd..830f8a3 100644 --- a/scorecard.pro +++ b/scorecard.pro @@ -1,4 +1,4 @@ -TARGET = bin/scorecard +TARGET = scorecard DESTDIR = bin MOC_DIR = moc OBJECTS_DIR = obj diff --git a/src/data.cpp b/src/data.cpp index 020d701..1b0a846 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -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; idump(); @@ -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 i(courseList); Course *c = 0; diff --git a/src/data.h b/src/data.h index 8936e21..e71bb50 100644 --- a/src/data.h +++ b/src/data.h @@ -34,19 +34,23 @@ class Score { Score(const QDomElement node); Score(QVector 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 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 getCourseList() { return courseList; } // HACK: fixme diff --git a/src/main-window.cpp b/src/main-window.cpp index c235605..6ca8d3a 100644 --- a/src/main-window.cpp +++ b/src/main-window.cpp @@ -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 &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 &list) @@ -383,7 +390,7 @@ void MainWindow::loadClubFile(QString &fileName, QList &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 &list) diff --git a/src/score-dialog.cpp b/src/score-dialog.cpp index 357e688..8fc9494 100644 --- a/src/score-dialog.cpp +++ b/src/score-dialog.cpp @@ -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; diff --git a/src/score-dialog.h b/src/score-dialog.h index 8b3defd..f595b3a 100644 --- a/src/score-dialog.h +++ b/src/score-dialog.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "data.h" @@ -35,6 +36,7 @@ private slots: // Widgets QListWidget *listClub; QLineEdit *lineEditDate; + QDateEdit *date; QLabel *labelClub; QLabel *labelCourse; QPushButton *pushButtonNext; diff --git a/src/table-model.cpp b/src/table-model.cpp index 98bbf18..8a10cfa 100644 --- a/src/table-model.cpp +++ b/src/table-model.cpp @@ -8,11 +8,19 @@ Qt::ItemFlags ScoreTableModel::flags ( const QModelIndex & index ) return Qt::NoItemFlags; } -void ScoreTableModel::setScore(QList &sList) +// Assign the 'sList' to internal 'scoreList'. Set the current score +// to 'currentScore', or to 's'. +void ScoreTableModel::setScore(QList &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 &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 i(clubList); Club *c; diff --git a/src/table-model.h b/src/table-model.h index bb4f44a..5837843 100644 --- a/src/table-model.h +++ b/src/table-model.h @@ -22,9 +22,9 @@ public: course = 0; } Qt::ItemFlags flags ( const QModelIndex & index ); - void setScore(QList &sList); + void setScore(QList &sList, Score *score = 0); void setClub(QList &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; -- 1.7.9.5