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(ScoreStyle::style());
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 QWidget *centralWidget = new QWidget(this);
205 createLayout(centralWidget);
208 ScoreDialog::~ScoreDialog()
211 // delete centralWidget;
217 // delete mainLayout;
222 void ScoreDialog::createLayout(QWidget *parent)
225 leftLayout = new QVBoxLayout;
226 leftLayout->addWidget(table);
228 QDialogButtonBox * buttonBoxUp = new QDialogButtonBox(Qt::Vertical);
229 buttonBoxUp->addButton(pushButtonUp, QDialogButtonBox::ActionRole);
230 buttonBoxUp->addButton(pushButtonDown, QDialogButtonBox::ActionRole);
231 buttonBoxUp->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
233 QDialogButtonBox * buttonBoxDown = new QDialogButtonBox(Qt::Vertical);
234 buttonBoxDown->addButton(pushButtonFinish, QDialogButtonBox::ActionRole);
236 rightLayout = new QVBoxLayout;
237 rightLayout->addWidget(buttonBoxUp);
238 rightLayout->addStretch();
239 rightLayout->addWidget(buttonBoxDown);
241 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
242 mainLayout->addLayout(leftLayout);
243 mainLayout->addLayout(rightLayout);
244 setLayout(mainLayout);
247 void ScoreDialog::createTable(QWidget *parent)
250 table = new QTableWidget(ROWS, COLS, parent);
252 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
253 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
254 table->horizontalHeader()->hide();
256 table->setStyleSheet(ScoreStyle::style());
259 headers << "" << "Par" << "HCP" << "Score" << "" << "Par" << "HCP" << "Score";
260 table->setVerticalHeaderLabels(headers);
263 void ScoreDialog::createButton(QWidget *parent)
267 pushButtonUp = new QPushButton(tr("+"));
268 connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
270 pushButtonDown = new QPushButton(tr("-"));
271 connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
273 pushButtonNext = new QPushButton(tr("Next"));
274 connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
276 pushButtonFinish = new QPushButton(tr("Finish"));
277 connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
280 void ScoreDialog::init(Course *course, Score *score)
283 QTableWidgetItem *par, *hcp, *scoreItem, *holeNum;
285 for (int i = 0; i < 18; i++) {
286 par = new QTableWidgetItem(course->getPar(i));
287 hcp = new QTableWidgetItem(course->getHcp(i));
289 scoreItem = new QTableWidgetItem(score->getScore(i));
291 scoreItem = new QTableWidgetItem("");
292 holeNum = new QTableWidgetItem(QString::number(i+1));
294 holeNum->setTextAlignment(Qt::AlignCenter);
295 holeNum->setFlags(Qt::NoItemFlags);
296 holeNum->setForeground(ScoreColor::holeBg());
298 par->setTextAlignment(Qt::AlignCenter);
299 par->setFlags(Qt::NoItemFlags);
301 hcp->setTextAlignment(Qt::AlignCenter);
302 hcp->setFlags(Qt::NoItemFlags);
304 scoreItem->setTextAlignment(Qt::AlignCenter);
307 table->setItem(ROW_HOLE, i, holeNum);
308 table->setItem(ROW_PAR, i, par);
309 table->setItem(ROW_HCP, i, hcp);
310 table->setItem(ROW_SCORE, i, scoreItem);
313 table->setItem(ROW_HOLE_2, i-9, holeNum);
314 table->setItem(ROW_PAR_2, i-9, par);
315 table->setItem(ROW_HCP_2, i-9, hcp);
316 table->setItem(ROW_SCORE_2, i-9, scoreItem);
320 // Set focus to 1st cell
321 table->setCurrentCell(ROW_SCORE, 0);
323 setDefaultScore(table);
326 // Set default score to par if not set
327 void ScoreDialog::setDefaultScore(QTableWidget *table)
329 int row = table->currentRow();
330 int col = table->currentColumn();
332 if (row == ROW_SCORE)
334 else if (row == ROW_SCORE_2)
337 qDebug() << "ERROR: unknown row in default score";
340 QTableWidgetItem *par = table->item(row, col);
341 QTableWidgetItem *score = table->item(row + 2, col);
343 if (par && score && score->text() == "") {
344 QVariant value(par->text());
345 score->setData(Qt::DisplayRole, value);
349 void ScoreDialog::up(void)
351 QTableWidgetItem *item = table->currentItem();
354 qWarning() << "ERROR: no current item";
358 int i = (item->text()).toInt();
360 item->setData(Qt::DisplayRole, value);
363 void ScoreDialog::down(void)
365 QTableWidgetItem *item = table->currentItem();
370 int i = (item->text()).toInt();
372 item->setData(Qt::DisplayRole, value);
375 void ScoreDialog::next(void)
378 QTableWidgetItem *item = table->currentItem();
379 moveToNextCell(item);
380 setDefaultScore(table);
384 void ScoreDialog::moveToNextCell(QTableWidgetItem *item)
389 QTableWidget *table = item->tableWidget();
394 int row = table->currentRow();
395 int col = table->currentColumn();
397 if (col < (COLS-1)) {
400 else if (col == (COLS-1)) {
402 row = (row == ROW_SCORE_2) ? ROW_SCORE : ROW_SCORE_2;
404 table->setCurrentCell(row, col);
407 void ScoreDialog::results(QVector<QString> &scores)
410 for (int i = 0; i < 9; i++) {
411 QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
412 QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
415 scores[i] = frontItem->text();
418 scores[i+9] = backItem->text();
422 bool ScoreDialog::validate(void)
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);
428 if (!frontItem || !backItem)
431 QString str1 = frontItem->text();
432 QString str2 = backItem->text();
434 if (str1.isEmpty() || str2.isEmpty())
440 void ScoreDialog::finish(void)
445 showNote("Invalid data - cancel or correct");
449 void ScoreDialog::reject(void)
454 void ScoreDialog::showNote(QString msg)
457 QMaemo5InformationBox::information(this,
459 QMaemo5InformationBox::DefaultTimeout);