2 * Copyright (C) 2009 Sakari Poussa
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 2.
11 #include "main-window.h"
12 #include "score-dialog.h"
13 #include "course-dialog.h"
14 #include "stat-model.h"
15 #include "xml-dom-parser.h"
17 QString appName("scorecard");
18 QString topDir("/opt");
19 QString mmcDir("/media/mmc1");
20 QString dataDirName("data");
22 QString imgDir(topDir + "/pixmaps");
23 QString scoreFileName("score.xml");
25 QString clubFileName("club.xml");
27 QString logFile("/tmp/scorecard.log");
29 bool dateLessThan(const Score *s1, const Score *s2)
34 MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
40 centralWidget = new QWidget(this);
42 setCentralWidget(centralWidget);
44 loadScoreFile(scoreFile, scoreList);
45 loadClubFile(clubFile, clubList);
47 // Sort the scores based on dates
48 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
52 createTableView(scoreList, clubList);
55 createLayout(centralWidget);
58 void MainWindow::loadSettings(void)
60 bool external = false;
66 // TODO: make via user option, automatic will never work
70 dataDir = "./" + dataDirName;
73 dataDir = mmcDir + "/" + appName + "/" + dataDirName;
76 dataDir = topDir + "/" + appName + "/" + dataDirName;
79 scoreFile = dataDir + "/" + scoreFileName;
80 clubFile = dataDir + "/" + clubFileName;
84 if (!dir.mkpath(dataDir)) {
85 qWarning() << "Unable to create: " + dataDir;
88 qDebug() << "Data is at:" + dataDir;
91 void MainWindow::createLayout(QWidget *parent)
94 buttonLayout = new QVBoxLayout;
95 //labelLayout->addStretch();
96 buttonLayout->addWidget(nextButton);
97 buttonLayout->addWidget(prevButton);
98 buttonLayout->addWidget(lastButton);
99 buttonLayout->addWidget(firstButton);
101 tableLayout = new QVBoxLayout;
102 tableLayout->addWidget(table);
104 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
105 mainLayout->addLayout(tableLayout);
106 mainLayout->addLayout(buttonLayout);
107 parent->setLayout(mainLayout);
110 // Setup 'score' tab view
111 void MainWindow::createTableView(QList<Score *> &scoreList, QList <Club *> &clubList)
113 table = new QTableView;
115 nextButton = new QPushButton(tr("Next"));
116 prevButton = new QPushButton(tr("Prev"));
117 firstButton = new QPushButton(tr("First"));
118 lastButton = new QPushButton(tr("Last"));
120 connect(nextButton, SIGNAL(clicked()), this, SLOT(nextButtonClicked()));
121 connect(prevButton, SIGNAL(clicked()), this, SLOT(prevButtonClicked()));
122 connect(firstButton, SIGNAL(clicked()), this, SLOT(firstButtonClicked()));
123 connect(lastButton, SIGNAL(clicked()), this, SLOT(lastButtonClicked()));
125 scoreTableModel = new ScoreTableModel();
129 table->setModel(scoreTableModel);
130 table->setSelectionMode(QAbstractItemView::NoSelection);
132 scoreTableModel->setScore(scoreList);
133 scoreTableModel->setClub(clubList);
135 // Fill out all the space with the tables
136 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
137 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
138 table->verticalHeader()->setAutoFillBackground(true);
139 table->horizontalHeader()->hide();
142 void MainWindow::createActions()
144 newScoreAct = new QAction(tr("New Score"), this);
145 connect(newScoreAct, SIGNAL(triggered()), this, SLOT(newScore()));
147 newCourseAct = new QAction(tr("New Course"), this);
148 connect(newCourseAct, SIGNAL(triggered()), this, SLOT(newCourse()));
150 editScoreAct = new QAction(tr("Edit Score"), this);
151 connect(editScoreAct, SIGNAL(triggered()), this, SLOT(editScore()));
153 editCourseAct = new QAction(tr("Edit Course"), this);
154 connect(editCourseAct, SIGNAL(triggered()), this, SLOT(editCourse()));
156 statAct = new QAction(tr("Statistics"), this);
157 connect(statAct, SIGNAL(triggered()), this, SLOT(viewStatistics()));
159 nextAct = new QAction(tr( " Next "), this);
160 connect(nextAct, SIGNAL(triggered()), this, SLOT(nextButtonClicked()));
162 prevAct = new QAction(" Prev ", this);
163 connect(prevAct, SIGNAL(triggered()), this, SLOT(prevButtonClicked()));
165 firstAct = new QAction(tr(" First "), this);
166 connect(firstAct, SIGNAL(triggered()), this, SLOT(firstButtonClicked()));
168 lastAct = new QAction(tr( " Last "), this);
169 connect(lastAct, SIGNAL(triggered()), this, SLOT(lastButtonClicked()));
172 void MainWindow::createMenus()
175 menu = menuBar()->addMenu("");
177 menu = menuBar()->addMenu("Menu");
180 menu->addAction(newScoreAct);
181 menu->addAction(newCourseAct);
182 menu->addAction(editScoreAct);
183 menu->addAction(editCourseAct);
184 menu->addAction(statAct);
187 void MainWindow::updateTitleBar()
189 QString title = scoreTableModel->getInfoText();
191 title = "ScoreCard - No Scores";
193 setWindowTitle(title);
196 void MainWindow::firstButtonClicked()
198 scoreTableModel->first();
202 void MainWindow::lastButtonClicked()
204 scoreTableModel->last();
208 void MainWindow::nextButtonClicked()
210 scoreTableModel->next();
214 void MainWindow::prevButtonClicked()
216 scoreTableModel->prev();
220 // FIXME: dup code from table-model.cpp
221 Club *MainWindow::findClub(QString &name)
223 QListIterator<Club *> i(clubList);
226 while (i.hasNext()) {
228 if (c->getName() == name)
234 void MainWindow::newCourse()
236 CourseSelectDialog *selectDialog = new CourseSelectDialog(this);
238 int result = selectDialog->exec();
244 selectDialog->results(clubName, courseName);
246 CourseDialog *courseDialog = new CourseDialog(this);
247 courseDialog->init();
249 QString title = "New Course: " + clubName + "," + courseName;
250 courseDialog->setWindowTitle(title);
252 int result = courseDialog->exec();
254 QVector<QString> par(18);
255 QVector<QString> hcp(18);
256 QVector<QString> len(18);
258 courseDialog->results(par, hcp, len);
261 Club *club = findClub(clubName);
263 course = club->getCourse(courseName);
265 qDebug() << "Error: club/course already in the database";
269 course = new Course(courseName, par, hcp);
270 club->addCourse(course);
274 // New club and course
275 club = new Club(clubName);
276 course = new Course(courseName, par, hcp);
277 club->addCourse(course);
280 saveClubFile(clubFile, clubList);
282 // TODO: does this really work? No mem leaks?
283 scoreTableModel->setClub(clubList);
288 void MainWindow::editCourse()
290 Course *course = scoreTableModel->getCourse();
293 qWarning() << "No course on edit";
297 CourseDialog *courseDialog = new CourseDialog(this);
298 courseDialog->init(course);
300 QString title = "Edit Course: " + course->getName();
301 courseDialog->setWindowTitle(title);
303 int result = courseDialog->exec();
305 QVector<QString> par(18);
306 QVector<QString> hcp(18);
307 QVector<QString> len(18);
309 courseDialog->results(par, hcp, len);
311 course->update(par, hcp, len);
312 saveClubFile(clubFile, clubList);
316 void MainWindow::newScore()
318 SelectDialog *selectDialog = new SelectDialog(this);
320 selectDialog->init(clubList);
322 int result = selectDialog->exec();
328 selectDialog->results(clubName, courseName, date);
330 ScoreDialog *scoreDialog = new ScoreDialog(this);
331 QString title = "New Score: " + courseName + ", " + date;
332 scoreDialog->setWindowTitle(title);
334 Club *club = findClub(clubName);
336 qWarning() << "Error: no such club:" << clubName;
339 Course *course = club->getCourse(courseName);
341 qWarning() << "Error: no such course:" << courseName;
344 scoreDialog->init(course);
345 result = scoreDialog->exec();
347 QVector<QString> scores(18);
349 scoreDialog->results(scores);
350 Score *score = new Score(scores, clubName, courseName, date);
353 // Sort the scores based on dates
354 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
356 saveScoreFile(scoreFile, scoreList);
358 // TODO: does this really work? No mem leaks?
359 scoreTableModel->setScore(scoreList, score);
365 void MainWindow::editScore()
367 Course *course = scoreTableModel->getCourse();
368 Score *score = scoreTableModel->getScore();
370 if (!course || !score) {
371 qDebug() << "No score/course to edit";
375 QString date = score->getDate();
377 ScoreDialog *scoreDialog = new ScoreDialog(this);
379 QString title = "Edit Score: " + course->getName() + ", " + date;
380 scoreDialog->setWindowTitle(title);
382 scoreDialog->init(course, score);
384 int result = scoreDialog->exec();
387 QVector<QString> scores(18);
389 scoreDialog->results(scores);
391 score->update(scores);
393 // Sort the scores based on dates
394 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
396 saveScoreFile(scoreFile, scoreList);
398 // TODO: does this really work? No mem leaks?
399 scoreTableModel->setScore(scoreList, score);
404 void MainWindow::viewStatistics()
406 QMainWindow *win = new QMainWindow(this);
407 QString title = "Statistics";
408 win->setWindowTitle(title);
410 StatModel *statModel = new StatModel(clubList, scoreList);
412 QTableView *table = new QTableView;
414 table->setSelectionMode(QAbstractItemView::NoSelection);
415 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
416 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
417 table->verticalHeader()->setAutoFillBackground(true);
418 table->setModel(statModel);
420 QWidget *central = new QWidget(win);
421 win->setCentralWidget(central);
423 QPushButton *b1 = new QPushButton("Graphs");
425 QVBoxLayout *buttonLayout = new QVBoxLayout;
426 buttonLayout->addWidget(b1);
428 QTextEdit *textEdit = new QTextEdit;
431 textEdit->setReadOnly(true);
433 QVBoxLayout *infoLayout = new QVBoxLayout;
434 infoLayout->addWidget(table);
436 QHBoxLayout *mainLayout = new QHBoxLayout(central);
437 mainLayout->addLayout(infoLayout);
438 //mainLayout->addLayout(buttonLayout);
440 central->setLayout(mainLayout);
445 void MainWindow::loadScoreFile(QString &fileName, QList<Score *> &list)
447 ScoreXmlHandler handler(list);
449 if (handler.parse(fileName))
450 qDebug() << "File loaded:" << fileName << " entries:" << list.size();
453 void MainWindow::saveScoreFile(QString &fileName, QList<Score *> &list)
455 ScoreXmlHandler handler(list);
457 if (handler.save(fileName))
459 qDebug() << "File saved:" << fileName << " entries:" << list.size();
461 qWarning() << "Unable to save:" << fileName;
464 void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list)
466 ClubXmlHandler handler(list);
468 if (handler.parse(fileName))
469 qDebug() << "File loaded:" << fileName << " entries:" << list.size();
472 void MainWindow::saveClubFile(QString &fileName, QList<Club *> &list)
474 ClubXmlHandler handler(list);
476 if (handler.save(fileName))
478 qDebug() << "File saved:" << fileName << " entries:" << list.size();
480 qWarning() << "Unable to save:" << fileName;