- Use QDialogButtonBox to get correct layout for buttons
[scorecard] / src / course-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 <QLayout>
11 #include <QLineEdit>
12 #include <QInputContext>
13 #include <QtGui/QTableWidget>
14
15 #include "course-dialog.h"
16 #include "score-common.h"
17
18 CourseSelectDialog::CourseSelectDialog(QWidget *parent) : QDialog(parent)
19 {
20   QWidget *centralWidget = new QWidget(this);
21   createLayout(centralWidget);
22
23   setWindowTitle(tr("ScoreCard: New Club and Course"));
24 }
25
26 void CourseSelectDialog::createLayout(QWidget *parent)
27 {
28   labelClub = new QLabel(tr("Club"));
29   labelCourse = new QLabel(tr("Course"));
30   lineEditClub = new QLineEdit;
31   lineEditCourse = new QLineEdit;
32   pushButtonNext = new QPushButton(tr("Next"));
33
34   connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
35
36   QDialogButtonBox * buttonBox = new QDialogButtonBox(Qt::Vertical);
37   buttonBox->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
38
39   leftLayout = new QVBoxLayout;
40   leftLayout->addWidget(lineEditClub);
41   leftLayout->addWidget(lineEditCourse);
42
43   rightLayout = new QVBoxLayout;
44   rightLayout->addStretch();
45   rightLayout->addWidget(buttonBox);
46
47   QHBoxLayout *mainLayout = new QHBoxLayout(parent);
48   mainLayout->addLayout(leftLayout);
49   mainLayout->addLayout(rightLayout);
50
51   setLayout(mainLayout);
52 }
53
54 void CourseSelectDialog::results(QString &club, 
55                                  QString &course)
56 {  
57   club = lineEditClub->text();
58   course = lineEditCourse->text();
59 }
60
61 bool CourseSelectDialog::validate(void)
62 {
63   QString str1 = lineEditClub->text();
64   QString str2 = lineEditCourse->text();
65
66   if (str1.isEmpty() || str2.isEmpty())
67     return false;
68   
69   return true;
70 }
71
72 void CourseSelectDialog::next(void)
73 {
74   if (validate())
75     done(1);
76   else {
77     qDebug() << "CourseDialog: invalid data, cancel or correct";
78   }
79 }
80
81 ////////////////////////////////////////////////////////////////////////////////
82 ////////////////////////////////////////////////////////////////////////////////
83 ////////////////////////////////////////////////////////////////////////////////
84
85 CourseDialog::CourseDialog(QWidget *parent) : QDialog(parent)
86 {
87   resize(800, 400);
88
89   QWidget *centralWidget = new QWidget(this);
90
91   createTable();
92   createButton();
93
94   createLayout(centralWidget);
95 }
96
97 void CourseDialog::createLayout(QWidget *parent)
98 {
99   leftLayout = new QVBoxLayout;
100   leftLayout->addWidget(table);
101
102   QDialogButtonBox * buttonBoxUp = new QDialogButtonBox(Qt::Vertical);
103   buttonBoxUp->addButton(pushButtonUp, QDialogButtonBox::ActionRole);
104   buttonBoxUp->addButton(pushButtonDown, QDialogButtonBox::ActionRole);
105   buttonBoxUp->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
106
107   QDialogButtonBox * buttonBoxDown = new QDialogButtonBox(Qt::Vertical);
108   buttonBoxDown->addButton(pushButtonFinish, QDialogButtonBox::ActionRole);
109
110   rightLayout = new QVBoxLayout;
111   rightLayout->addWidget(buttonBoxUp);
112   rightLayout->addStretch();
113   rightLayout->addWidget(buttonBoxDown);
114
115   QHBoxLayout *mainLayout = new QHBoxLayout(parent);
116   mainLayout->addLayout(leftLayout);
117   mainLayout->addLayout(rightLayout);
118   setLayout(mainLayout);
119 }
120
121 void CourseDialog::createButton(QWidget *parent)
122 {
123   Q_UNUSED(parent);
124   pushButtonUp = new QPushButton(tr("+"));
125   connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
126
127   pushButtonDown = new QPushButton(tr("-"));
128   connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
129
130   pushButtonNext = new QPushButton(tr("Next"));
131   connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
132
133   pushButtonFinish = new QPushButton(tr("Finish"));
134   connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
135 }
136
137 void CourseDialog::createTable(QWidget *parent)
138 {
139   table = new QTableWidget(ROWS, COLS, parent);
140
141   QStringList headers;
142   headers << "" << "Par" << "HCP" << "Len" << "" << "Par" << "HCP" << "Len";
143   table->setVerticalHeaderLabels(headers);
144   table->horizontalHeader()->hide();
145   table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
146   table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
147   table->setStyleSheet(ScoreStyle::style());
148 }
149
150 void CourseDialog::init(Course *course)
151 {
152   QTableWidgetItem *par, *hcp;
153
154   for (int i=0; i<18; i++) {
155     if (course) {
156       par = new QTableWidgetItem(course->getPar(i));
157       hcp = new QTableWidgetItem(course->getHcp(i));
158     }
159     else {
160       par = new QTableWidgetItem("4");
161       hcp = new QTableWidgetItem("9");
162     }
163     QTableWidgetItem *len = new QTableWidgetItem("");
164
165     QTableWidgetItem *holeNum = new QTableWidgetItem(QString::number(i+1));
166     holeNum->setForeground(ScoreColor::holeBg());
167     holeNum->setFlags(Qt::NoItemFlags);
168
169     holeNum->setTextAlignment(Qt::AlignCenter);
170     par->setTextAlignment(Qt::AlignCenter);
171     hcp->setTextAlignment(Qt::AlignCenter);
172     len->setTextAlignment(Qt::AlignCenter);
173     // len is not in use - here just to confuse ;(
174     len->setFlags(Qt::NoItemFlags);
175     
176     if (i < 9) {
177       table->setItem(0, i, holeNum);
178       table->setItem(1, i, par);
179       table->setItem(2, i, hcp);
180       table->setItem(3, i, len);
181     }
182     else {
183       table->setItem(4, i-9, holeNum);
184       table->setItem(5, i-9, par);
185       table->setItem(6, i-9, hcp);
186       table->setItem(7, i-9, len);
187     }
188   }
189   table->setCurrentCell(1, 0);
190 }
191
192 void CourseDialog::up(void)
193 {
194   QTableWidgetItem *item = table->currentItem();
195
196   if (!item)
197     return;
198
199   int i = (item->text()).toInt();
200   QVariant value(i+1);
201   item->setData(Qt::DisplayRole, value);
202 }
203
204 void CourseDialog::down(void)
205 {
206   QTableWidgetItem *item = table->currentItem();
207
208   if (!item)
209     return;
210
211   int i = (item->text()).toInt();
212   QVariant value(i-1);
213   item->setData(Qt::DisplayRole, value);
214 }
215
216 void CourseDialog::next(void)
217 {
218   if (table) {
219     QTableWidgetItem *item = table->currentItem();
220     moveToNextCell(item);
221     //setDefaultScore(table);
222   }
223 }
224
225 void CourseDialog::moveToNextCell(QTableWidgetItem *item)
226 {
227   if (!item)
228     return;
229
230   QTableWidget *table = item->tableWidget();
231
232   if (!table)
233     return;
234
235   int row = table->currentRow();
236   int col = table->currentColumn();
237
238   if (col < (COLS-1)) {
239     col++;
240   }
241   else if (col == (COLS-1)) {
242     col = 0;
243     row = (row == ROW_PAR_2) ? ROW_PAR : ROW_PAR_2;
244   }
245   //qDebug() << "new cell: " << row << "/" << col;
246   table->setCurrentCell(row, col);
247 }
248
249 void CourseDialog::results(QVector<QString> &par,
250                           QVector<QString> &hcp,
251                           QVector<QString> &len)
252 {
253   for (int i = 0; i < 9; i++) {
254     QTableWidgetItem *frontPar = table->item(ROW_PAR, i);
255     QTableWidgetItem *backPar = table->item(ROW_PAR_2, i);
256     QTableWidgetItem *frontHcp = table->item(ROW_HCP, i);
257     QTableWidgetItem *backHcp = table->item(ROW_HCP_2, i);
258     QTableWidgetItem *frontLen = table->item(ROW_LEN, i);
259     QTableWidgetItem *backLen = table->item(ROW_LEN_2, i);
260
261     if (frontPar)
262       par[i] = frontPar->text();
263     if (backPar)
264       par[i+9] = backPar->text();
265     if (frontHcp)
266       hcp[i] = frontHcp->text();
267     if (backHcp)
268       hcp[i+9] = backHcp->text();
269     if (frontLen)
270       len[i] = frontLen->text();
271     if (backLen)
272       len[i+9] = backLen->text();
273   }
274 }
275
276 // Only par is mandatory
277 bool CourseDialog::validate(void)
278 {
279   for (int i = 0; i < 9; i++) {
280     QTableWidgetItem *frontItem = table->item(ROW_PAR, i);
281     QTableWidgetItem *backItem = table->item(ROW_PAR_2, i);
282     
283     if (!frontItem || !backItem)
284       return false;
285     
286     QString str1 = frontItem->text();
287     QString str2 = backItem->text();
288     
289     if (str1.isEmpty() || str2.isEmpty())
290       return false;
291   }
292   return true;
293 }
294
295 void CourseDialog::finish(void)
296 {
297   if (validate())
298     done(1);
299   else {
300     qDebug() << "CourseDialog: invalid data, cancel or correct";
301   }
302 }
303
304