5 #include <QStandardItemModel>
7 #include "main-window.h"
8 #include "score-dialog.h"
9 #include "course-dialog.h"
10 #include "tree-widget.h"
11 #include "xml-parser.h"
12 #include "xml-dom-parser.h"
14 QString appName("scorecard");
15 QString topDir("/opt");
16 QString mmcDir("/media/mmc1");
17 QString dataDirName("data");
19 QString imgDir(topDir + "/pixmaps");
20 QString scoreFileName("score.xml");
22 QString clubFileName("club.xml");
24 QString logFile("/tmp/scorecard.log");
26 bool dateLessThan(const Score *s1, const Score *s2)
31 MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
37 QWidget *centralWidget = new QWidget(this);
39 setCentralWidget(centralWidget);
41 loadScoreFile(scoreFile, scoreList);
42 loadClubFile(clubFile, clubList);
44 // Sort the scores based on dates
45 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
50 createTableView(scoreList, clubList);
51 //createTreeView(scoreList, parent);
54 createLayout(centralWidget);
58 void MainWindow::loadSettings(void)
60 bool external = false;
71 dataDir = mmcDir + "/" + appName + "/" + dataDirName;
74 dataDir = topDir + "/" + appName + "/" + dataDirName;
76 scoreFile = dataDir + "/" + scoreFileName;
77 clubFile = dataDir + "/" + clubFileName;
81 if (!dir.mkpath(dataDir)) {
82 qDebug() << "Unable to create: " + dataDir;
85 qDebug() << "Data is at: " + dataDir;
88 void MainWindow::createLayout(QWidget *parent)
91 buttonLayout = new QVBoxLayout;
92 //labelLayout->addStretch();
93 buttonLayout->addWidget(nextButton);
94 buttonLayout->addWidget(prevButton);
95 buttonLayout->addWidget(lastButton);
96 buttonLayout->addWidget(firstButton);
98 tableLayout = new QVBoxLayout;
99 tableLayout->addWidget(table);
101 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
102 mainLayout->addLayout(tableLayout);
103 mainLayout->addLayout(buttonLayout);
104 setLayout(mainLayout);
107 // Setup 'score' tab view
108 void MainWindow::createTableView(QList<Score *> &scoreList, QList <Club *> &clubList)
110 table = new QTableView;
112 nextButton = new QPushButton(tr(">"));
113 prevButton = new QPushButton(tr("<"));
114 firstButton = new QPushButton(tr("<<"));
115 lastButton = new QPushButton(tr(">>"));
117 connect(nextButton, SIGNAL(clicked()), this, SLOT(nextButtonClicked()));
118 connect(prevButton, SIGNAL(clicked()), this, SLOT(prevButtonClicked()));
119 connect(firstButton, SIGNAL(clicked()), this, SLOT(firstButtonClicked()));
120 connect(lastButton, SIGNAL(clicked()), this, SLOT(lastButtonClicked()));
122 scoreTableModel = new ScoreTableModel();
123 scoreTableModel->setScore(scoreList);
124 scoreTableModel->setClub(clubList);
128 table->setModel(scoreTableModel);
130 // Fill out all the space with the tables
131 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
132 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
133 table->horizontalHeader()->hide();
136 // When selection down in 'stat' view, this is called.
137 void MainWindow::updateTreeView(const QModelIndex & index)
139 QString scope("Scope");
140 QString count("Rounds");
141 QString scoreAvg("Score (avg.)");
142 QString scoreBest("Score (best)");
143 QString score("Total");
144 QString scoreIn("Total in");
145 QString scoreOut("Total out");
147 QVariant str = scoreTreeModel->data(index, Qt::DisplayRole);
148 QVariant type = scoreTreeModel->data(index, ScoreTreeModel::Type);
150 qDebug() << "update(" << index.row() << "/" << index.column() << "):" << str << type;
152 tableModel->setData(tableModel->index(0, 0, QModelIndex()), scope);
153 tableModel->setData(tableModel->index(0, 1, QModelIndex()), str);
155 if (type == TreeItem::TypeDate) {
156 tableModel->setData(tableModel->index(1, 0, QModelIndex()), count);
157 tableModel->setData(tableModel->index(2, 0, QModelIndex()), scoreAvg);
158 tableModel->setData(tableModel->index(3, 0, QModelIndex()), scoreBest);
160 else if (type == TreeItem::TypeScore) {
161 QVariant value = scoreTreeModel->data(index, ScoreTreeModel::Total);
162 tableModel->setData(tableModel->index(1, 0, QModelIndex()), score);
163 tableModel->setData(tableModel->index(1, 1, QModelIndex()), value);
165 value = scoreTreeModel->data(index, ScoreTreeModel::TotalOut);
166 tableModel->setData(tableModel->index(2, 0, QModelIndex()), scoreOut);
167 tableModel->setData(tableModel->index(2, 1, QModelIndex()), value);
169 value = scoreTreeModel->data(index, ScoreTreeModel::TotalIn);
170 tableModel->setData(tableModel->index(3, 0, QModelIndex()), scoreIn);
171 tableModel->setData(tableModel->index(3, 1, QModelIndex()), value);
175 void MainWindow::createStatusBar()
178 // TODO: use toolbar or buttons. Toolbar seems not to be ready and
179 // requires more work.
180 toolbar = addToolBar(tr("foo"));
181 toolbar->addAction(firstAct);
182 toolbar->addAction(lastAct);
183 toolbar->addAction(prevAct);
184 toolbar->addAction(nextAct);
189 void MainWindow::createActions()
191 newScoreAct = new QAction(tr("New Score"), this);
192 connect(newScoreAct, SIGNAL(triggered()), this, SLOT(newScore()));
194 newCourseAct = new QAction(tr("New Course"), this);
195 connect(newCourseAct, SIGNAL(triggered()), this, SLOT(newCourse()));
197 editScoreAct = new QAction(tr("Edit Score"), this);
198 connect(editScoreAct, SIGNAL(triggered()), this, SLOT(editScore()));
200 editCourseAct = new QAction(tr("Edit Course"), this);
201 connect(editCourseAct, SIGNAL(triggered()), this, SLOT(editCourse()));
204 viewScoreAct = new QAction(tr("&View Scores"), this);
205 connect(viewScoreAct, SIGNAL(triggered()), this, SLOT(viewScore()));
207 viewCourseAct = new QAction(tr("&View Courses"), this);
208 connect(viewCourseAct, SIGNAL(triggered()), this, SLOT(viewCourse()));
210 viewStatisticAct = new QAction(tr("&View Statistics"), this);
211 connect(viewStatisticAct, SIGNAL(triggered()), this, SLOT(viewStatistic()));
214 nextAct = new QAction(tr( " Next "), this);
215 connect(nextAct, SIGNAL(triggered()), this, SLOT(nextButtonClicked()));
217 prevAct = new QAction(" Prev ", this);
218 connect(prevAct, SIGNAL(triggered()), this, SLOT(prevButtonClicked()));
220 firstAct = new QAction(tr(" First "), this);
221 connect(firstAct, SIGNAL(triggered()), this, SLOT(firstButtonClicked()));
223 lastAct = new QAction(tr( " Last "), this);
224 connect(lastAct, SIGNAL(triggered()), this, SLOT(lastButtonClicked()));
227 void MainWindow::createMenus()
229 menu = menuBar()->addMenu(tr("fremantle"));
231 menu->addAction(viewScoreAct);
232 menu->addAction(viewCourseAct);
233 menu->addAction(viewStatisticAct);
235 menu->addAction(newScoreAct);
236 menu->addAction(newCourseAct);
237 menu->addAction(editScoreAct);
238 menu->addAction(editCourseAct);
241 void MainWindow::updateStatusBar()
243 setWindowTitle(scoreTableModel->getInfoText());
246 void MainWindow::firstButtonClicked()
248 scoreTableModel->first();
252 void MainWindow::lastButtonClicked()
254 scoreTableModel->last();
258 void MainWindow::nextButtonClicked()
260 scoreTableModel->next();
264 void MainWindow::prevButtonClicked()
266 scoreTableModel->prev();
270 // FIXME: dup code from table-model.cpp
271 Club *MainWindow::findClub(QString &name)
273 QListIterator<Club *> i(clubList);
276 while (i.hasNext()) {
278 if (c->getName() == name)
284 void MainWindow::newCourse()
286 CourseSelectDialog *selectDialog = new CourseSelectDialog(this);
288 int result = selectDialog->exec();
294 selectDialog->results(clubName, courseName);
296 CourseDialog *courseDialog = new CourseDialog(this);
297 courseDialog->init();
299 QString title = "New Course: " + clubName + "," + courseName;
300 courseDialog->setWindowTitle(title);
302 int result = courseDialog->exec();
304 QVector<QString> par(18);
305 QVector<QString> hcp(18);
306 QVector<QString> len(18);
308 courseDialog->results(par, hcp, len);
311 Club *club = findClub(clubName);
313 course = club->getCourse(courseName);
315 qDebug() << "Error: club/course already in the database";
319 course = new Course(courseName, par, hcp);
320 club->addCourse(course);
324 club = new Club(clubName);
325 course = new Course(courseName, par, hcp);
326 club->addCourse(course);
329 saveClubFile(clubFile, clubList);
331 // TODO: does this really work? No mem leaks?
332 scoreTableModel->setClub(clubList);
338 void MainWindow::editCourse()
340 Course *course = scoreTableModel->getCourse();
342 CourseDialog *courseDialog = new CourseDialog(this);
343 courseDialog->init(course);
345 QString title = "Edit Course: " + course->getName();
346 courseDialog->setWindowTitle(title);
348 int result = courseDialog->exec();
350 QVector<QString> par(18);
351 QVector<QString> hcp(18);
352 QVector<QString> len(18);
354 courseDialog->results(par, hcp, len);
356 course->update(par, hcp, len);
357 saveClubFile(clubFile, clubList);
361 void MainWindow::newScore()
363 SelectDialog *selectDialog = new SelectDialog(this);
365 selectDialog->init(clubList);
367 int result = selectDialog->exec();
373 selectDialog->results(clubName, courseName, date);
375 ScoreDialog *scoreDialog = new ScoreDialog(this);
376 QString title = "New Score: " + courseName + ", " + date;
377 scoreDialog->setWindowTitle(title);
379 Club *club = findClub(clubName);
381 qDebug() << "Error: no such club: " << clubName;
384 Course *course = club->getCourse(courseName);
386 qDebug() << "Error: no such course: " << courseName;
389 scoreDialog->init(course);
390 result = scoreDialog->exec();
392 QVector<QString> scores(18);
394 scoreDialog->results(scores);
395 Score *score = new Score(scores, clubName, courseName, date);
398 // Sort the scores based on dates
399 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
401 saveScoreFile(scoreFile, scoreList);
403 // TODO: does this really work? No mem leaks?
404 scoreTableModel->setScore(scoreList, score);
410 void MainWindow::editScore()
412 Course *course = scoreTableModel->getCourse();
413 Score *score = scoreTableModel->getScore();
414 QString date = score->getDate();
416 ScoreDialog *scoreDialog = new ScoreDialog(this);
418 QString title = "Edit Score: " + course->getName() + ", " + date;
419 scoreDialog->setWindowTitle(title);
421 scoreDialog->init(course, score);
423 int result = scoreDialog->exec();
426 QVector<QString> scores(18);
428 scoreDialog->results(scores);
430 score->update(scores);
432 // Sort the scores based on dates
433 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
435 saveScoreFile(scoreFile, scoreList);
437 // TODO: does this really work? No mem leaks?
438 scoreTableModel->setScore(scoreList, score);
443 void MainWindow::loadScoreFile(QString &fileName, QList<Score *> &list)
445 ScoreXmlHandler handler(list);
447 if (handler.parse(fileName))
448 qDebug() << "File loaded: " << fileName << " entries: " << list.size();
451 void MainWindow::saveScoreFile(QString &fileName, QList<Score *> &list)
453 ScoreXmlHandler handler(list);
455 if (handler.save(fileName))
457 qDebug() << "File saved: " << fileName << " entries: " << list.size();
459 qDebug() << "Unable to save: " << fileName;
462 void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list)
464 ClubXmlHandler handler(list);
466 if (handler.parse(fileName))
467 qDebug() << "File loaded: " << fileName << " entries: " << list.size();
470 void MainWindow::saveClubFile(QString &fileName, QList<Club *> &list)
472 ClubXmlHandler handler(list);
474 if (handler.save(fileName))
476 qDebug() << "File saved: " << fileName << " entries: " << list.size();
478 qDebug() << "Unable to save: " << fileName;