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