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