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 "GraphicsScene.h"
22 #include <QGraphicsSceneMouseEvent>
23 #include <QSvgRenderer>
27 // #include <kdebug.h>
28 // #include <ksvgrenderer.h>
31 #include "ThemeManager.h"
34 #include "GameController.h"
35 #include "MainWindow.h"
37 GraphicsScene::GraphicsScene(GameInfo *gameInfo,ThemeManager *theme,GameController*
38 gameController ,QWidget *parent)
39 : QGraphicsScene(parent){
41 m_gameInfo = gameInfo;
43 m_parent = (MainWindow*)parent;
44 m_gameController = gameController;
46 setSceneRect(m_parent->windowRect());
47 setupGameNecessaries();
49 connect( this,SIGNAL(signalMouseClicked(int)),
50 m_gameController , SLOT(slotMouseClicked(int)) );
51 connect( m_gameController,SIGNAL(signalRestartGame()),this,SLOT(slotStartGame()));
55 void GraphicsScene::showGameName(){
57 qreal posX,posY,width,height,scaleX,scaleY;
59 m_gameName = new QGraphicsSvgItem(0);
60 m_gameName->setSharedRenderer(m_gameTheme);
61 m_gameName->setElementId("GameName");
63 posX = sceneRect().x() + sceneRect().width() * 0.40; // 0.30
64 posY = sceneRect().y() + sceneRect().height() * 0.05; // 0.10
65 width = 0.20 * sceneRect().width(); // 0.40
66 height = width / 10 ; // 5
68 scaleX = width / ( m_gameName->boundingRect().width() );
69 scaleY = height / ( m_gameName->boundingRect().height() ) ;
70 m_gameName->scale(scaleX,scaleY);
72 m_gameName->setPos(posX,posY);
78 void GraphicsScene::setupGameNecessaries(){
81 m_board = new Board(m_gameInfo, m_gameTheme);
83 for( int i = 0; i < m_gameInfo->stones(); i++)
84 m_stones << new Stone(i, m_gameInfo, m_gameTheme, m_board);
86 m_board->manipulateStones(m_stones);
90 m_gameController->setBoardItem(m_board);
92 renderBackground(sceneRect().size().toSize(),true);
96 void GraphicsScene::deleteGameNecessaries(){
101 while(m_stones.count()){
102 stone = m_stones.front();
103 m_stones.pop_front();
115 void GraphicsScene::slotStartGame(){
117 deleteGameNecessaries();
118 setupGameNecessaries();
122 m_gameController->initializeGameController();
123 m_gameController->setBoardItem(m_board);
128 void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event){
130 // kDebug() << event->scenePos();
131 int cupIndex = m_board->findCupIndex(event->scenePos());
132 emit signalMouseClicked(cupIndex);
135 QPixmap GraphicsScene::renderBackground(const QSize& size,bool newGame){
137 QString cacheName("BackGround");
139 if( !QPixmapCache::find(cacheName, pix) || newGame)
141 QSvgRenderer* m_renderer = new QSvgRenderer(m_gameTheme->current());
144 pix.fill(Qt::transparent);
145 QPainter painter(&pix);
146 m_renderer->render( &painter, "BackGround");
147 QPixmapCache::insert(cacheName, pix);
153 void GraphicsScene::drawBackground(QPainter* painter, const QRectF &rect){
155 QSize size = m_parent->windowRect().size().toSize();
156 painter->drawPixmap( 0, 0, renderBackground( size , false ) );