2 * Copyright (C) 2009 Sakari Poussa
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 2.
10 #include <QInputContext>
12 #include <QMaemo5InformationBox>
15 #include "score-dialog.h"
16 #include "score-common.h"
17 #include "table-model.h"
19 ////////////////////////////////////////////////////////////////////////////////
20 // ScoreWindow based on QMainWindow
21 // Usage: view scorecard
22 ////////////////////////////////////////////////////////////////////////////////
23 ScoreWindow::ScoreWindow(QWidget *parent) : QMainWindow(parent)
26 setAttribute(Qt::WA_Maemo5StackedWindow);
29 QAction *editAction = new QAction(tr("Edit"), this);
30 connect(editAction, SIGNAL(triggered()), parent, SLOT(editScore()));
31 menuBar()->addAction(editAction);
33 QAction *delAction = new QAction(tr("Delete"), this);
34 connect(delAction, SIGNAL(triggered()), parent, SLOT(deleteScore()));
35 menuBar()->addAction(delAction);
37 model = new ScoreTableModel;
39 QTableView * table = new QTableView;
41 table->setSelectionMode(QAbstractItemView::NoSelection);
42 table->setStyleSheet(defaultStyleSheet);
43 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
44 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
45 table->horizontalHeader()->hide();
46 table->setModel(model);
48 QWidget *central = new QWidget(this);
49 setCentralWidget(central);
51 QVBoxLayout *layout = new QVBoxLayout;
52 layout->addWidget(table);
54 central->setLayout(layout);
57 ScoreWindow::~ScoreWindow()
62 void ScoreWindow::setup(Score *score, Course *course)
65 QString title = QString("%1, %2 - %3").arg(score->getClubName()).arg(score->getCourseName()).arg(score->getDate());
66 setWindowTitle(title);
67 model->set(score, course);
70 ////////////////////////////////////////////////////////////////////////////////
71 // SelectDialog based on QDialog
72 // Usage: select course and date for entering score
73 ////////////////////////////////////////////////////////////////////////////////
74 SelectDialog::SelectDialog(QWidget *parent) : QDialog(parent)
78 QWidget *centralWidget = new QWidget(this);
79 createLayout(centralWidget);
81 setWindowTitle(tr("ScoreCard: Select Course and Date"));
84 void SelectDialog::createLayout(QWidget *parent)
86 listWidgetClub = new QListWidget(parent);
87 pushButtonNext = new QPushButton(tr("Next"));
89 connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
91 QDialogButtonBox * buttonBox = new QDialogButtonBox(Qt::Vertical);
92 buttonBox->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
94 leftLayout = new QVBoxLayout;
95 leftLayout->addWidget(listWidgetClub);
98 dateButton = new QMaemo5ValueButton();
99 dateButton->setValueLayout(QMaemo5ValueButton::ValueUnderText);
100 dateButton->setPickSelector(new QMaemo5DatePickSelector());
101 dateButton->setText(QString::fromUtf8("Date"));
102 leftLayout->addWidget(dateButton);
104 QDate today(QDate::currentDate());
105 lineEditDate = new QLineEdit;
106 lineEditDate->setText(today.toString("yyyy-MM-dd"));
107 date = new QDateEdit;
108 leftLayout->addWidget(date);
109 leftLayout->addWidget(lineEditDate);
112 rightLayout = new QVBoxLayout;
113 rightLayout->addStretch();
114 rightLayout->addWidget(buttonBox);
116 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
117 mainLayout->addLayout(leftLayout);
118 mainLayout->addLayout(rightLayout);
120 setLayout(mainLayout);
123 void SelectDialog::init(QList<Club *> &list)
128 QListIterator<Club *> i(clubList);
130 bool markedFlag = false;
132 while (i.hasNext()) {
133 Club *club = i.next();
135 QList<Course *> courseList = club->getCourseList();
137 QListIterator<Course *> j(courseList);
138 while (j.hasNext()) {
139 Course *course = j.next();
141 QListWidgetItem *newItem = new QListWidgetItem;
143 QString entry = club->getName() + ", " + course->getName();
145 newItem->setText(entry);
146 listWidgetClub->insertItem(index, newItem);
148 if (!markedFlag & club->isHomeClub()) {
149 listWidgetClub->setCurrentRow(index);
150 // Mark the 1st course of the home club the selection
158 void SelectDialog::results(QString &club,
162 QListWidgetItem *current = listWidgetClub->currentItem();
165 QString tmp = current->text();
167 QStringList stringList = tmp.split(", ");
168 club = stringList[0];
169 course = stringList[1];
171 QMaemo5DatePickSelector *sel = (QMaemo5DatePickSelector *)dateButton->pickSelector();
172 QDate d = sel->currentDate();
173 // TODO: change to QDate
174 date = d.toString(Qt::ISODate);
176 date = lineEditDate->text();
181 bool SelectDialog::validate(void)
186 void SelectDialog::next(void)
191 qDebug() << "SelectDialog: invalid data, cancel or correct";
195 void SelectDialog::reject(void)
200 ////////////////////////////////////////////////////////////////////////////////
201 // ScoreDialog based on QDialog
202 // Usage: edit scorecard data
203 ////////////////////////////////////////////////////////////////////////////////
204 ScoreDialog18::ScoreDialog18(QWidget *parent) : ScoreDialog(parent)
209 font.setPointSize(fontSize);
212 QWidget *centralWidget = new QWidget(this);
217 createLayout(centralWidget);
220 ScoreDialog18::~ScoreDialog18()
223 // delete centralWidget;
229 // delete mainLayout;
234 void ScoreDialog18::createLayout(QWidget *parent)
237 leftLayout = new QVBoxLayout;
238 leftLayout->addWidget(table);
240 QDialogButtonBox * buttonBoxUp = new QDialogButtonBox(Qt::Vertical);
241 buttonBoxUp->addButton(pushButtonUp, QDialogButtonBox::ActionRole);
242 buttonBoxUp->addButton(pushButtonDown, QDialogButtonBox::ActionRole);
243 buttonBoxUp->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
245 QDialogButtonBox * buttonBoxDown = new QDialogButtonBox(Qt::Vertical);
246 buttonBoxDown->addButton(pushButtonFinish, QDialogButtonBox::ActionRole);
248 rightLayout = new QVBoxLayout;
249 rightLayout->addWidget(buttonBoxUp);
250 rightLayout->addStretch();
251 rightLayout->addWidget(buttonBoxDown);
253 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
254 mainLayout->addLayout(leftLayout);
255 mainLayout->addLayout(rightLayout);
256 setLayout(mainLayout);
259 void ScoreDialog18::createTable(QWidget *parent)
262 table = new QTableWidget(ROWS, COLS, parent);
264 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
265 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
266 table->horizontalHeader()->hide();
268 table->setStyleSheet(defaultStyleSheet);
271 headers << "" << "Par" << "HCP" << "Score" << "" << "Par" << "HCP" << "Score";
272 table->setVerticalHeaderLabels(headers);
275 void ScoreDialog18::createButton(QWidget *parent)
279 pushButtonUp = new QPushButton(tr("+"));
280 connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
282 pushButtonDown = new QPushButton(tr("-"));
283 connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
285 pushButtonNext = new QPushButton(tr("Next"));
286 connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
288 pushButtonFinish = new QPushButton(tr("Finish"));
289 connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
292 void ScoreDialog18::init(Course *course, Score *score)
295 QTableWidgetItem *par, *hcp, *scoreItem, *holeNum;
297 for (int i = 0; i < 18; i++) {
298 par = new QTableWidgetItem(course->getPar(i));
299 hcp = new QTableWidgetItem(course->getHcp(i));
301 scoreItem = new QTableWidgetItem(score->getScore(i));
303 scoreItem = new QTableWidgetItem("");
304 holeNum = new QTableWidgetItem(QString::number(i+1));
306 holeNum->setTextAlignment(Qt::AlignCenter);
307 holeNum->setFlags(Qt::NoItemFlags);
308 holeNum->setForeground(ScoreColor::holeBg());
310 par->setTextAlignment(Qt::AlignCenter);
311 par->setFlags(Qt::NoItemFlags);
313 hcp->setTextAlignment(Qt::AlignCenter);
314 hcp->setFlags(Qt::NoItemFlags);
316 scoreItem->setTextAlignment(Qt::AlignCenter);
319 table->setItem(ROW_HOLE, i, holeNum);
320 table->setItem(ROW_PAR, i, par);
321 table->setItem(ROW_HCP, i, hcp);
322 table->setItem(ROW_SCORE, i, scoreItem);
325 table->setItem(ROW_HOLE_2, i-9, holeNum);
326 table->setItem(ROW_PAR_2, i-9, par);
327 table->setItem(ROW_HCP_2, i-9, hcp);
328 table->setItem(ROW_SCORE_2, i-9, scoreItem);
331 // This - for some unknown reason - does not work ...
332 table->setInputMethodHints(Qt::ImhDigitsOnly);
334 // Set focus to 1st cell
335 table->setCurrentCell(ROW_SCORE, 0);
337 setDefaultScore(table);
340 // Set default score to par if not set
341 void ScoreDialog18::setDefaultScore(QTableWidget *table)
343 int row = table->currentRow();
344 int col = table->currentColumn();
346 if (row == ROW_SCORE)
348 else if (row == ROW_SCORE_2)
351 qDebug() << "ERROR: unknown row in default score";
354 QTableWidgetItem *par = table->item(row, col);
355 QTableWidgetItem *score = table->item(row + 2, col);
357 if (par && score && score->text() == "") {
358 QVariant value(par->text());
359 score->setData(Qt::DisplayRole, value);
363 void ScoreDialog18::up(void)
365 QTableWidgetItem *item = table->currentItem();
368 qWarning() << "ERROR: no current item";
372 int i = (item->text()).toInt();
374 item->setData(Qt::DisplayRole, value);
377 void ScoreDialog18::down(void)
379 QTableWidgetItem *item = table->currentItem();
384 int i = (item->text()).toInt();
386 item->setData(Qt::DisplayRole, value);
389 void ScoreDialog18::next(void)
392 QTableWidgetItem *item = table->currentItem();
393 moveToNextCell(item);
394 setDefaultScore(table);
398 void ScoreDialog18::moveToNextCell(QTableWidgetItem *item)
403 QTableWidget *table = item->tableWidget();
408 int row = table->currentRow();
409 int col = table->currentColumn();
411 if (col < (COLS-1)) {
414 else if (col == (COLS-1)) {
416 row = (row == ROW_SCORE_2) ? ROW_SCORE : ROW_SCORE_2;
418 table->setCurrentCell(row, col);
421 void ScoreDialog18::results(QVector<QString> &scores)
424 for (int i = 0; i < 9; i++) {
425 QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
426 QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
429 scores[i] = frontItem->text();
432 scores[i+9] = backItem->text();
436 bool ScoreDialog18::validate(void)
438 for (int i=0; i<9; i++) {
439 QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
440 QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
442 if (!frontItem || !backItem)
445 QString str1 = frontItem->text();
446 QString str2 = backItem->text();
448 if (str1.isEmpty() || str2.isEmpty())
454 void ScoreDialog18::finish(void)
459 showNote("Invalid data - cancel or correct");
463 void ScoreDialog18::reject(void)
468 void ScoreDialog18::showNote(QString msg)
471 QMaemo5InformationBox::information(this,
473 QMaemo5InformationBox::DefaultTimeout);
477 ////////////////////////////////////////////////////////////////////////////////
478 // ScoreDialogSingle based on QDialog
479 // Usage: edit scorecard data for single hole
480 ////////////////////////////////////////////////////////////////////////////////
481 ScoreDialogSingle::ScoreDialogSingle(QWidget *parent) : ScoreDialog(parent)
486 font.setPointSize(fontSize);
489 QWidget *centralWidget = new QWidget(this);
494 createLayout(centralWidget);
497 ScoreDialogSingle::~ScoreDialogSingle()
500 // delete centralWidget;
506 // delete mainLayout;
511 void ScoreDialogSingle::createLayout(QWidget *parent)
514 leftLayout = new QVBoxLayout;
515 leftLayout->addWidget(table);
517 QDialogButtonBox * buttonBoxUp = new QDialogButtonBox(Qt::Vertical);
518 buttonBoxUp->addButton(pushButtonUp, QDialogButtonBox::ActionRole);
519 buttonBoxUp->addButton(pushButtonDown, QDialogButtonBox::ActionRole);
520 buttonBoxUp->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
522 QDialogButtonBox * buttonBoxDown = new QDialogButtonBox(Qt::Vertical);
523 buttonBoxDown->addButton(pushButtonFinish, QDialogButtonBox::ActionRole);
525 rightLayout = new QVBoxLayout;
526 rightLayout->addWidget(buttonBoxUp);
527 rightLayout->addStretch();
528 rightLayout->addWidget(buttonBoxDown);
530 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
531 mainLayout->addLayout(leftLayout);
532 mainLayout->addLayout(rightLayout);
533 setLayout(mainLayout);
536 void ScoreDialogSingle::createTable(QWidget *parent)
539 table = new QTableWidget(ROWS, COLS, parent);
541 font.setPointSize(32);
542 table->setFont(font);
544 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
545 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
546 table->horizontalHeader()->hide();
548 table->setStyleSheet(defaultStyleSheet);
551 headers << "Hole" << "Par" << "HCP" << "Score" << "Putts" << "Fairway" << "Green";
552 table->setVerticalHeaderLabels(headers);
555 void ScoreDialogSingle::createButton(QWidget *parent)
559 pushButtonUp = new QPushButton(tr("+"));
560 connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
562 pushButtonDown = new QPushButton(tr("-"));
563 connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
565 pushButtonNext = new QPushButton(tr("Next"));
566 connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
568 pushButtonFinish = new QPushButton(tr("Finish"));
569 connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
572 void ScoreDialogSingle::init(Course *course, Score *score)
575 QTableWidgetItem *holeNum, *par, *hcp, *scoreItem, *putts, *fairway, *green;
577 for (int i = 0; i < 3; i++) {
578 par = new QTableWidgetItem(course->getPar(i));
579 hcp = new QTableWidgetItem(course->getHcp(i));
582 scoreItem = new QTableWidgetItem(score->getScore(i));
584 scoreItem = new QTableWidgetItem("");
586 putts = new QTableWidgetItem("2");
587 fairway = new QTableWidgetItem("Yes");
588 green = new QTableWidgetItem("Yes");
590 holeNum = new QTableWidgetItem(QString::number(i+1));
592 holeNum->setTextAlignment(Qt::AlignCenter);
593 holeNum->setFlags(Qt::NoItemFlags);
594 holeNum->setForeground(ScoreColor::holeBg());
596 par->setTextAlignment(Qt::AlignCenter);
597 par->setFlags(Qt::NoItemFlags);
599 hcp->setTextAlignment(Qt::AlignCenter);
600 hcp->setFlags(Qt::NoItemFlags);
602 scoreItem->setTextAlignment(Qt::AlignCenter);
603 putts->setTextAlignment(Qt::AlignCenter);
604 fairway->setTextAlignment(Qt::AlignCenter);
605 green->setTextAlignment(Qt::AlignCenter);
607 table->setItem(ROW_HOLE, i, holeNum);
608 table->setItem(ROW_PAR, i, par);
609 table->setItem(ROW_HCP, i, hcp);
610 table->setItem(ROW_SCORE, i, scoreItem);
611 table->setItem(ROW_PUTTS, i, putts);
612 table->setItem(ROW_FAIRWAY, i, fairway);
613 table->setItem(ROW_GREEN, i, green);
615 // Set focus to 1st cell
616 table->setCurrentCell(ROW_SCORE, 0);
618 setDefaultScore(table);
621 // Set default score to par if not set
622 // TODO: merge the two methods since they are the same
623 void ScoreDialogSingle::setDefaultScore(QTableWidget *table)
625 int row = table->currentRow();
626 int col = table->currentColumn();
628 if (row == ROW_SCORE)
631 qDebug() << "ERROR: unknown row in default score";
634 QTableWidgetItem *par = table->item(row, col);
635 QTableWidgetItem *score = table->item(row + 2, col);
637 if (par && score && score->text() == "") {
638 QVariant value(par->text());
639 score->setData(Qt::DisplayRole, value);
643 void ScoreDialogSingle::up(void)
645 QTableWidgetItem *item = table->currentItem();
648 qWarning() << "ERROR: no current item";
652 int i = (item->text()).toInt();
654 item->setData(Qt::DisplayRole, value);
657 void ScoreDialogSingle::down(void)
659 QTableWidgetItem *item = table->currentItem();
664 int i = (item->text()).toInt();
666 item->setData(Qt::DisplayRole, value);
669 void ScoreDialogSingle::next(void)
672 QTableWidgetItem *item = table->currentItem();
673 moveToNextCell(item);
674 setDefaultScore(table);
678 void ScoreDialogSingle::moveToNextCell(QTableWidgetItem *item)
683 QTableWidget *table = item->tableWidget();
688 int row = table->currentRow();
689 int col = table->currentColumn();
691 qDebug() << row << col;
692 if (row < (ROWS - 1)) {
695 else if (row == (ROWS - 1)) {
697 col = (col == COLS-1) ? 0 : col + 1;
699 qDebug() << row << col;
700 table->setCurrentCell(row, col);
703 void ScoreDialogSingle::results(QVector<QString> &scores)