toruschess/src/testgame.cpp

136 lines
5.0 KiB
C++

/***************************************************************************
* Copyright (C) 2009 by Stefan Bühler *
* stbuehler@web.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "testgame.h"
#include <QGridLayout>
#include <QResizeEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QStatusBar>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QActionGroup>
#include "ai.h"
namespace toruschess {
static QAction *makeAction(QObject *parent, const QString &text, const QObject * receiver, const char * member, const QKeySequence &shortcut = QKeySequence(), QActionGroup *group = NULL) {
QAction *a = new QAction(text, parent);
a->setShortcut(shortcut);
if (group) a->setActionGroup(group);
QObject::connect(a, SIGNAL(triggered(bool)), receiver, member);
return a;
}
TestGame::TestGame(QWidget *parent)
: QMainWindow(parent), m_game(new Game()), m_optionDlg(new OptionDlg(m_game, this)), m_viewMode((ViewMode) -1) {
connect(this, SIGNAL(changedViewMode(ViewMode)), m_optionDlg, SLOT(changedViewMode(ViewMode)));
connect(m_optionDlg, SIGNAL(setViewMode(ViewMode)), this, SLOT(setViewMode(ViewMode)));
setWindowTitle("Torus Chess");
QWidget *centralWidget = new QWidget(this);
m_fieldsLayout = new QStackedLayout(centralWidget);
m_field2d = new Field2D(m_game, centralWidget);
m_field3d = new Field3D(m_game, centralWidget);
m_fieldsLayout->addWidget(m_field2d);
m_fieldsLayout->addWidget(m_field3d);
centralWidget->setLayout(m_fieldsLayout);
setCentralWidget(centralWidget);
statusBar()->show();
connect(m_game, SIGNAL(changed(GameState)), this, SLOT(gameChanged(GameState)));
m_game->restart();
QAction
*act_game_newGame = makeAction(this, "&New Game", this, SLOT(menuRestart()), Qt::Key_F2),
*act_game_options = makeAction(this, "&Options", this, SLOT(menuOptions()), Qt::CTRL + Qt::Key_O),
*act_game_quit = makeAction(this, "&Quit", this, SLOT(close()));
QActionGroup *viewGroup = new QActionGroup(this);
m_act_view_2d = makeAction(this, "&2D-View", this, SLOT(menuView2d()), Qt::CTRL + Qt::Key_2, viewGroup);
m_act_view_3d = makeAction(this, "&3D-View", this, SLOT(menuView3d()), Qt::CTRL + Qt::Key_3, viewGroup);
m_act_view_torus = makeAction(this, "&Torus-View", this, SLOT(menuViewTorus()), Qt::CTRL + Qt::Key_4, viewGroup);
act_game_quit->setMenuRole(QAction::QuitRole);
QMenuBar *menuBar = new QMenuBar(this);
QMenu *menuGame = menuBar->addMenu("&Game");
menuGame->addAction(act_game_newGame);
menuGame->addAction(act_game_options);
menuGame->addSeparator();
menuGame->addAction(act_game_quit);
QMenu *menuView = menuBar->addMenu("&View");
menuView->addActions(viewGroup->actions());
setMenuBar(menuBar);
setViewMode(VIEW_2D);
updateGeometry();
}
void TestGame::gameChanged(GameState state) {
statusBar()->showMessage(state2string(state));
}
void TestGame::menuRestart() {
m_game->restart();
}
void TestGame::menuOptions() {
m_optionDlg->exec();
}
void TestGame::setViewMode(ViewMode viewMode) {
if (m_viewMode == viewMode) return;
m_viewMode = viewMode;
switch (m_viewMode) {
case VIEW_2D:
m_fieldsLayout->setCurrentIndex(0);
m_act_view_2d->setChecked(true);
break;
case VIEW_3D:
m_fieldsLayout->setCurrentIndex(1);
m_act_view_3d->setChecked(true);
m_field3d->setBoardMode(Field3D::PLANE);
break;
case VIEW_TORUS:
m_fieldsLayout->setCurrentIndex(1);
m_act_view_torus->setChecked(true);
m_field3d->setBoardMode(Field3D::TORUS);
break;
}
emit changedViewMode(viewMode);
}
void TestGame::menuView2d() {
setViewMode(VIEW_2D);
}
void TestGame::menuView3d() {
setViewMode(VIEW_3D);
}
void TestGame::menuViewTorus() {
setViewMode(VIEW_TORUS);
}
}