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 ////////////////////////////////////////////////////////////////////////////////
22 ScoreWindow::ScoreWindow(QWidget *parent) : QMainWindow(parent)
25 setAttribute(Qt::WA_Maemo5StackedWindow);
28 QAction *editAction = new QAction(tr("Edit"), this);
29 connect(editAction, SIGNAL(triggered()), parent, SLOT(editScore()));
30 menuBar()->addAction(editAction);
32 QAction *delAction = new QAction(tr("Delete"), this);
33 connect(delAction, SIGNAL(triggered()), parent, SLOT(deleteScore()));
34 menuBar()->addAction(delAction);
36 model = new ScoreTableModel;
38 QTableView * table = new QTableView;
40 table->setSelectionMode(QAbstractItemView::NoSelection);
41 table->setStyleSheet(defaultStyleSheet);
42 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
43 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
44 table->horizontalHeader()->hide();
45 table->setModel(model);
47 QWidget *central = new QWidget(this);
48 setCentralWidget(central);
50 QVBoxLayout *layout = new QVBoxLayout;
51 layout->addWidget(table);
53 central->setLayout(layout);
56 ScoreWindow::~ScoreWindow()
61 void ScoreWindow::setup(Score *score, Course *course)
64 QString title = QString("%1, %2 - %3").arg(score->getClubName()).arg(score->getCourseName()).arg(score->getDate());
65 setWindowTitle(title);
66 model->set(score, course);
70 ////////////////////////////////////////////////////////////////////////////////
71 // SelectDialog based on QDialog
72 ////////////////////////////////////////////////////////////////////////////////
73 SelectDialog::SelectDialog(QWidget *parent) : QDialog(parent)
77 QWidget *centralWidget = new QWidget(this);
78 createLayout(centralWidget);
80 setWindowTitle(tr("ScoreCard: Select Course and Date"));
83 void SelectDialog::createLayout(QWidget *parent)
85 listWidgetClub = new QListWidget(parent);
86 pushButtonNext = new QPushButton(tr("Next"));
88 connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
90 QDialogButtonBox * buttonBox = new QDialogButtonBox(Qt::Vertical);
91 buttonBox->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
93 leftLayout = new QVBoxLayout;
94 leftLayout->addWidget(listWidgetClub);
97 dateButton = new QMaemo5ValueButton();
98 dateButton->setValueLayout(QMaemo5ValueButton::ValueUnderText);
99 dateButton->setPickSelector(new QMaemo5DatePickSelector());
100 dateButton->setText(QString::fromUtf8("Date"));
101 leftLayout->addWidget(dateButton);
103 QDate today(QDate::currentDate());
104 lineEditDate = new QLineEdit;
105 lineEditDate->setText(today.toString("yyyy-MM-dd"));
106 date = new QDateEdit;
107 leftLayout->addWidget(date);
108 leftLayout->addWidget(lineEditDate);
111 rightLayout = new QVBoxLayout;
112 rightLayout->addStretch();
113 rightLayout->addWidget(buttonBox);
115 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
116 mainLayout->addLayout(leftLayout);
117 mainLayout->addLayout(rightLayout);
119 setLayout(mainLayout);
122 void SelectDialog::init(QList<Club *> &list)
127 QListIterator<Club *> i(clubList);
129 bool markedFlag = false;
131 while (i.hasNext()) {
132 Club *club = i.next();
134 QList<Course *> courseList = club->getCourseList();
136 QListIterator<Course *> j(courseList);
137 while (j.hasNext()) {
138 Course *course = j.next();
140 QListWidgetItem *newItem = new QListWidgetItem;
142 QString entry = club->getName() + ", " + course->getName();
144 newItem->setText(entry);
145 listWidgetClub->insertItem(index, newItem);
147 if (!markedFlag & club->isHomeClub()) {
148 listWidgetClub->setCurrentRow(index);
149 // Mark the 1st course of the home club the selection
157 void SelectDialog::results(QString &club,
161 QListWidgetItem *current = listWidgetClub->currentItem();
164 QString tmp = current->text();
166 QStringList stringList = tmp.split(", ");
167 club = stringList[0];
168 course = stringList[1];
170 QMaemo5DatePickSelector *sel = (QMaemo5DatePickSelector *)dateButton->pickSelector();
171 QDate d = sel->currentDate();
172 // TODO: change to QDate
173 date = d.toString(Qt::ISODate);
175 date = lineEditDate->text();
180 bool SelectDialog::validate(void)
185 void SelectDialog::next(void)
190 qDebug() << "SelectDialog: invalid data, cancel or correct";
194 void SelectDialog::reject(void)
199 ////////////////////////////////////////////////////////////////////////////////
200 // ScoreDialog based on QDialog
201 ////////////////////////////////////////////////////////////////////////////////
202 ScoreDialog::ScoreDialog(QWidget *parent) : QDialog(parent)
207 font.setPointSize(fontSize);
210 QWidget *centralWidget = new QWidget(this);
215 createLayout(centralWidget);
218 ScoreDialog::~ScoreDialog()
221 // delete centralWidget;
227 // delete mainLayout;
232 void ScoreDialog::createLayout(QWidget *parent)
235 leftLayout = new QVBoxLayout;
236 leftLayout->addWidget(table);
238 QDialogButtonBox * buttonBoxUp = new QDialogButtonBox(Qt::Vertical);
239 buttonBoxUp->addButton(pushButtonUp, QDialogButtonBox::ActionRole);
240 buttonBoxUp->addButton(pushButtonDown, QDialogButtonBox::ActionRole);
241 buttonBoxUp->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
243 QDialogButtonBox * buttonBoxDown = new QDialogButtonBox(Qt::Vertical);
244 buttonBoxDown->addButton(pushButtonFinish, QDialogButtonBox::ActionRole);
246 rightLayout = new QVBoxLayout;
247 rightLayout->addWidget(buttonBoxUp);
248 rightLayout->addStretch();
249 rightLayout->addWidget(buttonBoxDown);
251 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
252 mainLayout->addLayout(leftLayout);
253 mainLayout->addLayout(rightLayout);
254 setLayout(mainLayout);
257 void ScoreDialog::createTable(QWidget *parent)
260 table = new QTableWidget(ROWS, COLS, parent);
262 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
263 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
264 table->horizontalHeader()->hide();
266 table->setStyleSheet(defaultStyleSheet);
269 headers << "" << "Par" << "HCP" << "Score" << "" << "Par" << "HCP" << "Score";
270 table->setVerticalHeaderLabels(headers);
273 void ScoreDialog::createButton(QWidget *parent)
277 pushButtonUp = new QPushButton(tr("+"));
278 connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
280 pushButtonDown = new QPushButton(tr("-"));
281 connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
283 pushButtonNext = new QPushButton(tr("Next"));
284 connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
286 pushButtonFinish = new QPushButton(tr("Finish"));
287 connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
290 void ScoreDialog::init(Course *course, Score *score)
293 QTableWidgetItem *par, *hcp, *scoreItem, *holeNum;
295 for (int i = 0; i < 18; i++) {
296 par = new QTableWidgetItem(course->getPar(i));
297 hcp = new QTableWidgetItem(course->getHcp(i));
299 scoreItem = new QTableWidgetItem(score->getScore(i));
301 scoreItem = new QTableWidgetItem("");
302 holeNum = new QTableWidgetItem(QString::number(i+1));
304 holeNum->setTextAlignment(Qt::AlignCenter);
305 holeNum->setFlags(Qt::NoItemFlags);
306 holeNum->setForeground(ScoreColor::holeBg());
308 par->setTextAlignment(Qt::AlignCenter);
309 par->setFlags(Qt::NoItemFlags);
311 hcp->setTextAlignment(Qt::AlignCenter);
312 hcp->setFlags(Qt::NoItemFlags);
314 scoreItem->setTextAlignment(Qt::AlignCenter);
317 table->setItem(ROW_HOLE, i, holeNum);
318 table->setItem(ROW_PAR, i, par);
319 table->setItem(ROW_HCP, i, hcp);
320 table->setItem(ROW_SCORE, i, scoreItem);
323 table->setItem(ROW_HOLE_2, i-9, holeNum);
324 table->setItem(ROW_PAR_2, i-9, par);
325 table->setItem(ROW_HCP_2, i-9, hcp);
326 table->setItem(ROW_SCORE_2, i-9, scoreItem);
329 // This - for some unknown reason - does not work ...
330 table->setInputMethodHints(Qt::ImhDigitsOnly);
332 // Set focus to 1st cell
333 table->setCurrentCell(ROW_SCORE, 0);
335 setDefaultScore(table);
338 // Set default score to par if not set
339 void ScoreDialog::setDefaultScore(QTableWidget *table)
341 int row = table->currentRow();
342 int col = table->currentColumn();
344 if (row == ROW_SCORE)
346 else if (row == ROW_SCORE_2)
349 qDebug() << "ERROR: unknown row in default score";
352 QTableWidgetItem *par = table->item(row, col);
353 QTableWidgetItem *score = table->item(row + 2, col);
355 if (par && score && score->text() == "") {
356 QVariant value(par->text());
357 score->setData(Qt::DisplayRole, value);
361 void ScoreDialog::up(void)
363 QTableWidgetItem *item = table->currentItem();
366 qWarning() << "ERROR: no current item";
370 int i = (item->text()).toInt();
372 item->setData(Qt::DisplayRole, value);
375 void ScoreDialog::down(void)
377 QTableWidgetItem *item = table->currentItem();
382 int i = (item->text()).toInt();
384 item->setData(Qt::DisplayRole, value);
387 void ScoreDialog::next(void)
390 QTableWidgetItem *item = table->currentItem();
391 moveToNextCell(item);
392 setDefaultScore(table);
396 void ScoreDialog::moveToNextCell(QTableWidgetItem *item)
401 QTableWidget *table = item->tableWidget();
406 int row = table->currentRow();
407 int col = table->currentColumn();
409 if (col < (COLS-1)) {
412 else if (col == (COLS-1)) {
414 row = (row == ROW_SCORE_2) ? ROW_SCORE : ROW_SCORE_2;
416 table->setCurrentCell(row, col);
419 void ScoreDialog::results(QVector<QString> &scores)
422 for (int i = 0; i < 9; i++) {
423 QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
424 QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
427 scores[i] = frontItem->text();
430 scores[i+9] = backItem->text();
434 bool ScoreDialog::validate(void)
436 for (int i=0; i<9; i++) {
437 QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
438 QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
440 if (!frontItem || !backItem)
443 QString str1 = frontItem->text();
444 QString str2 = backItem->text();
446 if (str1.isEmpty() || str2.isEmpty())
452 void ScoreDialog::finish(void)
457 showNote("Invalid data - cancel or correct");
461 void ScoreDialog::reject(void)
466 void ScoreDialog::showNote(QString msg)
469 QMaemo5InformationBox::information(this,
471 QMaemo5InformationBox::DefaultTimeout);