2 #include <QInputContext>
4 #include "score-dialog.h"
6 SelectDialog::SelectDialog(QWidget *parent) : QDialog(parent)
8 QWidget *centralWidget = new QWidget(this);
9 createLayout(centralWidget);
11 setWindowTitle(tr("ScoreCard: Select Club, Course and Date"));
14 void SelectDialog::createLayout(QWidget *parent)
16 labelClub = new QLabel(tr("Club"));
17 labelCourse = new QLabel(tr("Course"));
18 comboBoxClub = new QComboBox;
19 comboBoxCourse = new QComboBox;
20 lineEditDate = new QLineEdit;
21 pushButtonNext = new QPushButton(tr("Next"));
23 QDate today(QDate::currentDate());
24 lineEditDate->setText(today.toString("yyyy-MM-dd"));
26 connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
27 connect(comboBoxClub, SIGNAL(activated(int)), this, SLOT(comboBoxClubChanged()));
28 connect(comboBoxCourse, SIGNAL(activated(int)), this, SLOT(comboBoxCourseChanged()));
30 leftLayout = new QVBoxLayout;
31 leftLayout->addWidget(comboBoxClub);
32 leftLayout->addWidget(comboBoxCourse);
33 leftLayout->addWidget(lineEditDate);
35 rightLayout = new QVBoxLayout;
36 rightLayout->addStretch();
37 rightLayout->addWidget(pushButtonNext);
39 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
40 mainLayout->addLayout(leftLayout);
41 mainLayout->addLayout(rightLayout);
43 setLayout(mainLayout);
46 void SelectDialog::init(QList<Club *> &list)
50 QListIterator<Club *> i(clubList);
54 Club *club = i.next();
55 comboBoxClub->insertItem(index, club->getName());
58 comboBoxCourseUpdate();
61 void SelectDialog::results(QString &club,
65 club = comboBoxClub->currentText();
66 course = comboBoxCourse->currentText();
67 date = lineEditDate->text();
70 void SelectDialog::comboBoxCourseUpdate()
72 int index = comboBoxClub->currentIndex();
74 Club *club = clubList[index];
75 QList<Course *> courseList = club->getCourseList();
77 comboBoxCourse->clear();
78 QListIterator<Course *> i(courseList);
81 Course *course = i.next();
82 comboBoxCourse->insertItem(index, course->getName());
87 void SelectDialog::comboBoxClubChanged()
89 comboBoxCourseUpdate();
92 void SelectDialog::comboBoxCourseChanged()
96 bool SelectDialog::validate(void)
101 void SelectDialog::next(void)
106 qDebug() << "SelectDialog: invalid data, cancel or correct";
110 void SelectDialog::reject(void)
115 ////////////////////////////////////////////////////////////////////////////////
116 ////////////////////////////////////////////////////////////////////////////////
117 ////////////////////////////////////////////////////////////////////////////////
119 ScoreDialog::ScoreDialog(QWidget *parent) : QDialog(parent)
124 font.setPointSize(20);
127 QWidget *centralWidget = new QWidget(this);
132 createLayout(centralWidget);
134 setWindowTitle(tr("ScoreCard: New Score"));
137 void ScoreDialog::createLayout(QWidget *parent)
139 leftLayout = new QVBoxLayout;
140 leftLayout->addWidget(table);
142 rightLayout = new QVBoxLayout;
143 rightLayout->addWidget(pushButtonUp);
144 rightLayout->addWidget(pushButtonDown);
145 rightLayout->addWidget(pushButtonNext);
146 rightLayout->addStretch();
147 rightLayout->addWidget(pushButtonFinnish);
149 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
150 mainLayout->addLayout(leftLayout);
151 mainLayout->addLayout(rightLayout);
153 setLayout(mainLayout);
156 void ScoreDialog::createTable(QWidget *parent)
158 table = new QTableWidget(ROWS, COLS, parent);
160 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
161 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
162 table->horizontalHeader()->hide();
165 headers << "" << "Par" << "HCP" << "Score" << "" << "Par" << "HCP" << "Score";
166 table->setVerticalHeaderLabels(headers);
168 //connect(table, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(itemChanged(QTableWidgetItem *)));
169 //connect(table, SIGNAL(itemPressed(QTableWidgetItem *)), this, SLOT(itemChanged(QTableWidgetItem *)));
172 void ScoreDialog::createButton(QWidget *parent)
174 pushButtonFinnish = new QPushButton(tr("Finnish"));
175 connect(pushButtonFinnish, SIGNAL(clicked()), this, SLOT(finnish()));
177 pushButtonUp = new QPushButton(tr("+"));
178 connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
180 pushButtonDown = new QPushButton(tr("-"));
181 connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
183 pushButtonNext = new QPushButton(tr("Next"));
184 connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
187 void ScoreDialog::init(Course *course)
189 QTableWidgetItem *par, *hcp, *score;
191 for (int i = 0; i < 18; i++) {
192 par = new QTableWidgetItem(course->getPar(i));
193 hcp = new QTableWidgetItem(course->getHcp(i));
194 score = new QTableWidgetItem("");
195 //score->setInputMask("9");
197 QTableWidgetItem *holeNum = new QTableWidgetItem(QString::number(i+1));
198 QColor bgColor(Qt::gray);
199 holeNum->setBackgroundColor(bgColor);
201 holeNum->setTextAlignment(Qt::AlignCenter);
203 par->setTextAlignment(Qt::AlignCenter);
204 hcp->setTextAlignment(Qt::AlignCenter);
205 score->setTextAlignment(Qt::AlignCenter);
208 table->setItem(ROW_HOLE, i, holeNum);
209 table->setItem(ROW_PAR, i, par);
210 table->setItem(ROW_HCP, i, hcp);
211 table->setItem(ROW_SCORE, i, score);
214 table->setItem(ROW_HOLE_2, i-9, holeNum);
215 table->setItem(ROW_PAR_2, i-9, par);
216 table->setItem(ROW_HCP_2, i-9, hcp);
217 table->setItem(ROW_SCORE_2, i-9, score);
221 // Set focus to 1st cell
222 table->setCurrentCell(ROW_SCORE, 0);
223 setDefaultScore(table);
226 // Set default score to par if not set
227 void ScoreDialog::setDefaultScore(QTableWidget *table)
229 int row = table->currentRow();
230 int col = table->currentColumn();
232 if (row == ROW_SCORE)
234 else if (row == ROW_SCORE_2)
237 qDebug() << "ERROR: unknown row in default score";
240 QTableWidgetItem *par = table->item(row, col);
241 QTableWidgetItem *score = table->item(row + 2, col);
243 if (par && score && score->text() == "") {
244 QVariant value(par->text());
245 score->setData(Qt::DisplayRole, value);
249 void ScoreDialog::moveToNextCell(QTableWidgetItem *item)
254 QTableWidget *table = item->tableWidget();
259 int row = table->currentRow();
260 int col = table->currentColumn();
262 if (col < (COLS-1)) {
265 else if (col == (COLS-1)) {
267 row = (row == ROW_SCORE_2) ? ROW_SCORE : ROW_SCORE_2;
269 //qDebug() << "new cell: " << row << "/" << col;
270 table->setCurrentCell(row, col);
273 void ScoreDialog::next(void)
276 QTableWidgetItem *item = table->currentItem();
277 moveToNextCell(item);
278 setDefaultScore(table);
282 void ScoreDialog::up(void)
284 QTableWidgetItem *item = table->currentItem();
289 int i = (item->text()).toInt();
291 item->setData(Qt::DisplayRole, value);
294 void ScoreDialog::down(void)
296 QTableWidgetItem *item = table->currentItem();
301 int i = (item->text()).toInt();
303 item->setData(Qt::DisplayRole, value);
306 void ScoreDialog::itemChanged(QTableWidgetItem *item)
310 void ScoreDialog::results(QVector<QString> &scores)
312 for (int i = 0; i < 9; i++) {
313 QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
314 QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
317 scores[i] = frontItem->text();
320 scores[i+9] = backItem->text();
324 bool ScoreDialog::validate(void)
326 for (int i=0; i<9; i++) {
327 QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
328 QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
330 if (!frontItem || !backItem)
333 QString str1 = frontItem->text();
334 QString str2 = backItem->text();
336 if (str1.isEmpty() || str2.isEmpty())
342 void ScoreDialog::finnish(void)
347 qDebug() << "ScoreDialog: invalid data, cancel or correct";
351 void ScoreDialog::reject(void)