4 #include <QStandardItemModel>
6 #include "main-window.h"
7 #include "score-dialog.h"
8 #include "course-dialog.h"
9 #include "xml-parser.h"
10 #include "xml-dom-parser.h"
12 QString appName("scorecard");
13 QString topDir("/opt");
14 QString mmcDir("/media/mmc1");
15 QString dataDirName("data");
17 QString imgDir(topDir + "/pixmaps");
18 QString scoreFileName("score.xml");
20 QString clubFileName("club.xml");
22 QString logFile("/tmp/scorecard.log");
24 bool dateLessThan(const Score *s1, const Score *s2)
29 MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
35 centralWidget = new QWidget(this);
37 setCentralWidget(centralWidget);
39 loadScoreFile(scoreFile, scoreList);
40 loadClubFile(clubFile, clubList);
42 // Sort the scores based on dates
43 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
48 createTableView(scoreList, clubList);
51 createLayout(centralWidget);
54 void MainWindow::loadSettings(void)
56 bool external = false;
62 // TODO: make via user option, automatic will never work
66 dataDir = "./" + dataDirName;
69 dataDir = mmcDir + "/" + appName + "/" + dataDirName;
72 dataDir = topDir + "/" + appName + "/" + dataDirName;
75 scoreFile = dataDir + "/" + scoreFileName;
76 clubFile = dataDir + "/" + clubFileName;
80 if (!dir.mkpath(dataDir)) {
81 qWarning() << "Unable to create: " + dataDir;
84 qDebug() << "Data is at:" + dataDir;
87 void MainWindow::createLayout(QWidget *parent)
90 buttonLayout = new QVBoxLayout;
91 //labelLayout->addStretch();
92 buttonLayout->addWidget(nextButton);
93 buttonLayout->addWidget(prevButton);
94 buttonLayout->addWidget(lastButton);
95 buttonLayout->addWidget(firstButton);
97 tableLayout = new QVBoxLayout;
98 tableLayout->addWidget(table);
100 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
101 mainLayout->addLayout(tableLayout);
102 mainLayout->addLayout(buttonLayout);
103 parent->setLayout(mainLayout);
106 // Setup 'score' tab view
107 void MainWindow::createTableView(QList<Score *> &scoreList, QList <Club *> &clubList)
109 table = new QTableView;
111 nextButton = new QPushButton(tr("Next"));
112 prevButton = new QPushButton(tr("Prev"));
113 firstButton = new QPushButton(tr("First"));
114 lastButton = new QPushButton(tr("Last"));
116 connect(nextButton, SIGNAL(clicked()), this, SLOT(nextButtonClicked()));
117 connect(prevButton, SIGNAL(clicked()), this, SLOT(prevButtonClicked()));
118 connect(firstButton, SIGNAL(clicked()), this, SLOT(firstButtonClicked()));
119 connect(lastButton, SIGNAL(clicked()), this, SLOT(lastButtonClicked()));
121 scoreTableModel = new ScoreTableModel();
125 table->setModel(scoreTableModel);
126 QItemSelectionModel selectionModel();
127 table->setSelectionMode(QAbstractItemView::NoSelection);
129 scoreTableModel->setScore(scoreList);
130 scoreTableModel->setClub(clubList);
132 // Fill out all the space with the tables
133 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
134 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
135 table->verticalHeader()->setAutoFillBackground(true);
136 table->horizontalHeader()->hide();
139 void MainWindow::createStatusBar()
142 // TODO: use toolbar or buttons. Toolbar seems not to be ready and
143 // requires more work.
144 toolbar = addToolBar(tr("foo"));
145 toolbar->addAction(firstAct);
146 toolbar->addAction(lastAct);
147 toolbar->addAction(prevAct);
148 toolbar->addAction(nextAct);
153 void MainWindow::createActions()
155 newScoreAct = new QAction(tr("New Score"), this);
156 connect(newScoreAct, SIGNAL(triggered()), this, SLOT(newScore()));
158 newCourseAct = new QAction(tr("New Course"), this);
159 connect(newCourseAct, SIGNAL(triggered()), this, SLOT(newCourse()));
161 editScoreAct = new QAction(tr("Edit Score"), this);
162 connect(editScoreAct, SIGNAL(triggered()), this, SLOT(editScore()));
164 editCourseAct = new QAction(tr("Edit Course"), this);
165 connect(editCourseAct, SIGNAL(triggered()), this, SLOT(editCourse()));
168 viewScoreAct = new QAction(tr("&View Scores"), this);
169 connect(viewScoreAct, SIGNAL(triggered()), this, SLOT(viewScore()));
171 viewCourseAct = new QAction(tr("&View Courses"), this);
172 connect(viewCourseAct, SIGNAL(triggered()), this, SLOT(viewCourse()));
175 statAct = new QAction(tr("Statistics"), this);
176 connect(statAct, SIGNAL(triggered()), this, SLOT(viewStatistics()));
178 nextAct = new QAction(tr( " Next "), this);
179 connect(nextAct, SIGNAL(triggered()), this, SLOT(nextButtonClicked()));
181 prevAct = new QAction(" Prev ", this);
182 connect(prevAct, SIGNAL(triggered()), this, SLOT(prevButtonClicked()));
184 firstAct = new QAction(tr(" First "), this);
185 connect(firstAct, SIGNAL(triggered()), this, SLOT(firstButtonClicked()));
187 lastAct = new QAction(tr( " Last "), this);
188 connect(lastAct, SIGNAL(triggered()), this, SLOT(lastButtonClicked()));
191 void MainWindow::createMenus()
193 menu = menuBar()->addMenu("");
195 menu->addAction(viewScoreAct);
196 menu->addAction(viewCourseAct);
197 menu->addAction(viewStatisticAct);
199 menu->addAction(newScoreAct);
200 menu->addAction(newCourseAct);
201 menu->addAction(editScoreAct);
202 menu->addAction(editCourseAct);
203 menu->addAction(statAct);
206 void MainWindow::updateStatusBar()
208 QString title = scoreTableModel->getInfoText();
210 title = "ScoreCard - No Scores";
212 setWindowTitle(title);
215 void MainWindow::firstButtonClicked()
217 scoreTableModel->first();
221 void MainWindow::lastButtonClicked()
223 scoreTableModel->last();
227 void MainWindow::nextButtonClicked()
229 scoreTableModel->next();
233 void MainWindow::prevButtonClicked()
235 scoreTableModel->prev();
239 // FIXME: dup code from table-model.cpp
240 Club *MainWindow::findClub(QString &name)
242 QListIterator<Club *> i(clubList);
245 while (i.hasNext()) {
247 if (c->getName() == name)
253 void MainWindow::newCourse()
255 CourseSelectDialog *selectDialog = new CourseSelectDialog(this);
257 int result = selectDialog->exec();
263 selectDialog->results(clubName, courseName);
265 CourseDialog *courseDialog = new CourseDialog(this);
266 courseDialog->init();
268 QString title = "New Course: " + clubName + "," + courseName;
269 courseDialog->setWindowTitle(title);
271 int result = courseDialog->exec();
273 QVector<QString> par(18);
274 QVector<QString> hcp(18);
275 QVector<QString> len(18);
277 courseDialog->results(par, hcp, len);
280 Club *club = findClub(clubName);
282 course = club->getCourse(courseName);
284 qDebug() << "Error: club/course already in the database";
288 course = new Course(courseName, par, hcp);
289 club->addCourse(course);
293 // New club and course
294 club = new Club(clubName);
295 course = new Course(courseName, par, hcp);
296 club->addCourse(course);
299 saveClubFile(clubFile, clubList);
301 // TODO: does this really work? No mem leaks?
302 scoreTableModel->setClub(clubList);
307 void MainWindow::editCourse()
309 Course *course = scoreTableModel->getCourse();
312 qWarning() << "No course on edit";
316 CourseDialog *courseDialog = new CourseDialog(this);
317 courseDialog->init(course);
319 QString title = "Edit Course: " + course->getName();
320 courseDialog->setWindowTitle(title);
322 int result = courseDialog->exec();
324 QVector<QString> par(18);
325 QVector<QString> hcp(18);
326 QVector<QString> len(18);
328 courseDialog->results(par, hcp, len);
330 course->update(par, hcp, len);
331 saveClubFile(clubFile, clubList);
335 void MainWindow::newScore()
337 SelectDialog *selectDialog = new SelectDialog(this);
339 selectDialog->init(clubList);
341 int result = selectDialog->exec();
347 selectDialog->results(clubName, courseName, date);
349 ScoreDialog *scoreDialog = new ScoreDialog(this);
350 QString title = "New Score: " + courseName + ", " + date;
351 scoreDialog->setWindowTitle(title);
353 Club *club = findClub(clubName);
355 qWarning() << "Error: no such club:" << clubName;
358 Course *course = club->getCourse(courseName);
360 qWarning() << "Error: no such course:" << courseName;
363 scoreDialog->init(course);
364 result = scoreDialog->exec();
366 QVector<QString> scores(18);
368 scoreDialog->results(scores);
369 Score *score = new Score(scores, clubName, courseName, date);
372 // Sort the scores based on dates
373 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
375 saveScoreFile(scoreFile, scoreList);
377 // TODO: does this really work? No mem leaks?
378 scoreTableModel->setScore(scoreList, score);
384 void MainWindow::editScore()
386 Course *course = scoreTableModel->getCourse();
387 Score *score = scoreTableModel->getScore();
388 QString date = score->getDate();
390 ScoreDialog *scoreDialog = new ScoreDialog(this);
392 QString title = "Edit Score: " + course->getName() + ", " + date;
393 scoreDialog->setWindowTitle(title);
395 scoreDialog->init(course, score);
397 int result = scoreDialog->exec();
400 QVector<QString> scores(18);
402 scoreDialog->results(scores);
404 score->update(scores);
406 // Sort the scores based on dates
407 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
409 saveScoreFile(scoreFile, scoreList);
411 // TODO: does this really work? No mem leaks?
412 scoreTableModel->setScore(scoreList, score);
417 void MainWindow::viewStatistics()
419 QMainWindow *statWindow = new QMainWindow(this);
421 //QWidget *centralWidget = new QWidget(statWindow);
423 //setCentralWidget(centralWidget);
426 QString foo = "some longer text is needed here : 17";
427 QTextEdit *textEdit = new QTextEdit(foo);
429 QLabel *label = new QLabel;
430 label->setScaledContents(true);
432 QString text = "foo : bar";
434 text.append("Scores: 17");
436 label->setAlignment(Qt::AlignCenter);
437 label->setText(text);
439 QString title = "Statistics";
440 statWindow->setWindowTitle(title);
442 QVBoxLayout *layout = new QVBoxLayout;
443 //layout->addWidget(label);
444 layout->addWidget(textEdit);
446 //statWindow->setLayout(layout);
448 //setCentralWidget(statWindow);
452 void MainWindow::loadScoreFile(QString &fileName, QList<Score *> &list)
454 ScoreXmlHandler handler(list);
456 if (handler.parse(fileName))
457 qDebug() << "File loaded:" << fileName << " entries:" << list.size();
460 void MainWindow::saveScoreFile(QString &fileName, QList<Score *> &list)
462 ScoreXmlHandler handler(list);
464 if (handler.save(fileName))
466 qDebug() << "File saved:" << fileName << " entries:" << list.size();
468 qWarning() << "Unable to save:" << fileName;
471 void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list)
473 ClubXmlHandler handler(list);
475 if (handler.parse(fileName))
476 qDebug() << "File loaded:" << fileName << " entries:" << list.size();
479 void MainWindow::saveClubFile(QString &fileName, QList<Club *> &list)
481 ClubXmlHandler handler(list);
483 if (handler.save(fileName))
485 qDebug() << "File saved:" << fileName << " entries:" << list.size();
487 qWarning() << "Unable to save:" << fileName;