Merge branch 'master' of /opt/src/sb1/qt/scorecard
[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 #ifdef Q_WS_MAEMO_5
11 #include <QMaemo5InformationBox>
12 #endif
13 #include <QLayout>
14 #include <QLineEdit>
15 #include <QInputContext>
16 #include <QtGui/QTableWidget>
17
18 #include "course-dialog.h"
19 #include "score-common.h"
20 #include "table-model.h"
21
22 ////////////////////////////////////////////////////////////////////////////////
23 // CourseWindow based on QMainWindow
24 ////////////////////////////////////////////////////////////////////////////////
25 CourseWindow::CourseWindow(QWidget *parent) : QMainWindow(parent)
26 {
27 #ifdef Q_WS_MAEMO_5
28     setAttribute(Qt::WA_Maemo5StackedWindow);
29 #endif
30
31     QAction *editAction = new QAction(tr("Edit"), this);
32     connect(editAction, SIGNAL(triggered()), parent, SLOT(editCourse()));
33     menuBar()->addAction(editAction);
34
35     QAction *delAction = new QAction(tr("Delete"), this);
36     connect(delAction, SIGNAL(triggered()), parent, SLOT(deleteCourse()));
37     menuBar()->addAction(delAction);
38
39     model = new CourseTableModel;
40     
41     QTableView * table = new QTableView;
42     table->showGrid();
43     table->setSelectionMode(QAbstractItemView::NoSelection);
44     table->setStyleSheet(defaultStyleSheet);
45     table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
46     table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
47     table->horizontalHeader()->hide();
48     table->setModel(model);
49     
50     QWidget *central = new QWidget(this);
51     setCentralWidget(central);
52     
53     QVBoxLayout *layout = new QVBoxLayout;
54     layout->addWidget(table);
55     
56     central->setLayout(layout);
57 }
58
59 void CourseWindow::setup(Course *course)
60 {
61     QString title = QString("Course: %1, Par - %2").arg(course->getName()).arg(course->getTotal(Total));
62     setWindowTitle(title);
63
64     model->set(course);
65 }
66
67 ////////////////////////////////////////////////////////////////////////////////
68 // CourseSelectDialog based on QDialog
69 ////////////////////////////////////////////////////////////////////////////////
70 CourseSelectDialog::CourseSelectDialog(QWidget *parent) : QDialog(parent)
71 {
72     QWidget *centralWidget = new QWidget(this);
73     createLayout(centralWidget);
74
75     setWindowTitle(tr("ScoreCard: New Club and Course"));
76 }
77
78 void CourseSelectDialog::createLayout(QWidget *parent)
79 {
80     labelClub = new QLabel(tr("Club"));
81     labelCourse = new QLabel(tr("Course"));
82     lineEditClub = new QLineEdit;
83     lineEditCourse = new QLineEdit;
84     pushButtonNext = new QPushButton(tr("Next"));
85
86     connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
87
88     QDialogButtonBox * buttonBox = new QDialogButtonBox(Qt::Vertical);
89     buttonBox->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
90
91     leftLayout = new QVBoxLayout;
92     leftLayout->addWidget(lineEditClub);
93     leftLayout->addWidget(lineEditCourse);
94
95     rightLayout = new QVBoxLayout;
96     rightLayout->addStretch();
97     rightLayout->addWidget(buttonBox);
98
99     QHBoxLayout *mainLayout = new QHBoxLayout(parent);
100     mainLayout->addLayout(leftLayout);
101     mainLayout->addLayout(rightLayout);
102
103     setLayout(mainLayout);
104 }
105
106 void CourseSelectDialog::showNote(QString msg)
107 {
108 #ifdef Q_WS_MAEMO_5
109     QMaemo5InformationBox::information(this, 
110                                        msg,
111                                        QMaemo5InformationBox::DefaultTimeout);
112 #endif
113 }
114
115 void CourseSelectDialog::results(QString &club, 
116                                  QString &course)
117 {  
118     club = lineEditClub->text();
119     course = lineEditCourse->text();
120 }
121
122 bool CourseSelectDialog::validate(void)
123 {
124     QString str1 = lineEditClub->text();
125     QString str2 = lineEditCourse->text();
126
127     if (str1.isEmpty() || str2.isEmpty())
128         return false;
129   
130     if (str1.contains(",") || str2.contains(","))
131         return false;
132
133     return true;
134 }
135
136 void CourseSelectDialog::next(void)
137 {
138     if (validate())
139         done(1);
140     else {
141         showNote(tr("Invalid club or course name"));
142     }
143 }
144
145 ////////////////////////////////////////////////////////////////////////////////
146 // CourseDialog based on QDialog
147 ////////////////////////////////////////////////////////////////////////////////
148 CourseDialog::CourseDialog(QWidget *parent) : QDialog(parent)
149 {
150     resize(800, 400);
151
152     QWidget *centralWidget = new QWidget(this);
153
154     createTable();
155     createButton();
156
157     createLayout(centralWidget);
158 }
159
160 void CourseDialog::createLayout(QWidget *parent)
161 {
162     leftLayout = new QVBoxLayout;
163     leftLayout->addWidget(table);
164
165     QDialogButtonBox * buttonBoxUp = new QDialogButtonBox(Qt::Vertical);
166     buttonBoxUp->addButton(pushButtonUp, QDialogButtonBox::ActionRole);
167     buttonBoxUp->addButton(pushButtonDown, QDialogButtonBox::ActionRole);
168     buttonBoxUp->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
169
170     QDialogButtonBox * buttonBoxDown = new QDialogButtonBox(Qt::Vertical);
171     buttonBoxDown->addButton(pushButtonFinish, QDialogButtonBox::ActionRole);
172
173     rightLayout = new QVBoxLayout;
174     rightLayout->addWidget(buttonBoxUp);
175     rightLayout->addStretch();
176     rightLayout->addWidget(buttonBoxDown);
177
178     QHBoxLayout *mainLayout = new QHBoxLayout(parent);
179     mainLayout->addLayout(leftLayout);
180     mainLayout->addLayout(rightLayout);
181     setLayout(mainLayout);
182 }
183
184 void CourseDialog::createButton(QWidget *parent)
185 {
186     Q_UNUSED(parent);
187     pushButtonUp = new QPushButton(tr("+"));
188     connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
189
190     pushButtonDown = new QPushButton(tr("-"));
191     connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
192
193     pushButtonNext = new QPushButton(tr("Next"));
194     connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
195
196     pushButtonFinish = new QPushButton(tr("Finish"));
197     connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
198 }
199
200 void CourseDialog::createTable(QWidget *parent)
201 {
202     table = new QTableWidget(ROWS, COLS, parent);
203
204     QStringList headers;
205     headers << "" << "Par" << "HCP" << "Len" << "" << "Par" << "HCP" << "Len";
206     table->setVerticalHeaderLabels(headers);
207     table->horizontalHeader()->hide();
208     table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
209     table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
210     table->setStyleSheet(defaultStyleSheet);
211 }
212
213 void CourseDialog::init(Course *course)
214 {
215     QTableWidgetItem *par, *hcp;
216
217     for (int i=0; i<18; i++) {
218         if (course) {
219             // TODO: del or allocate statically...
220             par = new QTableWidgetItem(course->getPar(i));
221             hcp = new QTableWidgetItem(course->getHcp(i));
222         }
223         else {
224             par = new QTableWidgetItem("4");
225             if (i < 9)
226                 hcp = new QTableWidgetItem("9");
227             else
228                 hcp = new QTableWidgetItem("10");
229         }
230         QTableWidgetItem *len = new QTableWidgetItem("");
231
232         QTableWidgetItem *holeNum = new QTableWidgetItem(QString::number(i+1));
233         holeNum->setForeground(ScoreColor::holeBg());
234         holeNum->setFlags(Qt::NoItemFlags);
235
236         holeNum->setTextAlignment(Qt::AlignCenter);
237         par->setTextAlignment(Qt::AlignCenter);
238         hcp->setTextAlignment(Qt::AlignCenter);
239         len->setTextAlignment(Qt::AlignCenter);
240         // len is not in use - here just to confuse ;(
241         len->setFlags(Qt::NoItemFlags);
242     
243         if (i < 9) {
244             table->setItem(0, i, holeNum);
245             table->setItem(1, i, par);
246             table->setItem(2, i, hcp);
247             table->setItem(3, i, len);
248         }
249         else {
250             table->setItem(4, i-9, holeNum);
251             table->setItem(5, i-9, par);
252             table->setItem(6, i-9, hcp);
253             table->setItem(7, i-9, len);
254         }
255     }
256     table->setCurrentCell(1, 0);
257 }
258
259 void CourseDialog::up(void)
260 {
261     QVariant value;
262     QTableWidgetItem *item = table->currentItem();
263
264     if (!item)
265         return;
266
267     int i = (item->text()).toInt();
268     if (item->row() == ROW_HCP || item->row() == ROW_HCP_2)
269         value.setValue(i+2);
270     else
271         value.setValue(i+1);
272
273     item->setData(Qt::DisplayRole, value);
274 }
275
276 void CourseDialog::down(void)
277 {
278     QVariant value;
279     QTableWidgetItem *item = table->currentItem();
280
281     if (!item)
282         return;
283
284     int i = (item->text()).toInt();
285     if (item->row() == ROW_HCP || item->row() == ROW_HCP_2)
286         value.setValue(i-2);
287     else
288         value.setValue(i-1);
289
290     item->setData(Qt::DisplayRole, value);
291 }
292
293 void CourseDialog::next(void)
294 {
295     if (table) {
296         QTableWidgetItem *item = table->currentItem();
297         moveToNextCell(item);
298         //setDefaultScore(table);
299     }
300 }
301
302 void CourseDialog::moveToNextCell(QTableWidgetItem *item)
303 {
304     if (!item)
305         return;
306
307     QTableWidget *table = item->tableWidget();
308
309     if (!table)
310         return;
311
312     int row = table->currentRow();
313     int col = table->currentColumn();
314
315     if (col < (COLS-1)) {
316         col++;
317     }
318     else if (col == (COLS-1)) {
319         col = 0;
320         if (row == ROW_PAR)
321             row = ROW_PAR_2;
322         else if (row == ROW_PAR_2)
323             row = ROW_PAR;
324         else if (row == ROW_HCP_2)
325             row = ROW_HCP;
326         else if (row == ROW_HCP)
327             row = ROW_HCP_2;
328     }
329     //qDebug() << "new cell: " << row << "/" << col;
330     table->setCurrentCell(row, col);
331 }
332
333 void CourseDialog::results(QVector<QString> &par,
334                            QVector<QString> &hcp,
335                            QVector<QString> &len)
336 {
337     for (int i = 0; i < 9; i++) {
338         QTableWidgetItem *frontPar = table->item(ROW_PAR, i);
339         QTableWidgetItem *backPar = table->item(ROW_PAR_2, i);
340         QTableWidgetItem *frontHcp = table->item(ROW_HCP, i);
341         QTableWidgetItem *backHcp = table->item(ROW_HCP_2, i);
342         QTableWidgetItem *frontLen = table->item(ROW_LEN, i);
343         QTableWidgetItem *backLen = table->item(ROW_LEN_2, i);
344
345         if (frontPar)
346             par[i] = frontPar->text();
347         if (backPar)
348             par[i+9] = backPar->text();
349         if (frontHcp)
350             hcp[i] = frontHcp->text();
351         if (backHcp)
352             hcp[i+9] = backHcp->text();
353         if (frontLen)
354             len[i] = frontLen->text();
355         if (backLen)
356             len[i+9] = backLen->text();
357     }
358 }
359
360 // Only par is mandatory
361 bool CourseDialog::validate(void)
362 {
363     for (int i = 0; i < 9; i++) {
364         QTableWidgetItem *frontItem = table->item(ROW_PAR, i);
365         QTableWidgetItem *backItem = table->item(ROW_PAR_2, i);
366     
367         if (!frontItem || !backItem)
368             return false;
369     
370         QString str1 = frontItem->text();
371         QString str2 = backItem->text();
372     
373         if (str1.isEmpty() || str2.isEmpty())
374             return false;
375     }
376     return true;
377 }
378
379 void CourseDialog::finish(void)
380 {
381     if (validate())
382         done(1);
383     else {
384         qDebug() << "CourseDialog: invalid data, cancel or correct";
385     }
386 }
387
388