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);
57 void MainWindow::loadSettings(void)
59 bool external = false;
69 // TODO: make via user option, automatic will never work
73 dataDir = mmcDir + "/" + appName + "/" + dataDirName;
76 dataDir = topDir + "/" + appName + "/" + dataDirName;
78 scoreFile = dataDir + "/" + scoreFileName;
79 clubFile = dataDir + "/" + clubFileName;
83 if (!dir.mkpath(dataDir)) {
84 qWarning() << "Unable to create: " + dataDir;
87 qDebug() << "Data is at:" + dataDir;
90 void MainWindow::createLayout(QWidget *parent)
93 buttonLayout = new QVBoxLayout;
94 //labelLayout->addStretch();
95 buttonLayout->addWidget(nextButton);
96 buttonLayout->addWidget(prevButton);
97 buttonLayout->addWidget(lastButton);
98 buttonLayout->addWidget(firstButton);
100 tableLayout = new QVBoxLayout;
101 tableLayout->addWidget(table);
103 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
104 mainLayout->addLayout(tableLayout);
105 mainLayout->addLayout(buttonLayout);
106 parent->setLayout(mainLayout);
109 // Setup 'score' tab view
110 void MainWindow::createTableView(QList<Score *> &scoreList, QList <Club *> &clubList)
112 table = new QTableView;
114 nextButton = new QPushButton(tr("Next"));
115 prevButton = new QPushButton(tr("Prev"));
116 firstButton = new QPushButton(tr("First"));
117 lastButton = new QPushButton(tr("Last"));
119 connect(nextButton, SIGNAL(clicked()), this, SLOT(nextButtonClicked()));
120 connect(prevButton, SIGNAL(clicked()), this, SLOT(prevButtonClicked()));
121 connect(firstButton, SIGNAL(clicked()), this, SLOT(firstButtonClicked()));
122 connect(lastButton, SIGNAL(clicked()), this, SLOT(lastButtonClicked()));
124 scoreTableModel = new ScoreTableModel();
128 table->setModel(scoreTableModel);
129 QItemSelectionModel selectionModel();
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->horizontalHeader()->hide();
141 // When selection down in 'stat' view, this is called.
142 void MainWindow::updateTreeView(const QModelIndex & index)
144 QString scope("Scope");
145 QString count("Rounds");
146 QString scoreAvg("Score (avg.)");
147 QString scoreBest("Score (best)");
148 QString score("Total");
149 QString scoreIn("Total in");
150 QString scoreOut("Total out");
152 QVariant str = scoreTreeModel->data(index, Qt::DisplayRole);
153 QVariant type = scoreTreeModel->data(index, ScoreTreeModel::Type);
155 qDebug() << "update(" << index.row() << "/" << index.column() << "):" << str << type;
157 tableModel->setData(tableModel->index(0, 0, QModelIndex()), scope);
158 tableModel->setData(tableModel->index(0, 1, QModelIndex()), str);
160 if (type == TreeItem::TypeDate) {
161 tableModel->setData(tableModel->index(1, 0, QModelIndex()), count);
162 tableModel->setData(tableModel->index(2, 0, QModelIndex()), scoreAvg);
163 tableModel->setData(tableModel->index(3, 0, QModelIndex()), scoreBest);
165 else if (type == TreeItem::TypeScore) {
166 QVariant value = scoreTreeModel->data(index, ScoreTreeModel::Total);
167 tableModel->setData(tableModel->index(1, 0, QModelIndex()), score);
168 tableModel->setData(tableModel->index(1, 1, QModelIndex()), value);
170 value = scoreTreeModel->data(index, ScoreTreeModel::TotalOut);
171 tableModel->setData(tableModel->index(2, 0, QModelIndex()), scoreOut);
172 tableModel->setData(tableModel->index(2, 1, QModelIndex()), value);
174 value = scoreTreeModel->data(index, ScoreTreeModel::TotalIn);
175 tableModel->setData(tableModel->index(3, 0, QModelIndex()), scoreIn);
176 tableModel->setData(tableModel->index(3, 1, QModelIndex()), value);
180 void MainWindow::createStatusBar()
183 // TODO: use toolbar or buttons. Toolbar seems not to be ready and
184 // requires more work.
185 toolbar = addToolBar(tr("foo"));
186 toolbar->addAction(firstAct);
187 toolbar->addAction(lastAct);
188 toolbar->addAction(prevAct);
189 toolbar->addAction(nextAct);
194 void MainWindow::createActions()
196 newScoreAct = new QAction(tr("New Score"), this);
197 connect(newScoreAct, SIGNAL(triggered()), this, SLOT(newScore()));
199 newCourseAct = new QAction(tr("New Course"), this);
200 connect(newCourseAct, SIGNAL(triggered()), this, SLOT(newCourse()));
202 editScoreAct = new QAction(tr("Edit Score"), this);
203 connect(editScoreAct, SIGNAL(triggered()), this, SLOT(editScore()));
205 editCourseAct = new QAction(tr("Edit Course"), this);
206 connect(editCourseAct, SIGNAL(triggered()), this, SLOT(editCourse()));
209 viewScoreAct = new QAction(tr("&View Scores"), this);
210 connect(viewScoreAct, SIGNAL(triggered()), this, SLOT(viewScore()));
212 viewCourseAct = new QAction(tr("&View Courses"), this);
213 connect(viewCourseAct, SIGNAL(triggered()), this, SLOT(viewCourse()));
215 viewStatisticAct = new QAction(tr("&View Statistics"), this);
216 connect(viewStatisticAct, SIGNAL(triggered()), this, SLOT(viewStatistic()));
219 nextAct = new QAction(tr( " Next "), this);
220 connect(nextAct, SIGNAL(triggered()), this, SLOT(nextButtonClicked()));
222 prevAct = new QAction(" Prev ", this);
223 connect(prevAct, SIGNAL(triggered()), this, SLOT(prevButtonClicked()));
225 firstAct = new QAction(tr(" First "), this);
226 connect(firstAct, SIGNAL(triggered()), this, SLOT(firstButtonClicked()));
228 lastAct = new QAction(tr( " Last "), this);
229 connect(lastAct, SIGNAL(triggered()), this, SLOT(lastButtonClicked()));
232 void MainWindow::createMenus()
234 menu = menuBar()->addMenu(tr("fremantle"));
236 menu->addAction(viewScoreAct);
237 menu->addAction(viewCourseAct);
238 menu->addAction(viewStatisticAct);
240 menu->addAction(newScoreAct);
241 menu->addAction(newCourseAct);
242 menu->addAction(editScoreAct);
243 menu->addAction(editCourseAct);
246 void MainWindow::updateStatusBar()
248 setWindowTitle(scoreTableModel->getInfoText());
251 void MainWindow::firstButtonClicked()
253 scoreTableModel->first();
257 void MainWindow::lastButtonClicked()
259 scoreTableModel->last();
263 void MainWindow::nextButtonClicked()
265 scoreTableModel->next();
269 void MainWindow::prevButtonClicked()
271 scoreTableModel->prev();
275 // FIXME: dup code from table-model.cpp
276 Club *MainWindow::findClub(QString &name)
278 QListIterator<Club *> i(clubList);
281 while (i.hasNext()) {
283 if (c->getName() == name)
289 void MainWindow::newCourse()
291 CourseSelectDialog *selectDialog = new CourseSelectDialog(this);
293 int result = selectDialog->exec();
299 selectDialog->results(clubName, courseName);
301 CourseDialog *courseDialog = new CourseDialog(this);
302 courseDialog->init();
304 QString title = "New Course: " + clubName + "," + courseName;
305 courseDialog->setWindowTitle(title);
307 int result = courseDialog->exec();
309 QVector<QString> par(18);
310 QVector<QString> hcp(18);
311 QVector<QString> len(18);
313 courseDialog->results(par, hcp, len);
316 Club *club = findClub(clubName);
318 course = club->getCourse(courseName);
320 qDebug() << "Error: club/course already in the database";
324 course = new Course(courseName, par, hcp);
325 club->addCourse(course);
329 // New club and course
330 club = new Club(clubName);
331 course = new Course(courseName, par, hcp);
332 club->addCourse(course);
335 saveClubFile(clubFile, clubList);
337 // TODO: does this really work? No mem leaks?
338 scoreTableModel->setClub(clubList);
343 void MainWindow::editCourse()
345 Course *course = scoreTableModel->getCourse();
347 CourseDialog *courseDialog = new CourseDialog(this);
348 courseDialog->init(course);
350 QString title = "Edit Course: " + course->getName();
351 courseDialog->setWindowTitle(title);
353 int result = courseDialog->exec();
355 QVector<QString> par(18);
356 QVector<QString> hcp(18);
357 QVector<QString> len(18);
359 courseDialog->results(par, hcp, len);
361 course->update(par, hcp, len);
362 saveClubFile(clubFile, clubList);
366 void MainWindow::newScore()
368 SelectDialog *selectDialog = new SelectDialog(this);
370 selectDialog->init(clubList);
372 int result = selectDialog->exec();
378 selectDialog->results(clubName, courseName, date);
380 ScoreDialog *scoreDialog = new ScoreDialog(this);
381 QString title = "New Score: " + courseName + ", " + date;
382 scoreDialog->setWindowTitle(title);
384 Club *club = findClub(clubName);
386 qWarning() << "Error: no such club:" << clubName;
389 Course *course = club->getCourse(courseName);
391 qWarning() << "Error: no such course:" << courseName;
394 scoreDialog->init(course);
395 result = scoreDialog->exec();
397 QVector<QString> scores(18);
399 scoreDialog->results(scores);
400 Score *score = new Score(scores, clubName, courseName, date);
403 // Sort the scores based on dates
404 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
406 saveScoreFile(scoreFile, scoreList);
408 // TODO: does this really work? No mem leaks?
409 scoreTableModel->setScore(scoreList, score);
415 void MainWindow::editScore()
417 Course *course = scoreTableModel->getCourse();
418 Score *score = scoreTableModel->getScore();
419 QString date = score->getDate();
421 ScoreDialog *scoreDialog = new ScoreDialog(this);
423 QString title = "Edit Score: " + course->getName() + ", " + date;
424 scoreDialog->setWindowTitle(title);
426 scoreDialog->init(course, score);
428 int result = scoreDialog->exec();
431 QVector<QString> scores(18);
433 scoreDialog->results(scores);
435 score->update(scores);
437 // Sort the scores based on dates
438 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
440 saveScoreFile(scoreFile, scoreList);
442 // TODO: does this really work? No mem leaks?
443 scoreTableModel->setScore(scoreList, score);
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;