Use common colors and style sheets for all windows and dialogs
[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   lineEditDate = new QLineEdit;
27   pushButtonNext = new QPushButton(tr("Next"));
28
29   QDate today(QDate::currentDate());
30   lineEditDate->setText(today.toString("yyyy-MM-dd"));
31   date = new QDateEdit;
32
33   connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
34
35   leftLayout = new QVBoxLayout;
36   leftLayout->addWidget(listClub);
37   //leftLayout->addWidget(date);
38   leftLayout->addWidget(lineEditDate);
39
40   rightLayout = new QVBoxLayout;
41   rightLayout->addStretch();
42   rightLayout->addWidget(pushButtonNext);
43
44   QHBoxLayout *mainLayout = new QHBoxLayout(parent);
45   mainLayout->addLayout(leftLayout);
46   mainLayout->addLayout(rightLayout);
47
48   setLayout(mainLayout);
49 }
50
51 void SelectDialog::init(QList<Club *> &list)
52 {
53   clubList = list;
54
55   QListIterator<Club *> i(clubList);
56   int index = 0;
57
58   while (i.hasNext()) {
59     Club *club = i.next();
60
61     QList<Course *> courseList = club->getCourseList();
62
63     QListIterator<Course *> j(courseList);
64     while (j.hasNext()) {
65       Course *course = j.next();
66
67       QListWidgetItem *newItem = new QListWidgetItem;
68
69       QString entry = club->getName() + "," + course->getName();
70
71       newItem->setText(entry);
72       listClub->insertItem(index, newItem);
73
74       index++;
75     }
76   }
77 }
78
79 void SelectDialog::results(QString &club, 
80                            QString &course, 
81                            QString &date)
82 {  
83   QListWidgetItem *current = listClub->currentItem();
84
85   if (current) {
86     QString tmp = current->text();
87
88     QStringList stringList = tmp.split(",");
89     club = stringList[0];
90     course = stringList[1];
91     date   = lineEditDate->text();
92   }
93 }
94
95 bool SelectDialog::validate(void)
96 {
97   return true;
98 }
99
100 void SelectDialog::next(void)
101 {
102   if (validate())
103     done(1);
104   else {
105     qDebug() << "SelectDialog: invalid data, cancel or correct";
106   }
107 }
108
109 void SelectDialog::reject(void)
110 {
111   done(0);
112 }
113
114 ////////////////////////////////////////////////////////////////////////////////
115 ////////////////////////////////////////////////////////////////////////////////
116 ////////////////////////////////////////////////////////////////////////////////
117
118 ScoreDialog::ScoreDialog(QWidget *parent) : QDialog(parent)
119 {
120   resize(800, 400);
121
122   QWidget *centralWidget = new QWidget(this);
123
124   createTable();
125   createButton();
126
127   createLayout(centralWidget);
128 }
129
130 void ScoreDialog::createLayout(QWidget *parent)
131 {
132   leftLayout = new QVBoxLayout;
133   leftLayout->addWidget(table);
134
135   rightLayout = new QVBoxLayout;
136   rightLayout->addWidget(pushButtonUp);
137   rightLayout->addWidget(pushButtonDown);
138   rightLayout->addWidget(pushButtonNext);
139   rightLayout->addStretch();
140   rightLayout->addWidget(pushButtonFinish);
141
142   QHBoxLayout *mainLayout = new QHBoxLayout(parent);
143   mainLayout->addLayout(leftLayout);
144   mainLayout->addLayout(rightLayout);
145   setLayout(mainLayout);
146 }
147
148 void ScoreDialog::createTable(QWidget *parent)
149 {
150   table = new QTableWidget(ROWS, COLS, parent);
151
152   table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
153   table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
154   table->horizontalHeader()->hide();
155
156   table->setStyleSheet(ScoreColor::styleSheet());
157
158   QStringList headers;
159   headers << "" << "Par" << "HCP" << "Score" << "" << "Par" << "HCP" << "Score";
160   table->setVerticalHeaderLabels(headers);
161 }
162
163 void ScoreDialog::createButton(QWidget *parent)
164 {
165   pushButtonUp = new QPushButton(tr("+"));
166   connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
167
168   pushButtonDown = new QPushButton(tr("-"));
169   connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
170
171   pushButtonNext = new QPushButton(tr("Next"));
172   connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
173
174   pushButtonFinish = new QPushButton(tr("Finish"));
175   connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
176 }
177
178 void ScoreDialog::init(Course *course, Score *score)
179 {
180   QTableWidgetItem *par, *hcp, *scoreItem, *holeNum;
181
182   for (int i = 0; i < 18; i++) {
183     par = new QTableWidgetItem(course->getPar(i));
184     hcp = new QTableWidgetItem(course->getHcp(i));
185     if (score)
186       scoreItem = new QTableWidgetItem(score->getScore(i));
187     else
188       scoreItem = new QTableWidgetItem("");
189     holeNum = new QTableWidgetItem(QString::number(i+1));
190
191     holeNum->setBackgroundColor(ScoreColor::holeBg());
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 }