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

libkdegames/libkdegamesprivate

  • sources
  • kde-4.14
  • kdegames
  • libkdegames
  • libkdegamesprivate
kgamedifficulty.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2007, 2008 Nicolas Roffet, <nicolas-kde@roffet.com>
3 Copyright (c) 2007, Pino Toscano, <toscano.pino@tiscali.it>
4 
5 This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
6 
7 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details.
8 
9 You should have received a copy of the GNU Library General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
10 */
11 
12 #include "kgamedifficulty.h"
13 
14 
15 
16 #include <QMap>
17 
18 
19 #include <kactioncollection.h>
20 #include <kcombobox.h>
21 #include <kicon.h>
22 #include <klocale.h>
23 #include <kmessagebox.h>
24 #include <kstatusbar.h>
25 #include <kselectaction.h>
26 #include <kxmlguiwindow.h>
27 
28 
29 
30 class KGameDifficultyPrivate : public QObject
31 {
32  Q_OBJECT
33 
34  public:
35  ~KGameDifficultyPrivate();
36 
37  void init(KXmlGuiWindow* window, const QObject* recvr, const char* slotStandard, const char* slotCustom);
38 
39  void rebuildActions();
40 
44  QPair<QByteArray, QString> standardLevelString(KGameDifficulty::standardLevel level);
45 
46  void setLevel(KGameDifficulty::standardLevel level);
47  void setLevelCustom(int key);
48 
49 
53  int m_levelCustom;
54 
55  KGameDifficulty::standardLevel m_level;
56  QList<KGameDifficulty::standardLevel> m_standardLevels;
57  QMap<int, QString> m_customLevels;
58 
59  KSelectAction* m_menu;
60  KGameDifficulty::onChange m_restartOnChange;
61  bool m_running;
62  int m_oldSelection;
63  KComboBox* m_comboBox;
64 
65 
66  public Q_SLOTS:
73  void changeSelection(int newSelection);
74 
75  Q_SIGNALS:
82  void standardLevelChanged(KGameDifficulty::standardLevel level);
83 
90  void customLevelChanged(int key);
91 
92 
93  private:
94  void setSelection(int newSelection);
95 };
96 
97 
98 KGameDifficultyPrivate::~KGameDifficultyPrivate()
99 {
100  delete KGameDifficulty::self();
101 }
102 
103 
104 void KGameDifficultyPrivate::init(KXmlGuiWindow* window, const QObject* recvr, const char* slotStandard, const char* slotCustom = 0)
105 {
106  Q_ASSERT(recvr!=0);
107 
108  m_oldSelection = -1; // No valid selection
109  m_level = KGameDifficulty::NoLevel;
110  m_running = false;
111 
112  QObject::connect(this, SIGNAL(standardLevelChanged(KGameDifficulty::standardLevel)), recvr, slotStandard);
113  if (slotCustom!=0)
114  QObject::connect(this, SIGNAL(customLevelChanged(int)), recvr, slotCustom);
115 
116  m_menu = new KSelectAction(KIcon( QLatin1String( "games-difficult") ), i18nc("Game difficulty level", "Difficulty" ), window);
117  m_menu->setToolTip(i18n("Set the difficulty level"));
118  m_menu->setWhatsThis(i18n("Set the difficulty level of the game."));
119  QObject::connect(m_menu, SIGNAL(triggered(int)), this, SLOT(changeSelection(int)));
120  m_menu->setObjectName( QLatin1String("options_game_difficulty" ));
121  window->actionCollection()->addAction(m_menu->objectName(), m_menu);
122 
123  setParent(window);
124 
125  m_comboBox = new KComboBox(window);
126  m_comboBox->setToolTip(i18n("Difficulty"));
127  QObject::connect(m_comboBox, SIGNAL(activated(int)), this, SLOT(changeSelection(int)));
128  window->statusBar()->addPermanentWidget(m_comboBox);
129 
130  KGameDifficulty::setRestartOnChange(KGameDifficulty::RestartOnChange);
131 }
132 
133 
134 void KGameDifficultyPrivate::changeSelection(int newSelection)
135 {
136  if (newSelection!=m_oldSelection) {
137  bool mayChange = true;
138 
139  if (mayChange && (m_restartOnChange==KGameDifficulty::RestartOnChange) && m_running)
140  mayChange = ( KMessageBox::warningContinueCancel(0, i18n("Changing the difficulty level will end the current game!"), QString(), KGuiItem(i18n("Change the difficulty level"))) == KMessageBox::Continue );
141 
142  if (mayChange) {
143  setSelection(newSelection);
144  } else {
145  // restore current level selection
146  setSelection(m_oldSelection);
147  }
148  }
149 }
150 
151 QPair<QByteArray, QString> KGameDifficultyPrivate::standardLevelString(KGameDifficulty::standardLevel level)
152 {
153  //The first entry in the pair is to be used as a key so don't change it. It doesn't have to match the string to be translated
154  switch (level) {
155  case KGameDifficulty::RidiculouslyEasy:
156  return qMakePair(QByteArray("Ridiculously Easy"), i18nc("Game difficulty level 1 out of 8", "Ridiculously Easy"));
157  case KGameDifficulty::VeryEasy:
158  return qMakePair(QByteArray("Very Easy"), i18nc("Game difficulty level 2 out of 8", "Very Easy"));
159  case KGameDifficulty::Easy:
160  return qMakePair(QByteArray("Easy"), i18nc("Game difficulty level 3 out of 8", "Easy"));
161  case KGameDifficulty::Medium:
162  return qMakePair(QByteArray("Medium"), i18nc("Game difficulty level 4 out of 8", "Medium"));
163  case KGameDifficulty::Hard:
164  return qMakePair(QByteArray("Hard"), i18nc("Game difficulty level 5 out of 8", "Hard"));
165  case KGameDifficulty::VeryHard:
166  return qMakePair(QByteArray("Very Hard"), i18nc("Game difficulty level 6 out of 8", "Very Hard"));
167  case KGameDifficulty::ExtremelyHard:
168  return qMakePair(QByteArray("Extremely Hard"), i18nc("Game difficulty level 7 out of 8", "Extremely Hard"));
169  case KGameDifficulty::Impossible:
170  return qMakePair(QByteArray("Impossible"), i18nc("Game difficulty level 8 out of 8", "Impossible"));
171  case KGameDifficulty::Custom:
172  case KGameDifficulty::Configurable:
173  case KGameDifficulty::NoLevel:
174  // Do nothing
175  break;
176  }
177  return qMakePair(QByteArray(), QString());
178 }
179 
180 void KGameDifficultyPrivate::rebuildActions()
181 {
182  m_menu->clear();
183  m_comboBox->clear();
184  qSort(m_standardLevels.begin(), m_standardLevels.end());
185 
186  foreach(KGameDifficulty::standardLevel level, m_standardLevels) {
187  if (level!=KGameDifficulty::Configurable) {
188  m_menu->addAction(standardLevelString(level).second);
189  m_comboBox->addItem(KIcon( QLatin1String( "games-difficult" )), standardLevelString(level).second);
190  }
191  }
192 
193  if (m_customLevels.count()>0) {
194  foreach(const QString &s, m_customLevels) {
195  m_menu->addAction(s);
196  m_comboBox->addItem(KIcon( QLatin1String( "games-difficult" )), s);
197  }
198  }
199 
200  if (m_standardLevels.contains(KGameDifficulty::Configurable)) {
201  QAction* separator = new QAction(m_menu);
202  separator->setSeparator(true);
203  m_menu->addAction(separator);
204 
205  QString s = i18nc("Name of the game difficulty level that is customized by the user by setting up different game parameters", "Custom");
206  m_menu->addAction(s);
207  m_comboBox->addItem(KIcon( QLatin1String( "games-difficult" )), s);
208  }
209 
210  // reselect the previous selected item.
211  if (m_level==KGameDifficulty::Custom)
212  KGameDifficulty::setLevelCustom(m_levelCustom);
213  else if (m_standardLevels.contains(m_level))
214  KGameDifficulty::setLevel(m_level);
215 }
216 
217 void KGameDifficultyPrivate::setSelection(int newSelection)
218 {
219  int countWithoutConfigurable = m_standardLevels.count();
220  if (m_standardLevels.contains(KGameDifficulty::Configurable))
221  countWithoutConfigurable--;
222 
223  if ((m_standardLevels.contains(KGameDifficulty::Configurable)) && (newSelection>m_menu->actions().count()-3))
224  KGameDifficulty::setLevel(KGameDifficulty::Configurable);
225  else if(newSelection<countWithoutConfigurable)
226  KGameDifficulty::setLevel(m_standardLevels[newSelection]);
227  else
228  KGameDifficulty::setLevelCustom((m_customLevels.uniqueKeys()).value(newSelection - countWithoutConfigurable));
229 
230  m_oldSelection = newSelection;
231 }
232 
233 
234 void KGameDifficultyPrivate::setLevel(KGameDifficulty::standardLevel level)
235 {
236  if ((!m_standardLevels.contains(level)) && (level!=KGameDifficulty::Custom))
237  level = KGameDifficulty::NoLevel;
238 
239  if (level==KGameDifficulty::Configurable) {
240  m_menu->setCurrentItem(m_menu->actions().count()-1);
241  m_comboBox->setCurrentIndex(m_comboBox->count()-1);
242  } else if (level!=KGameDifficulty::Custom) {
243  int i = m_standardLevels.indexOf(level);
244  m_menu->setCurrentItem(i);
245  m_comboBox->setCurrentIndex(i);
246  }
247 
248  if (level != m_level) {
249  m_level = level;
250  emit standardLevelChanged(level);
251  }
252 
253  m_oldSelection = m_menu->currentItem();
254 }
255 
256 
257 void KGameDifficultyPrivate::setLevelCustom(int key)
258 {
259  m_level = KGameDifficulty::Custom;
260 
261  int a = m_standardLevels.count();
262  if (m_standardLevels.contains(KGameDifficulty::Configurable))
263  a -= 1;
264 
265  int i = (m_customLevels.uniqueKeys()).indexOf(key) + a;
266  m_menu->setCurrentItem(i);
267  m_comboBox->setCurrentIndex(i);
268 
269  if (key != m_levelCustom) {
270  m_levelCustom = key;
271  emit customLevelChanged(key);
272  }
273 
274  m_oldSelection = m_menu->currentItem();
275 }
276 
277 
278 
279 //---//
280 
281 
282 
283 KGameDifficulty* KGameDifficulty::instance = 0;
284 
285 
286 KGameDifficulty::~KGameDifficulty()
287 {
288  // We do not need to delete d, because d deletes us.
289 }
290 
291 
292 void KGameDifficulty::init(KXmlGuiWindow* window, const QObject* recvr, const char* slotStandard, const char* slotCustom)
293 {
294  self()->d->init(window, recvr, slotStandard, slotCustom);
295 }
296 
297 
298 void KGameDifficulty::setRestartOnChange(onChange restart)
299 {
300  Q_ASSERT(self()->d);
301 
302  self()->d->m_restartOnChange = restart;
303  if (restart==RestartOnChange)
304  self()->d->m_comboBox->setWhatsThis(i18n("Select the <b>difficulty</b> of the game.<br />If you change the difficulty level while a game is running, you will have to cancel it and start a new one."));
305  else
306  self()->d->m_comboBox->setWhatsThis(i18n("Select the <b>difficulty</b> of the game.<br />You can change the difficulty level during a running game."));
307 
308 }
309 
310 
311 void KGameDifficulty::addStandardLevel(standardLevel level)
312 {
313  Q_ASSERT(self()->d);
314 
315  if ((level!=Custom) && (level!=NoLevel)) {
316  self()->d->m_standardLevels.append(level);
317  self()->d->rebuildActions();
318  }
319 }
320 
321 
322 void KGameDifficulty::removeStandardLevel(standardLevel level)
323 {
324  Q_ASSERT(self()->d);
325 
326  self()->d->m_standardLevels.removeAll(level);
327  self()->d->rebuildActions();
328 }
329 
330 
331 void KGameDifficulty::addCustomLevel(int key, const QString& appellation)
332 {
333  Q_ASSERT(self()->d);
334 
335  self()->d->m_customLevels.insert(key, appellation);
336  self()->d->rebuildActions();
337 }
338 
339 
340 void KGameDifficulty::removeCustomLevel(int key)
341 {
342  Q_ASSERT(self()->d);
343 
344  self()->d->m_customLevels.remove(key);
345  self()->d->rebuildActions();
346 }
347 
348 
349 void KGameDifficulty::setEnabled(bool enabled)
350 {
351  Q_ASSERT(self()->d->m_menu);
352 
353  // TODO: Doing this never disable the combobox in the toolbar (just in the menu). It seems to be a bug in the class KSelectAction of kdelibs/kdeui/actions. To check and solve...
354  self()->d->m_menu->setEnabled(enabled);
355  self()->d->m_comboBox->setEnabled(enabled);
356 }
357 
358 
359 void KGameDifficulty::setLevel(standardLevel level)
360 {
361  Q_ASSERT(self()->d);
362 
363  self()->d->setLevel(level);
364 }
365 
366 
367 void KGameDifficulty::setLevelCustom(int key)
368 {
369  Q_ASSERT(self()->d);
370 
371  self()->d->setLevelCustom(key);
372 }
373 
374 
375 int KGameDifficulty::levelCustom()
376 {
377  Q_ASSERT(self()->d);
378 
379  return self()->d->m_levelCustom;
380 }
381 
382 
383 KGameDifficulty::standardLevel KGameDifficulty::level()
384 {
385  Q_ASSERT(self()->d);
386 
387  return self()->d->m_level;
388 }
389 
390 
391 QString KGameDifficulty::levelString()
392 {
393  Q_ASSERT(self()->d);
394 
395  return self()->d->standardLevelString(self()->d->m_level).second;
396 }
397 
398 
399 QPair<QByteArray, QString> KGameDifficulty::localizedLevelString()
400 {
401  Q_ASSERT(self()->d);
402 
403  return self()->d->standardLevelString(self()->d->m_level);
404 }
405 
406 
407 QMap<QByteArray, QString> KGameDifficulty::localizedLevelStrings()
408 {
409  Q_ASSERT(self()->d);
410 
411  QMap<QByteArray, QString> levelStrings;
412 
413  levelStrings.insert(self()->d->standardLevelString(RidiculouslyEasy).first, self()->d->standardLevelString(RidiculouslyEasy).second);
414  levelStrings.insert(self()->d->standardLevelString(VeryEasy).first, self()->d->standardLevelString(VeryEasy).second);
415  levelStrings.insert(self()->d->standardLevelString(Easy).first, self()->d->standardLevelString(Easy).second);
416  levelStrings.insert(self()->d->standardLevelString(Medium).first, self()->d->standardLevelString(Medium).second);
417  levelStrings.insert(self()->d->standardLevelString(Hard).first, self()->d->standardLevelString(Hard).second);
418  levelStrings.insert(self()->d->standardLevelString(VeryHard).first, self()->d->standardLevelString(VeryHard).second);
419  levelStrings.insert(self()->d->standardLevelString(ExtremelyHard).first, self()->d->standardLevelString(ExtremelyHard).second);
420  levelStrings.insert(self()->d->standardLevelString(Impossible).first, self()->d->standardLevelString(Impossible).second);
421 
422  return levelStrings;
423 }
424 
425 QMap<int, QByteArray> KGameDifficulty::levelWeights()
426 {
427  Q_ASSERT(self()->d);
428 
429  QMap<int, QByteArray> weights;
430  weights.insert(RidiculouslyEasy, self()->d->standardLevelString(RidiculouslyEasy).first);
431  weights.insert(VeryEasy, self()->d->standardLevelString(VeryEasy).first);
432  weights.insert(Easy, self()->d->standardLevelString(Easy).first);
433  weights.insert(Medium, self()->d->standardLevelString(Medium).first);
434  weights.insert(Hard, self()->d->standardLevelString(Hard).first);
435  weights.insert(VeryHard, self()->d->standardLevelString(VeryHard).first);
436  weights.insert(ExtremelyHard, self()->d->standardLevelString(ExtremelyHard).first);
437  weights.insert(Impossible, self()->d->standardLevelString(Impossible).first);
438 
439  return weights;
440 }
441 
442 void KGameDifficulty::setRunning(bool running)
443 {
444  Q_ASSERT(self()->d);
445 
446  self()->d->m_running = running;
447 }
448 
449 
450 KGameDifficulty::KGameDifficulty() : d(new KGameDifficultyPrivate())
451 {
452 }
453 
454 
455 KGameDifficulty* KGameDifficulty::self()
456 {
457  if (instance==0)
458  instance = new KGameDifficulty();
459  return instance;
460 }
461 
462 #include "kgamedifficulty.moc"
KGameDifficulty::addCustomLevel
static void addCustomLevel(int key, const QString &appellation)
Add a custom difficulty level.
Definition: kgamedifficulty.cpp:331
QAction::setSeparator
void setSeparator(bool b)
QByteArray
KGameDifficulty::localizedLevelString
static QPair< QByteArray, QString > localizedLevelString()
Definition: kgamedifficulty.cpp:399
KGameDifficulty::VeryHard
Level "Very hard".
Definition: kgamedifficulty.h:102
QMap< int, QString >
KGameDifficulty::NoLevel
No level.
Definition: kgamedifficulty.h:107
KGameDifficulty::levelCustom
static int levelCustom()
Get the current custom difficulty level.
Definition: kgamedifficulty.cpp:375
KGameDifficulty::Hard
Level "Hard".
Definition: kgamedifficulty.h:101
KGameDifficulty::localizedLevelStrings
static QMap< QByteArray, QString > localizedLevelStrings()
Definition: kgamedifficulty.cpp:407
KGameDifficulty
KGameDifficuty manages the game difficulty levels in a standard way.
Definition: kgamedifficulty.h:78
KGameDifficulty::removeStandardLevel
static void removeStandardLevel(standardLevel level)
Remove a standard difficulty level.
Definition: kgamedifficulty.cpp:322
KGameDifficulty::VeryEasy
Level "Very easy".
Definition: kgamedifficulty.h:98
KGameDifficulty::level
static standardLevel level()
Get the current standard difficulty level.
Definition: kgamedifficulty.cpp:383
kgamedifficulty.h
KGameDifficulty::RidiculouslyEasy
Level "Ridiculously easy".
Definition: kgamedifficulty.h:97
KGameDifficulty::ExtremelyHard
Level "Extremely hard".
Definition: kgamedifficulty.h:103
KGameDifficulty::Impossible
Level "Impossible".
Definition: kgamedifficulty.h:104
QObject
KGameDifficulty::Medium
Level "Medium".
Definition: kgamedifficulty.h:100
KGameDifficulty::Configurable
Level "Custom".
Definition: kgamedifficulty.h:105
QString
KGameDifficulty::setLevelCustom
static void setLevelCustom(int key)
Set the new current difficulty level as a custom one.
Definition: kgamedifficulty.cpp:367
QList< KGameDifficulty::standardLevel >
KGameDifficulty::~KGameDifficulty
virtual ~KGameDifficulty()
Destructor.
Definition: kgamedifficulty.cpp:286
QPair
KGameDifficulty::onChange
onChange
Behavior on change.
Definition: kgamedifficulty.h:86
KGameDifficulty::Easy
Level "Easy".
Definition: kgamedifficulty.h:99
KGameDifficulty::setLevel
static void setLevel(standardLevel level)
Set the new current difficulty level as a standard one.
Definition: kgamedifficulty.cpp:359
KGameDifficulty::removeCustomLevel
static void removeCustomLevel(int key)
Remove a custom difficulty level.
Definition: kgamedifficulty.cpp:340
QLatin1String
KGameDifficulty::setRunning
static void setRunning(bool running)
Set the game state: Running or not.
Definition: kgamedifficulty.cpp:442
KGameDifficulty::levelString
static QString levelString()
Definition: kgamedifficulty.cpp:391
QAction
KGameDifficulty::standardLevel
standardLevel
Standard difficulty levels.
Definition: kgamedifficulty.h:96
KGameDifficulty::Custom
Any custom appellations for levels.
Definition: kgamedifficulty.h:106
QMap::insert
iterator insert(const Key &key, const T &value)
KGameDifficulty::setRestartOnChange
static void setRestartOnChange(onChange restart)
Set if a new game has to be started by change.
Definition: kgamedifficulty.cpp:298
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KGameDifficulty::addStandardLevel
static void addStandardLevel(standardLevel level)
Add a standard difficulty level.
Definition: kgamedifficulty.cpp:311
KGameDifficulty::RestartOnChange
The current game has to be canceled and a new game will be started on change.
Definition: kgamedifficulty.h:87
KGameDifficulty::init
static void init(KXmlGuiWindow *window, const QObject *recvr, const char *slotStandard, const char *slotCustom=0)
Initialize the difficulty class.
Definition: kgamedifficulty.cpp:292
KGameDifficulty::levelWeights
static QMap< int, QByteArray > levelWeights()
Definition: kgamedifficulty.cpp:425
KGameDifficulty::setEnabled
static void setEnabled(bool enabled)
Set if the difficulty level may be changed.
Definition: kgamedifficulty.cpp:349
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:50 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdegames/libkdegamesprivate

Skip menu "libkdegames/libkdegamesprivate"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

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