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)
123 QWidget *centralWidget = new QWidget(this);
128 createLayout(centralWidget);
130 setWindowTitle(tr("ScoreCard: New Score"));
133 void ScoreDialog::createLayout(QWidget *parent)
135 leftLayout = new QVBoxLayout;
136 leftLayout->addWidget(table);
138 rightLayout = new QVBoxLayout;
139 rightLayout->addWidget(pushButtonUp);
140 rightLayout->addWidget(pushButtonDown);
141 rightLayout->addWidget(pushButtonNext);
142 rightLayout->addStretch();
143 rightLayout->addWidget(pushButtonFinnish);
145 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
146 mainLayout->addLayout(leftLayout);
147 mainLayout->addLayout(rightLayout);
149 setLayout(mainLayout);
152 void ScoreDialog::createTable(QWidget *parent)
154 table = new QTableWidget(ROWS, COLS, parent);
156 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
157 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
158 table->horizontalHeader()->hide();
161 headers << "" << "Par" << "HCP" << "Score" << "" << "Par" << "HCP" << "Score";
162 table->setVerticalHeaderLabels(headers);
164 //connect(table, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(itemChanged(QTableWidgetItem *)));
165 //connect(table, SIGNAL(itemPressed(QTableWidgetItem *)), this, SLOT(itemChanged(QTableWidgetItem *)));
168 void ScoreDialog::createButton(QWidget *parent)
170 pushButtonFinnish = new QPushButton(tr("Finnish"));
171 connect(pushButtonFinnish, SIGNAL(clicked()), this, SLOT(finnish()));
173 pushButtonUp = new QPushButton(tr("+"));
174 connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
176 pushButtonDown = new QPushButton(tr("-"));
177 connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
179 pushButtonNext = new QPushButton(tr("Next"));
180 connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
183 void ScoreDialog::init(Course *course)
185 QTableWidgetItem *par, *hcp, *score;
187 for (int i = 0; i < 18; i++) {
188 par = new QTableWidgetItem(course->getPar(i));
189 hcp = new QTableWidgetItem(course->getHcp(i));
190 score = new QTableWidgetItem("");
191 //score->setInputMask("9");
193 QTableWidgetItem *holeNum = new QTableWidgetItem(QString::number(i+1));
194 QColor bgColor(Qt::gray);
195 holeNum->setBackgroundColor(bgColor);
197 holeNum->setTextAlignment(Qt::AlignCenter);
199 par->setTextAlignment(Qt::AlignCenter);
200 hcp->setTextAlignment(Qt::AlignCenter);
201 score->setTextAlignment(Qt::AlignCenter);
204 table->setItem(ROW_HOLE, i, holeNum);
205 table->setItem(ROW_PAR, i, par);
206 table->setItem(ROW_HCP, i, hcp);
207 table->setItem(ROW_SCORE, i, score);
210 table->setItem(ROW_HOLE_2, i-9, holeNum);
211 table->setItem(ROW_PAR_2, i-9, par);
212 table->setItem(ROW_HCP_2, i-9, hcp);
213 table->setItem(ROW_SCORE_2, i-9, score);
217 // Set focus to 1st cell
218 table->setCurrentCell(ROW_SCORE, 0);
219 setDefaultScore(table);
222 // Set default score to par if not set
223 void ScoreDialog::setDefaultScore(QTableWidget *table)
225 int row = table->currentRow();
226 int col = table->currentColumn();
228 if (row == ROW_SCORE)
230 else if (row == ROW_SCORE_2)
233 qDebug() << "ERROR: unknown row in default score";
236 QTableWidgetItem *par = table->item(row, col);
237 QTableWidgetItem *score = table->item(row + 2, col);
239 if (par && score && score->text() == "") {
240 QVariant value(par->text());
241 score->setData(Qt::DisplayRole, value);
245 void ScoreDialog::moveToNextCell(QTableWidgetItem *item)
250 QTableWidget *table = item->tableWidget();
255 int row = table->currentRow();
256 int col = table->currentColumn();
258 if (col < (COLS-1)) {
261 else if (col == (COLS-1)) {
263 row = (row == ROW_SCORE_2) ? ROW_SCORE : ROW_SCORE_2;
265 //qDebug() << "new cell: " << row << "/" << col;
266 table->setCurrentCell(row, col);
269 void ScoreDialog::next(void)
272 QTableWidgetItem *item = table->currentItem();
273 moveToNextCell(item);
274 setDefaultScore(table);
278 void ScoreDialog::up(void)
280 QTableWidgetItem *item = table->currentItem();
285 int i = (item->text()).toInt();
287 item->setData(Qt::DisplayRole, value);
290 void ScoreDialog::down(void)
292 QTableWidgetItem *item = table->currentItem();
297 int i = (item->text()).toInt();
299 item->setData(Qt::DisplayRole, value);
302 void ScoreDialog::itemChanged(QTableWidgetItem *item)
306 void ScoreDialog::results(QVector<QString> &scores)
308 for (int i = 0; i < 9; i++) {
309 QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
310 QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
313 scores[i] = frontItem->text();
316 scores[i+9] = backItem->text();
320 bool ScoreDialog::validate(void)
322 for (int i=0; i<9; i++) {
323 QTableWidgetItem *frontItem = table->item(ROW_SCORE, i);
324 QTableWidgetItem *backItem = table->item(ROW_SCORE_2, i);
326 if (!frontItem || !backItem)
329 QString str1 = frontItem->text();
330 QString str2 = backItem->text();
332 if (str1.isEmpty() || str2.isEmpty())
338 void ScoreDialog::finnish(void)
343 qDebug() << "ScoreDialog: invalid data, cancel or correct";
347 void ScoreDialog::reject(void)