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