You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tdegames/qnetchess/src/mainwindow.cpp

295 lines
5.9 KiB

/*
* $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"