diff --git a/python/core/layout/qgslayoutitemregistry.sip b/python/core/layout/qgslayoutitemregistry.sip index 98841fcbcbb..adeb4972b8e 100644 --- a/python/core/layout/qgslayoutitemregistry.sip +++ b/python/core/layout/qgslayoutitemregistry.sip @@ -110,14 +110,23 @@ class QgsLayoutItemRegistry : QObject QgsLayoutItemRegistry( QObject *parent = 0 ); %Docstring - Creates a registry and populates it with standard item types. + Creates a new empty item registry. QgsLayoutItemRegistry is not usually directly created, but rather accessed through QgsApplication.layoutItemRegistry(). + +.. seealso:: populate() %End ~QgsLayoutItemRegistry(); + bool populate(); +%Docstring + Populates the registry with standard item types. If called on a non-empty registry + then this will have no effect and will return false. + :rtype: bool +%End + QgsLayoutItemAbstractMetadata *itemMetadata( int type ) const; %Docstring diff --git a/src/core/layout/qgslayoutitemregistry.cpp b/src/core/layout/qgslayoutitemregistry.cpp index 465cf1aa4fd..d93311b4bdf 100644 --- a/src/core/layout/qgslayoutitemregistry.cpp +++ b/src/core/layout/qgslayoutitemregistry.cpp @@ -20,6 +20,17 @@ QgsLayoutItemRegistry::QgsLayoutItemRegistry( QObject *parent ) : QObject( parent ) { +} + +QgsLayoutItemRegistry::~QgsLayoutItemRegistry() +{ + qDeleteAll( mMetadata ); +} + +bool QgsLayoutItemRegistry::populate() +{ + if ( !mMetadata.isEmpty() ) + return false; // add temporary item to register auto createTemporaryItem = []( QgsLayout * layout, const QVariantMap & )->QgsLayoutItem* @@ -28,11 +39,7 @@ QgsLayoutItemRegistry::QgsLayoutItemRegistry( QObject *parent ) }; addLayoutItemType( new QgsLayoutItemMetadata( 101, QStringLiteral( "temp type" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddLabel.svg" ) ), createTemporaryItem ) ); -} - -QgsLayoutItemRegistry::~QgsLayoutItemRegistry() -{ - qDeleteAll( mMetadata ); + return true; } QgsLayoutItemAbstractMetadata *QgsLayoutItemRegistry::itemMetadata( int type ) const diff --git a/src/core/layout/qgslayoutitemregistry.h b/src/core/layout/qgslayoutitemregistry.h index d44177feb31..d51986acaaa 100644 --- a/src/core/layout/qgslayoutitemregistry.h +++ b/src/core/layout/qgslayoutitemregistry.h @@ -232,15 +232,23 @@ class CORE_EXPORT QgsLayoutItemRegistry : public QObject }; /** - * Creates a registry and populates it with standard item types. + * Creates a new empty item registry. * * QgsLayoutItemRegistry is not usually directly created, but rather accessed through * QgsApplication::layoutItemRegistry(). + * + * \see populate() */ QgsLayoutItemRegistry( QObject *parent = nullptr ); ~QgsLayoutItemRegistry(); + /** + * Populates the registry with standard item types. If called on a non-empty registry + * then this will have no effect and will return false. + */ + bool populate(); + //! QgsLayoutItemRegistry cannot be copied. QgsLayoutItemRegistry( const QgsLayoutItemRegistry &rh ) = delete; //! QgsLayoutItemRegistryQgsLayoutItemRegistry cannot be copied. diff --git a/src/core/qgsapplication.cpp b/src/core/qgsapplication.cpp index 2eaf53a30b8..fef4b85cc20 100644 --- a/src/core/qgsapplication.cpp +++ b/src/core/qgsapplication.cpp @@ -1596,6 +1596,7 @@ QgsApplication::ApplicationMembers::ApplicationMembers() mProcessingRegistry = new QgsProcessingRegistry(); mPageSizeRegistry = new QgsPageSizeRegistry(); mLayoutItemRegistry = new QgsLayoutItemRegistry(); + mLayoutItemRegistry->populate(); mProcessingRegistry->addProvider( new QgsNativeAlgorithms( mProcessingRegistry ) ); mAnnotationRegistry = new QgsAnnotationRegistry(); } diff --git a/tests/src/core/testqgslayoutitem.cpp b/tests/src/core/testqgslayoutitem.cpp index 26c2485e74f..ea9a765a039 100644 --- a/tests/src/core/testqgslayoutitem.cpp +++ b/tests/src/core/testqgslayoutitem.cpp @@ -159,6 +159,13 @@ void TestQgsLayoutItem::registry() QCOMPARE( props.size(), 1 ); registry.resolvePaths( 2, props, QgsPathResolver(), true ); QVERIFY( props.isEmpty() ); + + //test populate + QgsLayoutItemRegistry reg2; + QVERIFY( reg2.itemTypes().isEmpty() ); + QVERIFY( reg2.populate() ); + QVERIFY( !reg2.itemTypes().isEmpty() ); + QVERIFY( !reg2.populate() ); } bool TestQgsLayoutItem::renderCheck( QString testName, QImage &image, int mismatchCount )