2 * Copyright (C) 2009 Sakari Poussa
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 2.
12 #include <QInputContext>
13 #include <QtGui/QTableWidget>
15 #include "course-dialog.h"
16 #include "score-common.h"
18 CourseSelectDialog::CourseSelectDialog(QWidget *parent) : QDialog(parent)
20 QWidget *centralWidget = new QWidget(this);
21 createLayout(centralWidget);
23 setWindowTitle(tr("ScoreCard: New Club and Course"));
26 void CourseSelectDialog::createLayout(QWidget *parent)
28 labelClub = new QLabel(tr("Club"));
29 labelCourse = new QLabel(tr("Course"));
30 lineEditClub = new QLineEdit;
31 lineEditCourse = new QLineEdit;
32 pushButtonNext = new QPushButton(tr("Next"));
34 connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
36 QDialogButtonBox * buttonBox = new QDialogButtonBox(Qt::Vertical);
37 buttonBox->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
39 leftLayout = new QVBoxLayout;
40 leftLayout->addWidget(lineEditClub);
41 leftLayout->addWidget(lineEditCourse);
43 rightLayout = new QVBoxLayout;
44 rightLayout->addStretch();
45 rightLayout->addWidget(buttonBox);
47 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
48 mainLayout->addLayout(leftLayout);
49 mainLayout->addLayout(rightLayout);
51 setLayout(mainLayout);
54 void CourseSelectDialog::results(QString &club,
57 club = lineEditClub->text();
58 course = lineEditCourse->text();
61 bool CourseSelectDialog::validate(void)
63 QString str1 = lineEditClub->text();
64 QString str2 = lineEditCourse->text();
66 if (str1.isEmpty() || str2.isEmpty())
72 void CourseSelectDialog::next(void)
77 qDebug() << "CourseDialog: invalid data, cancel or correct";
81 ////////////////////////////////////////////////////////////////////////////////
82 ////////////////////////////////////////////////////////////////////////////////
83 ////////////////////////////////////////////////////////////////////////////////
85 CourseDialog::CourseDialog(QWidget *parent) : QDialog(parent)
89 QWidget *centralWidget = new QWidget(this);
94 createLayout(centralWidget);
97 void CourseDialog::createLayout(QWidget *parent)
99 leftLayout = new QVBoxLayout;
100 leftLayout->addWidget(table);
102 QDialogButtonBox * buttonBoxUp = new QDialogButtonBox(Qt::Vertical);
103 buttonBoxUp->addButton(pushButtonUp, QDialogButtonBox::ActionRole);
104 buttonBoxUp->addButton(pushButtonDown, QDialogButtonBox::ActionRole);
105 buttonBoxUp->addButton(pushButtonNext, QDialogButtonBox::ActionRole);
107 QDialogButtonBox * buttonBoxDown = new QDialogButtonBox(Qt::Vertical);
108 buttonBoxDown->addButton(pushButtonFinish, QDialogButtonBox::ActionRole);
110 rightLayout = new QVBoxLayout;
111 rightLayout->addWidget(buttonBoxUp);
112 rightLayout->addStretch();
113 rightLayout->addWidget(buttonBoxDown);
115 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
116 mainLayout->addLayout(leftLayout);
117 mainLayout->addLayout(rightLayout);
118 setLayout(mainLayout);
121 void CourseDialog::createButton(QWidget *parent)
124 pushButtonUp = new QPushButton(tr("+"));
125 connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
127 pushButtonDown = new QPushButton(tr("-"));
128 connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
130 pushButtonNext = new QPushButton(tr("Next"));
131 connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
133 pushButtonFinish = new QPushButton(tr("Finish"));
134 connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
137 void CourseDialog::createTable(QWidget *parent)
139 table = new QTableWidget(ROWS, COLS, parent);
142 headers << "" << "Par" << "HCP" << "Len" << "" << "Par" << "HCP" << "Len";
143 table->setVerticalHeaderLabels(headers);
144 table->horizontalHeader()->hide();
145 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
146 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
147 table->setStyleSheet(ScoreStyle::style());
150 void CourseDialog::init(Course *course)
152 QTableWidgetItem *par, *hcp;
154 for (int i=0; i<18; i++) {
156 par = new QTableWidgetItem(course->getPar(i));
157 hcp = new QTableWidgetItem(course->getHcp(i));
160 par = new QTableWidgetItem("4");
161 hcp = new QTableWidgetItem("9");
163 QTableWidgetItem *len = new QTableWidgetItem("");
165 QTableWidgetItem *holeNum = new QTableWidgetItem(QString::number(i+1));
166 holeNum->setForeground(ScoreColor::holeBg());
167 holeNum->setFlags(Qt::NoItemFlags);
169 holeNum->setTextAlignment(Qt::AlignCenter);
170 par->setTextAlignment(Qt::AlignCenter);
171 hcp->setTextAlignment(Qt::AlignCenter);
172 len->setTextAlignment(Qt::AlignCenter);
173 // len is not in use - here just to confuse ;(
174 len->setFlags(Qt::NoItemFlags);
177 table->setItem(0, i, holeNum);
178 table->setItem(1, i, par);
179 table->setItem(2, i, hcp);
180 table->setItem(3, i, len);
183 table->setItem(4, i-9, holeNum);
184 table->setItem(5, i-9, par);
185 table->setItem(6, i-9, hcp);
186 table->setItem(7, i-9, len);
189 table->setCurrentCell(1, 0);
192 void CourseDialog::up(void)
194 QTableWidgetItem *item = table->currentItem();
199 int i = (item->text()).toInt();
201 item->setData(Qt::DisplayRole, value);
204 void CourseDialog::down(void)
206 QTableWidgetItem *item = table->currentItem();
211 int i = (item->text()).toInt();
213 item->setData(Qt::DisplayRole, value);
216 void CourseDialog::next(void)
219 QTableWidgetItem *item = table->currentItem();
220 moveToNextCell(item);
221 //setDefaultScore(table);
225 void CourseDialog::moveToNextCell(QTableWidgetItem *item)
230 QTableWidget *table = item->tableWidget();
235 int row = table->currentRow();
236 int col = table->currentColumn();
238 if (col < (COLS-1)) {
241 else if (col == (COLS-1)) {
243 row = (row == ROW_PAR_2) ? ROW_PAR : ROW_PAR_2;
245 //qDebug() << "new cell: " << row << "/" << col;
246 table->setCurrentCell(row, col);
249 void CourseDialog::results(QVector<QString> &par,
250 QVector<QString> &hcp,
251 QVector<QString> &len)
253 for (int i = 0; i < 9; i++) {
254 QTableWidgetItem *frontPar = table->item(ROW_PAR, i);
255 QTableWidgetItem *backPar = table->item(ROW_PAR_2, i);
256 QTableWidgetItem *frontHcp = table->item(ROW_HCP, i);
257 QTableWidgetItem *backHcp = table->item(ROW_HCP_2, i);
258 QTableWidgetItem *frontLen = table->item(ROW_LEN, i);
259 QTableWidgetItem *backLen = table->item(ROW_LEN_2, i);
262 par[i] = frontPar->text();
264 par[i+9] = backPar->text();
266 hcp[i] = frontHcp->text();
268 hcp[i+9] = backHcp->text();
270 len[i] = frontLen->text();
272 len[i+9] = backLen->text();
276 // Only par is mandatory
277 bool CourseDialog::validate(void)
279 for (int i = 0; i < 9; i++) {
280 QTableWidgetItem *frontItem = table->item(ROW_PAR, i);
281 QTableWidgetItem *backItem = table->item(ROW_PAR_2, i);
283 if (!frontItem || !backItem)
286 QString str1 = frontItem->text();
287 QString str2 = backItem->text();
289 if (str1.isEmpty() || str2.isEmpty())
295 void CourseDialog::finish(void)
300 qDebug() << "CourseDialog: invalid data, cancel or correct";