• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

granatier

  • sources
  • kde-4.14
  • kdegames
  • granatier
  • src
mainwindow.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Mathias Kraus <k.hias@gmx.de>
3  * Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
4  * Copyright 2007-2008 Pierre-Benoit Bessse <besse@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 #include "game.h"
22 #include "gameview.h"
23 #include "gamescene.h"
24 #include "settings.h"
25 #include "config/arenaselector.h"
26 #include "config/playersettings.h"
27 #include "config/playerselector.h"
28 #include "ui_generalsettings.h"
29 
30 #include <QGraphicsView>
31 #include <QTimer>
32 #include <KActionCollection>
33 #include <KStandardGameAction>
34 #include <KToggleAction>
35 #include <KMessageBox>
36 #include <KConfigDialog>
37 #include <KLocale>
38 #include <KgThemeSelector>
39 
40 class GeneralSettings : public QWidget
41 {
42 public:
43  GeneralSettings(QWidget *parent) : QWidget(parent)
44  {
45  ui.setupUi(this);
46  ui.groupBox->setVisible(false);
47  ui.kcfg_Dummy->setVisible(false); // this is only to notify changes in playerselector
48  }
49 private:
50  Ui::GeneralSettings ui;
51 };
52 
53 MainWindow::MainWindow()
54 {
55  // initialize random generator
56  qsrand(QDateTime::currentDateTime().toTime_t());
57 
58  m_settingsDialog = NULL;
59  // Initialize the game
60  m_game = NULL;
61  m_view = NULL;
62  m_scene = NULL;
63  m_playerSettings = new PlayerSettings();
64  m_themeProvider = new KgThemeProvider(QByteArray("Theme"), this);
65  m_themeProvider->discoverThemes("appdata", QLatin1String("themes"), "granatier");
66  // Set the window menus
67  KStandardGameAction::gameNew(this, SLOT(newGame()), actionCollection());
68  //KStandardGameAction::highscores(this, SLOT(showHighscores()), actionCollection());
69  KStandardAction::preferences(this, SLOT(showSettings()), actionCollection());
70  KStandardGameAction::quit(this, SLOT(close()), actionCollection());
71  KAction* soundAction = new KToggleAction(i18n("&Play sounds"), this);
72  soundAction->setChecked(Settings::sounds());
73  actionCollection()->addAction( QLatin1String( "sounds" ), soundAction);
74  connect(soundAction, SIGNAL(triggered(bool)), this, SLOT(setSoundsEnabled(bool)));
75  // init game
76  initGame();
77  // Setup the window
78  setupGUI(Keys | Save | Create);
79 }
80 
81 MainWindow::~MainWindow()
82 {
83  delete m_view;
84  delete m_scene;
85  delete m_game;
86  delete m_playerSettings;
87  delete m_settingsDialog;
88 }
89 
90 void MainWindow::initGame()
91 {
92  //the focus has to be set at the beginning and also at the end to cover all possible cases
93  //TODO: check why setting the focus only at the end doesn't work
94  this->setFocusProxy(m_view);
95  this->setFocus();
96 
97  // If a GameView instance already exists
98  if (m_view)
99  {
100  // Delete the GameView instance
101  delete m_view;
102  }
103 
104  // If a GameScene instance already exists
105  if (m_scene)
106  {
107  // Delete the GameScene instance
108  delete m_scene;
109  }
110 
111  // If a Game instance already exists
112  if (m_game)
113  {
114  // Delete the Game instance
115  delete m_game;
116  }
117  // Create a new Game instance
118  m_game = new Game(m_playerSettings);
119  connect(m_game, SIGNAL(gameOver()), this, SLOT(newGame()));
120 
121  m_scene = new GameScene(m_game, m_themeProvider);
122 
123  // Create a new GameView instance
124  m_view = new GameView(m_scene, m_game);
125  m_view->setBackgroundBrush(Qt::black);
126  setCentralWidget(m_view);
127  m_game->setGameScene(dynamic_cast <GameScene*> (m_view->scene()));
128 
129  this->setFocusProxy(m_view);
130  this->setFocus();
131 }
132 
133 void MainWindow::newGame()
134 {
135  bool gameRunning; // True if the game is running (game timer is active), false otherwise
136 
137  gameRunning = m_game->getTimer()->isActive();
138  // If the game is running
139  if (gameRunning)
140  {
141  // Pause the game
142  m_game->pause();
143  }
144  // If the game was not over
145  if (!m_game->getGameOver())
146  {
147  // Confirm before starting a new game
148  if (KMessageBox::warningYesNo(this, i18n("Are you sure you want to quit the current game?"), i18n("New game")) == KMessageBox::Yes)
149  {
150  // Start a new game
151  initGame();
152  }
153  else
154  {
155  // If the game was running
156  if (gameRunning)
157  {
158  // Resume the game
159  m_game->start();
160  }
161  }
162  }
163  else
164  {
165  // Start a new game
166  initGame();
167  }
168 }
169 
170 void MainWindow::setSoundsEnabled(bool p_enabled) {
171  m_game->setSoundsEnabled(p_enabled);
172 }
173 
174 void MainWindow::showSettings()
175 {
176  if (m_settingsDialog)//(KConfigDialog::showDialog("settings"))
177  {
178  delete m_settingsDialog;
179  }
180  KConfigDialog* settingsDialog = new KConfigDialog(this, "settings", Settings::self());
181  // General Settings
182  settingsDialog->addPage(new GeneralSettings(settingsDialog), i18nc("General settings", "General"), "games-config-options");
183  // Theme
184  m_themeProvider->rediscoverThemes();
185  m_currentThemeIdentifier = m_themeProvider->currentTheme()->identifier();
186  settingsDialog->addPage(new KgThemeSelector(m_themeProvider, KgThemeSelector::DefaultBehavior, settingsDialog), i18n("Theme"), "games-config-theme");
187  // Arena
188  settingsDialog->addPage(new ArenaSelector(settingsDialog, Settings::self(), &m_tempRandomArenaModeArenaList, ArenaSelector::DefaultBehavior), i18n("Arena"), "games-config-board");
189  // Player
190  settingsDialog->addPage(new PlayerSelector(m_playerSettings, PlayerSelector::DefaultBehavior, settingsDialog), i18n("Player"), "games-config-custom");
191 
192  m_settingsDialog = settingsDialog;
193 
194  connect(settingsDialog, SIGNAL(settingsChanged(QString)), this, SLOT(applyNewSettings()));
195  connect(settingsDialog, SIGNAL(cancelClicked()), this, SLOT(settingsDialogCanceled()));
196  settingsDialog->show();
197 }
198 
199 void MainWindow::applyNewSettings()
200 {
201  Settings::self()->setDummy(0);
202  m_playerSettings->savePlayerSettings();
203  if(!m_tempRandomArenaModeArenaList.isEmpty())
204  {
205  Settings::self()->setRandomArenaModeArenaList(m_tempRandomArenaModeArenaList);
206  }
207  initGame();
208 }
209 
210 void MainWindow::settingsDialogCanceled()
211 {
212  m_playerSettings->discardUnsavedSettings();
213  m_tempRandomArenaModeArenaList.clear();
214  if(m_currentThemeIdentifier != m_themeProvider->currentTheme()->identifier())
215  {
216  QList<const KgTheme*> themeList = m_themeProvider->themes();
217  foreach(const KgTheme* theme, themeList)
218  {
219  if(theme->identifier() == m_currentThemeIdentifier)
220  {
221  m_themeProvider->setCurrentTheme(theme);
222  break;
223  }
224  }
225  }
226 }
227 
228 void MainWindow::close()
229 {
230  bool gameRunning; // True if the game is running (game timer is active), false otherwise
231 
232  gameRunning = m_game->getTimer()->isActive();
233  // If the game is running
234  if (gameRunning)
235  {
236  // Pause the game
237  m_game->pause();
238  }
239  // Confirm before closing
240  if(KMessageBox::warningYesNo(this, i18n("Are you sure you want to quit Granatier?"), i18nc("To quit Granatier", "Quit")) == KMessageBox::Yes)
241  {
242  KXmlGuiWindow::close();
243  }
244  else
245  {
246  // If the game was running
247  if (gameRunning)
248  {
249  // Resume the game
250  m_game->start();
251  }
252  }
253 }
254 
QList::clear
void clear()
QWidget
GameView
This class manages the drawing of each element of the Game instance.
Definition: gameview.h:32
QWidget::setupUi
void setupUi(QWidget *widget)
ArenaSelector::DefaultBehavior
Definition: arenaselector.h:52
Game::getGameOver
bool getGameOver() const
Definition: game.cpp:316
PlayerSettings::savePlayerSettings
void savePlayerSettings()
Definition: playersettings.cpp:174
Game::setSoundsEnabled
void setSoundsEnabled(bool p_enabled)
Enables / disables the sounds.
Definition: game.cpp:427
Game::getTimer
QTimer * getTimer() const
Definition: game.cpp:296
QByteArray
Game::start
void start()
Starts the Game.
Definition: game.cpp:248
arenaselector.h
PlayerSettings::discardUnsavedSettings
void discardUnsavedSettings()
Definition: playersettings.cpp:257
game.h
QGraphicsView::scene
QGraphicsScene * scene() const
Settings::self
static Settings * self()
Definition: settings.cpp:17
PlayerSelector
Definition: playerselector.h:28
gameview.h
Game::setGameScene
void setGameScene(GameScene *p_gameScene)
Sets the games gamescene.
Definition: game.cpp:243
QGraphicsView::setBackgroundBrush
void setBackgroundBrush(const QBrush &brush)
QList::isEmpty
bool isEmpty() const
Game
This class manages the game main loop : it regularly checks the key press events, computes the charac...
Definition: game.h:43
mainwindow.h
PlayerSettings
Definition: playersettings.h:27
QString
QList
gamescene.h
playersettings.h
MainWindow::MainWindow
MainWindow()
Creates a new MainWindow instance.
Definition: mainwindow.cpp:53
QDateTime::currentDateTime
QDateTime currentDateTime()
settings.h
QLatin1String
ArenaSelector
A widget used to select the game's arena.
Definition: arenaselector.h:46
GameScene
This class contains all the Game elements to be drawn on the screen by the GameView instance...
Definition: gamescene.h:50
Settings::setRandomArenaModeArenaList
static void setRandomArenaModeArenaList(const QStringList &v)
Set List of arenas, used in random mode.
Definition: settings.h:96
Settings::sounds
static bool sounds()
Get Whether sound effects should be played.
Definition: settings.h:220
MainWindow::~MainWindow
~MainWindow()
Deletes the MainWindow instance.
Definition: mainwindow.cpp:81
Settings::setDummy
static void setDummy(int v)
Set This is a dummy setting for player setup.
Definition: settings.h:248
QTimer::isActive
bool isActive() const
playerselector.h
PlayerSelector::DefaultBehavior
Definition: playerselector.h:35
Game::pause
void pause(bool p_locked=false)
Pauses the Game.
Definition: game.cpp:257
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

granatier

Skip menu "granatier"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal