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