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::createStatusBar()
145 // TODO: use toolbar or buttons. Toolbar seems not to be ready and
146 // requires more work.
147 toolbar = addToolBar(tr("foo"));
148 toolbar->addAction(firstAct);
149 toolbar->addAction(lastAct);
150 toolbar->addAction(prevAct);
151 toolbar->addAction(nextAct);
156 void MainWindow::createActions()
158 newScoreAct = new QAction(tr("New Score"), this);
159 connect(newScoreAct, SIGNAL(triggered()), this, SLOT(newScore()));
161 newCourseAct = new QAction(tr("New Course"), this);
162 connect(newCourseAct, SIGNAL(triggered()), this, SLOT(newCourse()));
164 editScoreAct = new QAction(tr("Edit Score"), this);
165 connect(editScoreAct, SIGNAL(triggered()), this, SLOT(editScore()));
167 editCourseAct = new QAction(tr("Edit Course"), this);
168 connect(editCourseAct, SIGNAL(triggered()), this, SLOT(editCourse()));
170 statAct = new QAction(tr("Statistics"), this);
171 connect(statAct, SIGNAL(triggered()), this, SLOT(viewStatistics()));
173 nextAct = new QAction(tr( " Next "), this);
174 connect(nextAct, SIGNAL(triggered()), this, SLOT(nextButtonClicked()));
176 prevAct = new QAction(" Prev ", this);
177 connect(prevAct, SIGNAL(triggered()), this, SLOT(prevButtonClicked()));
179 firstAct = new QAction(tr(" First "), this);
180 connect(firstAct, SIGNAL(triggered()), this, SLOT(firstButtonClicked()));
182 lastAct = new QAction(tr( " Last "), this);
183 connect(lastAct, SIGNAL(triggered()), this, SLOT(lastButtonClicked()));
186 void MainWindow::createMenus()
189 menu = menuBar()->addMenu("");
191 menu = menuBar()->addMenu("Menu");
194 menu->addAction(newScoreAct);
195 menu->addAction(newCourseAct);
196 menu->addAction(editScoreAct);
197 menu->addAction(editCourseAct);
198 menu->addAction(statAct);
201 void MainWindow::updateStatusBar()
203 QString title = scoreTableModel->getInfoText();
205 title = "ScoreCard - No Scores";
207 setWindowTitle(title);
210 void MainWindow::firstButtonClicked()
212 scoreTableModel->first();
216 void MainWindow::lastButtonClicked()
218 scoreTableModel->last();
222 void MainWindow::nextButtonClicked()
224 scoreTableModel->next();
228 void MainWindow::prevButtonClicked()
230 scoreTableModel->prev();
234 // FIXME: dup code from table-model.cpp
235 Club *MainWindow::findClub(QString &name)
237 QListIterator<Club *> i(clubList);
240 while (i.hasNext()) {
242 if (c->getName() == name)
248 void MainWindow::newCourse()
250 CourseSelectDialog *selectDialog = new CourseSelectDialog(this);
252 int result = selectDialog->exec();
258 selectDialog->results(clubName, courseName);
260 CourseDialog *courseDialog = new CourseDialog(this);
261 courseDialog->init();
263 QString title = "New Course: " + clubName + "," + courseName;
264 courseDialog->setWindowTitle(title);
266 int result = courseDialog->exec();
268 QVector<QString> par(18);
269 QVector<QString> hcp(18);
270 QVector<QString> len(18);
272 courseDialog->results(par, hcp, len);
275 Club *club = findClub(clubName);
277 course = club->getCourse(courseName);
279 qDebug() << "Error: club/course already in the database";
283 course = new Course(courseName, par, hcp);
284 club->addCourse(course);
288 // New club and course
289 club = new Club(clubName);
290 course = new Course(courseName, par, hcp);
291 club->addCourse(course);
294 saveClubFile(clubFile, clubList);
296 // TODO: does this really work? No mem leaks?
297 scoreTableModel->setClub(clubList);
302 void MainWindow::editCourse()
304 Course *course = scoreTableModel->getCourse();
307 qWarning() << "No course on edit";
311 CourseDialog *courseDialog = new CourseDialog(this);
312 courseDialog->init(course);
314 QString title = "Edit Course: " + course->getName();
315 courseDialog->setWindowTitle(title);
317 int result = courseDialog->exec();
319 QVector<QString> par(18);
320 QVector<QString> hcp(18);
321 QVector<QString> len(18);
323 courseDialog->results(par, hcp, len);
325 course->update(par, hcp, len);
326 saveClubFile(clubFile, clubList);
330 void MainWindow::newScore()
332 SelectDialog *selectDialog = new SelectDialog(this);
334 selectDialog->init(clubList);
336 int result = selectDialog->exec();
342 selectDialog->results(clubName, courseName, date);
344 ScoreDialog *scoreDialog = new ScoreDialog(this);
345 QString title = "New Score: " + courseName + ", " + date;
346 scoreDialog->setWindowTitle(title);
348 Club *club = findClub(clubName);
350 qWarning() << "Error: no such club:" << clubName;
353 Course *course = club->getCourse(courseName);
355 qWarning() << "Error: no such course:" << courseName;
358 scoreDialog->init(course);
359 result = scoreDialog->exec();
361 QVector<QString> scores(18);
363 scoreDialog->results(scores);
364 Score *score = new Score(scores, clubName, courseName, date);
367 // Sort the scores based on dates
368 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
370 saveScoreFile(scoreFile, scoreList);
372 // TODO: does this really work? No mem leaks?
373 scoreTableModel->setScore(scoreList, score);
379 void MainWindow::editScore()
381 Course *course = scoreTableModel->getCourse();
382 Score *score = scoreTableModel->getScore();
383 QString date = score->getDate();
385 ScoreDialog *scoreDialog = new ScoreDialog(this);
387 QString title = "Edit Score: " + course->getName() + ", " + date;
388 scoreDialog->setWindowTitle(title);
390 scoreDialog->init(course, score);
392 int result = scoreDialog->exec();
395 QVector<QString> scores(18);
397 scoreDialog->results(scores);
399 score->update(scores);
401 // Sort the scores based on dates
402 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
404 saveScoreFile(scoreFile, scoreList);
406 // TODO: does this really work? No mem leaks?
407 scoreTableModel->setScore(scoreList, score);
412 void MainWindow::viewStatistics()
414 QMainWindow *win = new QMainWindow(this);
415 QString title = "Statistics";
416 win->setWindowTitle(title);
418 StatModel *statModel = new StatModel(clubList, scoreList);
420 QTableView *table = new QTableView;
422 table->setSelectionMode(QAbstractItemView::NoSelection);
423 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
424 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
425 table->verticalHeader()->setAutoFillBackground(true);
426 table->setModel(statModel);
428 QWidget *central = new QWidget(win);
429 win->setCentralWidget(central);
431 QPushButton *b1 = new QPushButton("Graphs");
433 QVBoxLayout *buttonLayout = new QVBoxLayout;
434 buttonLayout->addWidget(b1);
436 QTextEdit *textEdit = new QTextEdit;
439 textEdit->setReadOnly(true);
441 QVBoxLayout *infoLayout = new QVBoxLayout;
442 infoLayout->addWidget(table);
444 QHBoxLayout *mainLayout = new QHBoxLayout(central);
445 mainLayout->addLayout(infoLayout);
446 mainLayout->addLayout(buttonLayout);
448 central->setLayout(mainLayout);
453 void MainWindow::loadScoreFile(QString &fileName, QList<Score *> &list)
455 ScoreXmlHandler handler(list);
457 if (handler.parse(fileName))
458 qDebug() << "File loaded:" << fileName << " entries:" << list.size();
461 void MainWindow::saveScoreFile(QString &fileName, QList<Score *> &list)
463 ScoreXmlHandler handler(list);
465 if (handler.save(fileName))
467 qDebug() << "File saved:" << fileName << " entries:" << list.size();
469 qWarning() << "Unable to save:" << fileName;
472 void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list)
474 ClubXmlHandler handler(list);
476 if (handler.parse(fileName))
477 qDebug() << "File loaded:" << fileName << " entries:" << list.size();
480 void MainWindow::saveClubFile(QString &fileName, QList<Club *> &list)
482 ClubXmlHandler handler(list);
484 if (handler.save(fileName))
486 qDebug() << "File saved:" << fileName << " entries:" << list.size();
488 qWarning() << "Unable to save:" << fileName;