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"
18 SelectDialog::SelectDialog(QWidget *parent) : QDialog(parent)
22 QWidget *centralWidget = new QWidget(this);
23 createLayout(centralWidget);
25 setWindowTitle(tr("ScoreCard: Select Course and Date"));
28 void SelectDialog::createLayout(QWidget *parent)
30 listClub = new QListWidget(parent);
31 pushButtonNext = new QPushButton(tr("Next"));
33 connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
35 QDialogButtonBox * buttonBox = new QDialogButtonBox(Qt::Vertical);
36 buttonBox->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
38 leftLayout = new QVBoxLayout;
39 leftLayout->addWidget(listClub);
42 dateButton = new QMaemo5ValueButton();
43 dateButton->setValueLayout(QMaemo5ValueButton::ValueUnderText);
44 dateButton->setPickSelector(new QMaemo5DatePickSelector());
45 dateButton->setText(QString::fromUtf8("Date"));
46 leftLayout->addWidget(dateButton);
48 QDate today(QDate::currentDate());
49 lineEditDate = new QLineEdit;
50 lineEditDate->setText(today.toString("yyyy-MM-dd"));
52 leftLayout->addWidget(date);
53 leftLayout->addWidget(lineEditDate);
56 rightLayout = new QVBoxLayout;
57 rightLayout->addStretch();
58 rightLayout->addWidget(buttonBox);
60 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
61 mainLayout->addLayout(leftLayout);
62 mainLayout->addLayout(rightLayout);
64 setLayout(mainLayout);
67 void SelectDialog::init(QList<Club *> &list)
71 QListIterator<Club *> i(clubList);
75 Club *club = i.next();
77 QList<Course *> courseList = club->getCourseList();
79 QListIterator<Course *> j(courseList);
81 Course *course = j.next();
83 QListWidgetItem *newItem = new QListWidgetItem;
85 QString entry = club->getName() + "," + course->getName();
87 newItem->setText(entry);
88 listClub->insertItem(index, newItem);
95 void SelectDialog::results(QString &club,
99 QListWidgetItem *current = listClub->currentItem();
102 QString tmp = current->text();
104 QStringList stringList = tmp.split(",");
105 club = stringList[0];
106 course = stringList[1];
108 QMaemo5DatePickSelector *sel = (QMaemo5DatePickSelector *)dateButton->pickSelector();
109 QDate d = sel->currentDate();
110 // TODO: change to QDate
111 date = d.toString(Qt::ISODate);
113 date = lineEditDate->text();
118 bool SelectDialog::validate(void)
123 void SelectDialog::next(void)
128 qDebug() << "SelectDialog: invalid data, cancel or correct";
132 void SelectDialog::reject(void)
137 ////////////////////////////////////////////////////////////////////////////////
138 ////////////////////////////////////////////////////////////////////////////////
139 ////////////////////////////////////////////////////////////////////////////////
141 ScoreDialog::ScoreDialog(QWidget *parent) : QDialog(parent)
145 QWidget *centralWidget = new QWidget(this);
150 createLayout(centralWidget);
153 void ScoreDialog::createLayout(QWidget *parent)
155 leftLayout = new QVBoxLayout;
156 leftLayout->addWidget(table);
158 QDialogButtonBox * buttonBoxUp = new QDialogButtonBox(Qt::Vertical);
159 buttonBoxUp->addButton(pushButtonUp, QDialogButtonBox::ActionRole);
160 buttonBoxUp->addButton(pushButtonDown, QDialogButtonBox::ActionRole);
161 buttonBoxUp->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
163 QDialogButtonBox * buttonBoxDown = new QDialogButtonBox(Qt::Vertical);
164 buttonBoxDown->addButton(pushButtonFinish, QDialogButtonBox::ActionRole);
166 rightLayout = new QVBoxLayout;
167 rightLayout->addWidget(buttonBoxUp);
168 rightLayout->addStretch();
169 rightLayout->addWidget(buttonBoxDown);
171 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
172 mainLayout->addLayout(leftLayout);
173 mainLayout->addLayout(rightLayout);
174 setLayout(mainLayout);
177 void ScoreDialog::createTable(QWidget *parent)
179 table = new QTableWidget(ROWS, COLS, parent);
181 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
182 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
183 table->horizontalHeader()->hide();
185 table->setStyleSheet(ScoreStyle::style());
188 headers << "" << "Par" << "HCP" << "Score" << "" << "Par" << "HCP" << "Score";
189 table->setVerticalHeaderLabels(headers);
192 void ScoreDialog::createButton(QWidget *parent)
195 pushButtonUp = new QPushButton(tr("+"));
196 connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
198 pushButtonDown = new QPushButton(tr("-"));
199 connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
201 pushButtonNext = new QPushButton(tr("Next"));
202 connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
204 pushButtonFinish = new QPushButton(tr("Finish"));
205 connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
208 void ScoreDialog::init(Course *course, Score *score)
210 QTableWidgetItem *par, *hcp, *scoreItem, *holeNum;
212 for (int i = 0; i < 18; i++) {
213 par = new QTableWidgetItem(course->getPar(i));
214 hcp = new QTableWidgetItem(course->getHcp(i));
216 scoreItem = new QTableWidgetItem(score->getScore(i));
218 scoreItem = new QTableWidgetItem("");
219 holeNum = new QTableWidgetItem(QString::number(i+1));
221 holeNum->setTextAlignment(Qt::AlignCenter);
222 holeNum->setFlags(Qt::NoItemFlags);
223 holeNum->setForeground(ScoreColor::holeBg());
225 par->setTextAlignment(Qt::AlignCenter);
226 par->setFlags(Qt::NoItemFlags);
228 hcp->setTextAlignment(Qt::AlignCenter);
229 hcp->setFlags(Qt::NoItemFlags);
231 scoreItem->setTextAlignment(Qt::AlignCenter);
234 table->setItem(ROW_HOLE, i, holeNum);
235 table->setItem(ROW_PAR, i, par);
236 table->setItem(ROW_HCP, i, hcp);
237 table->setItem(ROW_SCORE, i, scoreItem);
240 table->setItem(ROW_HOLE_2, i-9, holeNum);
241 table->setItem(ROW_PAR_2, i-9, par);
242 table->setItem(ROW_HCP_2, i-9, hcp);
243 table->setItem(ROW_SCORE_2, i-9, scoreItem);
247 // Set focus to 1st cell
248 table->setCurrentCell(ROW_SCORE, 0);
250 setDefaultScore(table);
253 // Set default score to par if not set
254 void ScoreDialog::setDefaultScore(QTableWidget *table)
256 int row = table->currentRow();
257 int col = table->currentColumn();
259 if (row == ROW_SCORE)
261 else if (row == ROW_SCORE_2)
264 qDebug() << "ERROR: unknown row in default score";
267 QTableWidgetItem *par = table->item(row, col);
268 QTableWidgetItem *score = table->item(row + 2, col);
270 if (par && score && score->text() == "") {
271 QVariant value(par->text());
272 score->setData(Qt::DisplayRole, value);
276 void ScoreDialog::up(void)
278 QTableWidgetItem *item = table->currentItem();
281 qWarning() << "ERROR: no current item";
285 int i = (item->text()).toInt();
287 item->setData(Qt::DisplayRole, value);
290 void ScoreDialog::down(void)
292 QTableWidgetItem *item = table->currentItem();
297 int i = (item->text()).toInt();
299 item->setData(Qt::DisplayRole, value);
302 void ScoreDialog::next(void)
305 QTableWidgetItem *item = table->currentItem();
306 moveToNextCell(item);
307 setDefaultScore(table);
311 void ScoreDialog::moveToNextCell(QTableWidgetItem *item)
316 QTableWidget *table = item->tableWidget();
321 int row = table->currentRow();
322 int col = table->currentColumn();
324 if (col < (COLS-1)) {
327 else if (col == (COLS-1)) {
329 row = (row == ROW_SCORE_2) ? ROW_SCORE : ROW_SCORE_2;
331 table->setCurrentCell(row, col);
334 void ScoreDialog::results(QVector<QString> &scores)
336 for (int i = 0; i < 9; i++) {
337 QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
338 QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
341 scores[i] = frontItem->text();
344 scores[i+9] = backItem->text();
348 bool ScoreDialog::validate(void)
350 for (int i=0; i<9; i++) {
351 QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
352 QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
354 if (!frontItem || !backItem)
357 QString str1 = frontItem->text();
358 QString str2 = backItem->text();
360 if (str1.isEmpty() || str2.isEmpty())
366 void ScoreDialog::finish(void)
371 showNote("Invalid data - cancel or correct");
375 void ScoreDialog::reject(void)
380 void ScoreDialog::showNote(QString msg)
383 QMaemo5InformationBox::information(this,
385 QMaemo5InformationBox::DefaultTimeout);