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)
115 pushButtonUp = new QPushButton(tr("+"));
116 connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
118 pushButtonDown = new QPushButton(tr("-"));
119 connect(pushButtonDown, SIGNAL(clicked()), this, SLOT(down()));
121 pushButtonNext = new QPushButton(tr("Next"));
122 connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
124 pushButtonFinish = new QPushButton(tr("Finish"));
125 connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
128 void CourseDialog::createTable(QWidget *parent)
130 table = new QTableWidget(ROWS, COLS, parent);
133 headers << "" << "Par" << "HCP" << "Len" << "" << "Par" << "HCP" << "Len";
134 table->setVerticalHeaderLabels(headers);
135 table->horizontalHeader()->hide();
136 table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
137 table->verticalHeader()->setResizeMode(QHeaderView::Stretch);
138 table->setStyleSheet(ScoreColor::styleSheet());
141 void CourseDialog::init(Course *course)
143 QTableWidgetItem *par, *hcp;
145 for (int i=0; i<18; i++) {
147 par = new QTableWidgetItem(course->getPar(i));
148 hcp = new QTableWidgetItem(course->getHcp(i));
151 par = new QTableWidgetItem("4");
152 hcp = new QTableWidgetItem("9");
154 QTableWidgetItem *len = new QTableWidgetItem("");
156 QTableWidgetItem *holeNum = new QTableWidgetItem(QString::number(i+1));
157 holeNum->setBackgroundColor(ScoreColor::holeBg());
159 holeNum->setTextAlignment(Qt::AlignCenter);
160 par->setTextAlignment(Qt::AlignCenter);
161 hcp->setTextAlignment(Qt::AlignCenter);
162 len->setTextAlignment(Qt::AlignCenter);
165 table->setItem(0, i, holeNum);
166 table->setItem(1, i, par);
167 table->setItem(2, i, hcp);
168 table->setItem(3, i, len);
171 table->setItem(4, i-9, holeNum);
172 table->setItem(5, i-9, par);
173 table->setItem(6, i-9, hcp);
174 table->setItem(7, i-9, len);
179 void CourseDialog::up(void)
181 QTableWidgetItem *item = table->currentItem();
186 int i = (item->text()).toInt();
188 item->setData(Qt::DisplayRole, value);
191 void CourseDialog::down(void)
193 QTableWidgetItem *item = table->currentItem();
198 int i = (item->text()).toInt();
200 item->setData(Qt::DisplayRole, value);
203 void CourseDialog::next(void)
206 QTableWidgetItem *item = table->currentItem();
207 moveToNextCell(item);
208 //setDefaultScore(table);
212 void CourseDialog::moveToNextCell(QTableWidgetItem *item)
217 QTableWidget *table = item->tableWidget();
222 int row = table->currentRow();
223 int col = table->currentColumn();
225 if (col < (COLS-1)) {
228 else if (col == (COLS-1)) {
230 row = (row == ROW_PAR_2) ? ROW_PAR : ROW_PAR_2;
232 //qDebug() << "new cell: " << row << "/" << col;
233 table->setCurrentCell(row, col);
236 void CourseDialog::results(QVector<QString> &par,
237 QVector<QString> &hcp,
238 QVector<QString> &len)
240 for (int i = 0; i < 9; i++) {
241 QTableWidgetItem *frontPar = table->item(ROW_PAR, i);
242 QTableWidgetItem *backPar = table->item(ROW_PAR_2, i);
243 QTableWidgetItem *frontHcp = table->item(ROW_HCP, i);
244 QTableWidgetItem *backHcp = table->item(ROW_HCP_2, i);
245 QTableWidgetItem *frontLen = table->item(ROW_LEN, i);
246 QTableWidgetItem *backLen = table->item(ROW_LEN_2, i);
249 par[i] = frontPar->text();
251 par[i+9] = backPar->text();
253 hcp[i] = frontHcp->text();
255 hcp[i+9] = backHcp->text();
257 len[i] = frontLen->text();
259 len[i+9] = backLen->text();
263 // Only par is mandatory
264 bool CourseDialog::validate(void)
266 for (int i = 0; i < 9; i++) {
267 QTableWidgetItem *frontItem = table->item(ROW_PAR, i);
268 QTableWidgetItem *backItem = table->item(ROW_PAR_2, i);
270 if (!frontItem || !backItem)
273 QString str1 = frontItem->text();
274 QString str2 = backItem->text();
276 if (str1.isEmpty() || str2.isEmpty())
282 void CourseDialog::finish(void)
287 qDebug() << "CourseDialog: invalid data, cancel or correct";