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