summaryrefslogtreecommitdiffstats
path: root/qnetchess/src/mainwindow.cpp
blob: d490f85d13773c9360220d50025fc4b941bc80a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * $Id: mainwindow.cpp,v 0.1 2005/01/08 12:20:13 denis Exp $
 *
 * Author: Denis Kozadaev (denis@tambov.ru)
 * Description:
 *
 * See also: style(9)
 *
 * Hacked by:
 */

#include <ntqapplication.h>
#include <ntqmenubar.h>
#include <ntqstatusbar.h>
#include <ntqpixmap.h>
#include <ntqvalidator.h>
#include <ntqmessagebox.h>

#include "mainwindow.h"
#include "gameboard.h"

#include "xpm/chess.xpm"
#include "xpm/quit.xpm"
#include "xpm/new_game.xpm"

extern TQColor	cw, cb;

MainWindow::MainWindow(TQWidget *parent, const char *name)
	:TQMainWindow(parent, name)
{
	TQPixmap	xpm(chess_xpm);

	cw = TQColor(0x8F, 0xDF, 0xF0);
	cb = TQColor(0x70, 0x9F, 0xDF);
	setIcon(xpm);
	wrk = new TQWorkspace(this);
	sock = new GameSocket(this);
	game = new TQPopupMenu(this);
	game->insertItem(TQPixmap((const char **)new_game), tr("New"),
		this, SLOT(newGame()), CTRL + Key_N);
	id = game->insertItem(xpm, tr("Save image"), this,
		SLOT(saveImage()), CTRL + Key_S);
	game->setItemEnabled(id, FALSE);
	game->insertSeparator();
	game->insertItem(TQPixmap((const char **)quit_xpm), tr("Quit"), tqApp,
		SLOT(quit()), CTRL + Key_Q);
	help = new TQPopupMenu(this);
	help->insertItem(xpm, tr("About the game"), this, SLOT(about()));

	menuBar()->insertItem(tr("Game"), game);
	menuBar()->insertSeparator();
	menuBar()->insertItem(tr("Help"), help);

	setCentralWidget(wrk);
	ready_txt = tr("Ready");
	showStatus(ready_txt);

	TQObject::connect(sock, SIGNAL(acceptConnection(int)),
		this, SLOT(newGame(int)));
	TQObject::connect(wrk, SIGNAL(windowActivated(TQWidget *)),
		this, SLOT(activated(TQWidget *)));
}

MainWindow::~MainWindow()
{

	delete help;
	delete game;
	delete sock;
	delete wrk;
}


void
MainWindow::showStatus(const TQString &txt)
{

	statusBar()->message(txt);
}


void
MainWindow::newGame()
{
	SelectGame	*dlg;
	GameBoard	*brd;
	TQString	hst;

	dlg = new SelectGame(this);
	dlg->setHosts(hosts);
	if (dlg->exec()) {
		hosts = dlg->hosts();
		hst = dlg->host();
		brd = new GameBoard(dlg->gameType(), hst, wrk);
		showStatus(brd->status());
		TQObject::connect(brd, SIGNAL(showStatus(const TQString&)),
			this, SLOT(showStatus(const TQString&)));
		brd->show();
	}
	delete dlg;
}


void
MainWindow::newGame(int sock)
{
	GameBoard	*brd;

	brd = new GameBoard(sock, wrk);
	showStatus(brd->status());
	TQObject::connect(brd, SIGNAL(showStatus(const TQString&)),
		this, SLOT(showStatus(const TQString&)));
	brd->show();
	game->setItemEnabled(id, TRUE);
}


void
MainWindow::about()
{

	TQMessageBox::about(this, tr("About") + " QNetChess", "QNetChess " + tr(
		"is a network game chess for two players.\n"
		"It has a client and a server in the same program.\n"
		"You can modify and redistribute the source code\n"
		"because it is under GPL.\n\n"
		"Russia, Tambov, 2005, 2020 (denis@tambov.ru)"
	));
}


void
MainWindow::activated(TQWidget *w)
{
	GameBoard	*brd = (GameBoard *)w;

	game->setItemEnabled(id, brd != NULL);
	if (brd != NULL)
		showStatus(brd->status());
	else
		showStatus(ready_txt);
}


void
MainWindow::saveImage()
{
	GameBoard	*brd = (GameBoard *)wrk->activeWindow();

	if (brd != NULL)
		brd->saveImage();
}

//-----------------------------------------------------------------------------

SelectGame::SelectGame(TQWidget *parent, const char *name)
	:TQDialog(parent, name)
{

	setCaption(tr("New game with..."));
	l1 = new TQLabel(tr("To play with "), this);
	hst = new TQComboBox(TRUE, this);
	hst->setValidator(new TQRegExpValidator(
		TQRegExp("([a-zA-Z0-9]*\\.)*[a-zA-Z]"), hst));
	btn = new TQButtonGroup(tr("Choose your game"), this);
	wg = new TQRadioButton(tr("White game"), btn);
	bg = new TQRadioButton(tr("Black game"), btn);
	box = new TQGroupBox(this);
	Ok = new TQPushButton(tr("Play!"), box);
	Cancel = new TQPushButton(tr("Cancel"), box);

	resize(400, TQFontMetrics(font()).lineSpacing() * 7);
	setMinimumSize(size());
	setMaximumSize(size());

	TQObject::connect(Ok, SIGNAL(clicked()), this, SLOT(accept()));
	TQObject::connect(Cancel, SIGNAL(clicked()), this, SLOT(reject()));
	TQObject::connect(wg, SIGNAL(clicked()), this, SLOT(checkParams()));
	TQObject::connect(bg, SIGNAL(clicked()), this, SLOT(checkParams()));
	TQObject::connect(hst, SIGNAL(textChanged(const TQString&)),
		this, SLOT(checkParams(const TQString&)));

	checkParams();
}

SelectGame::~SelectGame()
{

	delete Cancel;
	delete Ok;
	delete box;
	delete bg;
	delete wg;
	delete btn;
	delete hst;
	delete l1;
}


void
SelectGame::resizeEvent(TQResizeEvent *e)
{
	TQFontMetrics	fm(font());
	int		w = e->size().width(),
			h = e->size().height(),
			fh = fm.lineSpacing() + 2,
			bl;

	TQDialog::resizeEvent(e);
	l1->setGeometry(0, 0, fm.width(l1->text()) + 20, fh);
	hst->move(l1->x() + l1->width(), l1->y());
	hst->resize(w - hst->x(), l1->height());
	btn->move(l1->x(), l1->y() + l1->height());
	btn->resize(w, l1->height() * 3 + 2);
	wg->setGeometry(2, fh, btn->width() - 4, fh);
	bg->setGeometry(wg->x(), wg->y() + wg->height(),
		wg->width(), wg->height());
	box->move(btn->x(), btn->y() + btn->height());
	box->resize(w, h - box->y());
	bl = box->width() / 5;
	Ok->move(bl, 4);
	Ok->resize(bl, box->height() - Ok->y() * 2);
	Cancel->setGeometry(Ok->x() + Ok->width() + bl, Ok->y(),
		Ok->width(), Ok->height());
}


void
SelectGame::checkParams()
{
	bool	res;

	res = !hst->currentText().isEmpty() &&
		(bg->isChecked() || wg->isChecked());
	Ok->setEnabled(res);
}


void
SelectGame::checkParams(const TQString&)
{

	checkParams();
}


TQString
SelectGame::host()
{
	TQString	h(hst->currentText());

	return h.left(h.findRev(':'));
}


TQStringList
SelectGame::hosts()
{
	TQStringList	lst;
	int		i, cnt;

	cnt = hst->count();
	lst += host();
	for (i = 0; i < cnt; i++)
		lst += hst->text(i);

	return (lst);
}


void
SelectGame::setHosts(const TQStringList &h)
{

	hst->insertStringList(h);
}


GameBoard::GameType
SelectGame::gameType()
{
	GameBoard::GameType	gt;

	if (wg->isChecked())
		gt = GameBoard::WHITE;
	else if (bg->isChecked())
		gt = GameBoard::BLACK;
	else
		gt = GameBoard::NOGAME;

	return (gt);
}

#include "mainwindow.moc"