- Use QDialogButtonBox to get correct layout for buttons
[scorecard] / src / score-dialog.cpp
1 /*
2  * Copyright (C) 2009 Sakari Poussa
3  *
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.
7  */
8
9 #include <QtGui>
10 #include <QInputContext>
11 #ifdef Q_WS_MAEMO_5
12 #include <QMaemo5InformationBox>
13 #endif
14
15 #include "score-dialog.h"
16 #include "score-common.h"
17
18 SelectDialog::SelectDialog(QWidget *parent) : QDialog(parent)
19 {
20   resize(800, 350);
21
22   QWidget *centralWidget = new QWidget(this);
23   createLayout(centralWidget);
24
25   setWindowTitle(tr("ScoreCard: Select Course and Date"));
26 }
27
28 void SelectDialog::createLayout(QWidget *parent)
29 {
30   listClub = new QListWidget(parent);
31   pushButtonNext = new QPushButton(tr("Next"));
32
33   connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
34
35   QDialogButtonBox * buttonBox = new QDialogButtonBox(Qt::Vertical);
36   buttonBox->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
37
38   leftLayout = new QVBoxLayout;
39   leftLayout->addWidget(listClub);
40
41 #ifdef Q_WS_MAEMO_5
42   dateButton = new QMaemo5ValueButton();
43   dateButton->setValueLayout(QMaemo5ValueButton::ValueUnderText);
44   dateButton->setPickSelector(new QMaemo5DatePickSelector());
45   dateButton->setText(QString::fromUtf8("Date"));
46   leftLayout->addWidget(dateButton);
47 #else
48   QDate today(QDate::currentDate());
49   lineEditDate = new QLineEdit;
50   lineEditDate->setText(today.toString("yyyy-MM-dd"));
51   date = new QDateEdit;
52   leftLayout->addWidget(date);
53   leftLayout->addWidget(lineEditDate);
54 #endif
55
56   rightLayout = new QVBoxLayout;
57   rightLayout->addStretch();
58   rightLayout->addWidget(buttonBox);
59
60   QHBoxLayout *mainLayout = new QHBoxLayout(parent);
61   mainLayout->addLayout(leftLayout);
62   mainLayout->addLayout(rightLayout);
63
64   setLayout(mainLayout);
65 }
66
67 void SelectDialog::init(QList<Club *> &list)
68 {
69   clubList = list;
70
71   QListIterator<Club *> i(clubList);
72   int index = 0;
73
74   while (i.hasNext()) {
75     Club *club = i.next();
76
77     QList<Course *> courseList = club->getCourseList();
78
79     QListIterator<Course *> j(courseList);
80     while (j.hasNext()) {
81       Course *course = j.next();
82
83       QListWidgetItem *newItem = new QListWidgetItem;
84
85       QString entry = club->getName() + "," + course->getName();
86
87       newItem->setText(entry);
88       listClub->insertItem(index, newItem);
89
90       index++;
91     }
92   }
93 }
94
95 void SelectDialog::results(QString &club, 
96                            QString &course, 
97                            QString &date)
98 {  
99   QListWidgetItem *current = listClub->currentItem();
100
101   if (current) {
102     QString tmp = current->text();
103
104     QStringList stringList = tmp.split(",");
105     club = stringList[0];
106     course = stringList[1];
107 #ifdef Q_WS_MAEMO_5
108     QMaemo5DatePickSelector *sel = (QMaemo5DatePickSelector *)dateButton->pickSelector();
109     QDate d = sel->currentDate();
110     // TODO: change to QDate
111     date = d.toString(Qt::ISODate);
112 #else
113     date = lineEditDate->text();
114 #endif
115   }
116 }
117
118 bool SelectDialog::validate(void)
119 {
120   return true;
121 }
122
123 void SelectDialog::next(void)
124 {
125   if (validate())
126     done(1);
127   else {
128     qDebug() << "SelectDialog: invalid data, cancel or correct";
129   }
130 }
131
132 void SelectDialog::reject(void)
133 {
134   done(0);
135 }
136
137 ////////////////////////////////////////////////////////////////////////////////
138 ////////////////////////////////////////////////////////////////////////////////
139 ////////////////////////////////////////////////////////////////////////////////
140
141 ScoreDialog::ScoreDialog(QWidget *parent) : QDialog(parent)
142 {
143   resize(800, 400);
144
145   QWidget *centralWidget = new QWidget(this);
146
147   createTable();
148   createButton();
149
150   createLayout(centralWidget);
151 }
152
153 void ScoreDialog::createLayout(QWidget *parent)
154 {
155   leftLayout = new QVBoxLayout;
156   leftLayout->addWidget(table);
157
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);
162
163   QDialogButtonBox * buttonBoxDown = new QDialogButtonBox(Qt::Vertical);
164   buttonBoxDown->addButton(pushButtonFinish, QDialogButtonBox::ActionRole);
165
166   rightLayout = new QVBoxLayout;
167   rightLayout->addWidget(buttonBoxUp);
168   rightLayout->addStretch();
169   rightLayout->addWidget(buttonBoxDown);
170
171   QHBoxLayout *mainLayout = new QHBoxLayout(parent);
172   mainLayout->addLayout(leftLayout);
173   mainLayout->addLayout(rightLayout);
174   setLayout(mainLayout);
175 }
176
177 void ScoreDialog::createTable(QWidget *parent)
178 {
179   table = new QTableWidget(ROWS, COLS, parent);
180
181   table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
182   table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
183   table->horizontalHeader()->hide();
184
185   table->setStyleSheet(ScoreStyle::style());
186
187   QStringList headers;
188   headers << "" << "Par" << "HCP" << "Score" << "" << "Par" << "HCP" << "Score";
189   table->setVerticalHeaderLabels(headers);
190 }
191
192 void ScoreDialog::createButton(QWidget *parent)
193 {
194   Q_UNUSED(parent);
195   pushButtonUp = new QPushButton(tr("+"));
196   connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
197
198   pushButtonDown = new QPushButton(tr("-"));
199   connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
200
201   pushButtonNext = new QPushButton(tr("Next"));
202   connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
203
204   pushButtonFinish = new QPushButton(tr("Finish"));
205   connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
206 }
207
208 void ScoreDialog::init(Course *course, Score *score)
209 {
210   QTableWidgetItem *par, *hcp, *scoreItem, *holeNum;
211
212   for (int i = 0; i < 18; i++) {
213     par = new QTableWidgetItem(course->getPar(i));
214     hcp = new QTableWidgetItem(course->getHcp(i));
215     if (score)
216       scoreItem = new QTableWidgetItem(score->getScore(i));
217     else
218       scoreItem = new QTableWidgetItem("");
219     holeNum = new QTableWidgetItem(QString::number(i+1));
220
221     holeNum->setTextAlignment(Qt::AlignCenter);
222     holeNum->setFlags(Qt::NoItemFlags);
223     holeNum->setForeground(ScoreColor::holeBg());
224
225     par->setTextAlignment(Qt::AlignCenter);
226     par->setFlags(Qt::NoItemFlags);
227
228     hcp->setTextAlignment(Qt::AlignCenter);
229     hcp->setFlags(Qt::NoItemFlags);
230
231     scoreItem->setTextAlignment(Qt::AlignCenter);
232
233     if (i < 9) {
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);
238     }
239     else {
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);
244     }
245   }
246
247   // Set focus to 1st cell
248   table->setCurrentCell(ROW_SCORE, 0);
249   if (!score)
250     setDefaultScore(table);
251 }
252
253 // Set default score to par if not set
254 void ScoreDialog::setDefaultScore(QTableWidget *table)
255 {
256   int row = table->currentRow();
257   int col = table->currentColumn();
258   
259   if (row == ROW_SCORE)
260     row = ROW_PAR;
261   else if (row == ROW_SCORE_2)
262     row = ROW_PAR_2;
263   else {
264     qDebug() << "ERROR: unknown row in default score";
265     return;
266   }
267   QTableWidgetItem *par = table->item(row, col);
268   QTableWidgetItem *score = table->item(row + 2, col);
269
270   if (par && score && score->text() == "") {
271     QVariant value(par->text());
272     score->setData(Qt::DisplayRole, value);
273   }
274 }
275
276 void ScoreDialog::up(void)
277 {
278   QTableWidgetItem *item = table->currentItem();
279
280   if (!item) {
281     qWarning() << "ERROR: no current item";
282     return;
283   }
284
285   int i = (item->text()).toInt();
286   QVariant value(i+1);
287   item->setData(Qt::DisplayRole, value);
288 }
289
290 void ScoreDialog::down(void)
291 {
292   QTableWidgetItem *item = table->currentItem();
293
294   if (!item)
295     return;
296
297   int i = (item->text()).toInt();
298   QVariant value(i-1);
299   item->setData(Qt::DisplayRole, value);
300 }
301
302 void ScoreDialog::next(void)
303 {
304   if (table) {
305     QTableWidgetItem *item = table->currentItem();
306     moveToNextCell(item);
307     setDefaultScore(table);
308   }
309 }
310
311 void ScoreDialog::moveToNextCell(QTableWidgetItem *item)
312 {
313   if (!item)
314     return;
315
316   QTableWidget *table = item->tableWidget();
317
318   if (!table)
319     return;
320
321   int row = table->currentRow();
322   int col = table->currentColumn();
323
324   if (col < (COLS-1)) {
325     col++;
326   }
327   else if (col == (COLS-1)) {
328     col = 0;
329     row = (row == ROW_SCORE_2) ? ROW_SCORE : ROW_SCORE_2;
330   }
331   table->setCurrentCell(row, col);
332 }
333
334 void ScoreDialog::results(QVector<QString> &scores)
335 {
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);
339
340     if (frontItem)
341       scores[i] = frontItem->text();
342
343     if (backItem)
344       scores[i+9] = backItem->text();
345   }
346 }
347
348 bool ScoreDialog::validate(void)
349 {
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);
353     
354     if (!frontItem || !backItem)
355       return false;
356     
357     QString str1 = frontItem->text();
358     QString str2 = backItem->text();
359     
360     if (str1.isEmpty() || str2.isEmpty())
361       return false;
362   }
363   return true;
364 }
365
366 void ScoreDialog::finish(void)
367 {
368     if (validate())
369         done(1);
370     else {
371         showNote("Invalid data - cancel or correct");
372     }
373 }
374
375 void ScoreDialog::reject(void)
376 {
377   done(0);
378 }
379
380 void ScoreDialog::showNote(QString msg)
381 {
382 #ifdef Q_WS_MAEMO_5
383     QMaemo5InformationBox::information(this, 
384                                        msg,
385                                        QMaemo5InformationBox::DefaultTimeout);
386 #endif
387 }