Added edit score functionality
authorSakari Poussa <sakari.poussa@nokia.com>
Sun, 25 Oct 2009 15:37:38 +0000 (17:37 +0200)
committerSakari Poussa <sakari.poussa@nokia.com>
Sun, 25 Oct 2009 15:37:38 +0000 (17:37 +0200)
TODO
src/data.cpp
src/data.h
src/main-window.cpp
src/main-window.h
src/score-dialog.cpp
src/score-dialog.h
src/table-model.cpp
src/table-model.h

diff --git a/TODO b/TODO
index f9cd1d8..4bd1beb 100644 (file)
--- a/TODO
+++ b/TODO
@@ -21,6 +21,9 @@ TODO Hildon 2.2
 - Date picker for the score dialog
 - Replace LineEdit w/ HildonEntry
  
+TODO Arch
+- move scorelist (and all data handling) to table-model 
+
 
 Know issues
   - Data on MMC does not work
index 1b0a846..edfff01 100644 (file)
@@ -65,6 +65,10 @@ QString Hole::getShots() {
   return shots;
 }
 
+void Hole::setShots(QString& s) {
+  shots = s;
+}
+
 QString Hole::getHcp() {
   return hcp;
 }
@@ -128,6 +132,16 @@ QDomElement Score::toElement(QDomDocument doc)
   return node;
 }
 
+int Score::update(QVector<QString> scores)
+{
+  for (int i = 0; i < scores.size(); i++) {
+    Hole *hole = holeList.at(i);
+    if (hole->getShots() != scores[i])
+      hole->setShots(scores[i]);
+  }
+  return 0;
+}
+
 void Score::addHole(Hole *iHole) {
   holeList << iHole;
 }
index e71bb50..0dabb10 100644 (file)
@@ -18,6 +18,7 @@ class Hole {
   Hole(int num, QString &par, QString &hcp);
   QDomElement toElement(QDomDocument doc);
   QString getShots();
+  void setShots(QString& shots);
   QString getHcp();
   QString getPar();
   void dump();
@@ -40,6 +41,7 @@ class Score {
   }
 
   QDomElement toElement(QDomDocument doc);
+  int update(QVector<QString> scores);
   void addHole(Hole *iHole);
   QString getScore(int i) const;
   QString getTotal(int what) const;
index 46d7328..34958fa 100644 (file)
@@ -28,7 +28,7 @@ bool dateLessThan(const Score *s1, const Score *s2)
   return (*s1) < (*s2);
 }
 
-MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
+MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
 {
   resize(800, 480);
 
@@ -36,9 +36,6 @@ MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
 
   QWidget *centralWidget = new QWidget(this);
 
-  // TODO: move to proper function
-  table = new QTableView(centralWidget);
-
   setCentralWidget(centralWidget);
 
   loadScoreFile(scoreFile, scoreList);
@@ -109,6 +106,8 @@ void MainWindow::createLayout(QWidget *parent)
 // Setup 'score' tab view
 void MainWindow::createTableView(QList<Score *> &scoreList, QList <Club *> &clubList)
 {
+  table = new QTableView;
+
   nextButton = new QPushButton(tr(">"));
   prevButton = new QPushButton(tr("<"));
   firstButton = new QPushButton(tr("<<"));
@@ -127,9 +126,6 @@ void MainWindow::createTableView(QList<Score *> &scoreList, QList <Club *> &club
 
   table->setModel(scoreTableModel);
 
-  // Flag model items which one belongs to front and back nines
-  table->setRootIndex(scoreTableModel->index(0, 1));
-
   // Fill out all the space with the tables
   table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
   table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
@@ -182,12 +178,15 @@ void MainWindow::createStatusBar()
 
 void MainWindow::createActions()
 {
-  newScoreAct = new QAction(tr("&New Score"), this);
+  newScoreAct = new QAction(tr("New Score"), this);
   connect(newScoreAct, SIGNAL(triggered()), this, SLOT(newScore()));
 
-  newCourseAct = new QAction(tr("&New Course"), this);
+  newCourseAct = new QAction(tr("New Course"), this);
   connect(newCourseAct, SIGNAL(triggered()), this, SLOT(newCourse()));
 
+  editScoreAct = new QAction(tr("Edit Score"), this);
+  connect(editScoreAct, SIGNAL(triggered()), this, SLOT(editScore()));
+
 #if 0
   viewScoreAct = new QAction(tr("&View Scores"), this);
   connect(viewScoreAct, SIGNAL(triggered()), this, SLOT(viewScore()));
@@ -210,6 +209,7 @@ void MainWindow::createMenus()
 #endif
   menu->addAction(newScoreAct);
   menu->addAction(newCourseAct);
+  menu->addAction(editScoreAct);
 }
 
 void MainWindow::updateStatusBar()
@@ -269,7 +269,7 @@ void MainWindow::newCourse()
 
     CourseDialog *courseDialog = new CourseDialog(this);
 
-    QString title = "New Course : " + clubName + "," + courseName;
+    QString title = "New Course: " + clubName + "," + courseName;
     courseDialog->setWindowTitle(title);
 
     int result = courseDialog->exec();
@@ -323,7 +323,7 @@ void MainWindow::newScore()
     selectDialog->results(clubName, courseName, date);
 
     ScoreDialog *scoreDialog = new ScoreDialog(this);
-    QString title = "New Score : " + courseName + ", " + date;
+    QString title = "New Score: " + courseName + ", " + date;
     scoreDialog->setWindowTitle(title);
 
     Club *club = findClub(clubName);
@@ -360,12 +360,45 @@ void MainWindow::newScore()
   }
 }
 
+void MainWindow::editScore()
+{
+  Course *course = scoreTableModel->getCourse();
+  Score *score = scoreTableModel->getScore();
+  QString date = score->getDate();
+
+  ScoreDialog *scoreDialog = new ScoreDialog(this);
+  
+  QString title = "Edit Score: " + course->getName() + ", " + date;
+  scoreDialog->setWindowTitle(title);
+
+  scoreDialog->init(course, score);
+
+  int result = scoreDialog->exec();
+
+  if (result) {
+    QVector<QString> scores(18);
+
+    scoreDialog->results(scores);
+    
+    score->update(scores);
+
+    // 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, score);
+    updateStatusBar();
+  }
+}
+
 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)
@@ -374,9 +407,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;
+    qDebug() << "Unable to save: " << fileName;
 }
 
 void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list)
@@ -384,7 +417,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)
@@ -393,8 +426,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;
+    qDebug() << "Unable to save: " << fileName;
 
 }
index 815a64c..7fdce84 100644 (file)
@@ -39,6 +39,7 @@ private slots:
   void updateTreeView(const QModelIndex & index);
   void newScore();
   void newCourse();
+  void editScore();
 
 private:
 
@@ -72,6 +73,7 @@ private:
   // Actions
   QAction *newScoreAct;
   QAction *newCourseAct;
+  QAction *editScoreAct;
   QAction *viewScoreAct;
   QAction *viewCourseAct;
   QAction *viewStatisticAct;
index 47c65b7..bea330d 100644 (file)
@@ -164,41 +164,45 @@ void ScoreDialog::createButton(QWidget *parent)
   connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
 }
 
-void ScoreDialog::init(Course *course)
+void ScoreDialog::init(Course *course, Score *score)
 {
-  QTableWidgetItem *par, *hcp, *score, *holeNum;
+  QTableWidgetItem *par, *hcp, *scoreItem, *holeNum;
   QColor bgColor(Qt::gray);
 
   for (int i = 0; i < 18; i++) {
     par = new QTableWidgetItem(course->getPar(i));
     hcp = new QTableWidgetItem(course->getHcp(i));
-    score = new QTableWidgetItem("");
+    if (score)
+      scoreItem = new QTableWidgetItem(score->getScore(i));
+    else
+      scoreItem = new QTableWidgetItem("");
     holeNum = new QTableWidgetItem(QString::number(i+1));
 
     holeNum->setBackgroundColor(bgColor);
 
     par->setTextAlignment(Qt::AlignCenter);
     hcp->setTextAlignment(Qt::AlignCenter);
-    score->setTextAlignment(Qt::AlignCenter);
+    scoreItem->setTextAlignment(Qt::AlignCenter);
     holeNum->setTextAlignment(Qt::AlignCenter);
 
     if (i < 9) {
       table->setItem(ROW_HOLE, i, holeNum);
       table->setItem(ROW_PAR, i, par);
       table->setItem(ROW_HCP, i, hcp);
-      table->setItem(ROW_SCORE, i, score);
+      table->setItem(ROW_SCORE, i, scoreItem);
     }
     else {
       table->setItem(ROW_HOLE_2, i-9, holeNum);
       table->setItem(ROW_PAR_2, i-9, par);
       table->setItem(ROW_HCP_2, i-9, hcp);
-      table->setItem(ROW_SCORE_2, i-9, score);
+      table->setItem(ROW_SCORE_2, i-9, scoreItem);
     }
   }
 
   // Set focus to 1st cell
   table->setCurrentCell(ROW_SCORE, 0);
-  setDefaultScore(table);
+  if (!score)
+    setDefaultScore(table);
 }
 
 // Set default score to par if not set
index f595b3a..854a6db 100644 (file)
@@ -54,7 +54,7 @@ class ScoreDialog: public QDialog
 
 public:
   ScoreDialog(QWidget *parent = 0);
-  void init(Course *course);
+  void init(Course *course, Score *score = 0);
   void results(QVector<QString> &scores);
   bool validate(void);
 
index 43fce9e..fe8749a 100644 (file)
@@ -6,7 +6,20 @@
 
 Qt::ItemFlags ScoreTableModel::flags ( const QModelIndex & index )
 {
-  return Qt::NoItemFlags;
+  Qt::ItemFlags flags = QAbstractItemModel::flags(index);
+  qDebug() << "flags " << currentMode;
+  
+  return flags;
+}
+
+void ScoreTableModel::setMode(int m)
+{
+  currentMode = m;
+}
+
+int ScoreTableModel::mode(void)
+{
+  return currentMode;
 }
 
 // Assign the 'sList' to internal 'scoreList'. Set the current score
@@ -51,6 +64,26 @@ QString ScoreTableModel::getCountText()
   return str;
 }
 
+QString& ScoreTableModel::clubName(void)
+{
+  QString str("");
+
+  if (club)
+    str = club->getName();
+
+  return str;
+}
+
+QString& ScoreTableModel::courseName(void)
+{
+  QString str("");
+
+  if (course)
+    str = course->getName();
+
+  return str;
+}
+
 Course *ScoreTableModel::findCourse(const QString &clubName, 
                                    const QString &courseName)
 {
@@ -66,6 +99,21 @@ Course *ScoreTableModel::findCourse(const QString &clubName,
   return 0;
 }
 
+Club *ScoreTableModel::getClub(void)
+{
+  return club;
+}
+
+Course *ScoreTableModel::getCourse(void)
+{
+  return course;
+}
+
+Score *ScoreTableModel::getScore(void)
+{
+  return score;
+}
+
 void ScoreTableModel::first()
 {
   if (score && course) {
index f0a7e31..c5a4021 100644 (file)
@@ -13,6 +13,7 @@ class ScoreTableModel : public QAbstractTableModel
   Q_OBJECT
 
 public:
+  enum { ViewMode = 0, EditMode = 1 };
 
   ScoreTableModel(QObject *parent = 0) : QAbstractTableModel(parent) 
   {
@@ -20,11 +21,20 @@ public:
     score = 0;
     club = 0;
     course = 0;
+    currentMode = ViewMode;
   }
   Qt::ItemFlags flags ( const QModelIndex & index );
+  void setMode(int m);
+  int mode(void);
   void setScore(QList<Score *> &sList, Score *score = 0);
+  Score *getScore(void);
   void setClub(QList<Club *> &cList);
+  Club *getClub(void);
+  Course *getCourse(void);
+
   Course *findCourse(const QString &clubName, const QString &courseName);
+  QString& clubName(void);
+  QString& courseName(void);
   int rowCount(const QModelIndex & parent) const;
   int columnCount(const QModelIndex & parent) const;
   QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
@@ -42,6 +52,7 @@ public:
   void last();
 
  private:
+  int currentMode;
   enum { ROWS = 8, COLS = 9 };
   enum { ROW_HOLE = 0, ROW_PAR = 1, ROW_HCP = 2, ROW_SCORE = 3, 
         ROW_HOLE_2 = 4, ROW_PAR_2 = 5, ROW_HCP_2 = 6, ROW_SCORE_2 = 7};