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