Changed the color scheme to more appealing
[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)
168 {
169   QTableWidgetItem *par, *hcp, *score, *holeNum;
170   QColor bgColor(Qt::gray);
171
172   for (int i = 0; i < 18; i++) {
173     par = new QTableWidgetItem(course->getPar(i));
174     hcp = new QTableWidgetItem(course->getHcp(i));
175     score = new QTableWidgetItem("");
176     holeNum = new QTableWidgetItem(QString::number(i+1));
177
178     holeNum->setBackgroundColor(bgColor);
179
180     par->setTextAlignment(Qt::AlignCenter);
181     hcp->setTextAlignment(Qt::AlignCenter);
182     score->setTextAlignment(Qt::AlignCenter);
183     holeNum->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     qDebug() << "ERROR: no current item";
233     return;
234   }
235
236   int i = (item->text()).toInt();
237   QVariant value(i+1);
238   item->setData(Qt::DisplayRole, value);
239 }
240
241 void ScoreDialog::down(void)
242 {
243   QTableWidgetItem *item = table->currentItem();
244
245   if (!item)
246     return;
247
248   int i = (item->text()).toInt();
249   QVariant value(i-1);
250   item->setData(Qt::DisplayRole, value);
251 }
252
253 void ScoreDialog::next(void)
254 {
255   if (table) {
256     QTableWidgetItem *item = table->currentItem();
257     moveToNextCell(item);
258     setDefaultScore(table);
259   }
260 }
261
262 void ScoreDialog::moveToNextCell(QTableWidgetItem *item)
263 {
264   if (!item)
265     return;
266
267   QTableWidget *table = item->tableWidget();
268
269   if (!table)
270     return;
271
272   int row = table->currentRow();
273   int col = table->currentColumn();
274
275   if (col < (COLS-1)) {
276     col++;
277   }
278   else if (col == (COLS-1)) {
279     col = 0;
280     row = (row == ROW_SCORE_2) ? ROW_SCORE : ROW_SCORE_2;
281   }
282   //qDebug() << "new cell: " << row << "/" << col;
283   table->setCurrentCell(row, col);
284 }
285
286 void ScoreDialog::results(QVector<QString> &scores)
287 {
288   for (int i = 0; i < 9; i++) {
289     QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
290     QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
291
292     if (frontItem)
293       scores[i] = frontItem->text();
294
295     if (backItem)
296       scores[i+9] = backItem->text();
297   }
298 }
299
300 bool ScoreDialog::validate(void)
301 {
302   for (int i=0; i<9; i++) {
303     QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
304     QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
305     
306     if (!frontItem || !backItem)
307       return false;
308     
309     QString str1 = frontItem->text();
310     QString str2 = backItem->text();
311     
312     if (str1.isEmpty() || str2.isEmpty())
313       return false;
314   }
315   return true;
316 }
317
318 void ScoreDialog::finish(void)
319 {
320   if (validate())
321     done(1);
322   else {
323     qDebug() << "ScoreDialog: invalid data, cancel or correct";
324   }
325 }
326
327 void ScoreDialog::reject(void)
328 {
329   done(0);
330 }