[Kde-games-devel] KDE/kdegames/libkdegames

Stefan Majewsky majewsky at gmx.net
Wed Mar 7 20:02:25 UTC 2012


SVN commit 1284130 by majewsky:

Introduce a singleton instance of KgDifficulty.

This assists the very common usecase of one KgDifficulty instance and
simplifies porting from KGameDifficulty which consisted of static
class methods only. In case someone objects to this API addition:

CCMAIL: kde-games-devel at kde.org

 M  +19 -2     kgdifficulty.cpp  
 M  +13 -2     kgdifficulty.h  


--- trunk/KDE/kdegames/libkdegames/kgdifficulty.cpp #1284129:1284130
@@ -316,6 +316,19 @@
 }
 
 //END KgDifficulty
+
+K_GLOBAL_STATIC(KgDifficulty, g_difficulty)
+
+KgDifficulty* Kg::difficulty()
+{
+	return g_difficulty;
+}
+
+KgDifficultyLevel::StandardLevel Kg::difficultyLevel()
+{
+	return g_difficulty->currentLevel()->standardLevel();
+}
+
 //BEGIN KgDifficultyGUI
 
 namespace KgDifficultyGUI
@@ -351,8 +364,12 @@
 	};
 }
 
-void KgDifficultyGUI::init(KgDifficulty* difficulty, KXmlGuiWindow* window)
+void KgDifficultyGUI::init(KXmlGuiWindow* window, KgDifficulty* difficulty)
 {
+	const bool useSingleton = !difficulty;
+	if (useSingleton)
+		difficulty = Kg::difficulty();
+
 	//create selector (resides in status bar)
 	KgDifficultyGUI::Selector* selector = new KgDifficultyGUI::Selector(difficulty, window);
 	selector->setToolTip(i18nc("Game difficulty level", "Difficulty"));
@@ -387,7 +404,7 @@
 	window->actionCollection()->addAction(menu->objectName(), menu);
 
 	//ensure that the KgDifficulty instance gets deleted
-	if (!difficulty->parent())
+	if (!useSingleton && !difficulty->parent())
 	{
 		difficulty->setParent(window);
 	}
--- trunk/KDE/kdegames/libkdegames/kgdifficulty.h #1284129:1284130
@@ -174,14 +174,25 @@
 
 Q_DECLARE_METATYPE(const KgDifficultyLevel*)
 
+//TODO: Where to put documentation for this namespace?
+namespace Kg
+{
+	///@return a singleton instance of KgDifficulty
+	KDEGAMES_EXPORT KgDifficulty* difficulty();
+	///A shortcut for Kg::difficulty()->currentLevel()->standardLevel().
+	KDEGAMES_EXPORT KgDifficultyLevel::StandardLevel difficultyLevel();
+}
+
 class KXmlGuiWindow;
 
-//TODO: move this into a separate QtWidgets support library
+//TODO KDE5: move this into a separate QtWidgets support library
 namespace KgDifficultyGUI
 {
 	///Install standard GUI components for the manipulation of the given
 	///KgDifficulty instance in the given @a window.
-	KDEGAMES_EXPORT void init(KgDifficulty* difficulty, KXmlGuiWindow* window);
+	///
+	///Without a second parameter, the Kg::difficulty() singleton is used.
+	KDEGAMES_EXPORT void init(KXmlGuiWindow* window, KgDifficulty* difficulty = 0);
 }
 
 #endif // KGDIFFICULTY_H


More information about the kde-games-devel mailing list