3 #include "main-window.h"
4 #include "score-dialog.h"
5 #include "course-dialog.h"
6 #include "stat-model.h"
7 #include "xml-dom-parser.h"
9 QString appName("scorecard");
10 QString topDir("/opt");
11 QString mmcDir("/media/mmc1");
12 QString dataDirName("data");
14 QString imgDir(topDir + "/pixmaps");
15 QString scoreFileName("score.xml");
17 QString clubFileName("club.xml");
19 QString logFile("/tmp/scorecard.log");
21 bool dateLessThan(const Score *s1, const Score *s2)
26 MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
32 centralWidget = new QWidget(this);
34 setCentralWidget(centralWidget);
36 loadScoreFile(scoreFile, scoreList);
37 loadClubFile(clubFile, clubList);
39 // Sort the scores based on dates
40 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
44 createTableView(scoreList, clubList);
47 createLayout(centralWidget);
50 void MainWindow::loadSettings(void)
52 bool external = false;
58 // TODO: make via user option, automatic will never work
62 dataDir = "./" + dataDirName;
65 dataDir = mmcDir + "/" + appName + "/" + dataDirName;
68 dataDir = topDir + "/" + appName + "/" + dataDirName;
71 scoreFile = dataDir + "/" + scoreFileName;
72 clubFile = dataDir + "/" + clubFileName;
76 if (!dir.mkpath(dataDir)) {
77 qWarning() << "Unable to create: " + dataDir;
80 qDebug() << "Data is at:" + dataDir;
83 void MainWindow::createLayout(QWidget *parent)
86 buttonLayout = new QVBoxLayout;
87 //labelLayout->addStretch();
88 buttonLayout->addWidget(nextButton);
89 buttonLayout->addWidget(prevButton);
90 buttonLayout->addWidget(lastButton);
91 buttonLayout->addWidget(firstButton);
93 tableLayout = new QVBoxLayout;
94 tableLayout->addWidget(table);
96 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
97 mainLayout->addLayout(tableLayout);
98 mainLayout->addLayout(buttonLayout);
99 parent->setLayout(mainLayout);
102 // Setup 'score' tab view
103 void MainWindow::createTableView(QList<Score *> &scoreList, QList <Club *> &clubList)
105 table = new QTableView;
107 nextButton = new QPushButton(tr("Next"));
108 prevButton = new QPushButton(tr("Prev"));
109 firstButton = new QPushButton(tr("First"));
110 lastButton = new QPushButton(tr("Last"));
112 connect(nextButton, SIGNAL(clicked()), this, SLOT(nextButtonClicked()));
113 connect(prevButton, SIGNAL(clicked()), this, SLOT(prevButtonClicked()));
114 connect(firstButton, SIGNAL(clicked()), this, SLOT(firstButtonClicked()));
115 connect(lastButton, SIGNAL(clicked()), this, SLOT(lastButtonClicked()));
117 scoreTableModel = new ScoreTableModel();
121 table->setModel(scoreTableModel);
122 table->setSelectionMode(QAbstractItemView::NoSelection);
124 scoreTableModel->setScore(scoreList);
125 scoreTableModel->setClub(clubList);
127 // Fill out all the space with the tables
128 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
129 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
130 table->verticalHeader()->setAutoFillBackground(true);
131 table->horizontalHeader()->hide();
134 void MainWindow::createStatusBar()
137 // TODO: use toolbar or buttons. Toolbar seems not to be ready and
138 // requires more work.
139 toolbar = addToolBar(tr("foo"));
140 toolbar->addAction(firstAct);
141 toolbar->addAction(lastAct);
142 toolbar->addAction(prevAct);
143 toolbar->addAction(nextAct);
148 void MainWindow::createActions()
150 newScoreAct = new QAction(tr("New Score"), this);
151 connect(newScoreAct, SIGNAL(triggered()), this, SLOT(newScore()));
153 newCourseAct = new QAction(tr("New Course"), this);
154 connect(newCourseAct, SIGNAL(triggered()), this, SLOT(newCourse()));
156 editScoreAct = new QAction(tr("Edit Score"), this);
157 connect(editScoreAct, SIGNAL(triggered()), this, SLOT(editScore()));
159 editCourseAct = new QAction(tr("Edit Course"), this);
160 connect(editCourseAct, SIGNAL(triggered()), this, SLOT(editCourse()));
163 viewScoreAct = new QAction(tr("&View Scores"), this);
164 connect(viewScoreAct, SIGNAL(triggered()), this, SLOT(viewScore()));
166 viewCourseAct = new QAction(tr("&View Courses"), this);
167 connect(viewCourseAct, SIGNAL(triggered()), this, SLOT(viewCourse()));
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()
188 menu = menuBar()->addMenu("");
190 menu->addAction(viewScoreAct);
191 menu->addAction(viewCourseAct);
192 menu->addAction(viewStatisticAct);
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;