Use QListWidget instead of comboboxes
[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   labelClub = new QLabel(tr("Club"));
17   labelCourse = new QLabel(tr("Course"));
18   listClub = new QListWidget(parent);
19   lineEditDate = new QLineEdit;
20   pushButtonNext = new QPushButton(tr("Next"));
21
22   QDate today(QDate::currentDate());
23   lineEditDate->setText(today.toString("yyyy-MM-dd"));
24
25   connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
26
27   leftLayout = new QVBoxLayout;
28   leftLayout->addWidget(listClub);
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
137   setLayout(mainLayout);
138 }
139
140 void ScoreDialog::createTable(QWidget *parent)
141 {
142   table = new QTableWidget(ROWS, COLS, parent);
143
144   table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
145   table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
146   table->horizontalHeader()->hide();
147
148   QStringList headers;
149   headers << "" << "Par" << "HCP" << "Score" << "" << "Par" << "HCP" << "Score";
150   table->setVerticalHeaderLabels(headers);
151
152   //connect(table, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(itemChanged(QTableWidgetItem *)));
153   //connect(table, SIGNAL(itemPressed(QTableWidgetItem *)), this, SLOT(itemChanged(QTableWidgetItem *)));
154 }
155
156 void ScoreDialog::createButton(QWidget *parent)
157 {
158   pushButtonFinish = new QPushButton(tr("Finish"));
159   connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
160
161   pushButtonUp = new QPushButton(tr("+"));
162   connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
163
164   pushButtonDown = new QPushButton(tr("-"));
165   connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
166
167   pushButtonNext = new QPushButton(tr("Next"));
168   connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
169 }
170
171 void ScoreDialog::init(Course *course)
172 {
173   QTableWidgetItem *par, *hcp, *score;
174
175   for (int i = 0; i < 18; i++) {
176     par = new QTableWidgetItem(course->getPar(i));
177     hcp = new QTableWidgetItem(course->getHcp(i));
178     score = new QTableWidgetItem("");
179     //score->setInputMask("9");
180
181     QTableWidgetItem *holeNum = new QTableWidgetItem(QString::number(i+1));
182     QColor bgColor(Qt::gray);
183     holeNum->setBackgroundColor(bgColor);
184
185     holeNum->setTextAlignment(Qt::AlignCenter);
186
187     par->setTextAlignment(Qt::AlignCenter);
188     hcp->setTextAlignment(Qt::AlignCenter);
189     score->setTextAlignment(Qt::AlignCenter);
190
191     if (i < 9) {
192       table->setItem(ROW_HOLE, i, holeNum);
193       table->setItem(ROW_PAR, i, par);
194       table->setItem(ROW_HCP, i, hcp);
195       table->setItem(ROW_SCORE, i, score);
196     }
197     else {
198       table->setItem(ROW_HOLE_2, i-9, holeNum);
199       table->setItem(ROW_PAR_2, i-9, par);
200       table->setItem(ROW_HCP_2, i-9, hcp);
201       table->setItem(ROW_SCORE_2, i-9, score);
202     }
203   }
204
205   // Set focus to 1st cell
206   table->setCurrentCell(ROW_SCORE, 0);
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::moveToNextCell(QTableWidgetItem *item)
234 {
235   if (!item)
236     return;
237
238   QTableWidget *table = item->tableWidget();
239
240   if (!table)
241     return;
242
243   int row = table->currentRow();
244   int col = table->currentColumn();
245
246   if (col < (COLS-1)) {
247     col++;
248   }
249   else if (col == (COLS-1)) {
250     col = 0;
251     row = (row == ROW_SCORE_2) ? ROW_SCORE : ROW_SCORE_2;
252   }
253   //qDebug() << "new cell: " << row << "/" << col;
254   table->setCurrentCell(row, col);
255 }
256
257 void ScoreDialog::next(void)
258 {
259   if (table) {
260     QTableWidgetItem *item = table->currentItem();
261     moveToNextCell(item);
262     setDefaultScore(table);
263   }
264 }
265
266 void ScoreDialog::up(void)
267 {
268   QTableWidgetItem *item = table->currentItem();
269
270   if (!item)
271     return;
272
273   int i = (item->text()).toInt();
274   QVariant value(i+1);
275   item->setData(Qt::DisplayRole, value);
276 }
277
278 void ScoreDialog::down(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::itemChanged(QTableWidgetItem *item)
291 {
292 }
293
294 void ScoreDialog::results(QVector<QString> &scores)
295 {
296   for (int i = 0; i < 9; i++) {
297     QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
298     QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
299
300     if (frontItem)
301       scores[i] = frontItem->text();
302
303     if (backItem)
304       scores[i+9] = backItem->text();
305   }
306 }
307
308 bool ScoreDialog::validate(void)
309 {
310   for (int i=0; i<9; i++) {
311     QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
312     QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
313     
314     if (!frontItem || !backItem)
315       return false;
316     
317     QString str1 = frontItem->text();
318     QString str2 = backItem->text();
319     
320     if (str1.isEmpty() || str2.isEmpty())
321       return false;
322   }
323   return true;
324 }
325
326 void ScoreDialog::finish(void)
327 {
328   if (validate())
329     done(1);
330   else {
331     qDebug() << "ScoreDialog: invalid data, cancel or correct";
332   }
333 }
334
335 void ScoreDialog::reject(void)
336 {
337   done(0);
338 }