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("Score: %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 listClub = 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(listClub);
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)
126 QListIterator<Club *> i(clubList);
129 while (i.hasNext()) {
130 Club *club = i.next();
132 QList<Course *> courseList = club->getCourseList();
134 QListIterator<Course *> j(courseList);
135 while (j.hasNext()) {
136 Course *course = j.next();
138 QListWidgetItem *newItem = new QListWidgetItem;
140 QString entry = club->getName() + ", " + course->getName();
142 newItem->setText(entry);
143 listClub->insertItem(index, newItem);
150 void SelectDialog::results(QString &club,
154 QListWidgetItem *current = listClub->currentItem();
157 QString tmp = current->text();
159 QStringList stringList = tmp.split(", ");
160 club = stringList[0];
161 course = stringList[1];
163 QMaemo5DatePickSelector *sel = (QMaemo5DatePickSelector *)dateButton->pickSelector();
164 QDate d = sel->currentDate();
165 // TODO: change to QDate
166 date = d.toString(Qt::ISODate);
168 date = lineEditDate->text();
173 bool SelectDialog::validate(void)
178 void SelectDialog::next(void)
183 qDebug() << "SelectDialog: invalid data, cancel or correct";
187 void SelectDialog::reject(void)
192 ////////////////////////////////////////////////////////////////////////////////
193 // ScoreDialog based on QDialog
194 ////////////////////////////////////////////////////////////////////////////////
195 ScoreDialog::ScoreDialog(QWidget *parent) : QDialog(parent)
200 font.setPointSize(fontSize);
203 QWidget *centralWidget = new QWidget(this);
208 createLayout(centralWidget);
211 ScoreDialog::~ScoreDialog()
214 // delete centralWidget;
220 // delete mainLayout;
225 void ScoreDialog::createLayout(QWidget *parent)
228 leftLayout = new QVBoxLayout;
229 leftLayout->addWidget(table);
231 QDialogButtonBox * buttonBoxUp = new QDialogButtonBox(Qt::Vertical);
232 buttonBoxUp->addButton(pushButtonUp, QDialogButtonBox::ActionRole);
233 buttonBoxUp->addButton(pushButtonDown, QDialogButtonBox::ActionRole);
234 buttonBoxUp->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
236 QDialogButtonBox * buttonBoxDown = new QDialogButtonBox(Qt::Vertical);
237 buttonBoxDown->addButton(pushButtonFinish, QDialogButtonBox::ActionRole);
239 rightLayout = new QVBoxLayout;
240 rightLayout->addWidget(buttonBoxUp);
241 rightLayout->addStretch();
242 rightLayout->addWidget(buttonBoxDown);
244 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
245 mainLayout->addLayout(leftLayout);
246 mainLayout->addLayout(rightLayout);
247 setLayout(mainLayout);
250 void ScoreDialog::createTable(QWidget *parent)
253 table = new QTableWidget(ROWS, COLS, parent);
255 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
256 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
257 table->horizontalHeader()->hide();
259 table->setStyleSheet(defaultStyleSheet);
262 headers << "" << "Par" << "HCP" << "Score" << "" << "Par" << "HCP" << "Score";
263 table->setVerticalHeaderLabels(headers);
266 void ScoreDialog::createButton(QWidget *parent)
270 pushButtonUp = new QPushButton(tr("+"));
271 connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
273 pushButtonDown = new QPushButton(tr("-"));
274 connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
276 pushButtonNext = new QPushButton(tr("Next"));
277 connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
279 pushButtonFinish = new QPushButton(tr("Finish"));
280 connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
283 void ScoreDialog::init(Course *course, Score *score)
286 QTableWidgetItem *par, *hcp, *scoreItem, *holeNum;
288 for (int i = 0; i < 18; i++) {
289 par = new QTableWidgetItem(course->getPar(i));
290 hcp = new QTableWidgetItem(course->getHcp(i));
292 scoreItem = new QTableWidgetItem(score->getScore(i));
294 scoreItem = new QTableWidgetItem("");
295 holeNum = new QTableWidgetItem(QString::number(i+1));
297 holeNum->setTextAlignment(Qt::AlignCenter);
298 holeNum->setFlags(Qt::NoItemFlags);
299 holeNum->setForeground(ScoreColor::holeBg());
301 par->setTextAlignment(Qt::AlignCenter);
302 par->setFlags(Qt::NoItemFlags);
304 hcp->setTextAlignment(Qt::AlignCenter);
305 hcp->setFlags(Qt::NoItemFlags);
307 scoreItem->setTextAlignment(Qt::AlignCenter);
310 table->setItem(ROW_HOLE, i, holeNum);
311 table->setItem(ROW_PAR, i, par);
312 table->setItem(ROW_HCP, i, hcp);
313 table->setItem(ROW_SCORE, i, scoreItem);
316 table->setItem(ROW_HOLE_2, i-9, holeNum);
317 table->setItem(ROW_PAR_2, i-9, par);
318 table->setItem(ROW_HCP_2, i-9, hcp);
319 table->setItem(ROW_SCORE_2, i-9, scoreItem);
323 // Set focus to 1st cell
324 table->setCurrentCell(ROW_SCORE, 0);
326 setDefaultScore(table);
329 // Set default score to par if not set
330 void ScoreDialog::setDefaultScore(QTableWidget *table)
332 int row = table->currentRow();
333 int col = table->currentColumn();
335 if (row == ROW_SCORE)
337 else if (row == ROW_SCORE_2)
340 qDebug() << "ERROR: unknown row in default score";
343 QTableWidgetItem *par = table->item(row, col);
344 QTableWidgetItem *score = table->item(row + 2, col);
346 if (par && score && score->text() == "") {
347 QVariant value(par->text());
348 score->setData(Qt::DisplayRole, value);
352 void ScoreDialog::up(void)
354 QTableWidgetItem *item = table->currentItem();
357 qWarning() << "ERROR: no current item";
361 int i = (item->text()).toInt();
363 item->setData(Qt::DisplayRole, value);
366 void ScoreDialog::down(void)
368 QTableWidgetItem *item = table->currentItem();
373 int i = (item->text()).toInt();
375 item->setData(Qt::DisplayRole, value);
378 void ScoreDialog::next(void)
381 QTableWidgetItem *item = table->currentItem();
382 moveToNextCell(item);
383 setDefaultScore(table);
387 void ScoreDialog::moveToNextCell(QTableWidgetItem *item)
392 QTableWidget *table = item->tableWidget();
397 int row = table->currentRow();
398 int col = table->currentColumn();
400 if (col < (COLS-1)) {
403 else if (col == (COLS-1)) {
405 row = (row == ROW_SCORE_2) ? ROW_SCORE : ROW_SCORE_2;
407 table->setCurrentCell(row, col);
410 void ScoreDialog::results(QVector<QString> &scores)
413 for (int i = 0; i < 9; i++) {
414 QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
415 QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
418 scores[i] = frontItem->text();
421 scores[i+9] = backItem->text();
425 bool ScoreDialog::validate(void)
427 for (int i=0; i<9; i++) {
428 QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
429 QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
431 if (!frontItem || !backItem)
434 QString str1 = frontItem->text();
435 QString str2 = backItem->text();
437 if (str1.isEmpty() || str2.isEmpty())
443 void ScoreDialog::finish(void)
448 showNote("Invalid data - cancel or correct");
452 void ScoreDialog::reject(void)
457 void ScoreDialog::showNote(QString msg)
460 QMaemo5InformationBox::information(this,
462 QMaemo5InformationBox::DefaultTimeout);