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/scorecard");
16 QString mmcDir("/media/mmc1/scorecard");
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);
47 createTableView(scoreList, clubList);
48 //createTreeView(scoreList, parent);
51 createLayout(centralWidget);
57 void MainWindow::loadSettings(void)
59 bool external = false;
70 dataDir = mmcDir + dataDirName;
73 dataDir = topDir + dataDirName;
75 scoreFile = dataDir + scoreFileName;
76 clubFile = dataDir + clubFileName;
80 if (!dir.mkdir(dataDir)) {
81 // TODO: mkdir does not work...
82 qDebug() << "Unable to create: " + dataDir;
85 qDebug() << "Data is at: " + dataDir;
88 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();
122 scoreTableModel->setScore(scoreList);
123 scoreTableModel->setClub(clubList);
127 table->setModel(scoreTableModel);
129 // Fill out all the space with the tables
130 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
131 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
132 table->horizontalHeader()->hide();
135 // When selection down in 'stat' view, this is called.
136 void MainWindow::updateTreeView(const QModelIndex & index)
138 QString scope("Scope");
139 QString count("Rounds");
140 QString scoreAvg("Score (avg.)");
141 QString scoreBest("Score (best)");
142 QString score("Total");
143 QString scoreIn("Total in");
144 QString scoreOut("Total out");
146 QVariant str = scoreTreeModel->data(index, Qt::DisplayRole);
147 QVariant type = scoreTreeModel->data(index, ScoreTreeModel::Type);
149 qDebug() << "update(" << index.row() << "/" << index.column() << "):" << str << type;
151 tableModel->setData(tableModel->index(0, 0, QModelIndex()), scope);
152 tableModel->setData(tableModel->index(0, 1, QModelIndex()), str);
154 if (type == TreeItem::TypeDate) {
155 tableModel->setData(tableModel->index(1, 0, QModelIndex()), count);
156 tableModel->setData(tableModel->index(2, 0, QModelIndex()), scoreAvg);
157 tableModel->setData(tableModel->index(3, 0, QModelIndex()), scoreBest);
159 else if (type == TreeItem::TypeScore) {
160 QVariant value = scoreTreeModel->data(index, ScoreTreeModel::Total);
161 tableModel->setData(tableModel->index(1, 0, QModelIndex()), score);
162 tableModel->setData(tableModel->index(1, 1, QModelIndex()), value);
164 value = scoreTreeModel->data(index, ScoreTreeModel::TotalOut);
165 tableModel->setData(tableModel->index(2, 0, QModelIndex()), scoreOut);
166 tableModel->setData(tableModel->index(2, 1, QModelIndex()), value);
168 value = scoreTreeModel->data(index, ScoreTreeModel::TotalIn);
169 tableModel->setData(tableModel->index(3, 0, QModelIndex()), scoreIn);
170 tableModel->setData(tableModel->index(3, 1, QModelIndex()), value);
174 void MainWindow::createStatusBar()
179 void MainWindow::createActions()
181 newScoreAct = new QAction(tr("New Score"), this);
182 connect(newScoreAct, SIGNAL(triggered()), this, SLOT(newScore()));
184 newCourseAct = new QAction(tr("New Course"), this);
185 connect(newCourseAct, SIGNAL(triggered()), this, SLOT(newCourse()));
187 editScoreAct = new QAction(tr("Edit Score"), this);
188 connect(editScoreAct, SIGNAL(triggered()), this, SLOT(editScore()));
190 editCourseAct = new QAction(tr("Edit Course"), this);
191 connect(editCourseAct, SIGNAL(triggered()), this, SLOT(editCourse()));
194 viewScoreAct = new QAction(tr("&View Scores"), this);
195 connect(viewScoreAct, SIGNAL(triggered()), this, SLOT(viewScore()));
197 viewCourseAct = new QAction(tr("&View Courses"), this);
198 connect(viewCourseAct, SIGNAL(triggered()), this, SLOT(viewCourse()));
200 viewStatisticAct = new QAction(tr("&View Statistics"), this);
201 connect(viewStatisticAct, SIGNAL(triggered()), this, SLOT(viewStatistic()));
205 void MainWindow::createMenus()
207 menu = menuBar()->addMenu(tr("fremantle"));
209 menu->addAction(viewScoreAct);
210 menu->addAction(viewCourseAct);
211 menu->addAction(viewStatisticAct);
213 menu->addAction(newScoreAct);
214 menu->addAction(newCourseAct);
215 menu->addAction(editScoreAct);
216 menu->addAction(editCourseAct);
219 void MainWindow::updateStatusBar()
221 setWindowTitle(scoreTableModel->getInfoText());
224 void MainWindow::firstButtonClicked()
226 scoreTableModel->first();
230 void MainWindow::lastButtonClicked()
232 scoreTableModel->last();
236 void MainWindow::nextButtonClicked()
238 scoreTableModel->next();
242 void MainWindow::prevButtonClicked()
244 scoreTableModel->prev();
248 // FIXME: dup code from table-model.cpp
249 Club *MainWindow::findClub(QString &name)
251 QListIterator<Club *> i(clubList);
254 while (i.hasNext()) {
256 if (c->getName() == name)
262 void MainWindow::newCourse()
264 CourseSelectDialog *selectDialog = new CourseSelectDialog(this);
266 int result = selectDialog->exec();
272 selectDialog->results(clubName, courseName);
274 CourseDialog *courseDialog = new CourseDialog(this);
275 courseDialog->init();
277 QString title = "New Course: " + clubName + "," + courseName;
278 courseDialog->setWindowTitle(title);
280 int result = courseDialog->exec();
282 QVector<QString> par(18);
283 QVector<QString> hcp(18);
284 QVector<QString> len(18);
286 courseDialog->results(par, hcp, len);
289 Club *club = findClub(clubName);
291 course = club->getCourse(courseName);
293 qDebug() << "Error: club/course already in the database";
297 course = new Course(courseName, par, hcp);
298 club->addCourse(course);
302 club = new Club(clubName);
303 course = new Course(courseName, par, hcp);
304 club->addCourse(course);
307 saveClubFile(clubFile, clubList);
309 // TODO: does this really work? No mem leaks?
310 scoreTableModel->setClub(clubList);
316 void MainWindow::editCourse()
318 Course *course = scoreTableModel->getCourse();
320 CourseDialog *courseDialog = new CourseDialog(this);
321 courseDialog->init(course);
323 QString title = "Edit Course: " + course->getName();
324 courseDialog->setWindowTitle(title);
326 int result = courseDialog->exec();
328 QVector<QString> par(18);
329 QVector<QString> hcp(18);
330 QVector<QString> len(18);
332 courseDialog->results(par, hcp, len);
334 course->update(par, hcp, len);
335 saveClubFile(clubFile, clubList);
339 void MainWindow::newScore()
341 SelectDialog *selectDialog = new SelectDialog(this);
343 selectDialog->init(clubList);
345 int result = selectDialog->exec();
351 selectDialog->results(clubName, courseName, date);
353 ScoreDialog *scoreDialog = new ScoreDialog(this);
354 QString title = "New Score: " + courseName + ", " + date;
355 scoreDialog->setWindowTitle(title);
357 Club *club = findClub(clubName);
359 qDebug() << "Error: no such club: " << clubName;
362 Course *course = club->getCourse(courseName);
364 qDebug() << "Error: no such course: " << courseName;
367 scoreDialog->init(course);
368 result = scoreDialog->exec();
370 QVector<QString> scores(18);
372 scoreDialog->results(scores);
373 Score *score = new Score(scores, clubName, courseName, date);
376 // Sort the scores based on dates
377 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
379 saveScoreFile(scoreFile, scoreList);
381 // TODO: does this really work? No mem leaks?
382 scoreTableModel->setScore(scoreList, score);
388 void MainWindow::editScore()
390 Course *course = scoreTableModel->getCourse();
391 Score *score = scoreTableModel->getScore();
392 QString date = score->getDate();
394 ScoreDialog *scoreDialog = new ScoreDialog(this);
396 QString title = "Edit Score: " + course->getName() + ", " + date;
397 scoreDialog->setWindowTitle(title);
399 scoreDialog->init(course, score);
401 int result = scoreDialog->exec();
404 QVector<QString> scores(18);
406 scoreDialog->results(scores);
408 score->update(scores);
410 // Sort the scores based on dates
411 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
413 saveScoreFile(scoreFile, scoreList);
415 // TODO: does this really work? No mem leaks?
416 scoreTableModel->setScore(scoreList, score);
421 void MainWindow::loadScoreFile(QString &fileName, QList<Score *> &list)
423 ScoreXmlHandler handler(list);
425 if (handler.parse(fileName))
426 qDebug() << "File loaded: " << fileName << " entries: " << list.size();
429 void MainWindow::saveScoreFile(QString &fileName, QList<Score *> &list)
431 ScoreXmlHandler handler(list);
433 if (handler.save(fileName))
435 qDebug() << "File saved: " << fileName << " entries: " << list.size();
437 qDebug() << "Unable to save: " << fileName;
440 void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list)
442 ClubXmlHandler handler(list);
444 if (handler.parse(fileName))
445 qDebug() << "File loaded: " << fileName << " entries: " << list.size();
448 void MainWindow::saveClubFile(QString &fileName, QList<Club *> &list)
450 ClubXmlHandler handler(list);
452 if (handler.save(fileName))
454 qDebug() << "File saved: " << fileName << " entries: " << list.size();
456 qDebug() << "Unable to save: " << fileName;