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()));
191 viewScoreAct = new QAction(tr("&View Scores"), this);
192 connect(viewScoreAct, SIGNAL(triggered()), this, SLOT(viewScore()));
194 viewCourseAct = new QAction(tr("&View Courses"), this);
195 connect(viewCourseAct, SIGNAL(triggered()), this, SLOT(viewCourse()));
197 viewStatisticAct = new QAction(tr("&View Statistics"), this);
198 connect(viewStatisticAct, SIGNAL(triggered()), this, SLOT(viewStatistic()));
202 void MainWindow::createMenus()
204 menu = menuBar()->addMenu(tr("fremantle"));
206 menu->addAction(viewScoreAct);
207 menu->addAction(viewCourseAct);
208 menu->addAction(viewStatisticAct);
210 menu->addAction(newScoreAct);
211 menu->addAction(newCourseAct);
212 menu->addAction(editScoreAct);
215 void MainWindow::updateStatusBar()
217 setWindowTitle(scoreTableModel->getInfoText());
220 void MainWindow::firstButtonClicked()
222 scoreTableModel->first();
226 void MainWindow::lastButtonClicked()
228 scoreTableModel->last();
232 void MainWindow::nextButtonClicked()
234 scoreTableModel->next();
238 void MainWindow::prevButtonClicked()
240 scoreTableModel->prev();
244 // FIXME: dup code from table-model.cpp
245 Club *MainWindow::findClub(QString &name)
247 QListIterator<Club *> i(clubList);
250 while (i.hasNext()) {
252 if (c->getName() == name)
258 void MainWindow::newCourse()
260 CourseSelectDialog *selectDialog = new CourseSelectDialog(this);
262 int result = selectDialog->exec();
268 selectDialog->results(clubName, courseName);
270 CourseDialog *courseDialog = new CourseDialog(this);
272 QString title = "New Course: " + clubName + "," + courseName;
273 courseDialog->setWindowTitle(title);
275 int result = courseDialog->exec();
277 QVector<QString> par(18);
278 QVector<QString> hcp(18);
279 QVector<QString> len(18);
281 courseDialog->results(par, hcp, len);
284 Club *club = findClub(clubName);
286 course = club->getCourse(courseName);
288 qDebug() << "Error: club/course already in the database";
292 course = new Course(courseName, par, hcp);
293 club->addCourse(course);
297 club = new Club(clubName);
298 course = new Course(courseName, par, hcp);
299 club->addCourse(course);
302 saveClubFile(clubFile, clubList);
304 // TODO: does this really work? No mem leaks?
305 scoreTableModel->setClub(clubList);
311 void MainWindow::newScore()
313 SelectDialog *selectDialog = new SelectDialog(this);
315 selectDialog->init(clubList);
317 int result = selectDialog->exec();
323 selectDialog->results(clubName, courseName, date);
325 ScoreDialog *scoreDialog = new ScoreDialog(this);
326 QString title = "New Score: " + courseName + ", " + date;
327 scoreDialog->setWindowTitle(title);
329 Club *club = findClub(clubName);
331 qDebug() << "Error: no such club: " << clubName;
334 Course *course = club->getCourse(courseName);
336 qDebug() << "Error: no such course: " << courseName;
340 scoreDialog->init(course);
342 result = scoreDialog->exec();
345 QVector<QString> scores(18);
347 scoreDialog->results(scores);
348 Score *score = new Score(scores, clubName, courseName, date);
351 // Sort the scores based on dates
352 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
354 saveScoreFile(scoreFile, scoreList);
356 // TODO: does this really work? No mem leaks?
357 scoreTableModel->setScore(scoreList, score);
363 void MainWindow::editScore()
365 Course *course = scoreTableModel->getCourse();
366 Score *score = scoreTableModel->getScore();
367 QString date = score->getDate();
369 ScoreDialog *scoreDialog = new ScoreDialog(this);
371 QString title = "Edit Score: " + course->getName() + ", " + date;
372 scoreDialog->setWindowTitle(title);
374 scoreDialog->init(course, score);
376 int result = scoreDialog->exec();
379 QVector<QString> scores(18);
381 scoreDialog->results(scores);
383 score->update(scores);
385 // Sort the scores based on dates
386 qSort(scoreList.begin(), scoreList.end(), dateLessThan);
388 saveScoreFile(scoreFile, scoreList);
390 // TODO: does this really work? No mem leaks?
391 scoreTableModel->setScore(scoreList, score);
396 void MainWindow::loadScoreFile(QString &fileName, QList<Score *> &list)
398 ScoreXmlHandler handler(list);
400 if (handler.parse(fileName))
401 qDebug() << "File loaded: " << fileName << " entries: " << list.size();
404 void MainWindow::saveScoreFile(QString &fileName, QList<Score *> &list)
406 ScoreXmlHandler handler(list);
408 if (handler.save(fileName))
410 qDebug() << "File saved: " << fileName << " entries: " << list.size();
412 qDebug() << "Unable to save: " << fileName;
415 void MainWindow::loadClubFile(QString &fileName, QList<Club *> &list)
417 ClubXmlHandler handler(list);
419 if (handler.parse(fileName))
420 qDebug() << "File loaded: " << fileName << " entries: " << list.size();
423 void MainWindow::saveClubFile(QString &fileName, QList<Club *> &list)
425 ClubXmlHandler handler(list);
427 if (handler.save(fileName))
429 qDebug() << "File saved: " << fileName << " entries: " << list.size();
431 qDebug() << "Unable to save: " << fileName;