diff --git a/src/KPropertyFactory.h b/src/KPropertyFactory.h --- a/src/KPropertyFactory.h +++ b/src/KPropertyFactory.h @@ -125,6 +125,11 @@ KPropertyFactoryManager(); ~KPropertyFactoryManager(); + + //! Adds function @a initFunction that will be called after the manager is created. + //! Useful for creation custom factories. + static void addInitFunction(void (*initFunction)()); + private: class Private; diff --git a/src/KPropertyFactory.cpp b/src/KPropertyFactory.cpp --- a/src/KPropertyFactory.cpp +++ b/src/KPropertyFactory.cpp @@ -38,7 +38,7 @@ QHash composedPropertyCreators; }; - Q_GLOBAL_STATIC(KPropertyFactoryManager, _self) +Q_GLOBAL_STATIC(KPropertyFactoryManager, _self) //! @internal class KPropertyFactory::Private @@ -55,6 +55,10 @@ QSet composedPropertyCreatorsSet; }; +typedef QList InitFunctions; +//! @internal Used by KPropertyFactoryManager::addInitFunction() +Q_GLOBAL_STATIC(InitFunctions, _initFunctions) + KPropertyFactory::KPropertyFactory() : d( new Private ) { @@ -89,8 +93,6 @@ , d(new Private) { setObjectName(QLatin1String("KPropertyFactoryManager")); - //TODO ??? registerFactory(new KDefaultPropertyFactory); - } KPropertyFactoryManager::~KPropertyFactoryManager() @@ -100,6 +102,14 @@ KPropertyFactoryManager* KPropertyFactoryManager::self() { + if (_self.exists()) { // avoid recursion: initFunctions below may call self() + return _self; + } + _self(); // KPropertyFactoryManager should exist as initFunctions may need it + foreach(void (*initFunction)(), *_initFunctions) { + initFunction(); + } + _initFunctions->clear(); return _self; } @@ -121,6 +131,12 @@ return creator ? creator->createComposedProperty(parent) : 0; } +//static +void KPropertyFactoryManager::addInitFunction(void (*initFunction)()) +{ + _initFunctions->append(initFunction); +} + //! @todo #if 0 const int type = parent->type(); diff --git a/src/KPropertyWidgetsFactory.h b/src/KPropertyWidgetsFactory.h --- a/src/KPropertyWidgetsFactory.h +++ b/src/KPropertyWidgetsFactory.h @@ -1,5 +1,5 @@ /* This file is part of the KDE project - Copyright (C) 2008 Jarosław Staniek + Copyright (C) 2008-2015 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -27,7 +27,6 @@ #include #include #include - #include #include #include @@ -179,9 +178,8 @@ class KProperty; class KCustomProperty; -class KPROPERTYWIDGETS_EXPORT KPropertyWidgetsFactoryManager : public KPropertyFactoryManager +class KPROPERTYWIDGETS_EXPORT KPropertyWidgetsFactoryManager { - Q_OBJECT public: bool isEditorForTypeAvailable( int type ) const; @@ -196,8 +194,6 @@ const QStyleOptionViewItem & option, const QModelIndex & index ) const; - KComposedPropertyInterface* createComposedProperty(KProperty *parent); - bool canConvertValueToText( int type ) const; bool canConvertValueToText( const KProperty* property ) const; @@ -207,8 +203,6 @@ //! Registers factory @a factory. It becomes owned by the manager. void registerFactory(KPropertyWidgetsFactory *factory); - KCustomProperty* createCustomProperty( KProperty *parent ); - /*! \return a pointer to a factory manager instance.*/ static KPropertyWidgetsFactoryManager* self(); diff --git a/src/KPropertyWidgetsFactory.cpp b/src/KPropertyWidgetsFactory.cpp --- a/src/KPropertyWidgetsFactory.cpp +++ b/src/KPropertyWidgetsFactory.cpp @@ -1,5 +1,5 @@ /* This file is part of the KDE project - Copyright (C) 2008 Jarosław Staniek + Copyright (C) 2008-2015 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -61,17 +61,26 @@ } ~Private() { - qDeleteAll(factories); } - QSet factories; QHash editorCreators; QHash valuePainters; QHash valueDisplays; - }; - Q_GLOBAL_STATIC(KPropertyWidgetsFactoryManager, _self) +Q_GLOBAL_STATIC(KPropertyWidgetsFactoryManager, _self) + +//! @internal Make sure sure the KPropertyWidgetsFactoryManager is created after +//! KPropertyFactoryManager (delayed). Unless KPropertyFactoryManager is created, +//! KPropertyWidgetsFactoryManager isn't created. +//! @todo is this worth putting in a reusable macro? +struct KPropertyWidgetsFactoryManagerInitializer { + KPropertyWidgetsFactoryManagerInitializer() { + KPropertyFactoryManager::addInitFunction(&initMe); + } + static void initMe() { KPropertyWidgetsFactoryManager::self(); } +}; +KPropertyWidgetsFactoryManagerInitializer init; //! @internal class KPropertyWidgetsFactory::Private @@ -94,7 +103,6 @@ QSet editorCreatorsSet; QSet valuePaintersSet; QSet valueDisplaysSet; - }; KPropertyWidgetsFactory::KPropertyWidgetsFactory() @@ -215,10 +223,8 @@ //------------ KPropertyWidgetsFactoryManager::KPropertyWidgetsFactoryManager() - : KPropertyFactoryManager() - , d(new Private) + : d(new Private) { - setObjectName(QLatin1String("KPropertyWidgetsFactoryManager")); registerFactory(new KDefaultPropertyFactory); } @@ -234,7 +240,7 @@ void KPropertyWidgetsFactoryManager::registerFactory(KPropertyWidgetsFactory *factory) { - d->factories.insert(factory); + KPropertyFactoryManager::self()->registerFactory(factory); QHash::ConstIterator editorCreatorsItEnd = factory->editorCreators().constEnd();