bump up
[mancala] / src / GraphicsScene.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 "GraphicsScene.h"
21
22 #include <QGraphicsSceneMouseEvent>
23 #include <QSvgRenderer>
24 #include <QPainter>
25 #include <QWidget>
26
27 // #include <kdebug.h>
28 // #include <ksvgrenderer.h>
29
30 #include "GameInfo.h"
31 #include "ThemeManager.h"
32 #include "Board.h"
33 #include "Stone.h"
34 #include "GameController.h"
35 #include "MainWindow.h"
36
37 GraphicsScene::GraphicsScene(GameInfo *gameInfo,ThemeManager *theme,GameController*
38                              gameController ,QWidget *parent)
39     : QGraphicsScene(parent){
40
41     m_gameInfo = gameInfo;
42     m_gameTheme = theme;
43     m_parent = (MainWindow*)parent;
44     m_gameController = gameController;
45
46     setSceneRect(m_parent->windowRect());
47     setupGameNecessaries();
48
49     connect( this,SIGNAL(signalMouseClicked(int)),
50              m_gameController , SLOT(slotMouseClicked(int)) );
51     connect( m_gameController,SIGNAL(signalRestartGame()),this,SLOT(slotStartGame()));
52
53 }
54
55 void GraphicsScene::showGameName(){
56
57     qreal posX,posY,width,height,scaleX,scaleY;
58
59     m_gameName = new QGraphicsSvgItem(0);
60     m_gameName->setSharedRenderer(m_gameTheme);
61     m_gameName->setElementId("GameName");
62
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
67
68     scaleX = width / ( m_gameName->boundingRect().width() );
69     scaleY = height / ( m_gameName->boundingRect().height() ) ;
70     m_gameName->scale(scaleX,scaleY);
71
72     m_gameName->setPos(posX,posY);
73
74     addItem(m_gameName);
75 }
76
77
78 void GraphicsScene::setupGameNecessaries(){
79
80     // showGameName();
81     m_board = new Board(m_gameInfo, m_gameTheme);
82
83     for( int i = 0; i < m_gameInfo->stones(); i++)
84         m_stones << new Stone(i, m_gameInfo, m_gameTheme, m_board);
85
86     m_board->manipulateStones(m_stones);
87     addItem(m_board);
88
89     //For the first time
90     m_gameController->setBoardItem(m_board);
91
92     renderBackground(sceneRect().size().toSize(),true);
93
94 }
95
96 void GraphicsScene::deleteGameNecessaries(){
97
98     Stone *stone;
99     // delete m_gameName;
100
101     while(m_stones.count()){
102         stone = m_stones.front();
103         m_stones.pop_front();
104         delete stone;
105     }
106
107     if(m_board != NULL){
108         delete m_board;
109         m_board = NULL;
110     }
111
112 }
113
114
115 void GraphicsScene::slotStartGame(){
116
117     deleteGameNecessaries();
118     setupGameNecessaries();
119
120     update(sceneRect());
121
122     m_gameController->initializeGameController();
123     m_gameController->setBoardItem(m_board);
124
125 }
126
127
128 void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event){
129
130     // kDebug() << event->scenePos();
131     int cupIndex = m_board->findCupIndex(event->scenePos());
132     emit signalMouseClicked(cupIndex);
133 }
134
135 QPixmap GraphicsScene::renderBackground(const QSize& size,bool newGame){
136     QPixmap pix;
137     QString cacheName("BackGround");
138
139     if( !QPixmapCache::find(cacheName, pix) || newGame)
140     {
141         QSvgRenderer* m_renderer = new QSvgRenderer(m_gameTheme->current());
142
143         pix = QPixmap(size);
144         pix.fill(Qt::transparent);
145         QPainter painter(&pix);
146         m_renderer->render( &painter, "BackGround");
147         QPixmapCache::insert(cacheName, pix);
148     }
149
150     return pix;
151 }
152
153 void GraphicsScene::drawBackground(QPainter* painter, const QRectF &rect){
154
155     QSize size = m_parent->windowRect().size().toSize();
156     painter->drawPixmap( 0, 0, renderBackground( size , false ) );
157
158 }
159