d8192ea0ea7e6ede3119503d8a910b93157fcad1
[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       par = new QTableWidgetItem(course->getPar(i));
205       hcp = new QTableWidgetItem(course->getHcp(i));
206     }
207     else {
208       par = new QTableWidgetItem("4");
209       hcp = new QTableWidgetItem("9");
210     }
211     QTableWidgetItem *len = new QTableWidgetItem("");
212
213     QTableWidgetItem *holeNum = new QTableWidgetItem(QString::number(i+1));
214     holeNum->setForeground(ScoreColor::holeBg());
215     holeNum->setFlags(Qt::NoItemFlags);
216
217     holeNum->setTextAlignment(Qt::AlignCenter);
218     par->setTextAlignment(Qt::AlignCenter);
219     hcp->setTextAlignment(Qt::AlignCenter);
220     len->setTextAlignment(Qt::AlignCenter);
221     // len is not in use - here just to confuse ;(
222     len->setFlags(Qt::NoItemFlags);
223     
224     if (i < 9) {
225       table->setItem(0, i, holeNum);
226       table->setItem(1, i, par);
227       table->setItem(2, i, hcp);
228       table->setItem(3, i, len);
229     }
230     else {
231       table->setItem(4, i-9, holeNum);
232       table->setItem(5, i-9, par);
233       table->setItem(6, i-9, hcp);
234       table->setItem(7, i-9, len);
235     }
236   }
237   table->setCurrentCell(1, 0);
238 }
239
240 void CourseDialog::up(void)
241 {
242   QTableWidgetItem *item = table->currentItem();
243
244   if (!item)
245     return;
246
247   int i = (item->text()).toInt();
248   QVariant value(i+1);
249   item->setData(Qt::DisplayRole, value);
250 }
251
252 void CourseDialog::down(void)
253 {
254   QTableWidgetItem *item = table->currentItem();
255
256   if (!item)
257     return;
258
259   int i = (item->text()).toInt();
260   QVariant value(i-1);
261   item->setData(Qt::DisplayRole, value);
262 }
263
264 void CourseDialog::next(void)
265 {
266   if (table) {
267     QTableWidgetItem *item = table->currentItem();
268     moveToNextCell(item);
269     //setDefaultScore(table);
270   }
271 }
272
273 void CourseDialog::moveToNextCell(QTableWidgetItem *item)
274 {
275   if (!item)
276     return;
277
278   QTableWidget *table = item->tableWidget();
279
280   if (!table)
281     return;
282
283   int row = table->currentRow();
284   int col = table->currentColumn();
285
286   if (col < (COLS-1)) {
287     col++;
288   }
289   else if (col == (COLS-1)) {
290     col = 0;
291     row = (row == ROW_PAR_2) ? ROW_PAR : ROW_PAR_2;
292   }
293   //qDebug() << "new cell: " << row << "/" << col;
294   table->setCurrentCell(row, col);
295 }
296
297 void CourseDialog::results(QVector<QString> &par,
298                           QVector<QString> &hcp,
299                           QVector<QString> &len)
300 {
301   for (int i = 0; i < 9; i++) {
302     QTableWidgetItem *frontPar = table->item(ROW_PAR, i);
303     QTableWidgetItem *backPar = table->item(ROW_PAR_2, i);
304     QTableWidgetItem *frontHcp = table->item(ROW_HCP, i);
305     QTableWidgetItem *backHcp = table->item(ROW_HCP_2, i);
306     QTableWidgetItem *frontLen = table->item(ROW_LEN, i);
307     QTableWidgetItem *backLen = table->item(ROW_LEN_2, i);
308
309     if (frontPar)
310       par[i] = frontPar->text();
311     if (backPar)
312       par[i+9] = backPar->text();
313     if (frontHcp)
314       hcp[i] = frontHcp->text();
315     if (backHcp)
316       hcp[i+9] = backHcp->text();
317     if (frontLen)
318       len[i] = frontLen->text();
319     if (backLen)
320       len[i+9] = backLen->text();
321   }
322 }
323
324 // Only par is mandatory
325 bool CourseDialog::validate(void)
326 {
327   for (int i = 0; i < 9; i++) {
328     QTableWidgetItem *frontItem = table->item(ROW_PAR, i);
329     QTableWidgetItem *backItem = table->item(ROW_PAR_2, i);
330     
331     if (!frontItem || !backItem)
332       return false;
333     
334     QString str1 = frontItem->text();
335     QString str2 = backItem->text();
336     
337     if (str1.isEmpty() || str2.isEmpty())
338       return false;
339   }
340   return true;
341 }
342
343 void CourseDialog::finish(void)
344 {
345   if (validate())
346     done(1);
347   else {
348     qDebug() << "CourseDialog: invalid data, cancel or correct";
349   }
350 }
351
352