initial commit
[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   QFont font;
124   font.setPointSize(20);
125   setFont(font);
126
127   QWidget *centralWidget = new QWidget(this);
128
129   createTable();
130   createButton();
131
132   createLayout(centralWidget);
133
134   setWindowTitle(tr("ScoreCard: New Score"));
135 }
136
137 void ScoreDialog::createLayout(QWidget *parent)
138 {
139   leftLayout = new QVBoxLayout;
140   leftLayout->addWidget(table);
141
142   rightLayout = new QVBoxLayout;
143   rightLayout->addWidget(pushButtonUp);
144   rightLayout->addWidget(pushButtonDown);
145   rightLayout->addWidget(pushButtonNext);
146   rightLayout->addStretch();
147   rightLayout->addWidget(pushButtonFinnish);
148
149   QHBoxLayout *mainLayout = new QHBoxLayout(parent);
150   mainLayout->addLayout(leftLayout);
151   mainLayout->addLayout(rightLayout);
152
153   setLayout(mainLayout);
154 }
155
156 void ScoreDialog::createTable(QWidget *parent)
157 {
158   table = new QTableWidget(ROWS, COLS, parent);
159
160   table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
161   table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
162   table->horizontalHeader()->hide();
163
164   QStringList headers;
165   headers << "" << "Par" << "HCP" << "Score" << "" << "Par" << "HCP" << "Score";
166   table->setVerticalHeaderLabels(headers);
167
168   //connect(table, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(itemChanged(QTableWidgetItem *)));
169   //connect(table, SIGNAL(itemPressed(QTableWidgetItem *)), this, SLOT(itemChanged(QTableWidgetItem *)));
170 }
171
172 void ScoreDialog::createButton(QWidget *parent)
173 {
174   pushButtonFinnish = new QPushButton(tr("Finnish"));
175   connect(pushButtonFinnish, SIGNAL(clicked()), this, SLOT(finnish()));
176
177   pushButtonUp = new QPushButton(tr("+"));
178   connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
179
180   pushButtonDown = new QPushButton(tr("-"));
181   connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
182
183   pushButtonNext = new QPushButton(tr("Next"));
184   connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
185 }
186
187 void ScoreDialog::init(Course *course)
188 {
189   QTableWidgetItem *par, *hcp, *score;
190
191   for (int i = 0; i < 18; i++) {
192     par = new QTableWidgetItem(course->getPar(i));
193     hcp = new QTableWidgetItem(course->getHcp(i));
194     score = new QTableWidgetItem("");
195     //score->setInputMask("9");
196
197     QTableWidgetItem *holeNum = new QTableWidgetItem(QString::number(i+1));
198     QColor bgColor(Qt::gray);
199     holeNum->setBackgroundColor(bgColor);
200
201     holeNum->setTextAlignment(Qt::AlignCenter);
202
203     par->setTextAlignment(Qt::AlignCenter);
204     hcp->setTextAlignment(Qt::AlignCenter);
205     score->setTextAlignment(Qt::AlignCenter);
206
207     if (i < 9) {
208       table->setItem(ROW_HOLE, i, holeNum);
209       table->setItem(ROW_PAR, i, par);
210       table->setItem(ROW_HCP, i, hcp);
211       table->setItem(ROW_SCORE, i, score);
212     }
213     else {
214       table->setItem(ROW_HOLE_2, i-9, holeNum);
215       table->setItem(ROW_PAR_2, i-9, par);
216       table->setItem(ROW_HCP_2, i-9, hcp);
217       table->setItem(ROW_SCORE_2, i-9, score);
218     }
219   }
220
221   // Set focus to 1st cell
222   table->setCurrentCell(ROW_SCORE, 0);
223   setDefaultScore(table);
224 }
225
226 // Set default score to par if not set
227 void ScoreDialog::setDefaultScore(QTableWidget *table)
228 {
229   int row = table->currentRow();
230   int col = table->currentColumn();
231   
232   if (row == ROW_SCORE)
233     row = ROW_PAR;
234   else if (row == ROW_SCORE_2)
235     row = ROW_PAR_2;
236   else {
237     qDebug() << "ERROR: unknown row in default score";
238     return;
239   }
240   QTableWidgetItem *par = table->item(row, col);
241   QTableWidgetItem *score = table->item(row + 2, col);
242
243   if (par && score && score->text() == "") {
244     QVariant value(par->text());
245     score->setData(Qt::DisplayRole, value);
246   }
247 }
248
249 void ScoreDialog::moveToNextCell(QTableWidgetItem *item)
250 {
251   if (!item)
252     return;
253
254   QTableWidget *table = item->tableWidget();
255
256   if (!table)
257     return;
258
259   int row = table->currentRow();
260   int col = table->currentColumn();
261
262   if (col < (COLS-1)) {
263     col++;
264   }
265   else if (col == (COLS-1)) {
266     col = 0;
267     row = (row == ROW_SCORE_2) ? ROW_SCORE : ROW_SCORE_2;
268   }
269   //qDebug() << "new cell: " << row << "/" << col;
270   table->setCurrentCell(row, col);
271 }
272
273 void ScoreDialog::next(void)
274 {
275   if (table) {
276     QTableWidgetItem *item = table->currentItem();
277     moveToNextCell(item);
278     setDefaultScore(table);
279   }
280 }
281
282 void ScoreDialog::up(void)
283 {
284   QTableWidgetItem *item = table->currentItem();
285
286   if (!item)
287     return;
288
289   int i = (item->text()).toInt();
290   QVariant value(i+1);
291   item->setData(Qt::DisplayRole, value);
292 }
293
294 void ScoreDialog::down(void)
295 {
296   QTableWidgetItem *item = table->currentItem();
297
298   if (!item)
299     return;
300
301   int i = (item->text()).toInt();
302   QVariant value(i-1);
303   item->setData(Qt::DisplayRole, value);
304 }
305
306 void ScoreDialog::itemChanged(QTableWidgetItem *item)
307 {
308 }
309
310 void ScoreDialog::results(QVector<QString> &scores)
311 {
312   for (int i = 0; i < 9; i++) {
313     QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
314     QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
315
316     if (frontItem)
317       scores[i] = frontItem->text();
318
319     if (backItem)
320       scores[i+9] = backItem->text();
321   }
322 }
323
324 bool ScoreDialog::validate(void)
325 {
326   for (int i=0; i<9; i++) {
327     QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
328     QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
329     
330     if (!frontItem || !backItem)
331       return false;
332     
333     QString str1 = frontItem->text();
334     QString str2 = backItem->text();
335     
336     if (str1.isEmpty() || str2.isEmpty())
337       return false;
338   }
339   return true;
340 }
341
342 void ScoreDialog::finnish(void)
343 {
344   if (validate())
345     done(1);
346   else {
347     qDebug() << "ScoreDialog: invalid data, cancel or correct";
348   }
349 }
350
351 void ScoreDialog::reject(void)
352 {
353   done(0);
354 }