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;
70 dataDir = mmcDir + "/" + appName + "/" + dataDirName;
73 dataDir = topDir + "/" + appName + "/" + dataDirName;
75 scoreFile = dataDir + "/" + scoreFileName;
76 clubFile = dataDir + "/" + clubFileName;
80 if (!dir.mkpath(dataDir)) {
81 qDebug() << "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 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(">"));
112 prevButton = new QPushButton(tr("<"));
113 firstButton = new QPushButton(tr("<<"));
114 lastButton = new QPushButton(tr(">>"));
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->horizontalHeader()->hide();
138 // When selection down in 'stat' view, this is called.
139 void MainWindow::updateTreeView(const QModelIndex & index)
141 QString scope("Scope");
142 QString count("Rounds");
143 QString scoreAvg("Score (avg.)");
144 QString scoreBest("Score (best)");
145 QString score("Total");
146 QString scoreIn("Total in");
147 QString scoreOut("Total out");
149 QVariant str = scoreTreeModel->data(index, Qt::DisplayRole);
150 QVariant type = scoreTreeModel->data(index, ScoreTreeModel::Type);
152 qDebug() << "update(" << index.row() << "/" << index.column() << "):" << str << type;
154 tableModel->setData(tableModel->index(0, 0, QModelIndex()), scope);
155 tableModel->setData(tableModel->index(0, 1, QModelIndex()), str);
157 if (type == TreeItem::TypeDate) {
158 tableModel->setData(tableModel->index(1, 0, QModelIndex()), count);
159 tableModel->setData(tableModel->index(2, 0, QModelIndex()), scoreAvg);
160 tableModel->setData(tableModel->index(3, 0, QModelIndex()), scoreBest);
162 else if (type == TreeItem::TypeScore) {
163 QVariant value = scoreTreeModel->data(index, ScoreTreeModel::Total);
164 tableModel->setData(tableModel->index(1, 0, QModelIndex()), score);
165 tableModel->setData(tableModel->index(1, 1, QModelIndex()), value);
167 value = scoreTreeModel->data(index, ScoreTreeModel::TotalOut);
168 tableModel->setData(tableModel->index(2, 0, QModelIndex()), scoreOut);
169 tableModel->setData(tableModel->index(2, 1, QModelIndex()), value);
171 value = scoreTreeModel->data(index, ScoreTreeModel::TotalIn);
172 tableModel->setData(tableModel->index(3, 0, QModelIndex()), scoreIn);
173 tableModel->setData(tableModel->index(3, 1, QModelIndex()), value);
177 void MainWindow::createStatusBar()
180 // TODO: use toolbar or buttons. Toolbar seems not to be ready and
181 // requires more work.
182 toolbar = addToolBar(tr("foo"));
183 toolbar->addAction(firstAct);
184 toolbar->addAction(lastAct);
185 toolbar->addAction(prevAct);
186 toolbar->addAction(nextAct);
191 void MainWindow::createActions()
193 newScoreAct = new QAction(tr("New Score"), this);
194 connect(newScoreAct, SIGNAL(triggered()), this, SLOT(newScore()));
196 newCourseAct = new QAction(tr("New Course"), this);
197 connect(newCourseAct, SIGNAL(triggered()), this, SLOT(newCourse()));
199 editScoreAct = new QAction(tr("Edit Score"), this);
200 connect(editScoreAct, SIGNAL(triggered()), this, SLOT(editScore()));
202 editCourseAct = new QAction(tr("Edit Course"), this);
203 connect(editCourseAct, SIGNAL(triggered()), this, SLOT(editCourse()));
206 viewScoreAct = new QAction(tr("&View Scores"), this);
207 connect(viewScoreAct, SIGNAL(triggered()), this, SLOT(viewScore()));
209 viewCourseAct = new QAction(tr("&View Courses"), this);
210 connect(viewCourseAct, SIGNAL(triggered()), this, SLOT(viewCourse()));
212 viewStatisticAct = new QAction(tr("&View Statistics"), this);
213 connect(viewStatisticAct, SIGNAL(triggered()), this, SLOT(viewStatistic()));
216 nextAct = new QAction(tr( " Next "), this);
217 connect(nextAct, SIGNAL(triggered()), this, SLOT(nextButtonClicked()));
219 prevAct = new QAction(" Prev ", this);
220 connect(prevAct, SIGNAL(triggered()), this, SLOT(prevButtonClicked()));
222 firstAct = new QAction(tr(" First "), this);
223 connect(firstAct, SIGNAL(triggered()), this, SLOT(firstButtonClicked()));
225 lastAct = new QAction(tr( " Last "), this);
226 connect(lastAct, SIGNAL(triggered()), this, SLOT(lastButtonClicked()));
229 void MainWindow::createMenus()
231 menu = menuBar()->addMenu(tr("fremantle"));
233 menu->addAction(viewScoreAct);
234 menu->addAction(viewCourseAct);
235 menu->addAction(viewStatisticAct);
237 menu->addAction(newScoreAct);
238 menu->addAction(newCourseAct);
239 menu->addAction(editScoreAct);
240 menu->addAction(editCourseAct);
243 void MainWindow::updateStatusBar()
245 setWindowTitle(scoreTableModel->getInfoText());
248 void MainWindow::firstButtonClicked()
250 scoreTableModel->first();
254 void MainWindow::lastButtonClicked()
256 scoreTableModel->last();
260 void MainWindow::nextButtonClicked()
262 scoreTableModel->next();
266 void MainWindow::prevButtonClicked()
268 scoreTableModel->prev();
272 // FIXME: dup code from table-model.cpp
273 Club *MainWindow::findClub(QString &name)
275 QListIterator<Club *> i(clubList);
278 while (i.hasNext()) {
280 if (c->getName() == name)
286 void MainWindow::newCourse()
288 CourseSelectDialog *selectDialog = new CourseSelectDialog(this);
290 int result = selectDialog->exec();
296 selectDialog->results(clubName, courseName);
298 CourseDialog *courseDialog = new CourseDialog(this);
299 courseDialog->init();
301 QString title = "New Course: " + clubName + "," + courseName;
302 courseDialog->setWindowTitle(title);
304 int result = courseDialog->exec();
306 QVector<QString> par(18);
307 QVector<QString> hcp(18);
308 QVector<QString> len(18);
310 courseDialog->results(par, hcp, len);
313 Club *club = findClub(clubName);
315 course = club->getCourse(courseName);
317 qDebug() << "Error: club/course already in the database";
321 course = new Course(courseName, par, hcp);
322 club->addCourse(course);
326 // New club and course
327 club = new Club(clubName);
328 course = new Course(courseName, par, hcp);
329 club->addCourse(course);
332 saveClubFile(clubFile, clubList);
334 // TODO: does this really work? No mem leaks?
335 scoreTableModel->setClub(clubList);
340 void MainWindow::editCourse()
342 Course *course = scoreTableModel->getCourse();
344 CourseDialog *courseDialog = new CourseDialog(this);
345 courseDialog->init(course);
347 QString title = "Edit Course: " + course->getName();
348 courseDialog->setWindowTitle(title);
350 int result = courseDialog->exec();
352 QVector<QString> par(18);
353 QVector<QString> hcp(18);
354 QVector<QString> len(18);
356 courseDialog->results(par, hcp, len);
358 course->update(par, hcp, len);
359 saveClubFile(clubFile, clubList);
363 void MainWindow::newScore()
365 SelectDialog *selectDialog = new SelectDialog(this);
367 selectDialog->init(clubList);
369 int result = selectDialog->exec();
375 selectDialog->results(clubName, courseName, date);
377 ScoreDialog *scoreDialog = new ScoreDialog(this);
378 QString title = "New Score: " + courseName + ", " + date;
379 scoreDialog->setWindowTitle(title);
381 Club *club = findClub(clubName);
383 qDebug() << "Error: no such club: " << clubName;
386 Course *course = club->getCourse(courseName);
388 qDebug() << "Error: no such course: " << courseName;
391 scoreDialog->init(course);
392 result = scoreDialog->exec();
394 QVector<QString> scores(18);
396 scoreDialog->results(scores);
397 Score *score = new Score(scores, clubName, courseName, date);
400 // Sort the scores based on dates
401 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
403 saveScoreFile(scoreFile, scoreList);
405 // TODO: does this really work? No mem leaks?
406 scoreTableModel->setScore(scoreList, score);
412 void MainWindow::editScore()
414 Course *course = scoreTableModel->getCourse();
415 Score *score = scoreTableModel->getScore();
416 QString date = score->getDate();
418 ScoreDialog *scoreDialog = new ScoreDialog(this);
420 QString title = "Edit Score: " + course->getName() + ", " + date;
421 scoreDialog->setWindowTitle(title);
423 scoreDialog->init(course, score);
425 int result = scoreDialog->exec();
428 QVector<QString> scores(18);
430 scoreDialog->results(scores);
432 score->update(scores);
434 // Sort the scores based on dates
435 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
437 saveScoreFile(scoreFile, scoreList);
439 // TODO: does this really work? No mem leaks?
440 scoreTableModel->setScore(scoreList, score);
445 void MainWindow::loadScoreFile(QString &fileName, QList<Score *> &list)
447 ScoreXmlHandler handler(list);
449 if (handler.parse(fileName))
450 qDebug() << "File loaded: " << fileName << " entries: " << list.size();
453 void MainWindow::saveScoreFile(QString &fileName, QList<Score *> &list)
455 ScoreXmlHandler handler(list);
457 if (handler.save(fileName))
459 qDebug() << "File saved: " << fileName << " entries: " << list.size();
461 qDebug() << "Unable to save: " << fileName;
464 void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list)
466 ClubXmlHandler handler(list);
468 if (handler.parse(fileName))
469 qDebug() << "File loaded: " << fileName << " entries: " << list.size();
472 void MainWindow::saveClubFile(QString &fileName, QList<Club *> &list)
474 ClubXmlHandler handler(list);
476 if (handler.save(fileName))
478 qDebug() << "File saved: " << fileName << " entries: " << list.size();
480 qDebug() << "Unable to save: " << fileName;