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