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