remove old files add new sources
[mancala] / src / MainWindow.cpp
1 /*
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
5
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.
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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "MainWindow.h"
21
22 #include <QMainWindow>
23 #include <QStatusBar>
24 #include <QGraphicsView>
25 #include <QFile>
26 #include <QPushButton>
27 #include <QLabel>
28 #include <QAction>
29 #include <QtGui>
30 #include <QStackedWidget>
31
32 //#include <kdebug.h>
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>
40 //#include <kmenu.h>
41 //#include <kstandardgameaction.h>
42 //#include <kselectaction.h>
43 //#include <kgameclock.h>
44
45 #include "ThemeManager.h"
46 #include "GameInfo.h"
47 #include "GraphicsScene.h"
48 #include "settingswidget.h"
49 //#include "GraphicsView.h"
50
51 MainWindow::MainWindow(QWidget* parent) {
52
53     m_StackedWidget = new QStackedWidget();
54     setWindowTitle("Mancala");
55     setupGame();
56     setupWidgets();
57     setupActions();
58
59 }
60
61 MainWindow::~MainWindow(){}
62
63 void MainWindow::setupGame(){
64
65     //gameInfo
66     setGameName(QString("kalah")) ;
67     m_gameNameId = 0;
68     m_gameInfo = new GameInfo();
69
70     m_gameInfo->setGameInfo(m_gameName);
71
72     //gameTheme
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);
76
77     //gameController
78     m_gameController = new GameController(m_gameInfo);
79
80 }
81
82 void MainWindow::setupWidgets(){
83
84     //status bar
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);
89
90     settings = new SettingsWidget(this);
91
92     //set to m_gameNameList - we should done it initially
93     reader = new QXmlStreamReader();
94     setGameName();
95     settings->setGameNames(m_gameNameList);
96
97     connect(settings,
98             SIGNAL(start()),
99             this,
100             SLOT(slotStartGame()));
101
102     connect(settings,
103             SIGNAL(levelChanged(int)),
104             this,
105             SLOT(slotChangeDifficulty(int)));
106
107     connect(settings,
108             SIGNAL(gameChanged(int)),
109             this,
110             SLOT(slotChangeGameName(int)));
111
112     m_StackedWidget->addWidget(settings);
113
114     msgLabel = new QLabel();
115     turnLabel = new QLabel("Turn: your");
116     humanScore = new QLabel("Human: 0");
117     aiScore = new QLabel("Computer: 0");
118
119     statusBar()->addWidget(turnLabel);
120     statusBar()->addWidget(msgLabel,1);
121     statusBar()->addWidget(humanScore);
122     statusBar()->addWidget(aiScore);
123
124
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();
128     //showScore
129     connect(m_gameController,SIGNAL(signalShowScore(int,bool)),
130             this,SLOT(slotShowScore(int,bool)));
131     //showTurn
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)));
137
138
139     //GraphicsScene and GraphicsView
140     m_scene = new GraphicsScene(m_gameInfo,m_gameTheme,m_gameController,this);
141     m_view = new QGraphicsView(m_scene, this);
142
143     m_StackedWidget->addWidget(m_view);
144
145     m_StackedWidget->setCurrentIndex(0);
146
147     setCentralWidget(m_StackedWidget);
148
149 }
150
151
152
153 void MainWindow::setupActions(){
154
155     mainMenu = menuBar()->addMenu("Main");
156
157 //    helpAction = new QAction(tr("Help"), this);
158 //    connect(helpAction,
159 //            SIGNAL(triggered()),
160 //            this,
161 //            SLOT(help()));
162
163     //New Game KSelectAction ---------------------------
164     // newGameAct = new KSelectAction(i18n("&New Game"),this);
165     // actionCollection()->addAction("new_game",newGameAct);
166
167     restartGameAction = new QAction(tr("restart game"), this);
168
169     connect(restartGameAction,
170             SIGNAL(triggered()),
171             this,
172             SLOT(slotStartGame()));
173
174     mainMenu->addAction(restartGameAction);
175
176     newGameAction = new QAction(tr("new game"), this);
177
178     connect(newGameAction,
179             SIGNAL(triggered()),
180             this,
181             SLOT(slotShowSettings()));
182
183     mainMenu->addAction(newGameAction);
184
185
186     helpAction = new QAction(tr("Help"), this);
187
188     connect(helpAction,
189             SIGNAL(triggered()),
190             this,
191             SLOT(help()));
192
193     mainMenu->addAction(helpAction);
194
195 //    //set to m_gameNameList - we should done it initially
196 //    reader = new QXmlStreamReader();
197 //    setGameName();
198
199 //    newGameAct->setItems(m_gameNameList);
200 //    newGameAct->setCurrentItem(0);
201     //kDebug() << "Current Text:" << newGameAct->currentText();
202
203 //    connect( newGameAct , SIGNAL(triggered(int)),m_gameController, SLOT(slotGameOverForNewGame(int) ) );
204 //    connect( m_gameController,SIGNAL(signalNewGame(int)),this,SLOT(newGame(int)));
205
206
207     //Quit Action ------------------
208     // KStandardAction::quit(kapp, SLOT(quit()),  actionCollection());
209
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()) );
214
215     // Difficulty
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);
221
222     // setupGUI();
223 }
224
225
226 //---------------------parsting starts -------------------------
227
228 //ready the file to be parsed by reader
229 void MainWindow::setGameName(){
230
231     QString fileName = QString("/opt/mancala/data/gamelist.xml");
232     //kDebug() << "File Name: " << fileName;
233     if(fileName.isEmpty()) return;
234
235     QFile infoFile(fileName);
236     if (!infoFile.open(QFile::ReadOnly | QFile::Text)) return;
237     parseXml(&infoFile);
238
239 }
240
241 //parse gamelist.xml file
242 void MainWindow::parseXml(QIODevice *device){
243
244     reader->setDevice(device);
245
246     while(!reader->atEnd()){
247         reader->readNext();
248
249         if(reader->isStartElement()){
250
251             if(reader->name() == "gamelist")
252                 parseNames();
253             else reader->raiseError(QObject::tr("This is not the exact file Needed"));
254         }
255     }
256
257 }
258
259 //set the names in m_gameNameList
260 void MainWindow::parseNames(){
261
262     Q_ASSERT(reader->isStartElement() && reader->name() == "gamelist");
263
264     while(!reader->atEnd()){
265         reader->readNext();
266
267         if(reader->isEndElement())
268             break;
269
270         if(reader->isStartElement()){
271
272             Q_ASSERT( reader->name() == "name");
273             m_gameNameList << reader->readElementText();
274
275         }
276     }
277
278 }
279
280 //------------------------------ parsing ends ---------------------------
281
282
283
284 //------------------------------ slot zone starts -------------------------
285
286 //Now working with this
287 void MainWindow::newGame(int gameId){
288
289     //kDebug() << "newGame called";
290     qDebug() << "newGame called" << gameId;
291     m_gameName = m_gameNameList[gameId];
292
293     //gameInfo
294     m_gameInfo->initializeAll();
295     m_gameInfo->setGameInfo(m_gameName);
296
297     //gameTheme
298     m_gameTheme->setTheme(m_gameName);
299     emit signalLoadTheme();
300
301     m_scene->slotStartGame();
302 }
303
304 void MainWindow::slotStartGame(){
305
306     newGame(m_gameNameId);
307
308     m_StackedWidget->setCurrentIndex(1);
309
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);
315
316     slotMessage(QString("New game started..."));
317
318 }
319
320 void MainWindow::slotShowScore(int score,bool human){
321
322     if(human){
323         // statusBar()->changeItem(tr("Human Player: %1",score),1);
324        humanScore->setText(tr("Human: %1").arg(score) );
325     }
326     else{
327         // statusBar()->changeItem(tr("Computer: %1",score),2);
328         aiScore->setText(tr("Computer: %1").arg(score) );
329     }
330 }
331
332 void MainWindow::slotShowTurn(bool human){
333
334 //    if(human)
335 //        // statusBar()->changeItem(tr("Turn: your"),0);
336 //    else
337 //        // statusBar()->changeItem(tr("Turn: computer"),0);
338
339     if(human){
340         turnLabel->setText(tr("Turn: your"));
341     }
342     else{
343         turnLabel->setText(tr("Turn: computer"));
344     }
345 }
346
347 void MainWindow::slotMessage(QString str){
348
349     // statusBar()->changeItem(str,4);
350     msgLabel->setText(str);
351 }
352
353
354 void MainWindow::slotPause(){
355
356 }
357
358 void MainWindow::slotShowGameOver(){
359     slotMessage( tr("Game Over") );
360
361     QTimer::singleShot(2500,this,SLOT(slotShowSettings()));
362 }
363
364 void MainWindow::slotShowSettings(){
365     m_StackedWidget->setCurrentIndex(0);
366 }
367
368 //void MainWindow::slotChangeDifficulty(KGameDifficulty::standardLevel level){
369 //
370 //    switch(level) {
371 //
372 //        case KGameDifficulty::Easy:
373 //            emit signalGameDifficulty(GameController::EASY);
374 //            break;
375 //        case KGameDifficulty::Medium:
376 //            emit signalGameDifficulty(GameController::MEDIUM);
377 //            break;
378 //        case KGameDifficulty::Hard:
379 //            emit signalGameDifficulty(GameController::HARD);
380 //            break;
381 //    }
382 //}
383
384 void MainWindow::slotChangeDifficulty(int level){
385
386     switch(level){
387     case Easy:
388         emit signalGameDifficulty(GameController::EASY);
389         qDebug() << "Level: " << level;
390         break;
391     case Medium:
392         emit signalGameDifficulty(GameController::MEDIUM);
393         qDebug() << "Level: " << level;
394         break;
395     case Hard:
396         emit signalGameDifficulty(GameController::HARD);
397         qDebug() << "Level: " << level;
398         break;
399     }
400 }
401
402 void MainWindow::slotChangeGameName(int game){
403     qDebug() << "slotChangeGameName:" << game;
404     setGameName(m_gameNameList[game]);
405     m_gameNameId = game;
406 }
407
408 void MainWindow::help(){
409     QDesktopServices::openUrl(QUrl("file:///opt/mancala/data/mancala.html"));
410 }
411
412
413 //----------------------------- slot zone ends --------------------------
414
415
416 void MainWindow::resizeEvent(QResizeEvent *event){
417
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);
422
423 }
424
425 void MainWindow::setGameName(QString gameName){
426     m_gameName = gameName; 
427 }