kgamedifficulty.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2007, Nicolas Roffet, <nicolas-kde@roffet.com>
00003 Copyright (c) 2007, Pino Toscano, <toscano.pino@tiscali.it>
00004 
00005 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.
00006 
00007 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.
00008 
00009 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
00010 */
00011 
00012 #include "kgamedifficulty.h"
00013 
00014 
00015 
00016 #include <QMap>
00017 
00018 
00019 #include <kactioncollection.h>
00020 #include <kcombobox.h>
00021 #include <kicon.h>
00022 #include <klocale.h>
00023 #include <kmessagebox.h>
00024 #include <kstatusbar.h>
00025 #include <kselectaction.h>
00026 #include <kxmlguiwindow.h>
00027 
00028 
00029 
00030 class KGameDifficultyPrivate : public QObject
00031 {
00032         Q_OBJECT
00033 
00034         public:
00035                 ~KGameDifficultyPrivate();
00036 
00037                 void init(KXmlGuiWindow* window, const QObject* recvr, const char* slotStandard, const char* slotCustom);
00038 
00039                 void rebuildActions();
00040 
00044                 QString standardLevelString(KGameDifficulty::standardLevel level);
00045 
00046                 void setLevel(KGameDifficulty::standardLevel level);
00047                 void setLevelCustom(int key);
00048 
00049 
00053                 int m_levelCustom;
00054 
00055                 KGameDifficulty::standardLevel m_level;
00056                 QList<KGameDifficulty::standardLevel> m_standardLevels;
00057                 QMap<int, QString> m_customLevels;
00058 
00059                 KSelectAction* m_menu;
00060                 KGameDifficulty::onChange m_restartOnChange;
00061                 bool m_running;
00062                 int m_oldSelection;
00063                 KComboBox* m_comboBox;
00064 
00065 
00066         public Q_SLOTS:
00073                 void changeSelection(int newSelection);
00074 
00075         Q_SIGNALS:
00082                 void standardLevelChanged(KGameDifficulty::standardLevel level);
00083 
00090                 void customLevelChanged(int key);
00091 
00092 
00093         private:
00094                 void setSelection(int newSelection);
00095 };
00096 
00097 
00098 KGameDifficultyPrivate::~KGameDifficultyPrivate()
00099 {
00100         delete KGameDifficulty::self();
00101 }
00102 
00103 
00104 void KGameDifficultyPrivate::init(KXmlGuiWindow* window, const QObject* recvr, const char* slotStandard, const char* slotCustom = 0)
00105 {
00106         Q_ASSERT(recvr!=0);
00107 
00108         m_oldSelection = -1; // No valid selection
00109         m_level = KGameDifficulty::NoLevel;
00110         m_running = false;
00111 
00112         QObject::connect(this, SIGNAL(standardLevelChanged(KGameDifficulty::standardLevel)), recvr, slotStandard);
00113         if (slotCustom!=0)
00114                 QObject::connect(this, SIGNAL(customLevelChanged(int)), recvr, slotCustom);
00115 
00116         m_menu = new KSelectAction(KIcon("games-difficult"), i18n("Difficulty"), window);
00117         m_menu->setToolTip(i18n("Set the difficulty level"));
00118         m_menu->setWhatsThis(i18n("Set the difficulty level of the game."));
00119         QObject::connect(m_menu, SIGNAL(triggered(int)), this, SLOT(changeSelection(int)));
00120         m_menu->setObjectName("options_game_difficulty");
00121         window->actionCollection()->addAction(m_menu->objectName(), m_menu);
00122 
00123         setParent(window);
00124 
00125         m_comboBox = new KComboBox(window);
00126         m_comboBox->setToolTip(i18n("Difficulty"));
00127         QObject::connect(m_comboBox, SIGNAL(activated(int)), this, SLOT(changeSelection(int)));
00128         window->statusBar()->addPermanentWidget(m_comboBox);
00129 
00130         KGameDifficulty::setRestartOnChange(KGameDifficulty::RestartOnChange);
00131 }
00132 
00133 
00134 void KGameDifficultyPrivate::changeSelection(int newSelection)
00135 {
00136         if (newSelection!=m_oldSelection) {
00137                 bool mayChange = true;
00138 
00139                 if (mayChange && (m_restartOnChange==KGameDifficulty::RestartOnChange) && m_running)
00140                         mayChange = ( KMessageBox::warningContinueCancel(0, i18n("Changing the difficulty level will end the current game!"), QString(), KGuiItem(i18n("Change the difficulty level"))) == KMessageBox::Continue );
00141 
00142                 if (mayChange) {
00143                         setSelection(newSelection);
00144                 } else {
00145                         // restore current level selection
00146                         setSelection(m_oldSelection);
00147                 }
00148         }
00149 }
00150 
00151 QString KGameDifficultyPrivate::standardLevelString(KGameDifficulty::standardLevel level)
00152 {
00153     switch (level) {
00154         case KGameDifficulty::RidiculouslyEasy:
00155             return i18n("Ridiculously Easy");
00156         case KGameDifficulty::VeryEasy:
00157             return i18n("Very Easy");
00158         case KGameDifficulty::Easy:
00159             return i18n("Easy");
00160         case KGameDifficulty::Medium:
00161             return i18n("Medium");
00162         case KGameDifficulty::Hard:
00163             return i18n("Hard");
00164         case KGameDifficulty::VeryHard:
00165             return i18n("Very Hard");
00166         case KGameDifficulty::ExtremelyHard:
00167             return i18n("Extremely Hard");
00168         case KGameDifficulty::Impossible:
00169             return i18n("Impossible");
00170         case KGameDifficulty::Custom:
00171         case KGameDifficulty::Configurable:
00172         case KGameDifficulty::NoLevel:
00173             // Do nothing
00174             break;
00175     }
00176     return QString();
00177 }
00178 
00179 void KGameDifficultyPrivate::rebuildActions()
00180 {
00181         m_menu->clear();
00182         m_comboBox->clear();
00183         qSort(m_standardLevels.begin(), m_standardLevels.end());
00184 
00185         foreach(KGameDifficulty::standardLevel level, m_standardLevels) {
00186                 if (level!=KGameDifficulty::Configurable) {
00187                         m_menu->addAction(standardLevelString(level));
00188                         m_comboBox->addItem(KIcon("games-difficult"), standardLevelString(level));
00189                 }
00190         }
00191 
00192         if (m_customLevels.count()>0) {
00193                 foreach(const QString &s, m_customLevels) {
00194                         m_menu->addAction(s);
00195                         m_comboBox->addItem(KIcon("games-difficult"), s);
00196                 }
00197         }
00198 
00199         if (m_standardLevels.contains(KGameDifficulty::Configurable)) {
00200                 QAction* separator = new QAction(m_menu);
00201                 separator->setSeparator(true);
00202                 m_menu->addAction(separator);
00203 
00204                 QString s = i18n("Custom");
00205                 m_menu->addAction(s);
00206                 m_comboBox->addItem(KIcon("games-difficult"), s);
00207         }
00208 
00209         // reselect the previous selected item.
00210         if (m_level==KGameDifficulty::Custom)
00211                 KGameDifficulty::setLevelCustom(m_levelCustom);
00212         else if (m_standardLevels.contains(m_level))
00213                 KGameDifficulty::setLevel(m_level);
00214 }
00215 
00216 void KGameDifficultyPrivate::setSelection(int newSelection)
00217 {
00218         int countWithoutConfigurable = m_standardLevels.count();
00219         if (m_standardLevels.contains(KGameDifficulty::Configurable))
00220                 countWithoutConfigurable--;
00221 
00222         if ((m_standardLevels.contains(KGameDifficulty::Configurable)) && (newSelection>m_menu->actions().count()-3))
00223                 KGameDifficulty::setLevel(KGameDifficulty::Configurable);
00224         else if(newSelection<countWithoutConfigurable)
00225                 KGameDifficulty::setLevel(m_standardLevels[newSelection]);
00226         else
00227                 KGameDifficulty::setLevelCustom((m_customLevels.uniqueKeys()).value(newSelection - countWithoutConfigurable));
00228 
00229         m_oldSelection = newSelection;
00230 }
00231 
00232 
00233 void KGameDifficultyPrivate::setLevel(KGameDifficulty::standardLevel level)
00234 {
00235         if ((!m_standardLevels.contains(level)) && (level!=KGameDifficulty::Custom))
00236                 level = KGameDifficulty::NoLevel;
00237 
00238         if (level==KGameDifficulty::Configurable) {
00239                 m_menu->setCurrentItem(m_menu->actions().count()-1);
00240                 m_comboBox->setCurrentIndex(m_comboBox->count()-1);
00241         } else if (level!=KGameDifficulty::Custom) {
00242                 int i = m_standardLevels.indexOf(level);
00243                 m_menu->setCurrentItem(i);
00244                 m_comboBox->setCurrentIndex(i);
00245         }
00246 
00247         if (level != m_level) {
00248                 m_level = level;
00249                 emit standardLevelChanged(level);
00250         }
00251 
00252         m_oldSelection = m_menu->currentItem();
00253 }
00254 
00255 
00256 void KGameDifficultyPrivate::setLevelCustom(int key)
00257 {
00258         m_level = KGameDifficulty::Custom;
00259 
00260         int a = m_standardLevels.count();
00261         if (m_standardLevels.contains(KGameDifficulty::Configurable))
00262                 a -= 1;
00263 
00264         int i = (m_customLevels.uniqueKeys()).indexOf(key) + a;
00265         m_menu->setCurrentItem(i);
00266         m_comboBox->setCurrentIndex(i);
00267 
00268         if (key != m_levelCustom) {
00269                 m_levelCustom = key;
00270                 emit customLevelChanged(key);
00271         }
00272 
00273         m_oldSelection = m_menu->currentItem();
00274 }
00275 
00276 
00277 
00278 //---//
00279 
00280 
00281 
00282 KGameDifficulty* KGameDifficulty::instance = 0;
00283 
00284 
00285 KGameDifficulty::~KGameDifficulty()
00286 {
00287         // We do not need to delete d, because d deletes us.
00288 }
00289 
00290 
00291 void KGameDifficulty::init(KXmlGuiWindow* window, const QObject* recvr, const char* slotStandard, const char* slotCustom)
00292 {
00293         self()->d->init(window, recvr, slotStandard, slotCustom);
00294 }
00295 
00296 
00297 void KGameDifficulty::setRestartOnChange(onChange restart)
00298 {
00299         Q_ASSERT(self()->d);
00300 
00301         self()->d->m_restartOnChange = restart;
00302         if (restart==RestartOnChange)
00303                 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."));
00304         else
00305                 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."));
00306 
00307 }
00308 
00309 
00310 void KGameDifficulty::addStandardLevel(standardLevel level)
00311 {
00312         Q_ASSERT(self()->d);
00313 
00314         if ((level!=Custom) && (level!=NoLevel)) {
00315                 self()->d->m_standardLevels.append(level);
00316                 self()->d->rebuildActions();
00317         }
00318 }
00319 
00320 
00321 void KGameDifficulty::removeStandardLevel(standardLevel level)
00322 {
00323         Q_ASSERT(self()->d);
00324 
00325         self()->d->m_standardLevels.removeAll(level);
00326         self()->d->rebuildActions();
00327 }
00328 
00329 
00330 void KGameDifficulty::addCustomLevel(int key, const QString& appellation)
00331 {
00332         Q_ASSERT(self()->d);
00333 
00334         self()->d->m_customLevels.insert(key, appellation);
00335         self()->d->rebuildActions();
00336 }
00337 
00338 
00339 void KGameDifficulty::removeCustomLevel(int key)
00340 {
00341         Q_ASSERT(self()->d);
00342 
00343         self()->d->m_customLevels.remove(key);
00344         self()->d->rebuildActions();
00345 }
00346 
00347 
00348 void KGameDifficulty::setEnabled(bool enabled)
00349 {
00350         Q_ASSERT(self()->d->m_menu);
00351 
00352         // 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...
00353         self()->d->m_menu->setEnabled(enabled);
00354         self()->d->m_comboBox->setEnabled(enabled);
00355 }
00356 
00357 
00358 void KGameDifficulty::setLevel(standardLevel level)
00359 {
00360         Q_ASSERT(self()->d);
00361 
00362         self()->d->setLevel(level);
00363 }
00364 
00365 
00366 void KGameDifficulty::setLevelCustom(int key)
00367 {
00368         Q_ASSERT(self()->d);
00369 
00370         self()->d->setLevelCustom(key);
00371 }
00372 
00373 
00374 int KGameDifficulty::levelCustom()
00375 {
00376         Q_ASSERT(self()->d);
00377 
00378         return self()->d->m_levelCustom;
00379 }
00380 
00381 
00382 KGameDifficulty::standardLevel KGameDifficulty::level()
00383 {
00384         Q_ASSERT(self()->d);
00385 
00386         return self()->d->m_level;
00387 }
00388 
00389 
00390 QString KGameDifficulty::levelString()
00391 {
00392         Q_ASSERT(self()->d);
00393 
00394         return self()->d->standardLevelString(self()->d->m_level);
00395 }
00396 
00397 
00398 void KGameDifficulty::setRunning(bool running)
00399 {
00400         Q_ASSERT(self()->d);
00401 
00402         self()->d->m_running = running;
00403 }
00404 
00405 
00406 KGameDifficulty::KGameDifficulty() : d(new KGameDifficultyPrivate())
00407 {
00408 }
00409 
00410 
00411 KGameDifficulty* KGameDifficulty::self()
00412 {
00413         if (instance==0)
00414                 instance = new KGameDifficulty();
00415         return instance;
00416 }
00417 
00418 #include "kgamedifficulty.moc"

Generated on Sun Mar 16 08:02:53 2008 for Libkdegames by  doxygen 1.5.3