Debian Codegraph @

src/newgamedlg.cc

raw symbols SCIP
1 // Version: $Id $
2 /***************************************************************************
3 * Copyright (C) 2004-2005 Artur Wiebe *
4 * [email protected] *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 ***************************************************************************/
21 #include <stdlib.h>
22
23 #include <QLayout>
24 #include <QDebug>
25
26 #include "newgamedlg.h"
27 #include "pdn.h"
28 #include "common.h"
29 #include "history.h"
30
31 #include "player.h"
32
33
34 #define BEGINNER 2
35 #define NOVICE 4
36 #define AVERAGE 6
37 #define GOOD 7
38 #define EXPERT 8
39 #define MASTER 9
40
41 #define CFG_SKILL CFG_KEY"Skill"
42 #define CFG_RULES CFG_KEY"Rules"
43 #define CFG_WHITE CFG_KEY"White"
44 #define CFG_PLAYER1 CFG_KEY"Player1"
45 #define CFG_PLAYER2 CFG_KEY"Player2"
46
47
48 myNewGameDlg::myNewGameDlg(QWidget* parent)
49 : QDialog(parent)
50 {
51 setModal(true);
52 setWindowTitle(tr("New Game")+QString(" - "APPNAME));
53
54
55 /*
56 * buttons, options.
57 */
58 start_button = new QPushButton(tr("&Start"), this);
59 start_button->setDefault(true);
60 connect(start_button, SIGNAL(clicked()), this, SLOT(slot_start()));
61
62 QPushButton* cn = new QPushButton(tr("&Cancel"), this);
63 connect(cn, SIGNAL(clicked()), this, SLOT(slot_reject()));
64
65 // TODO - better text
66 m_freeplace = new QCheckBox(tr("Free Men Placement"), this);
67
68 QHBoxLayout* buttons_layout = new QHBoxLayout();
69 buttons_layout->addWidget(m_freeplace);
70 buttons_layout->addStretch();
71 buttons_layout->addWidget(start_button);
72 buttons_layout->addWidget(cn);
73
74
75 /*
76 * global layout.
77 */
78 QHBoxLayout* players_layout = new QHBoxLayout();
79 players_layout->addWidget(create_player_one());
80 players_layout->addWidget(create_player_two());
81
82 QVBoxLayout* global_layout = new QVBoxLayout(this);
83 global_layout->addLayout(players_layout);
84 global_layout->addLayout(buttons_layout);
85 }
86
87
88 myNewGameDlg::~myNewGameDlg()
89 {
90 }
91
92
93 QWidget* myNewGameDlg::create_human_options()
94 {
95 QFrame* frm = new QFrame();
96 QVBoxLayout* frm_layout = new QVBoxLayout(frm);
97 frm_layout->addWidget(new QLabel("No options available."));
98 frm_layout->addStretch();
99
100 return frm;
101 }
102
103
104 QWidget* myNewGameDlg::create_player_one()
105 {
106 m_player_one.box = new QGroupBox(tr("Player One"), this);
107
108 // name
109 m_player_one.name = new QLineEdit(m_player_one.box);
110
111 // rules group box
112 m_player_one.rules = new QGroupBox(tr("Rules"), m_player_one.box);
113 m_player_one.rule_english = new QRadioButton(
114 myHistory::typeToString(ENGLISH), m_player_one.rules);
115 m_player_one.rule_russian = new QRadioButton(
116 myHistory::typeToString(RUSSIAN), m_player_one.rules);
117
118 QVBoxLayout* rules_layout = new QVBoxLayout(m_player_one.rules);
119 rules_layout->addWidget(m_player_one.rule_english);
120 rules_layout->addWidget(m_player_one.rule_russian);
121
122 // play white men?
123 m_player_one.white = new QCheckBox(tr("White"), m_player_one.box);
124
125 // layout
126 QVBoxLayout* vb1_layout = new QVBoxLayout(m_player_one.box);
127 vb1_layout->addWidget(m_player_one.name);
128 vb1_layout->addWidget(m_player_one.rules);
129 vb1_layout->addWidget(m_player_one.white);
130
131 return m_player_one.box;
132 }
133
134
135 QWidget* myNewGameDlg::create_player_two()
136 {
137 m_player_two.box = new QGroupBox(tr("Player Two"), this);
138
139 // name
140 m_player_two.name = new QLineEdit(m_player_two.box);
141
142 // options
143 m_player_two.options = new QTabWidget(m_player_two.box);
144 m_player_two.options->insertTab(COMPUTER, create_computer_options(), tr("Computer"));
145 m_player_two.options->insertTab(HUMAN, create_human_options(), tr("Human"));
146 connect(m_player_two.options, SIGNAL(currentChanged(int)),
147 this, SLOT(slot_game(int)));
148
149 /*
150 * frame layout
151 */
152 QVBoxLayout* frm_layout = new QVBoxLayout(m_player_two.box);
153 frm_layout->addWidget(m_player_two.name);
154 frm_layout->addWidget(m_player_two.options);
155
156 return m_player_two.box;
157 }
158
159
160 QWidget* myNewGameDlg::create_computer_options()
161 {
162 QFrame* frm = new QFrame();
163
164 // skills
165 QGroupBox* skills = new QGroupBox(tr("Skill"), frm);
166 m_player_two.computer.skills[BEGINNER] = new QRadioButton(tr("Beginner"),
167 skills);
168 m_player_two.computer.skills[NOVICE] = new QRadioButton(tr("Novice"),
169 skills);
170 m_player_two.computer.skills[AVERAGE] = new QRadioButton(tr("Average"),
171 skills);
172 m_player_two.computer.skills[GOOD] = new QRadioButton(tr("Good"),
173 skills);
174 m_player_two.computer.skills[EXPERT] = new QRadioButton(tr("Expert"),
175 skills);
176 m_player_two.computer.skills[MASTER] = new QRadioButton(tr("Master"),
177 skills);
178
179 QGridLayout* skills_layout = new QGridLayout(skills);
180 int row = 0;
181 int col = 0;
182 foreach(QRadioButton* rb, m_player_two.computer.skills) {
183 skills_layout->addWidget(rb, row++, col);
184 connect(rb, SIGNAL(clicked()), this, SLOT(slot_skills()));
185 if(row > 2) {
186 row = 0;
187 col = 1;
188 }
189 }
190
191 // layout
192 QHBoxLayout* frm_layout = new QHBoxLayout(frm);
193 frm_layout->addWidget(skills);
194
195 return frm;
196 }
197
198
199 void myNewGameDlg::slot_reject()
200 {
201 reject();
202 }
203
204
205 void myNewGameDlg::slot_start()
206 {
207 accept();
208 }
209
210
211 void myNewGameDlg::slot_skills()
212 {
213 QRadioButton* skill = 0;
214 foreach(QRadioButton* rb, m_player_two.computer.skills) {
215 if(rb->isChecked()) {
216 skill = rb;
217 break;
218 }
219 }
220
221 if(skill)
222 m_player_two.name->setText("*"+skill->text()+"*");
223 }
224
225
226 void myNewGameDlg::slot_game_start(int id)
227 {
228 slot_game(id);
229 slot_start();
230 }
231
232
233 void myNewGameDlg::slot_game(int id)
234 {
235 start_button->setEnabled(true);
236 m_player_one.box->setEnabled(true);
237 m_player_two.options->setEnabled(true);
238
239 if(m_player_two.last_game_index==HUMAN) {
240 m_cfg_player2 = m_player_two.name->text();
241 }
242
243 m_player_two.last_game_index = id;
244
245 switch(id) {
246 case COMPUTER:
247 m_player_two.name->setReadOnly(true);
248 slot_skills();
249
250 m_player_one.rules->setEnabled(true);
251 m_player_one.white->setEnabled(true);
252 break;
253
254 case HUMAN:
255 m_player_two.name->setReadOnly(false);
256 m_player_two.name->setText(m_cfg_player2);
257
258 m_player_one.rules->setEnabled(true);
259 m_player_one.white->setEnabled(true);
260 break;
261
262 default:
263 qDebug() << __PRETTY_FUNCTION__ << "ERR";
264 break;
265 }
266 }
267
268
269 void myNewGameDlg::writeSettings(QSettings* cfg)
270 {
271 cfg->setValue(CFG_SKILL, skill());
272 cfg->setValue(CFG_RULES, rules());
273 cfg->setValue(CFG_WHITE, m_player_one.white->isChecked());
274
275 cfg->setValue(CFG_PLAYER1, m_player_one.name->text());
276 cfg->setValue(CFG_PLAYER2, m_cfg_player2);
277 }
278
279
280 void myNewGameDlg::readSettings(QSettings* cfg)
281 {
282 int skills = cfg->value(CFG_SKILL, BEGINNER).toInt();
283 QMap<int, QRadioButton*>::iterator it;
284 it = m_player_two.computer.skills.find(skills);
285 if(it != m_player_two.computer.skills.end()) {
286 it.value()->setChecked(true);
287 } else {
288 m_player_two.computer.skills[BEGINNER]->setChecked(true);
289 }
290 slot_skills();
291
292 int rules = cfg->value(CFG_RULES, ENGLISH).toInt();
293 if(rules == ENGLISH) {
294 m_player_one.rule_english->setChecked(true);
295 } else {
296 m_player_one.rule_russian->setChecked(true);
297 }
298
299 m_player_one.white->setChecked(cfg->value(CFG_WHITE, false).toBool());
300
301 m_player_one.name->setText(cfg->value(CFG_PLAYER1,
302 getenv("USER")).toString());
303 m_cfg_player2 = cfg->value(CFG_PLAYER2, "Player2").toString();
304 }
305
306
307 int myNewGameDlg::skill() const
308 {
309 QMap<int, QRadioButton*>::const_iterator it;
310 it = m_player_two.computer.skills.begin();
311 for(; it!=m_player_two.computer.skills.end(); ++it) {
312 if(it.value()->isChecked()) {
313 return it.key();
314 }
315 }
316
317 qDebug() << __PRETTY_FUNCTION__ << "No skill selected.";
318 return BEGINNER;
319 }
320
321
322 int myNewGameDlg::rules() const
323 {
324 if(m_player_one.rule_english->isChecked()) {
325 return ENGLISH;
326 }
327 return RUSSIAN;
328 }
329