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 "score-common.h"
12 #include "main-window.h"
13 #include "score-dialog.h"
14 #include "course-dialog.h"
15 #include "stat-model.h"
16 #include "xml-dom-parser.h"
18 QString appName("scorecard");
19 QString topDir("/opt");
20 QString mmcDir("/media/mmc1");
21 QString dataDirName("data");
23 QString imgDir(topDir + "/pixmaps");
24 QString scoreFileName("score.xml");
26 QString clubFileName("club.xml");
28 QString logFile("/tmp/scorecard.log");
30 bool dateLessThan(const Score *s1, const Score *s2)
35 MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
41 centralWidget = new QWidget(this);
43 setCentralWidget(centralWidget);
45 loadScoreFile(scoreFile, scoreList);
46 loadClubFile(clubFile, clubList);
48 // Sort the scores based on dates
49 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
53 createTableView(scoreList, clubList);
56 createLayout(centralWidget);
59 void MainWindow::loadSettings(void)
61 bool external = false;
67 // TODO: make via user option, automatic will never work
71 dataDir = "./" + dataDirName;
74 dataDir = mmcDir + "/" + appName + "/" + dataDirName;
77 dataDir = topDir + "/" + appName + "/" + dataDirName;
80 scoreFile = dataDir + "/" + scoreFileName;
81 clubFile = dataDir + "/" + clubFileName;
85 if (!dir.mkpath(dataDir)) {
86 qWarning() << "Unable to create: " + dataDir;
89 qDebug() << "Data is at:" + dataDir;
92 void MainWindow::createLayout(QWidget *parent)
95 buttonLayout = new QVBoxLayout;
96 //labelLayout->addStretch();
97 buttonLayout->addWidget(nextButton);
98 buttonLayout->addWidget(prevButton);
99 buttonLayout->addWidget(lastButton);
100 buttonLayout->addWidget(firstButton);
102 tableLayout = new QVBoxLayout;
103 tableLayout->addWidget(table);
105 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
106 mainLayout->addLayout(tableLayout);
107 mainLayout->addLayout(buttonLayout);
108 parent->setLayout(mainLayout);
111 // Setup 'score' tab view
112 void MainWindow::createTableView(QList<Score *> &scoreList, QList <Club *> &clubList)
114 table = new QTableView;
116 nextButton = new QPushButton(tr("Next"));
117 prevButton = new QPushButton(tr("Prev"));
118 firstButton = new QPushButton(tr("First"));
119 lastButton = new QPushButton(tr("Last"));
121 connect(nextButton, SIGNAL(clicked()), this, SLOT(nextButtonClicked()));
122 connect(prevButton, SIGNAL(clicked()), this, SLOT(prevButtonClicked()));
123 connect(firstButton, SIGNAL(clicked()), this, SLOT(firstButtonClicked()));
124 connect(lastButton, SIGNAL(clicked()), this, SLOT(lastButtonClicked()));
126 scoreTableModel = new ScoreTableModel();
130 table->setStyleSheet(ScoreColor::styleSheet());
132 table->setModel(scoreTableModel);
133 table->setSelectionMode(QAbstractItemView::NoSelection);
135 scoreTableModel->setScore(scoreList);
136 scoreTableModel->setClub(clubList);
138 // Fill out all the space with the tables
139 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
140 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
141 table->horizontalHeader()->hide();
142 //table->verticalHeader()->setStyleSheet("color : gray }");
145 void MainWindow::createActions()
147 newScoreAct = new QAction(tr("New Score"), this);
148 connect(newScoreAct, SIGNAL(triggered()), this, SLOT(newScore()));
150 newCourseAct = new QAction(tr("New Course"), this);
151 connect(newCourseAct, SIGNAL(triggered()), this, SLOT(newCourse()));
153 editScoreAct = new QAction(tr("Edit Score"), this);
154 connect(editScoreAct, SIGNAL(triggered()), this, SLOT(editScore()));
156 editCourseAct = new QAction(tr("Edit Course"), this);
157 connect(editCourseAct, SIGNAL(triggered()), this, SLOT(editCourse()));
159 statAct = new QAction(tr("Statistics"), this);
160 connect(statAct, SIGNAL(triggered()), this, SLOT(viewStatistics()));
162 nextAct = new QAction(tr( " Next "), this);
163 connect(nextAct, SIGNAL(triggered()), this, SLOT(nextButtonClicked()));
165 prevAct = new QAction(" Prev ", this);
166 connect(prevAct, SIGNAL(triggered()), this, SLOT(prevButtonClicked()));
168 firstAct = new QAction(tr(" First "), this);
169 connect(firstAct, SIGNAL(triggered()), this, SLOT(firstButtonClicked()));
171 lastAct = new QAction(tr( " Last "), this);
172 connect(lastAct, SIGNAL(triggered()), this, SLOT(lastButtonClicked()));
175 void MainWindow::createMenus()
178 menu = menuBar()->addMenu("");
180 menu = menuBar()->addMenu("Menu");
183 menu->addAction(newScoreAct);
184 menu->addAction(newCourseAct);
185 menu->addAction(editScoreAct);
186 menu->addAction(editCourseAct);
187 menu->addAction(statAct);
190 void MainWindow::updateTitleBar()
192 QString title = scoreTableModel->getInfoText();
194 title = "ScoreCard - No Scores";
196 setWindowTitle(title);
199 void MainWindow::firstButtonClicked()
201 scoreTableModel->first();
205 void MainWindow::lastButtonClicked()
207 scoreTableModel->last();
211 void MainWindow::nextButtonClicked()
213 scoreTableModel->next();
217 void MainWindow::prevButtonClicked()
219 scoreTableModel->prev();
223 // FIXME: dup code from table-model.cpp
224 Club *MainWindow::findClub(QString &name)
226 QListIterator<Club *> i(clubList);
229 while (i.hasNext()) {
231 if (c->getName() == name)
237 void MainWindow::newCourse()
239 CourseSelectDialog *selectDialog = new CourseSelectDialog(this);
241 int result = selectDialog->exec();
247 selectDialog->results(clubName, courseName);
249 CourseDialog *courseDialog = new CourseDialog(this);
250 courseDialog->init();
252 QString title = "New Course: " + clubName + "," + courseName;
253 courseDialog->setWindowTitle(title);
255 int result = courseDialog->exec();
257 QVector<QString> par(18);
258 QVector<QString> hcp(18);
259 QVector<QString> len(18);
261 courseDialog->results(par, hcp, len);
264 Club *club = findClub(clubName);
266 course = club->getCourse(courseName);
268 qDebug() << "Error: club/course already in the database";
272 course = new Course(courseName, par, hcp);
273 club->addCourse(course);
277 // New club and course
278 club = new Club(clubName);
279 course = new Course(courseName, par, hcp);
280 club->addCourse(course);
283 saveClubFile(clubFile, clubList);
285 // TODO: does this really work? No mem leaks?
286 scoreTableModel->setClub(clubList);
291 void MainWindow::editCourse()
293 Course *course = scoreTableModel->getCourse();
296 qWarning() << "No course on edit";
300 CourseDialog *courseDialog = new CourseDialog(this);
301 courseDialog->init(course);
303 QString title = "Edit Course: " + course->getName();
304 courseDialog->setWindowTitle(title);
306 int result = courseDialog->exec();
308 QVector<QString> par(18);
309 QVector<QString> hcp(18);
310 QVector<QString> len(18);
312 courseDialog->results(par, hcp, len);
314 course->update(par, hcp, len);
315 saveClubFile(clubFile, clubList);
319 void MainWindow::newScore()
321 SelectDialog *selectDialog = new SelectDialog(this);
323 selectDialog->init(clubList);
325 int result = selectDialog->exec();
331 selectDialog->results(clubName, courseName, date);
333 ScoreDialog *scoreDialog = new ScoreDialog(this);
334 QString title = "New Score: " + courseName + ", " + date;
335 scoreDialog->setWindowTitle(title);
337 Club *club = findClub(clubName);
339 qWarning() << "Error: no such club:" << clubName;
342 Course *course = club->getCourse(courseName);
344 qWarning() << "Error: no such course:" << courseName;
347 scoreDialog->init(course);
348 result = scoreDialog->exec();
350 QVector<QString> scores(18);
352 scoreDialog->results(scores);
353 Score *score = new Score(scores, clubName, courseName, date);
356 // Sort the scores based on dates
357 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
359 saveScoreFile(scoreFile, scoreList);
361 // TODO: does this really work? No mem leaks?
362 scoreTableModel->setScore(scoreList, score);
368 void MainWindow::editScore()
370 Course *course = scoreTableModel->getCourse();
371 Score *score = scoreTableModel->getScore();
373 if (!course || !score) {
374 qDebug() << "No score/course to edit";
378 QString date = score->getDate();
380 ScoreDialog *scoreDialog = new ScoreDialog(this);
382 QString title = "Edit Score: " + course->getName() + ", " + date;
383 scoreDialog->setWindowTitle(title);
385 scoreDialog->init(course, score);
387 int result = scoreDialog->exec();
390 QVector<QString> scores(18);
392 scoreDialog->results(scores);
394 score->update(scores);
396 // Sort the scores based on dates
397 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
399 saveScoreFile(scoreFile, scoreList);
401 // TODO: does this really work? No mem leaks?
402 scoreTableModel->setScore(scoreList, score);
407 void MainWindow::viewStatistics()
409 QMainWindow *win = new QMainWindow(this);
410 QString title = "Statistics";
411 win->setWindowTitle(title);
413 StatModel *statModel = new StatModel(clubList, scoreList);
415 QTableView *table = new QTableView;
417 table->setSelectionMode(QAbstractItemView::NoSelection);
418 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
419 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
420 table->verticalHeader()->setAutoFillBackground(true);
421 table->setModel(statModel);
423 QWidget *central = new QWidget(win);
424 win->setCentralWidget(central);
426 QPushButton *b1 = new QPushButton("Graphs");
428 QVBoxLayout *buttonLayout = new QVBoxLayout;
429 buttonLayout->addWidget(b1);
431 QTextEdit *textEdit = new QTextEdit;
434 textEdit->setReadOnly(true);
436 QVBoxLayout *infoLayout = new QVBoxLayout;
437 infoLayout->addWidget(table);
439 QHBoxLayout *mainLayout = new QHBoxLayout(central);
440 mainLayout->addLayout(infoLayout);
441 //mainLayout->addLayout(buttonLayout);
443 central->setLayout(mainLayout);
448 void MainWindow::loadScoreFile(QString &fileName, QList<Score *> &list)
450 ScoreXmlHandler handler(list);
452 if (handler.parse(fileName))
453 qDebug() << "File loaded:" << fileName << " entries:" << list.size();
456 void MainWindow::saveScoreFile(QString &fileName, QList<Score *> &list)
458 ScoreXmlHandler handler(list);
460 if (handler.save(fileName))
462 qDebug() << "File saved:" << fileName << " entries:" << list.size();
464 qWarning() << "Unable to save:" << fileName;
467 void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list)
469 ClubXmlHandler handler(list);
471 if (handler.parse(fileName))
472 qDebug() << "File loaded:" << fileName << " entries:" << list.size();
475 void MainWindow::saveClubFile(QString &fileName, QList<Club *> &list)
477 ClubXmlHandler handler(list);
479 if (handler.save(fileName))
481 qDebug() << "File saved:" << fileName << " entries:" << list.size();
483 qWarning() << "Unable to save:" << fileName;