2 Mancala - A Historical Board Game
3 Copyright (C) 2009-2010 A.H.M.Mahfuzur Rahman 65mahfuz90@gmail.com
4 Copyright (c) 2010 Reto Zingg g.d0b3rm4n@gmail.com
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License, or (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "MainWindow.h"
22 #include <QMainWindow>
24 #include <QGraphicsView>
26 #include <QPushButton>
30 #include <QStackedWidget>
33 //#include <kaction.h>
34 //#include <klocale.h>
35 //#include <kactioncollection.h>
36 //#include <kstandardaction.h>
37 //#include <kapplication.h>
38 //#include <kstandarddirs.h>
39 //#include <kstatusbar.h>
41 //#include <kstandardgameaction.h>
42 //#include <kselectaction.h>
43 //#include <kgameclock.h>
45 #include "ThemeManager.h"
47 #include "GraphicsScene.h"
48 #include "settingswidget.h"
49 //#include "GraphicsView.h"
51 MainWindow::MainWindow(QWidget* parent) {
53 m_StackedWidget = new QStackedWidget();
54 setWindowTitle("Mancala");
61 MainWindow::~MainWindow(){}
63 void MainWindow::setupGame(){
66 setGameName(QString("kalah")) ;
68 m_gameInfo = new GameInfo();
70 m_gameInfo->setGameInfo(m_gameName);
73 QString fileName = QString("/opt/mancala/data/themes/theme_%1.svg").arg(m_gameName);
74 m_gameTheme = new ThemeManager(fileName);
75 m_gameTheme->setTheme(m_gameName);
78 m_gameController = new GameController(m_gameInfo);
82 void MainWindow::setupWidgets(){
85 // statusBar()->insertItem(QString(),4);
86 // statusBar()->insertItem(tr("Turn: your"), 0 );
87 // statusBar()->insertItem(tr( "Human Player: 0"),1 );
88 // statusBar()->insertItem(tr( "Computer: 0"),2);
90 settings = new SettingsWidget(this);
92 //set to m_gameNameList - we should done it initially
93 reader = new QXmlStreamReader();
95 settings->setGameNames(m_gameNameList);
100 SLOT(slotStartGame()));
103 SIGNAL(levelChanged(int)),
105 SLOT(slotChangeDifficulty(int)));
108 SIGNAL(gameChanged(int)),
110 SLOT(slotChangeGameName(int)));
112 m_StackedWidget->addWidget(settings);
114 msgLabel = new QLabel();
115 turnLabel = new QLabel("Turn: your");
116 humanScore = new QLabel("Human: 0");
117 aiScore = new QLabel("Computer: 0");
119 statusBar()->addWidget(turnLabel);
120 statusBar()->addWidget(msgLabel,1);
121 statusBar()->addWidget(humanScore);
122 statusBar()->addWidget(aiScore);
125 /*setting up the connections for m_gameTheme and m_gameController with this*/
126 connect(this, SIGNAL( signalLoadTheme()), m_gameTheme, SLOT( slotLoadTheme() ));
127 emit signalLoadTheme();
129 connect(m_gameController,SIGNAL(signalShowScore(int,bool)),
130 this,SLOT(slotShowScore(int,bool)));
132 connect(m_gameController,SIGNAL(signalShowTurn(bool)),this,SLOT(slotShowTurn(bool)));
133 //Message from gameController
134 connect(m_gameController,SIGNAL(signalMessage(QString)),this,SLOT(slotMessage(QString)));
135 connect(m_gameController,SIGNAL(signalGameOver()),this,SLOT(slotShowGameOver()));
136 connect(this,SIGNAL(signalGameDifficulty(GameController::Difficulty)),m_gameController,SLOT(slotGameDifficulty(GameController::Difficulty)));
139 //GraphicsScene and GraphicsView
140 m_scene = new GraphicsScene(m_gameInfo,m_gameTheme,m_gameController,this);
141 m_view = new QGraphicsView(m_scene, this);
143 m_StackedWidget->addWidget(m_view);
145 m_StackedWidget->setCurrentIndex(0);
147 setCentralWidget(m_StackedWidget);
153 void MainWindow::setupActions(){
155 mainMenu = menuBar()->addMenu("Main");
157 // helpAction = new QAction(tr("Help"), this);
158 // connect(helpAction,
159 // SIGNAL(triggered()),
163 //New Game KSelectAction ---------------------------
164 // newGameAct = new KSelectAction(i18n("&New Game"),this);
165 // actionCollection()->addAction("new_game",newGameAct);
167 restartGameAction = new QAction(tr("restart game"), this);
169 connect(restartGameAction,
172 SLOT(slotStartGame()));
174 mainMenu->addAction(restartGameAction);
176 newGameAction = new QAction(tr("new game"), this);
178 connect(newGameAction,
181 SLOT(slotShowSettings()));
183 mainMenu->addAction(newGameAction);
186 helpAction = new QAction(tr("Help"), this);
193 mainMenu->addAction(helpAction);
195 // //set to m_gameNameList - we should done it initially
196 // reader = new QXmlStreamReader();
199 // newGameAct->setItems(m_gameNameList);
200 // newGameAct->setCurrentItem(0);
201 //kDebug() << "Current Text:" << newGameAct->currentText();
203 // connect( newGameAct , SIGNAL(triggered(int)),m_gameController, SLOT(slotGameOverForNewGame(int) ) );
204 // connect( m_gameController,SIGNAL(signalNewGame(int)),this,SLOT(newGame(int)));
207 //Quit Action ------------------
208 // KStandardAction::quit(kapp, SLOT(quit()), actionCollection());
210 // KAction* restartGame = new KAction(this);
211 // restartGame->setText(tr("&Restart Game"));
212 // actionCollection()->addAction("restartgame",restartGame);
213 // connect(restartGame,SIGNAL(triggered(bool)),m_gameController,SLOT(slotGameOverForRestartGame()) );
216 // KGameDifficulty::init(this, this, SLOT(slotChangeDifficulty(KGameDifficulty::standardLevel)));
217 // KGameDifficulty::addStandardLevel(KGameDifficulty::Easy);
218 // KGameDifficulty::addStandardLevel(KGameDifficulty::Medium);
219 // KGameDifficulty::addStandardLevel(KGameDifficulty::Hard);
220 // KGameDifficulty::setLevel(KGameDifficulty::Easy);
226 //---------------------parsting starts -------------------------
228 //ready the file to be parsed by reader
229 void MainWindow::setGameName(){
231 QString fileName = QString("/opt/mancala/data/gamelist.xml");
232 //kDebug() << "File Name: " << fileName;
233 if(fileName.isEmpty()) return;
235 QFile infoFile(fileName);
236 if (!infoFile.open(QFile::ReadOnly | QFile::Text)) return;
241 //parse gamelist.xml file
242 void MainWindow::parseXml(QIODevice *device){
244 reader->setDevice(device);
246 while(!reader->atEnd()){
249 if(reader->isStartElement()){
251 if(reader->name() == "gamelist")
253 else reader->raiseError(QObject::tr("This is not the exact file Needed"));
259 //set the names in m_gameNameList
260 void MainWindow::parseNames(){
262 Q_ASSERT(reader->isStartElement() && reader->name() == "gamelist");
264 while(!reader->atEnd()){
267 if(reader->isEndElement())
270 if(reader->isStartElement()){
272 Q_ASSERT( reader->name() == "name");
273 m_gameNameList << reader->readElementText();
280 //------------------------------ parsing ends ---------------------------
284 //------------------------------ slot zone starts -------------------------
286 //Now working with this
287 void MainWindow::newGame(int gameId){
289 //kDebug() << "newGame called";
290 qDebug() << "newGame called" << gameId;
291 m_gameName = m_gameNameList[gameId];
294 m_gameInfo->initializeAll();
295 m_gameInfo->setGameInfo(m_gameName);
298 m_gameTheme->setTheme(m_gameName);
299 emit signalLoadTheme();
301 m_scene->slotStartGame();
304 void MainWindow::slotStartGame(){
306 newGame(m_gameNameId);
308 m_StackedWidget->setCurrentIndex(1);
310 // TODO: this looks nasty, is there no other way?
311 // w/o the gameboard is tiny, just after a resize it adapts!?
312 QSize oldSize = this->size();
313 this->resize(100,100);
314 this->resize(oldSize);
316 slotMessage(QString("New game started..."));
320 void MainWindow::slotShowScore(int score,bool human){
323 // statusBar()->changeItem(tr("Human Player: %1",score),1);
324 humanScore->setText(tr("Human: %1").arg(score) );
327 // statusBar()->changeItem(tr("Computer: %1",score),2);
328 aiScore->setText(tr("Computer: %1").arg(score) );
332 void MainWindow::slotShowTurn(bool human){
335 // // statusBar()->changeItem(tr("Turn: your"),0);
337 // // statusBar()->changeItem(tr("Turn: computer"),0);
340 turnLabel->setText(tr("Turn: your"));
343 turnLabel->setText(tr("Turn: computer"));
347 void MainWindow::slotMessage(QString str){
349 // statusBar()->changeItem(str,4);
350 msgLabel->setText(str);
354 void MainWindow::slotPause(){
358 void MainWindow::slotShowGameOver(){
359 slotMessage( tr("Game Over") );
361 QTimer::singleShot(2500,this,SLOT(slotShowSettings()));
364 void MainWindow::slotShowSettings(){
365 m_StackedWidget->setCurrentIndex(0);
368 //void MainWindow::slotChangeDifficulty(KGameDifficulty::standardLevel level){
372 // case KGameDifficulty::Easy:
373 // emit signalGameDifficulty(GameController::EASY);
375 // case KGameDifficulty::Medium:
376 // emit signalGameDifficulty(GameController::MEDIUM);
378 // case KGameDifficulty::Hard:
379 // emit signalGameDifficulty(GameController::HARD);
384 void MainWindow::slotChangeDifficulty(int level){
388 emit signalGameDifficulty(GameController::EASY);
389 qDebug() << "Level: " << level;
392 emit signalGameDifficulty(GameController::MEDIUM);
393 qDebug() << "Level: " << level;
396 emit signalGameDifficulty(GameController::HARD);
397 qDebug() << "Level: " << level;
402 void MainWindow::slotChangeGameName(int game){
403 qDebug() << "slotChangeGameName:" << game;
404 setGameName(m_gameNameList[game]);
408 void MainWindow::help(){
409 QDesktopServices::openUrl(QUrl("file:///opt/mancala/data/mancala.html"));
413 //----------------------------- slot zone ends --------------------------
416 void MainWindow::resizeEvent(QResizeEvent *event){
418 //We need scroll Bars to Off so that there is no probability of recursing fitInView
419 //m_view->fitInView(QRect(QPoint(0,0), m_scene->sceneRect().size().toSize()), Qt::KeepAspectRatio);
420 m_view->fitInView(QRect(QPoint(0,0), m_scene->sceneRect().size().toSize()));
421 // KXmlGuiWindow::resizeEvent(event);
425 void MainWindow::setGameName(QString gameName){
426 m_gameName = gameName;