- Added course selection dialog
[scorecard] / src / score-dialog.cpp
1 #include <QtGui>
2 #include <QInputContext>
3
4 #include "score-dialog.h"
5
6 SelectDialog::SelectDialog(QWidget *parent) : QDialog(parent)
7 {
8   QWidget *centralWidget = new QWidget(this);
9   createLayout(centralWidget);
10
11   setWindowTitle(tr("ScoreCard: Select Club, Course and Date"));
12 }
13
14 void SelectDialog::createLayout(QWidget *parent)
15 {
16   labelClub = new QLabel(tr("Club"));
17   labelCourse = new QLabel(tr("Course"));
18   comboBoxClub = new QComboBox;
19   comboBoxCourse = new QComboBox;
20   lineEditDate = new QLineEdit;
21   pushButtonNext = new QPushButton(tr("Next"));
22
23   QDate today(QDate::currentDate());
24   lineEditDate->setText(today.toString("yyyy-MM-dd"));
25
26   connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
27   connect(comboBoxClub, SIGNAL(activated(int)), this, SLOT(comboBoxClubChanged()));
28   connect(comboBoxCourse, SIGNAL(activated(int)), this, SLOT(comboBoxCourseChanged()));
29
30   leftLayout = new QVBoxLayout;
31   leftLayout->addWidget(comboBoxClub);
32   leftLayout->addWidget(comboBoxCourse);
33   leftLayout->addWidget(lineEditDate);
34
35   rightLayout = new QVBoxLayout;
36   rightLayout->addStretch();
37   rightLayout->addWidget(pushButtonNext);
38
39   QHBoxLayout *mainLayout = new QHBoxLayout(parent);
40   mainLayout->addLayout(leftLayout);
41   mainLayout->addLayout(rightLayout);
42
43   setLayout(mainLayout);
44 }
45
46 void SelectDialog::init(QList<Club *> &list)
47 {
48   clubList = list;
49
50   QListIterator<Club *> i(clubList);
51   int index = 0;
52
53   while (i.hasNext()) {
54     Club *club = i.next();
55     comboBoxClub->insertItem(index, club->getName());
56     index++;
57   }
58   comboBoxCourseUpdate();
59 }
60
61 void SelectDialog::results(QString &club, 
62                            QString &course, 
63                            QString &date)
64 {  
65   club   = comboBoxClub->currentText();
66   course = comboBoxCourse->currentText();
67   date   = lineEditDate->text();
68 }
69
70 void SelectDialog::comboBoxCourseUpdate()
71 {
72   int index = comboBoxClub->currentIndex();
73
74   Club *club = clubList[index];
75   QList<Course *> courseList = club->getCourseList();
76
77   comboBoxCourse->clear();
78   QListIterator<Course *> i(courseList);
79   index = 0;
80   while (i.hasNext()) {
81     Course *course = i.next();
82     comboBoxCourse->insertItem(index, course->getName());
83     index++;
84   }
85 }
86
87 void SelectDialog::comboBoxClubChanged()
88 {
89   comboBoxCourseUpdate();
90 }
91
92 void SelectDialog::comboBoxCourseChanged()
93 {
94 }
95
96 bool SelectDialog::validate(void)
97 {
98   return true;
99 }
100
101 void SelectDialog::next(void)
102 {
103   if (validate())
104     done(1);
105   else {
106     qDebug() << "SelectDialog: invalid data, cancel or correct";
107   }
108 }
109
110 void SelectDialog::reject(void)
111 {
112   done(0);
113 }
114
115 ////////////////////////////////////////////////////////////////////////////////
116 ////////////////////////////////////////////////////////////////////////////////
117 ////////////////////////////////////////////////////////////////////////////////
118
119 ScoreDialog::ScoreDialog(QWidget *parent) : QDialog(parent)
120 {
121   resize(800, 400);
122
123   QWidget *centralWidget = new QWidget(this);
124
125   createTable();
126   createButton();
127
128   createLayout(centralWidget);
129
130   setWindowTitle(tr("ScoreCard: New Score"));
131 }
132
133 void ScoreDialog::createLayout(QWidget *parent)
134 {
135   leftLayout = new QVBoxLayout;
136   leftLayout->addWidget(table);
137
138   rightLayout = new QVBoxLayout;
139   rightLayout->addWidget(pushButtonUp);
140   rightLayout->addWidget(pushButtonDown);
141   rightLayout->addWidget(pushButtonNext);
142   rightLayout->addStretch();
143   rightLayout->addWidget(pushButtonFinnish);
144
145   QHBoxLayout *mainLayout = new QHBoxLayout(parent);
146   mainLayout->addLayout(leftLayout);
147   mainLayout->addLayout(rightLayout);
148
149   setLayout(mainLayout);
150 }
151
152 void ScoreDialog::createTable(QWidget *parent)
153 {
154   table = new QTableWidget(ROWS, COLS, parent);
155
156   table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
157   table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
158   table->horizontalHeader()->hide();
159
160   QStringList headers;
161   headers << "" << "Par" << "HCP" << "Score" << "" << "Par" << "HCP" << "Score";
162   table->setVerticalHeaderLabels(headers);
163
164   //connect(table, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(itemChanged(QTableWidgetItem *)));
165   //connect(table, SIGNAL(itemPressed(QTableWidgetItem *)), this, SLOT(itemChanged(QTableWidgetItem *)));
166 }
167
168 void ScoreDialog::createButton(QWidget *parent)
169 {
170   pushButtonFinnish = new QPushButton(tr("Finnish"));
171   connect(pushButtonFinnish, SIGNAL(clicked()), this, SLOT(finnish()));
172
173   pushButtonUp = new QPushButton(tr("+"));
174   connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
175
176   pushButtonDown = new QPushButton(tr("-"));
177   connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
178
179   pushButtonNext = new QPushButton(tr("Next"));
180   connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
181 }
182
183 void ScoreDialog::init(Course *course)
184 {
185   QTableWidgetItem *par, *hcp, *score;
186
187   for (int i = 0; i < 18; i++) {
188     par = new QTableWidgetItem(course->getPar(i));
189     hcp = new QTableWidgetItem(course->getHcp(i));
190     score = new QTableWidgetItem("");
191     //score->setInputMask("9");
192
193     QTableWidgetItem *holeNum = new QTableWidgetItem(QString::number(i+1));
194     QColor bgColor(Qt::gray);
195     holeNum->setBackgroundColor(bgColor);
196
197     holeNum->setTextAlignment(Qt::AlignCenter);
198
199     par->setTextAlignment(Qt::AlignCenter);
200     hcp->setTextAlignment(Qt::AlignCenter);
201     score->setTextAlignment(Qt::AlignCenter);
202
203     if (i < 9) {
204       table->setItem(ROW_HOLE, i, holeNum);
205       table->setItem(ROW_PAR, i, par);
206       table->setItem(ROW_HCP, i, hcp);
207       table->setItem(ROW_SCORE, i, score);
208     }
209     else {
210       table->setItem(ROW_HOLE_2, i-9, holeNum);
211       table->setItem(ROW_PAR_2, i-9, par);
212       table->setItem(ROW_HCP_2, i-9, hcp);
213       table->setItem(ROW_SCORE_2, i-9, score);
214     }
215   }
216
217   // Set focus to 1st cell
218   table->setCurrentCell(ROW_SCORE, 0);
219   setDefaultScore(table);
220 }
221
222 // Set default score to par if not set
223 void ScoreDialog::setDefaultScore(QTableWidget *table)
224 {
225   int row = table->currentRow();
226   int col = table->currentColumn();
227   
228   if (row == ROW_SCORE)
229     row = ROW_PAR;
230   else if (row == ROW_SCORE_2)
231     row = ROW_PAR_2;
232   else {
233     qDebug() << "ERROR: unknown row in default score";
234     return;
235   }
236   QTableWidgetItem *par = table->item(row, col);
237   QTableWidgetItem *score = table->item(row + 2, col);
238
239   if (par && score && score->text() == "") {
240     QVariant value(par->text());
241     score->setData(Qt::DisplayRole, value);
242   }
243 }
244
245 void ScoreDialog::moveToNextCell(QTableWidgetItem *item)
246 {
247   if (!item)
248     return;
249
250   QTableWidget *table = item->tableWidget();
251
252   if (!table)
253     return;
254
255   int row = table->currentRow();
256   int col = table->currentColumn();
257
258   if (col < (COLS-1)) {
259     col++;
260   }
261   else if (col == (COLS-1)) {
262     col = 0;
263     row = (row == ROW_SCORE_2) ? ROW_SCORE : ROW_SCORE_2;
264   }
265   //qDebug() << "new cell: " << row << "/" << col;
266   table->setCurrentCell(row, col);
267 }
268
269 void ScoreDialog::next(void)
270 {
271   if (table) {
272     QTableWidgetItem *item = table->currentItem();
273     moveToNextCell(item);
274     setDefaultScore(table);
275   }
276 }
277
278 void ScoreDialog::up(void)
279 {
280   QTableWidgetItem *item = table->currentItem();
281
282   if (!item)
283     return;
284
285   int i = (item->text()).toInt();
286   QVariant value(i+1);
287   item->setData(Qt::DisplayRole, value);
288 }
289
290 void ScoreDialog::down(void)
291 {
292   QTableWidgetItem *item = table->currentItem();
293
294   if (!item)
295     return;
296
297   int i = (item->text()).toInt();
298   QVariant value(i-1);
299   item->setData(Qt::DisplayRole, value);
300 }
301
302 void ScoreDialog::itemChanged(QTableWidgetItem *item)
303 {
304 }
305
306 void ScoreDialog::results(QVector<QString> &scores)
307 {
308   for (int i = 0; i < 9; i++) {
309     QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
310     QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
311
312     if (frontItem)
313       scores[i] = frontItem->text();
314
315     if (backItem)
316       scores[i+9] = backItem->text();
317   }
318 }
319
320 bool ScoreDialog::validate(void)
321 {
322   for (int i=0; i<9; i++) {
323     QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
324     QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
325     
326     if (!frontItem || !backItem)
327       return false;
328     
329     QString str1 = frontItem->text();
330     QString str2 = backItem->text();
331     
332     if (str1.isEmpty() || str2.isEmpty())
333       return false;
334   }
335   return true;
336 }
337
338 void ScoreDialog::finnish(void)
339 {
340   if (validate())
341     done(1);
342   else {
343     qDebug() << "ScoreDialog: invalid data, cancel or correct";
344   }
345 }
346
347 void ScoreDialog::reject(void)
348 {
349   done(0);
350 }