From f322e3807769eb7bf63e9a3edf2431bc13b6a742 Mon Sep 17 00:00:00 2001 From: Sakari Poussa Date: Mon, 12 Oct 2009 23:48:02 +0300 Subject: [PATCH] - Added course selection dialog - Added more code to data classes to handle new clubs and courses - Removed few debug prints --- Makefile | 2 +- data/club.xml | 102 +++++++++++++++++ data/save/club.xml | 300 ++++++++++++++++++++++++------------------------- data/score.xml | 80 +++++++++++++ src/course-dialog.cpp | 111 ++++++++++++++++-- src/course-dialog.h | 55 ++++----- src/data.cpp | 35 +++++- src/data.h | 4 + src/main-window.cpp | 76 +++++++++++-- src/main-window.h | 2 + src/score-dialog.cpp | 4 - 11 files changed, 570 insertions(+), 201 deletions(-) diff --git a/Makefile b/Makefile index a8ddea2..3728e4a 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ ############################################################################# # Makefile for building: bin/scorecard -# Generated by qmake (2.01a) (Qt 4.5.0) on: Mon Oct 12 19:08:14 2009 +# Generated by qmake (2.01a) (Qt 4.5.0) on: Mon Oct 12 22:55:01 2009 # Project: scorecard.pro # Template: app # Command: /opt/qtsdk-2009.01/qt/bin/qmake -unix -o Makefile scorecard.pro diff --git a/data/club.xml b/data/club.xml index 894f1d9..36a1546 100644 --- a/data/club.xml +++ b/data/club.xml @@ -149,4 +149,106 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/data/save/club.xml b/data/save/club.xml index 480d26e..894f1d9 100644 --- a/data/save/club.xml +++ b/data/save/club.xml @@ -1,152 +1,152 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/data/score.xml b/data/score.xml index 7405258..12a6cfd 100644 --- a/data/score.xml +++ b/data/score.xml @@ -839,4 +839,84 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/course-dialog.cpp b/src/course-dialog.cpp index dd6bedb..463718c 100644 --- a/src/course-dialog.cpp +++ b/src/course-dialog.cpp @@ -8,20 +8,60 @@ #include "cell-delegate.h" +CourseSelectDialog::CourseSelectDialog(QWidget *parent) : QDialog(parent) +{ + QWidget *centralWidget = new QWidget(this); + createLayout(centralWidget); + + setWindowTitle(tr("ScoreCard: New Club and/or Course")); +} + +void CourseSelectDialog::createLayout(QWidget *parent) +{ + labelClub = new QLabel(tr("Club")); + labelCourse = new QLabel(tr("Course")); + lineEditClub = new QLineEdit; + lineEditCourse = new QLineEdit; + pushButtonNext = new QPushButton(tr("Next")); + + connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next())); + + leftLayout = new QVBoxLayout; + leftLayout->addWidget(lineEditClub); + leftLayout->addWidget(lineEditCourse); + + rightLayout = new QVBoxLayout; + rightLayout->addStretch(); + rightLayout->addWidget(pushButtonNext); + + QHBoxLayout *mainLayout = new QHBoxLayout(parent); + mainLayout->addLayout(leftLayout); + mainLayout->addLayout(rightLayout); + + setLayout(mainLayout); +} + +void CourseSelectDialog::results(QString &club, + QString &course) +{ + club = lineEditClub->text(); + course = lineEditCourse->text(); +} + +void CourseSelectDialog::next(void) +{ + done(1); +} + CourseDialog::CourseDialog(QWidget *parent) : QDialog(parent) { resize(800, 400); - QFont font; - font.setPointSize(20); - setFont(font); - createTable(); createLabel(); createLayout(this); - setWindowTitle(tr("New Course")); } void CourseDialog::createLayout(QWidget *parent) @@ -31,7 +71,7 @@ void CourseDialog::createLayout(QWidget *parent) rightLayout = new QVBoxLayout; rightLayout->addStretch(); - rightLayout->addWidget(pushButtonOk); + rightLayout->addWidget(pushButtonFinnish); QHBoxLayout *mainLayout = new QHBoxLayout(parent); mainLayout->addLayout(leftLayout); @@ -47,7 +87,8 @@ void CourseDialog::createLabel(QWidget *parent) clubEdit = new QLineEdit; courseEdit = new QLineEdit; - pushButtonOk = new QPushButton(tr("OK")); + pushButtonFinnish = new QPushButton(tr("Finnish")); + connect(pushButtonFinnish, SIGNAL(clicked()), this, SLOT(finnish())); } void CourseDialog::createTable(QWidget *parent) @@ -132,3 +173,59 @@ void CourseDialog::nextCell(QObject *obj) #endif } +void CourseDialog::results(QVector &par, + QVector &hcp, + QVector &len) +{ + for (int i = 0; i < 9; i++) { + QTableWidgetItem *frontPar = table->item(ROW_PAR, i); + QTableWidgetItem *backPar = table->item(ROW_PAR_2, i); + QTableWidgetItem *frontHcp = table->item(ROW_HCP, i); + QTableWidgetItem *backHcp = table->item(ROW_HCP_2, i); + QTableWidgetItem *frontLen = table->item(ROW_LEN, i); + QTableWidgetItem *backLen = table->item(ROW_LEN_2, i); + + if (frontPar) + par[i] = frontPar->text(); + if (backPar) + par[i+9] = backPar->text(); + if (frontHcp) + hcp[i] = frontHcp->text(); + if (backHcp) + hcp[i+9] = backHcp->text(); + if (frontLen) + len[i] = frontLen->text(); + if (backLen) + len[i+9] = backLen->text(); + } +} + +// Only par is mandatory +bool CourseDialog::validate(void) +{ + for (int i = 0; i < 9; i++) { + QTableWidgetItem *frontItem = table->item(ROW_PAR, i); + QTableWidgetItem *backItem = table->item(ROW_PAR_2, i); + + if (!frontItem || !backItem) + return false; + + QString str1 = frontItem->text(); + QString str2 = backItem->text(); + + if (str1.isEmpty() || str2.isEmpty()) + return false; + } + return true; +} + +void CourseDialog::finnish(void) +{ + if (validate()) + done(1); + else { + qDebug() << "CourseDialog: invalid data, cancel or correct"; + } +} + + diff --git a/src/course-dialog.h b/src/course-dialog.h index 06bb9b5..343e34b 100644 --- a/src/course-dialog.h +++ b/src/course-dialog.h @@ -14,28 +14,31 @@ class QTableWidget; class QTableWidgetItem; QT_END_NAMESPACE -class ScoreTable : public QTableWidget +class CourseSelectDialog: public QDialog { -public: - ScoreTable(int r, int c, QWidget *p=0) : QTableWidget(r, c, p) {} - - QSize minimumSizeHint() const - { - QSize size( QTableWidget::sizeHint() ); - int width = 0; - - for (int c = 0; c < columnCount(); ++c) - width += columnWidth( c ); - - size.setWidth( width + 4 ); - return size; - } - - QSize sizeHint() const - { - return minimumSizeHint(); - } + Q_OBJECT + + public: + CourseSelectDialog(QWidget *parent = 0); + + void results(QString &club, QString &course); + +private slots: + void next(void); + + private: + + void createLayout(QWidget *parent); + + QLineEdit *lineEditClub; + QLineEdit *lineEditCourse; + QLabel *labelClub; + QLabel *labelCourse; + QPushButton *pushButtonNext; + // Layouts + QVBoxLayout *leftLayout; + QVBoxLayout *rightLayout; }; class CourseDialog: public QDialog @@ -44,23 +47,23 @@ class CourseDialog: public QDialog public: CourseDialog(QWidget *w); + void results(QVector &, QVector &, QVector &); + bool validate(); public slots: void itemChanged(QTableWidgetItem *item); void nextCell(QObject *); + void finnish(void); private: + enum { ROW_HOLE = 0, ROW_PAR = 1, ROW_HCP = 2, ROW_LEN = 3, + ROW_HOLE_2 = 4, ROW_PAR_2 = 5, ROW_HCP_2 = 6, ROW_LEN_2 = 7}; enum { rows = 8, cols = 9, separator_row = 3 }; - enum { NumGridRows = 3, NumButtons = 4 }; void createTable(QWidget *parent = 0); void createLabel(QWidget *parent = 0); void createLayout(QWidget *parent = 0); - QLabel *labels[NumGridRows]; - QLineEdit *lineEdits[NumGridRows]; - QPushButton *buttons[NumButtons]; - // Widgets //ScoreTable *table; QTableWidget *table; @@ -68,7 +71,7 @@ class CourseDialog: public QDialog QLabel *courseLabel; QLineEdit *clubEdit; QLineEdit *courseEdit; - QPushButton *pushButtonOk; + QPushButton *pushButtonFinnish; // Layouts QVBoxLayout *rightLayout; diff --git a/src/data.cpp b/src/data.cpp index d49082c..020d701 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -28,6 +28,13 @@ Hole::Hole(const QDomElement node) { par = node.attribute("par", ""); } +Hole::Hole(int num, QString &par, QString &hcp) +{ + this->num = QString::number(num); + this->par = par; + this->hcp = hcp; +} + Hole::Hole(int num, QString &shots) { this->num = QString::number(num); @@ -94,13 +101,12 @@ Score::Score(QVector scores, QString &club, QString &course, QString &d this->course = course; this->date = date; - for (int i=0; i &par, + QVector &hcp) +{ + this->name = name; + + for (int i = 0; i < par.size(); i++) { + Hole *hole = new Hole(i+1, par[i], hcp[i]); + holeList << hole; + } +} + QDomElement Course::toElement(QDomDocument doc) { QDomElement node = doc.createElement("course"); @@ -254,6 +272,11 @@ Club::Club(const QDomElement node) { name = node.attribute("name", ""); } +Club::Club(QString &name) +{ + this->name = name; +} + void Club::addCourse(Course *iCourse) { courseList << iCourse; } @@ -283,16 +306,16 @@ Course *Club::getCourse(int pos) { return courseList.at(pos); } -Course *Club::getCourse(QString &courseName) { +Course *Club::getCourse(QString &courseName) +{ QListIterator i(courseList); Course *c = 0; while (i.hasNext()) { c = i.next(); if (c->getName() == courseName) { - qDebug() << "Match " << courseName; - break; + return c; } } - return c; + return 0; } diff --git a/src/data.h b/src/data.h index 77c74f1..8936e21 100644 --- a/src/data.h +++ b/src/data.h @@ -15,6 +15,7 @@ class Hole { Hole(const QXmlAttributes &attrs); Hole(const QDomElement node); Hole(int num, QString &shots); + Hole(int num, QString &par, QString &hcp); QDomElement toElement(QDomDocument doc); QString getShots(); QString getHcp(); @@ -52,6 +53,7 @@ class Course { public: Course(const QXmlAttributes &attrs); Course(const QDomElement node); + Course(QString &name, QVector &, QVector &); QDomElement toElement(QDomDocument doc); void addHole(Hole *iHole); QString getPar(int i); @@ -71,6 +73,8 @@ class Club { Club(const QXmlAttributes &attrs); Club(const QDomElement node); + Club(QString &name); + QDomElement toElement(QDomDocument doc); void addCourse(Course *iCourse); void dump(); diff --git a/src/main-window.cpp b/src/main-window.cpp index d19bf8b..a9f7cf3 100644 --- a/src/main-window.cpp +++ b/src/main-window.cpp @@ -204,11 +204,69 @@ void MainWindow::prevButtonClicked() updateStatusBar(); } +// FIXME: dup code from table-model.cpp +Club *MainWindow::findClub(QString &name) +{ + QListIterator i(clubList); + Club *c; + + while (i.hasNext()) { + c = i.next(); + if (c->getName() == name) + return c; + } + return 0; +} + void MainWindow::newCourse() { - CourseDialog *dlg = new CourseDialog(this); + CourseSelectDialog *selectDialog = new CourseSelectDialog(this); + + int result = selectDialog->exec(); + if (result) { + QString clubName; + QString courseName; + QString date; - int result = dlg->exec(); + selectDialog->results(clubName, courseName); + qDebug() << "club=" << clubName; + qDebug() << "course=" << courseName; + + CourseDialog *courseDialog = new CourseDialog(this); + + QString title = "New Course : " + clubName + "," + courseName; + courseDialog->setWindowTitle(title); + + int result = courseDialog->exec(); + if (result) { + QVector par(18); + QVector hcp(18); + QVector len(18); + + courseDialog->results(par, hcp, len); + + Course *course = 0; + Club *club = findClub(clubName); + if (club) { + course = club->getCourse(courseName); + if (course) { + qDebug() << "Error: club/course already in the database"; + return; + } + else { + course = new Course(courseName, par, hcp); + club->addCourse(course); + } + } + else { + club = new Club(clubName); + course = new Course(courseName, par, hcp); + club->addCourse(course); + clubList << club; + } + saveClubFile(clubFile, clubList); + } + } } void MainWindow::newScore() @@ -230,9 +288,16 @@ void MainWindow::newScore() ScoreDialog *scoreDialog = new ScoreDialog(this); - // FIXME - Club *club = clubList[0]; + Club *club = findClub(clubName); + if (!club) { + qDebug() << "Error: no such club: " << clubName; + return; + } Course *course = club->getCourse(courseName); + if (!course) { + qDebug() << "Error: no such course: " << courseName; + return; + } scoreDialog->init(course); @@ -242,9 +307,6 @@ void MainWindow::newScore() QVector scores(18); scoreDialog->results(scores); - for (int i=0; i scoreList; QList clubList; diff --git a/src/score-dialog.cpp b/src/score-dialog.cpp index b33f12e..589f61a 100644 --- a/src/score-dialog.cpp +++ b/src/score-dialog.cpp @@ -120,10 +120,6 @@ ScoreDialog::ScoreDialog(QWidget *parent) : QDialog(parent) { resize(800, 400); - QFont font; - font.setPointSize(20); - setFont(font); - QWidget *centralWidget = new QWidget(this); createTable(); -- 1.7.9.5