#include "main-window.h"
#include "score-dialog.h"
#include "course-dialog.h"
+#include "settings-dialog.h"
#include "stat-model.h"
#include "xml-dom-parser.h"
QString scoreFile;
QString clubFileName("club.xml");
QString clubFile;
+QString masterFileName("club-master.xml");
+QString masterFile;
QString logFile("/tmp/scorecard.log");
+QString titleScores("ScoreCard - Scores");
+QString titleCourses("ScoreCard - Courses");
bool dateLessThan(const Score *s1, const Score *s2)
{
// Find score based on club and course name
Score *MainWindow::findScore(QString & clubName, QString & courseName)
{
+ TRACE;
QListIterator<Score *> i(scoreList);
Score * s;
// Find club based on name
Club *MainWindow::findClub(QString &name)
{
+ TRACE;
QListIterator<Club *> i(clubList);
Club *c;
Course *MainWindow::findCourse(const QString &clubName,
const QString &courseName)
{
+ TRACE;
QListIterator<Club *> i(clubList);
Club *c;
}
// Find course based on current selection on the list
-Course *MainWindow::findCourse()
+// TODO: make sure this is only called when course list is the model...
+Course *MainWindow::currentCourse()
{
+ TRACE;
QModelIndex index = selectionModel->currentIndex();
+
+ if (!index.isValid())
+ return 0;
+
const QAbstractItemModel *model = selectionModel->model();
QString str = model->data(index, Qt::DisplayRole).toString();
- QStringList strList = str.split(",");
+ QStringList strList = str.split(", ");
if (strList.count() != 2) {
showNote(tr("Invalid course selection"));
return 0;
return findCourse(strList[0], strList[1]);
}
+// Find score based on current selection on the list
+// TODO: make sure this is only called when score list is the model...
+Score *MainWindow::currentScore()
+{
+ TRACE;
+ QModelIndex index = selectionModel->currentIndex();
+
+ if (!index.isValid())
+ return 0;
+
+ return scoreList.at(index.row());
+}
+
+void MainWindow::flushReadOnlyItems()
+{
+ TRACE;
+ QMutableListIterator<Club *> i(clubList);
+ Club *c;
+
+ while (i.hasNext()) {
+ c = i.next();
+ if (c->isReadOnly()) {
+ qDebug() << "Del:" << c->getName();
+ i.remove();
+ }
+ }
+}
+
+void MainWindow::markHomeClub()
+{
+ TRACE;
+ QListIterator<Club *> i(clubList);
+ Club *c;
+
+ while (i.hasNext()) {
+ c = i.next();
+ if (c->getName() == conf.homeClub)
+ c->setHomeClub(true);
+ else
+ c->setHomeClub(false);
+ }
+}
+
MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
{
- resize(800, 480);
+ resize(800, 480);
#ifdef Q_WS_MAEMO_5
- setAttribute(Qt::WA_Maemo5StackedWindow);
+ setAttribute(Qt::WA_Maemo5StackedWindow);
#endif
- loadSettings();
+ loadSettings();
- centralWidget = new QWidget(this);
+ centralWidget = new QWidget(this);
- setCentralWidget(centralWidget);
+ setCentralWidget(centralWidget);
- loadScoreFile(scoreFile, scoreList);
- loadClubFile(clubFile, clubList);
+ loadScoreFile(scoreFile, scoreList);
+ if (conf.defaultCourses == "Yes")
+ loadClubFile(masterFile, clubList, true);
+ loadClubFile(clubFile, clubList);
+ markHomeClub();
- // Sort the scores based on dates
- qSort(scoreList.begin(), scoreList.end(), dateMoreThan);
- createActions();
- createMenus();
+ // Sort the scores based on dates
+ qSort(scoreList.begin(), scoreList.end(), dateMoreThan);
+ createActions();
+ createMenus();
- createListView(scoreList, clubList);
- updateTitleBar();
+ createListView(scoreList, clubList);
- createLayoutList(centralWidget);
+ createLayoutList(centralWidget);
+
+ scoreWindow = new ScoreWindow(this);
+ courseWindow = new CourseWindow(this);
}
void MainWindow::loadSettings(void)
{
+ TRACE;
bool external = false;
QDir mmc(mmcDir);
#endif
scoreFile = dataDir + "/" + scoreFileName;
clubFile = dataDir + "/" + clubFileName;
+ masterFile = dataDir + "/" + masterFileName;
QDir dir(dataDir);
if (!dir.exists())
return;
}
qDebug() << "Data is at:" + dataDir;
+
+ settings.beginGroup(settingsGroup);
+ conf.hcp = settings.value(settingsHcp);
+ conf.homeClub = settings.value(settingsHomeClub);
+ conf.defaultCourses = settings.value(settingsDefaultCourses);
+ settings.endGroup();
+
+ // Use default courses if no settings for that
+ if (!conf.defaultCourses.isValid())
+ conf.defaultCourses = "Yes";
+
+ qDebug() << "Settings: " << conf.hcp << conf.homeClub << conf.defaultCourses;
+}
+
+void MainWindow::saveSettings(void)
+{
+ TRACE;
+ settings.beginGroup(settingsGroup);
+ if (conf.hcp.isValid())
+ settings.setValue(settingsHcp, conf.hcp);
+ if (conf.homeClub.isValid())
+ settings.setValue(settingsHomeClub, conf.homeClub);
+ if (conf.defaultCourses.isValid())
+ settings.setValue(settingsDefaultCourses, conf.defaultCourses);
+ settings.endGroup();
}
void MainWindow::createLayoutList(QWidget *parent)
{
+ TRACE;
QVBoxLayout * tableLayout = new QVBoxLayout;
tableLayout->addWidget(list);
void MainWindow::createListView(QList<Score *> &scoreList,
QList <Club *> &clubList)
{
- list = new QListView;
+ TRACE;
+ list = new QListView(this);
scoreListModel = new ScoreListModel(scoreList, clubList);
courseListModel = new CourseListModel(clubList);
- list->setStyleSheet(ScoreStyle::style());
+ list->setStyleSheet(defaultStyleSheet);
list->setSelectionMode(QAbstractItemView::SingleSelection);
+ list->setProperty("FingerScrolling", true);
// Initial view
listScores();
+
+ connect(list, SIGNAL(clicked(QModelIndex)),
+ this, SLOT(clickedList(QModelIndex)));
}
void MainWindow::listScores()
{
+ TRACE;
list->setModel(scoreListModel);
selectionModel = list->selectionModel();
- connect(selectionModel, SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)),
- this, SLOT(scoreSelectionChanged(const QItemSelection &, const QItemSelection &)));
+ updateTitleBar(titleScores);
}
void MainWindow::listCourses()
{
+ TRACE;
list->setModel(courseListModel);
selectionModel = list->selectionModel();
- connect(selectionModel, SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)),
- this, SLOT(courseSelectionChanged(const QItemSelection &, const QItemSelection &)));
+ updateTitleBar(titleCourses);
}
void MainWindow::createActions()
{
+ TRACE;
newScoreAction = new QAction(tr("New Score"), this);
connect(newScoreAction, SIGNAL(triggered()), this, SLOT(newScore()));
statAction = new QAction(tr("Statistics"), this);
connect(statAction, SIGNAL(triggered()), this, SLOT(viewStatistics()));
+ settingsAction = new QAction(tr("Settings"), this);
+ connect(settingsAction, SIGNAL(triggered()), this, SLOT(viewSettings()));
+
// Maemo5 style menu filters
filterGroup = new QActionGroup(this);
filterGroup->setExclusive(true);
void MainWindow::createMenus()
{
+ TRACE;
#ifdef Q_WS_MAEMO_5
menu = menuBar()->addMenu("");
#else
menu->addAction(newScoreAction);
menu->addAction(newCourseAction);
menu->addAction(statAction);
+ menu->addAction(settingsAction);
menu->addActions(filterGroup->actions());
}
-void MainWindow::updateTitleBar()
+void MainWindow::updateTitleBar(QString & msg)
{
- QString title("ScoreCard");
- setWindowTitle(title);
+ TRACE;
+ setWindowTitle(msg);
}
void MainWindow::showNote(QString msg)
#endif
}
-void MainWindow::scoreSelectionChanged(const QItemSelection &selected,
- const QItemSelection &deselected)
+void MainWindow::clickedList(const QModelIndex &index)
{
- QModelIndexList list = selected.indexes();
+ TRACE;
+ int row = index.row();
- int row = list.at(0).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(", ");
- Score * score = scoreList.at(row);
- Course * course = findCourse(score->getClubName(), score->getCourseName());
- viewScore(score, course);
+ if (strList.count() != 2) {
+ showNote(QString("Invalid course selection"));
+ return;
+ }
+ Course * course = findCourse(strList.at(0), strList.at(1));
+ viewCourse(course);
+ }
}
-void MainWindow::courseSelectionChanged(const QItemSelection &selected,
- const QItemSelection &deselected)
+void MainWindow::viewScore(Score * score, Course * course)
{
- QModelIndexList indexes = selected.indexes();
+ TRACE;
+ qDebug() << score << course;
+ scoreWindow->setup(score, course);
+ scoreWindow->show();
+}
- int row = indexes.at(0).row();
+void MainWindow::newScore()
+{
+ TRACE;
+ SelectDialog *selectDialog = new SelectDialog(this);
- QString str = courseListModel->data(indexes.at(0), Qt::DisplayRole).toString();
+ selectDialog->init(clubList);
- QStringList strList = str.split(",");
+ int result = selectDialog->exec();
+ if (result) {
+ QString clubName;
+ QString courseName;
+ QString date;
- if (strList.count() != 2) {
- showNote(QString("Invalid course selection"));
+ 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<QString> 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::editScore()
+{
+ TRACE;
+ Course * course = 0;
+ Score *score = currentScore();
+
+ if (score)
+ course = findCourse(score->getClubName(), score->getCourseName());
+
+ if (!course || !score) {
+ qDebug() << "No score/course to edit";
+ return;
+ }
+
+ QString date = score->getDate();
+
+ ScoreDialog *scoreDialog = new ScoreDialog(this);
+ scoreDialog->init(course, score);
+
+ QString title = "Edit Score: " + course->getName() + ", " + date;
+ scoreDialog->setWindowTitle(title);
+
+ 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(), dateMoreThan);
+ // Save it
+ saveScoreFile(scoreFile, scoreList);
+ }
+ if (scoreDialog)
+ delete scoreDialog;
+}
+
+void MainWindow::deleteScore()
+{
+ TRACE;
+ if (scoreWindow)
+ scoreWindow->close();
+
+ QModelIndex index = selectionModel->currentIndex();
+ if (!index.isValid()) {
+ qDebug() << "Invalid index";
return;
}
- Course * course = findCourse(strList.at(0), strList.at(1));
- viewCourse(course);
+
+ scoreList.removeAt(index.row());
+ // Save it
+ saveScoreFile(scoreFile, scoreList);
+ scoreListModel->update(scoreList);
+ list->update();
}
-void MainWindow::changeCurrent(const QModelIndex ¤t,
- const QModelIndex &previous)
+void MainWindow::viewCourse(Course * course)
{
- qDebug() << "Current: " << current;
+ TRACE;
+ courseWindow->setup(course);
+ courseWindow->show();
}
void MainWindow::newCourse()
{
+ TRACE;
CourseSelectDialog *selectDialog = new CourseSelectDialog(this);
int result = selectDialog->exec();
if (club) {
course = club->getCourse(courseName);
if (course) {
- qDebug() << "Error: club/course already in the database";
+ showNote(tr("Club/course already in the database"));
return;
}
else {
}
// Save it
saveClubFile(clubFile, clubList);
- // TODO: update on live
- //courseListModel->update(courseList);
+ courseListModel->update(clubList);
list->update();
}
}
}
-void MainWindow::deleteCourse()
-{
- Course * course = findCourse();
- Club * club = course->parent();
-
- // 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::editCourse()
{
- Course *course = findCourse();
+ TRACE;
+ Course *course = currentCourse();
if (!course) {
showNote(tr("No course on edit"));
course->update(par, hcp, len);
saveClubFile(clubFile, clubList);
}
+ if (courseDialog)
+ delete courseDialog;
}
-void MainWindow::newScore()
+void MainWindow::deleteCourse()
{
- SelectDialog *selectDialog = new SelectDialog(this);
-
- selectDialog->init(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<QString> scores(18);
-
- scoreDialog->results(scores);
- Score *score = new Score(scores, clubName, courseName, date);
- scoreList << score;
+ TRACE;
+ Club *club = 0;
+ Course * course = currentCourse();
- // Sort the scores based on dates
- qSort(scoreList.begin(), scoreList.end(), dateMoreThan);
- // Save it
- saveScoreFile(scoreFile, scoreList);
- scoreListModel->update(scoreList);
- list->update();
- }
+ if (!course) {
+ qDebug() << "Invalid course for deletion";
+ return;
}
-}
+ club = course->parent();
-void MainWindow::deleteScore()
-{
- if (scoreWin)
- scoreWin->close();
-
- QModelIndex index = selectionModel->currentIndex();
- scoreList.removeAt(index.row());
- // Save it
- saveScoreFile(scoreFile, scoreList);
- scoreListModel->update(scoreList);
- list->update();
-}
-
-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";
+ // 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 (courseWindow)
+ courseWindow->close();
- QString date = score->getDate();
-
- ScoreDialog *scoreDialog = new ScoreDialog;
-
- 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);
+ club->delCourse(course);
- // Sort the scores based on dates
- qSort(scoreList.begin(), scoreList.end(), dateMoreThan);
- // Save it
- saveScoreFile(scoreFile, scoreList);
- // Update the model
- scoreListModel->update(scoreList);
+ if (club->isEmpty()) {
+ int index = clubList.indexOf(club);
+ if (index != -1)
+ clubList.removeAt(index);
}
-}
-
-void MainWindow::viewScore(Score * score,
- Course * course)
-{
- 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
-
- QAction *editAction = new QAction(tr("Edit"), scoreWin);
- connect(editAction, SIGNAL(triggered()), this, SLOT(editScore()));
- scoreWin->menuBar()->addAction(editAction);
-
- QAction *delAction = new QAction(tr("Delete"), scoreWin);
- connect(delAction, SIGNAL(triggered()), this, SLOT(deleteScore()));
- scoreWin->menuBar()->addAction(delAction);
- 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();
+ // Save it
+ saveClubFile(clubFile, clubList);
+ courseListModel->update(clubList);
+ list->update();
}
-void MainWindow::viewCourse(Course * course)
+void MainWindow::viewStatistics()
{
- courseWin = new QMainWindow(this);
- QString title = QString("Course: %1, Par - %2").arg(course->getName()).arg(course->getTotal(Total));
- courseWin->setWindowTitle(title);
+ TRACE;
+ QMainWindow *win = new QMainWindow(this);
+ QString title = "Statistics";
+ win->setWindowTitle(title);
#ifdef Q_WS_MAEMO_5
- courseWin->setAttribute(Qt::WA_Maemo5StackedWindow);
+ win->setAttribute(Qt::WA_Maemo5StackedWindow);
#endif
- QAction *editAction = new QAction(tr("Edit"), courseWin);
- connect(editAction, SIGNAL(triggered()), this, SLOT(editCourse()));
- courseWin->menuBar()->addAction(editAction);
-
- QAction *delAction = new QAction(tr("Delete"), courseWin);
- connect(delAction, SIGNAL(triggered()), this, SLOT(deleteCourse()));
- courseWin->menuBar()->addAction(delAction);
+ StatModel *model = new StatModel(clubList, scoreList);
- CourseTableModel *model = new CourseTableModel(course);
-
- QTableView * table = new QTableView;
+ QTableView *table = new QTableView;
table->showGrid();
table->setSelectionMode(QAbstractItemView::NoSelection);
- //table->setStyleSheet(ScoreStyle::headerView());
- table->setStyleSheet(ScoreStyle::style());
+ table->setStyleSheet(statStyleSheet);
table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
- table->horizontalHeader()->hide();
+ table->verticalHeader()->setAutoFillBackground(true);
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()
-{
- QMainWindow *win = new QMainWindow(this);
- QString title = "Statistics";
- win->setWindowTitle(title);
-#ifdef Q_WS_MAEMO_5
- win->setAttribute(Qt::WA_Maemo5StackedWindow);
-#endif
+ QWidget *central = new QWidget(win);
+ win->setCentralWidget(central);
- StatModel *model = new StatModel(clubList, scoreList);
+ QTextEdit *textEdit = new QTextEdit;
- 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(model);
+ textEdit->setReadOnly(true);
- QWidget *central = new QWidget(win);
- win->setCentralWidget(central);
+ QVBoxLayout *infoLayout = new QVBoxLayout;
+ infoLayout->addWidget(table);
- QTextEdit *textEdit = new QTextEdit;
+ QHBoxLayout *mainLayout = new QHBoxLayout(central);
+ mainLayout->addLayout(infoLayout);
+ central->setLayout(mainLayout);
- textEdit->setReadOnly(true);
+ win->show();
+}
- QVBoxLayout *infoLayout = new QVBoxLayout;
- infoLayout->addWidget(table);
+void MainWindow::viewSettings()
+{
+ TRACE;
+ SettingsDialog *dlg = new SettingsDialog(this);
- QHBoxLayout *mainLayout = new QHBoxLayout(central);
- mainLayout->addLayout(infoLayout);
- central->setLayout(mainLayout);
+ dlg->init(conf, clubList);
- win->show();
+ int result = dlg->exec();
+ if (result) {
+ QString oldValue = conf.defaultCourses.toString();
+ dlg->results(conf);
+ QString newValue = conf.defaultCourses.toString();
+ saveSettings();
+
+ // Reload club list, or drop r/o courses from list
+ if (oldValue == "Yes" && newValue == "No") {
+ flushReadOnlyItems();
+ courseListModel->update(clubList);
+ list->update();
+ }
+ else if ((oldValue == "No" || oldValue == "") && newValue == "Yes") {
+ loadClubFile(masterFile, clubList, true);
+ courseListModel->update(clubList);
+ list->update();
+ }
+ markHomeClub();
+ }
}
void MainWindow::loadScoreFile(QString &fileName, QList<Score *> &list)
qWarning() << "Unable to save:" << fileName;
}
-void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list)
+void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list, bool readOnly)
{
- ClubXmlHandler handler(list);
+ ClubXmlHandler handler(list);
- if (handler.parse(fileName))
- qDebug() << "File loaded:" << fileName << " entries:" << list.size();
+ if (handler.parse(fileName, readOnly))
+ qDebug() << "File loaded:" << fileName << " entries:" << list.size();
}
void MainWindow::saveClubFile(QString &fileName, QList<Club *> &list)