1/*
2 Copyright 2007 Dmitry Suzdalev <dimsuz@gmail.com>
3 Copyright 2010 Brian Croom <brian.s.croom@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20#include "scene.h"
21#include "settings.h"
22
23#include <QResizeEvent>
24#include <KGamePopupItem>
25#include <KLocale>
26#include <KgThemeProvider>
27
28#include "minefielditem.h"
29// --------------- KMinesView ---------------
30
31KMinesView::KMinesView( KMinesScene* scene, QWidget *parent )
32 : QGraphicsView(scene, parent), m_scene(scene)
33{
34}
35
36void KMinesView::resizeEvent( QResizeEvent *ev )
37{
38 m_scene->resizeScene( ev->size().width(), ev->size().height() );
39}
40
41// -------------- KMinesScene --------------------
42
43static KgThemeProvider* provider()
44{
45 KgThemeProvider* prov = new KgThemeProvider;
46 prov->discoverThemes("appdata", QLatin1String("themes"));
47 return prov;
48}
49
50KMinesScene::KMinesScene( QObject* parent )
51 : QGraphicsScene(parent), m_renderer(provider())
52{
53 setItemIndexMethod( NoIndex );
54 m_fieldItem = new MineFieldItem(&m_renderer);
55 connect(m_fieldItem, SIGNAL(flaggedMinesCountChanged(int)), SIGNAL(minesCountChanged(int)));
56 connect(m_fieldItem, SIGNAL(firstClickDone()), SIGNAL(firstClickDone()));
57 connect(m_fieldItem, SIGNAL(gameOver(bool)), SLOT(onGameOver(bool)));
58 // and re-emit it for others
59 connect(m_fieldItem, SIGNAL(gameOver(bool)), SIGNAL(gameOver(bool)));
60 addItem(m_fieldItem);
61
62 m_messageItem = new KGamePopupItem;
63 m_messageItem->setMessageOpacity(0.9);
64 m_messageItem->setMessageTimeout(4000);
65 addItem(m_messageItem);
66
67 m_gamePausedMessageItem = new KGamePopupItem;
68 m_gamePausedMessageItem->setMessageOpacity(0.9);
69 m_gamePausedMessageItem->setMessageTimeout(0);
70 m_gamePausedMessageItem->setHideOnMouseClick(false);
71 addItem(m_gamePausedMessageItem);
72
73 setBackgroundBrush(m_renderer.spritePixmap(QLatin1String( "mainWidget" ), sceneRect().size().toSize()));
74}
75
76void KMinesScene::resizeScene(int width, int height)
77{
78 setSceneRect(0, 0, width, height);
79 setBackgroundBrush(m_renderer.spritePixmap(QLatin1String( "mainWidget" ), sceneRect().size().toSize()));
80 m_fieldItem->resizeToFitInRect( sceneRect() );
81 m_fieldItem->setPos( sceneRect().width()/2 - m_fieldItem->boundingRect().width()/2,
82 sceneRect().height()/2 - m_fieldItem->boundingRect().height()/2 );
83 m_gamePausedMessageItem->setPos( sceneRect().width()/2 - m_gamePausedMessageItem->boundingRect().width()/2,
84 sceneRect().height()/2 - m_gamePausedMessageItem->boundingRect().height()/2 );
85 m_messageItem->setPos( sceneRect().width()/2 - m_messageItem->boundingRect().width()/2,
86 sceneRect().height()/2 - m_messageItem->boundingRect().height()/2 );
87}
88
89void KMinesScene::startNewGame(int rows, int cols, int numMines)
90{
91 // hide message if any
92 m_messageItem->forceHide();
93
94 m_fieldItem->initField(rows, cols, numMines);
95 // reposition items
96 resizeScene((int)sceneRect().width(), (int)sceneRect().height());
97}
98
99int KMinesScene::totalMines() const
100{
101 return m_fieldItem->minesCount();
102}
103
104void KMinesScene::setGamePaused(bool paused)
105{
106 m_fieldItem->setVisible(!paused);
107 if(paused)
108 m_gamePausedMessageItem->showMessage(i18n("Game is paused."), KGamePopupItem::Center);
109 else
110 m_gamePausedMessageItem->forceHide();
111}
112
113void KMinesScene::onGameOver(bool won)
114{
115 if(won)
116 m_messageItem->showMessage(i18n("Congratulations! You have won!"), KGamePopupItem::Center);
117 else
118 m_messageItem->showMessage(i18n("You have lost."), KGamePopupItem::Center);
119}
120
121#include "scene.moc"
122