X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=src%2Fmain-window.cpp;h=deda9efdae5396d2a089e4caa91946df5be9724a;hb=306a8cca83cf28073b95bbd84e30a0f5c7fae167;hp=02080e3f93b197989aae57d2be196effe955d005;hpb=74f6fa62b271a126a4488a2b20c0033a05288f0f;p=scorecard diff --git a/src/main-window.cpp b/src/main-window.cpp index 02080e3..deda9ef 100644 --- a/src/main-window.cpp +++ b/src/main-window.cpp @@ -1,5 +1,17 @@ +/* + * Copyright (C) 2009 Sakari Poussa + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2. + */ + #include +#ifdef Q_WS_MAEMO_5 +#include +#endif +#include "score-common.h" #include "main-window.h" #include "score-dialog.h" #include "course-dialog.h" @@ -17,16 +29,87 @@ QString scoreFile; QString clubFileName("club.xml"); QString clubFile; QString logFile("/tmp/scorecard.log"); +QString titleScores("ScoreCard - Scores"); +QString titleCourses("ScoreCard - Courses"); bool dateLessThan(const Score *s1, const Score *s2) { return (*s1) < (*s2); } +bool dateMoreThan(const Score *s1, const Score *s2) +{ + return (*s1) > (*s2); +} +// Find score based on club and course name +Score *MainWindow::findScore(QString & clubName, QString & courseName) +{ + QListIterator i(scoreList); + Score * s; + + while (i.hasNext()) { + s = i.next(); + if ((s->getClubName() == clubName) && + (s->getCourseName() == courseName)) + return s; + } + return 0; +} + +// Find club based on name +Club *MainWindow::findClub(QString &name) +{ + QListIterator i(clubList); + Club *c; + + while (i.hasNext()) { + c = i.next(); + if (c->getName() == name) + return c; + } + return 0; +} + +// Find course based on club & course name +Course *MainWindow::findCourse(const QString &clubName, + const QString &courseName) +{ + QListIterator i(clubList); + Club *c; + + while (i.hasNext()) { + c = i.next(); + if (c->getName() == clubName) { + return c->getCourse(courseName); + } + } + return 0; +} + +// Find course based on current selection on the list +// TODO: make sure this is only called when course list is the model... +Course *MainWindow::findCourse() +{ + QModelIndex index = selectionModel->currentIndex(); + const QAbstractItemModel *model = selectionModel->model(); + QString str = model->data(index, Qt::DisplayRole).toString(); + + QStringList strList = str.split(","); + if (strList.count() != 2) { + showNote(tr("Invalid course selection")); + return 0; + } + return findCourse(strList[0], strList[1]); +} + MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent) { resize(800, 480); +#ifdef Q_WS_MAEMO_5 + setAttribute(Qt::WA_Maemo5StackedWindow); +#endif + loadSettings(); centralWidget = new QWidget(this); @@ -37,14 +120,13 @@ MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent) loadClubFile(clubFile, clubList); // Sort the scores based on dates - qSort(scoreList.begin(), scoreList.end(), dateLessThan); + qSort(scoreList.begin(), scoreList.end(), dateMoreThan); createActions(); createMenus(); - createTableView(scoreList, clubList); - createStatusBar(); + createListView(scoreList, clubList); - createLayout(centralWidget); + createLayoutList(centralWidget); } void MainWindow::loadSettings(void) @@ -58,7 +140,7 @@ void MainWindow::loadSettings(void) // TODO: make via user option, automatic will never work external = false; -#ifndef Q_WS_HILDON +#ifndef Q_WS_MAEMO_5 dataDir = "./" + dataDirName; #else if (external) { @@ -80,325 +162,414 @@ void MainWindow::loadSettings(void) qDebug() << "Data is at:" + dataDir; } -void MainWindow::createLayout(QWidget *parent) +void MainWindow::createLayoutList(QWidget *parent) { + QVBoxLayout * tableLayout = new QVBoxLayout; + tableLayout->addWidget(list); - buttonLayout = new QVBoxLayout; - //labelLayout->addStretch(); - buttonLayout->addWidget(nextButton); - buttonLayout->addWidget(prevButton); - buttonLayout->addWidget(lastButton); - buttonLayout->addWidget(firstButton); - - tableLayout = new QVBoxLayout; - tableLayout->addWidget(table); - - QHBoxLayout *mainLayout = new QHBoxLayout(parent); - mainLayout->addLayout(tableLayout); - mainLayout->addLayout(buttonLayout); - parent->setLayout(mainLayout); + QHBoxLayout *mainLayout = new QHBoxLayout(parent); + mainLayout->addLayout(tableLayout); + parent->setLayout(mainLayout); } -// Setup 'score' tab view -void MainWindow::createTableView(QList &scoreList, QList &clubList) +void MainWindow::createListView(QList &scoreList, + QList &clubList) { - table = new QTableView; + list = new QListView(this); - nextButton = new QPushButton(tr("Next")); - prevButton = new QPushButton(tr("Prev")); - firstButton = new QPushButton(tr("First")); - lastButton = new QPushButton(tr("Last")); + scoreListModel = new ScoreListModel(scoreList, clubList); + courseListModel = new CourseListModel(clubList); - connect(nextButton, SIGNAL(clicked()), this, SLOT(nextButtonClicked())); - connect(prevButton, SIGNAL(clicked()), this, SLOT(prevButtonClicked())); - connect(firstButton, SIGNAL(clicked()), this, SLOT(firstButtonClicked())); - connect(lastButton, SIGNAL(clicked()), this, SLOT(lastButtonClicked())); + list->setStyleSheet(ScoreStyle::style()); - scoreTableModel = new ScoreTableModel(); + list->setSelectionMode(QAbstractItemView::SingleSelection); + list->setProperty("FingerScrolling", true); - table->showGrid(); + // Initial view + listScores(); - table->setModel(scoreTableModel); - table->setSelectionMode(QAbstractItemView::NoSelection); - - scoreTableModel->setScore(scoreList); - scoreTableModel->setClub(clubList); + connect(list, SIGNAL(clicked(QModelIndex)), + this, SLOT(clickedList(QModelIndex))); +} - // Fill out all the space with the tables - table->horizontalHeader()->setResizeMode(QHeaderView::Stretch); - table->verticalHeader()->setResizeMode(QHeaderView::Stretch); - table->verticalHeader()->setAutoFillBackground(true); - table->horizontalHeader()->hide(); +void MainWindow::listScores() +{ + list->setModel(scoreListModel); + selectionModel = list->selectionModel(); + updateTitleBar(titleScores); } -void MainWindow::createStatusBar() +void MainWindow::listCourses() { -#if 0 - // TODO: use toolbar or buttons. Toolbar seems not to be ready and - // requires more work. - toolbar = addToolBar(tr("foo")); - toolbar->addAction(firstAct); - toolbar->addAction(lastAct); - toolbar->addAction(prevAct); - toolbar->addAction(nextAct); -#endif - updateStatusBar(); + list->setModel(courseListModel); + selectionModel = list->selectionModel(); + updateTitleBar(titleCourses); } void MainWindow::createActions() { - newScoreAct = new QAction(tr("New Score"), this); - connect(newScoreAct, SIGNAL(triggered()), this, SLOT(newScore())); - - 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())); - - editCourseAct = new QAction(tr("Edit Course"), this); - connect(editCourseAct, SIGNAL(triggered()), this, SLOT(editCourse())); + newScoreAction = new QAction(tr("New Score"), this); + connect(newScoreAction, SIGNAL(triggered()), this, SLOT(newScore())); - statAct = new QAction(tr("Statistics"), this); - connect(statAct, SIGNAL(triggered()), this, SLOT(viewStatistics())); + newCourseAction = new QAction(tr("New Course"), this); + connect(newCourseAction, SIGNAL(triggered()), this, SLOT(newCourse())); - nextAct = new QAction(tr( " Next "), this); - connect(nextAct, SIGNAL(triggered()), this, SLOT(nextButtonClicked())); + statAction = new QAction(tr("Statistics"), this); + connect(statAction, SIGNAL(triggered()), this, SLOT(viewStatistics())); - prevAct = new QAction(" Prev ", this); - connect(prevAct, SIGNAL(triggered()), this, SLOT(prevButtonClicked())); + // Maemo5 style menu filters + filterGroup = new QActionGroup(this); + filterGroup->setExclusive(true); - firstAct = new QAction(tr(" First "), this); - connect(firstAct, SIGNAL(triggered()), this, SLOT(firstButtonClicked())); + listScoreAction = new QAction(tr("Scores"), filterGroup); + listScoreAction->setCheckable(true); + listScoreAction->setChecked(true); + connect(listScoreAction, SIGNAL(triggered()), this, SLOT(listScores())); - lastAct = new QAction(tr( " Last "), this); - connect(lastAct, SIGNAL(triggered()), this, SLOT(lastButtonClicked())); + listCourseAction = new QAction(tr("Courses"), filterGroup); + listCourseAction->setCheckable(true); + connect(listCourseAction, SIGNAL(triggered()), this, SLOT(listCourses())); } void MainWindow::createMenus() { -#ifdef Q_WS_HILDON - menu = menuBar()->addMenu(""); +#ifdef Q_WS_MAEMO_5 + menu = menuBar()->addMenu(""); #else - menu = menuBar()->addMenu("Menu"); + menu = menuBar()->addMenu("Menu"); #endif - menu->addAction(newScoreAct); - menu->addAction(newCourseAct); - menu->addAction(editScoreAct); - menu->addAction(editCourseAct); - menu->addAction(statAct); + menu->addAction(newScoreAction); + menu->addAction(newCourseAction); + menu->addAction(statAction); + menu->addActions(filterGroup->actions()); } -void MainWindow::updateStatusBar() +void MainWindow::updateTitleBar(QString & msg) { - QString title = scoreTableModel->getInfoText(); - if (title.isEmpty()) - title = "ScoreCard - No Scores"; - - setWindowTitle(title); + setWindowTitle(msg); } -void MainWindow::firstButtonClicked() +void MainWindow::showNote(QString msg) { - scoreTableModel->first(); - updateStatusBar(); +#ifdef Q_WS_MAEMO_5 + QMaemo5InformationBox::information(this, + msg, + QMaemo5InformationBox::DefaultTimeout); +#endif } -void MainWindow::lastButtonClicked() +void MainWindow::clickedList(const QModelIndex &index) { - scoreTableModel->last(); - updateStatusBar(); + int row = index.row(); + + const QAbstractItemModel *m = index.model(); + if (m == scoreListModel) { + if (row < scoreList.count()) { + Score * score = scoreList.at(row); + Course * course = findCourse(score->getClubName(), score->getCourseName()); + viewScore(score, course); + } + } + else if (m == courseListModel) { + QString str = courseListModel->data(index, Qt::DisplayRole).toString(); + QStringList strList = str.split(","); + + if (strList.count() != 2) { + showNote(QString("Invalid course selection")); + return; + } + Course * course = findCourse(strList.at(0), strList.at(1)); + viewCourse(course); + } } -void MainWindow::nextButtonClicked() -{ - scoreTableModel->next(); - updateStatusBar(); -} -void MainWindow::prevButtonClicked() +void MainWindow::newCourse() { - scoreTableModel->prev(); - updateStatusBar(); + CourseSelectDialog *selectDialog = new CourseSelectDialog(this); + + int result = selectDialog->exec(); + if (result) { + QString clubName; + QString courseName; + QString date; + + selectDialog->results(clubName, courseName); + + CourseDialog *courseDialog = new CourseDialog(this); + courseDialog->init(); + 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 { + // New club and course + club = new Club(clubName); + course = new Course(courseName, par, hcp); + club->addCourse(course); + clubList << club; + } + // Save it + saveClubFile(clubFile, clubList); + courseListModel->update(clubList); + list->update(); + } + } } -// FIXME: dup code from table-model.cpp -Club *MainWindow::findClub(QString &name) +void MainWindow::deleteCourse() { - QListIterator i(clubList); - Club *c; + Course * course = findCourse(); + Club * club = course->parent(); - while (i.hasNext()) { - c = i.next(); - if (c->getName() == name) - return c; - } - return 0; + // Can not delete course if it has scores -- check + if (findScore(club->getName(), course->getName()) != 0) { + showNote(tr("Can not delete course, delete scores on the course first")); + return; + } + // Close the window + if (courseWin) + courseWin->close(); + + club->delCourse(course); + + if (club->isEmpty()) { + int index = clubList.indexOf(club); + if (index != -1) + clubList.removeAt(index); + } + + // Save it + saveClubFile(clubFile, clubList); + courseListModel->update(clubList); + list->update(); } -void MainWindow::newCourse() +void MainWindow::editCourse() { - CourseSelectDialog *selectDialog = new CourseSelectDialog(this); + Course *course = findCourse(); - int result = selectDialog->exec(); - if (result) { - QString clubName; - QString courseName; - QString date; - - selectDialog->results(clubName, courseName); + if (!course) { + showNote(tr("No course on edit")); + return; + } CourseDialog *courseDialog = new CourseDialog(this); - courseDialog->init(); + courseDialog->init(course); - QString title = "New Course: " + clubName + "," + courseName; + QString title = "Edit Course: " + course->getName(); 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 { - // New club and course - club = new Club(clubName); - course = new Course(courseName, par, hcp); - club->addCourse(course); - clubList << club; - } - saveClubFile(clubFile, clubList); - - // TODO: does this really work? No mem leaks? - scoreTableModel->setClub(clubList); + QVector par(18); + QVector hcp(18); + QVector len(18); + + courseDialog->results(par, hcp, len); + + course->update(par, hcp, len); + saveClubFile(clubFile, clubList); } - } } -void MainWindow::editCourse() +void MainWindow::newScore() { - Course *course = scoreTableModel->getCourse(); - - if (!course) { - qWarning() << "No course on edit"; - return; - } + SelectDialog *selectDialog = new SelectDialog(this); - CourseDialog *courseDialog = new CourseDialog(this); - courseDialog->init(course); + selectDialog->init(clubList); - QString title = "Edit Course: " + course->getName(); - courseDialog->setWindowTitle(title); - - int result = courseDialog->exec(); - if (result) { - QVector par(18); - QVector hcp(18); - QVector len(18); - - courseDialog->results(par, hcp, len); - - course->update(par, hcp, len); - saveClubFile(clubFile, clubList); - } + int result = selectDialog->exec(); + if (result) { + QString clubName; + QString courseName; + QString date; + + selectDialog->results(clubName, courseName, date); + + ScoreDialog *scoreDialog = new ScoreDialog(this); + QString title = "New Score: " + courseName + ", " + date; + scoreDialog->setWindowTitle(title); + + Club *club = findClub(clubName); + if (!club) { + showNote(tr("Error: no such club")); + return; + } + Course *course = club->getCourse(courseName); + if (!course) { + showNote(tr("Error: no such course:")); + return; + } + scoreDialog->init(course); + result = scoreDialog->exec(); + if (result) { + QVector scores(18); + + scoreDialog->results(scores); + Score *score = new Score(scores, clubName, courseName, date); + scoreList << score; + + // Sort the scores based on dates + qSort(scoreList.begin(), scoreList.end(), dateMoreThan); + // Save it + saveScoreFile(scoreFile, scoreList); + scoreListModel->update(scoreList); + list->update(); + } + } } -void MainWindow::newScore() +void MainWindow::deleteScore() { - SelectDialog *selectDialog = new SelectDialog(this); + if (scoreWin) + scoreWin->close(); - selectDialog->init(clubList); + QModelIndex index = selectionModel->currentIndex(); + scoreList.removeAt(index.row()); + // Save it + saveScoreFile(scoreFile, scoreList); + scoreListModel->update(scoreList); + list->update(); +} - int result = selectDialog->exec(); - if (result) { - QString clubName; - QString courseName; - QString date; +void MainWindow::editScore() +{ + QModelIndex index = selectionModel->currentIndex(); + Score * score = scoreList.at(index.row()); + Course * course = 0; + if (score) + course = findCourse(score->getClubName(), score->getCourseName()); + + if (!course || !score) { + qDebug() << "No score/course to edit"; + return; + } - selectDialog->results(clubName, courseName, date); + QString date = score->getDate(); - ScoreDialog *scoreDialog = new ScoreDialog(this); - QString title = "New Score: " + courseName + ", " + date; + ScoreDialog *scoreDialog = new ScoreDialog; + + QString title = "Edit Score: " + course->getName() + ", " + date; scoreDialog->setWindowTitle(title); - Club *club = findClub(clubName); - if (!club) { - qWarning() << "Error: no such club:" << clubName; - return; - } - Course *course = club->getCourse(courseName); - if (!course) { - qWarning() << "Error: no such course:" << courseName; - return; - } - scoreDialog->init(course); - result = scoreDialog->exec(); - if (result) { - QVector scores(18); + scoreDialog->init(course, score); - scoreDialog->results(scores); - Score *score = new Score(scores, clubName, courseName, date); - scoreList << score; + int result = scoreDialog->exec(); - // Sort the scores based on dates - qSort(scoreList.begin(), scoreList.end(), dateLessThan); - // Save it - saveScoreFile(scoreFile, scoreList); + if (result) { + QVector scores(18); - // TODO: does this really work? No mem leaks? - scoreTableModel->setScore(scoreList, score); - updateStatusBar(); + scoreDialog->results(scores); + + score->update(scores); + + // Sort the scores based on dates + qSort(scoreList.begin(), scoreList.end(), dateMoreThan); + // Save it + saveScoreFile(scoreFile, scoreList); + // Update the model + scoreListModel->update(scoreList); } - } } -void MainWindow::editScore() +void MainWindow::viewScore(Score * score, Course * course) { - Course *course = scoreTableModel->getCourse(); - Score *score = scoreTableModel->getScore(); - QString date = score->getDate(); + scoreWin = new QMainWindow(this); + QString title = QString("Score: %1, %2 - %3").arg(score->getClubName()).arg(score->getCourseName()).arg(score->getDate()); + scoreWin->setWindowTitle(title); +#ifdef Q_WS_MAEMO_5 + scoreWin->setAttribute(Qt::WA_Maemo5StackedWindow); +#endif - ScoreDialog *scoreDialog = new ScoreDialog(this); - - QString title = "Edit Score: " + course->getName() + ", " + date; - scoreDialog->setWindowTitle(title); + QAction *editAction = new QAction(tr("Edit"), scoreWin); + connect(editAction, SIGNAL(triggered()), this, SLOT(editScore())); + scoreWin->menuBar()->addAction(editAction); - scoreDialog->init(course, score); + QAction *delAction = new QAction(tr("Delete"), scoreWin); + connect(delAction, SIGNAL(triggered()), this, SLOT(deleteScore())); + scoreWin->menuBar()->addAction(delAction); - int result = scoreDialog->exec(); + ScoreTableModel *model = new ScoreTableModel(score, course); + + QTableView * table = new QTableView; + table->showGrid(); + table->setSelectionMode(QAbstractItemView::NoSelection); + //table->setStyleSheet(ScoreStyle::headerView()); + table->setStyleSheet(ScoreStyle::style()); + table->horizontalHeader()->setResizeMode(QHeaderView::Stretch); + table->verticalHeader()->setResizeMode(QHeaderView::Stretch); + table->horizontalHeader()->hide(); + table->setModel(model); + + QWidget *central = new QWidget(scoreWin); + scoreWin->setCentralWidget(central); + + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(table); + + central->setLayout(layout); + scoreWin->show(); +} - if (result) { - QVector scores(18); +void MainWindow::viewCourse(Course * course) +{ + courseWin = new QMainWindow(this); + QString title = QString("Course: %1, Par - %2").arg(course->getName()).arg(course->getTotal(Total)); + courseWin->setWindowTitle(title); +#ifdef Q_WS_MAEMO_5 + courseWin->setAttribute(Qt::WA_Maemo5StackedWindow); +#endif - scoreDialog->results(scores); - - score->update(scores); + QAction *editAction = new QAction(tr("Edit"), courseWin); + connect(editAction, SIGNAL(triggered()), this, SLOT(editCourse())); + courseWin->menuBar()->addAction(editAction); - // Sort the scores based on dates - qSort(scoreList.begin(), scoreList.end(), dateLessThan); - // Save it - saveScoreFile(scoreFile, scoreList); + QAction *delAction = new QAction(tr("Delete"), courseWin); + connect(delAction, SIGNAL(triggered()), this, SLOT(deleteCourse())); + courseWin->menuBar()->addAction(delAction); - // TODO: does this really work? No mem leaks? - scoreTableModel->setScore(scoreList, score); - updateStatusBar(); - } + CourseTableModel *model = new CourseTableModel(course); + + QTableView * table = new QTableView; + table->showGrid(); + table->setSelectionMode(QAbstractItemView::NoSelection); + //table->setStyleSheet(ScoreStyle::headerView()); + table->setStyleSheet(ScoreStyle::style()); + table->horizontalHeader()->setResizeMode(QHeaderView::Stretch); + table->verticalHeader()->setResizeMode(QHeaderView::Stretch); + table->horizontalHeader()->hide(); + table->setModel(model); + + QWidget *central = new QWidget(courseWin); + courseWin->setCentralWidget(central); + + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(table); + + central->setLayout(layout); + courseWin->show(); } void MainWindow::viewStatistics() @@ -406,27 +577,25 @@ void MainWindow::viewStatistics() QMainWindow *win = new QMainWindow(this); QString title = "Statistics"; win->setWindowTitle(title); +#ifdef Q_WS_MAEMO_5 + win->setAttribute(Qt::WA_Maemo5StackedWindow); +#endif - StatModel *statModel = new StatModel(clubList, scoreList); + StatModel *model = new StatModel(clubList, scoreList); QTableView *table = new QTableView; table->showGrid(); table->setSelectionMode(QAbstractItemView::NoSelection); + table->setStyleSheet(ScoreStyle::headerView()); table->horizontalHeader()->setResizeMode(QHeaderView::Stretch); table->verticalHeader()->setResizeMode(QHeaderView::Stretch); table->verticalHeader()->setAutoFillBackground(true); - table->setModel(statModel); + table->setModel(model); QWidget *central = new QWidget(win); win->setCentralWidget(central); - QPushButton *b1 = new QPushButton("Graphs"); - - QVBoxLayout *buttonLayout = new QVBoxLayout; - buttonLayout->addWidget(b1); - QTextEdit *textEdit = new QTextEdit; - //getStat(textEdit); textEdit->setReadOnly(true); @@ -435,8 +604,6 @@ void MainWindow::viewStatistics() QHBoxLayout *mainLayout = new QHBoxLayout(central); mainLayout->addLayout(infoLayout); - mainLayout->addLayout(buttonLayout); - central->setLayout(mainLayout); win->show();