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 leftLayout = new QVBoxLayout;
37 leftLayout->addWidget(lineEditClub);
38 leftLayout->addWidget(lineEditCourse);
40 rightLayout = new QVBoxLayout;
41 rightLayout->addStretch();
42 rightLayout->addWidget(pushButtonNext);
44 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
45 mainLayout->addLayout(leftLayout);
46 mainLayout->addLayout(rightLayout);
48 setLayout(mainLayout);
51 void CourseSelectDialog::results(QString &club,
54 club = lineEditClub->text();
55 course = lineEditCourse->text();
58 bool CourseSelectDialog::validate(void)
60 QString str1 = lineEditClub->text();
61 QString str2 = lineEditCourse->text();
63 if (str1.isEmpty() || str2.isEmpty())
69 void CourseSelectDialog::next(void)
74 qDebug() << "CourseDialog: invalid data, cancel or correct";
78 ////////////////////////////////////////////////////////////////////////////////
79 ////////////////////////////////////////////////////////////////////////////////
80 ////////////////////////////////////////////////////////////////////////////////
82 CourseDialog::CourseDialog(QWidget *parent) : QDialog(parent)
86 QWidget *centralWidget = new QWidget(this);
91 createLayout(centralWidget);
94 void CourseDialog::createLayout(QWidget *parent)
96 leftLayout = new QVBoxLayout;
97 leftLayout->addWidget(table);
99 rightLayout = new QVBoxLayout;
100 rightLayout->addWidget(pushButtonUp);
101 rightLayout->addWidget(pushButtonDown);
102 rightLayout->addWidget(pushButtonNext);
103 rightLayout->addStretch();
104 rightLayout->addWidget(pushButtonFinish);
106 QHBoxLayout *mainLayout = new QHBoxLayout(parent);
107 mainLayout->addLayout(leftLayout);
108 mainLayout->addLayout(rightLayout);
109 setLayout(mainLayout);
112 void CourseDialog::createButton(QWidget *parent)
114 pushButtonUp = new QPushButton(tr("+"));
115 connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
117 pushButtonDown = new QPushButton(tr("-"));
118 connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
120 pushButtonNext = new QPushButton(tr("Next"));
121 connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
123 pushButtonFinish = new QPushButton(tr("Finish"));
124 connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
127 void CourseDialog::createTable(QWidget *parent)
129 table = new QTableWidget(ROWS, COLS, parent);
132 headers << "" << "Par" << "HCP" << "Len" << "" << "Par" << "HCP" << "Len";
133 table->setVerticalHeaderLabels(headers);
134 table->horizontalHeader()->hide();
135 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
136 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
137 table->setStyleSheet(ScoreColor::styleSheet());
140 void CourseDialog::init(Course *course)
142 QTableWidgetItem *par, *hcp;
144 for (int i=0; i<18; i++) {
146 par = new QTableWidgetItem(course->getPar(i));
147 hcp = new QTableWidgetItem(course->getHcp(i));
150 par = new QTableWidgetItem("4");
151 hcp = new QTableWidgetItem("9");
153 QTableWidgetItem *len = new QTableWidgetItem("");
155 QTableWidgetItem *holeNum = new QTableWidgetItem(QString::number(i+1));
156 holeNum->setBackgroundColor(ScoreColor::holeBg());
158 holeNum->setTextAlignment(Qt::AlignCenter);
159 par->setTextAlignment(Qt::AlignCenter);
160 hcp->setTextAlignment(Qt::AlignCenter);
161 len->setTextAlignment(Qt::AlignCenter);
164 table->setItem(0, i, holeNum);
165 table->setItem(1, i, par);
166 table->setItem(2, i, hcp);
167 table->setItem(3, i, len);
170 table->setItem(4, i-9, holeNum);
171 table->setItem(5, i-9, par);
172 table->setItem(6, i-9, hcp);
173 table->setItem(7, i-9, len);
178 void CourseDialog::up(void)
180 QTableWidgetItem *item = table->currentItem();
185 int i = (item->text()).toInt();
187 item->setData(Qt::DisplayRole, value);
190 void CourseDialog::down(void)
192 QTableWidgetItem *item = table->currentItem();
197 int i = (item->text()).toInt();
199 item->setData(Qt::DisplayRole, value);
202 void CourseDialog::next(void)
205 QTableWidgetItem *item = table->currentItem();
206 moveToNextCell(item);
207 //setDefaultScore(table);
211 void CourseDialog::moveToNextCell(QTableWidgetItem *item)
216 QTableWidget *table = item->tableWidget();
221 int row = table->currentRow();
222 int col = table->currentColumn();
224 if (col < (COLS-1)) {
227 else if (col == (COLS-1)) {
229 row = (row == ROW_PAR_2) ? ROW_PAR : ROW_PAR_2;
231 //qDebug() << "new cell: " << row << "/" << col;
232 table->setCurrentCell(row, col);
235 void CourseDialog::results(QVector<QString> &par,
236 QVector<QString> &hcp,
237 QVector<QString> &len)
239 for (int i = 0; i < 9; i++) {
240 QTableWidgetItem *frontPar = table->item(ROW_PAR, i);
241 QTableWidgetItem *backPar = table->item(ROW_PAR_2, i);
242 QTableWidgetItem *frontHcp = table->item(ROW_HCP, i);
243 QTableWidgetItem *backHcp = table->item(ROW_HCP_2, i);
244 QTableWidgetItem *frontLen = table->item(ROW_LEN, i);
245 QTableWidgetItem *backLen = table->item(ROW_LEN_2, i);
248 par[i] = frontPar->text();
250 par[i+9] = backPar->text();
252 hcp[i] = frontHcp->text();
254 hcp[i+9] = backHcp->text();
256 len[i] = frontLen->text();
258 len[i+9] = backLen->text();
262 // Only par is mandatory
263 bool CourseDialog::validate(void)
265 for (int i = 0; i < 9; i++) {
266 QTableWidgetItem *frontItem = table->item(ROW_PAR, i);
267 QTableWidgetItem *backItem = table->item(ROW_PAR_2, i);
269 if (!frontItem || !backItem)
272 QString str1 = frontItem->text();
273 QString str2 = backItem->text();
275 if (str1.isEmpty() || str2.isEmpty())
281 void CourseDialog::finish(void)
286 qDebug() << "CourseDialog: invalid data, cancel or correct";