From 4f5f6b5607d36eb9982b96712c6c0e97f9fbd04d Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Wed, 3 Jul 2019 14:58:23 +1000 Subject: [PATCH] Add a generic method for adding style entities to a QgsStyle --- .../auto_generated/symbology/qgsstyle.sip.in | 15 ++++++++++ src/core/symbology/qgsstyle.cpp | 28 +++++++++++++++++++ src/core/symbology/qgsstyle.h | 14 ++++++++++ 3 files changed, 57 insertions(+) diff --git a/python/core/auto_generated/symbology/qgsstyle.sip.in b/python/core/auto_generated/symbology/qgsstyle.sip.in index 994184f1108..97223e0dea5 100644 --- a/python/core/auto_generated/symbology/qgsstyle.sip.in +++ b/python/core/auto_generated/symbology/qgsstyle.sip.in @@ -98,6 +98,21 @@ Constructor for QgsStyle. LabelSettingsEntity, }; + bool addEntity( const QString &name, const QgsStyleEntityInterface *entity, bool update = false ); +%Docstring +Adds an ``entity`` to the style, with the specified ``name``. Ownership is not transferred. + +If ``update`` is ``True`` then the style database is updated automatically as a result. + +Returns ``True`` if the add operation was successful. + +.. note:: + + Adding an entity with the name of existing one replaces the existing one automatically. + +.. versionadded:: 3.10 +%End + bool addSymbol( const QString &name, QgsSymbol *symbol /Transfer/, bool update = false ); %Docstring Adds a symbol to style and takes symbol's ownership diff --git a/src/core/symbology/qgsstyle.cpp b/src/core/symbology/qgsstyle.cpp index bd995b26068..864a6cf4869 100644 --- a/src/core/symbology/qgsstyle.cpp +++ b/src/core/symbology/qgsstyle.cpp @@ -43,6 +43,34 @@ QgsStyle::~QgsStyle() clear(); } +bool QgsStyle::addEntity( const QString &name, const QgsStyleEntityInterface *entity, bool update ) +{ + switch ( entity->type() ) + { + case SymbolEntity: + if ( !static_cast< const QgsStyleSymbolEntity * >( entity )->symbol() ) + return false; + return addSymbol( name, static_cast< const QgsStyleSymbolEntity * >( entity )->symbol()->clone(), update ); + + case ColorrampEntity: + if ( !static_cast< const QgsStyleColorRampEntity * >( entity )->ramp() ) + return false; + return addColorRamp( name, static_cast< const QgsStyleColorRampEntity * >( entity )->ramp()->clone(), update ); + + case TextFormatEntity: + return addTextFormat( name, static_cast< const QgsStyleTextFormatEntity * >( entity )->format(), update ); + + case LabelSettingsEntity: + return addLabelSettings( name, static_cast< const QgsStyleLabelSettingsEntity * >( entity )->settings(), update ); + + case TagEntity: + case SmartgroupEntity: + break; + + } + return false; +} + QgsStyle *QgsStyle::defaultStyle() // static { if ( !sDefaultStyle ) diff --git a/src/core/symbology/qgsstyle.h b/src/core/symbology/qgsstyle.h index 6c239320ee9..9960361f6dc 100644 --- a/src/core/symbology/qgsstyle.h +++ b/src/core/symbology/qgsstyle.h @@ -32,6 +32,7 @@ class QgsSymbol; class QgsSymbolLayer; class QgsColorRamp; +class QgsStyleEntityInterface; class QDomDocument; class QDomElement; @@ -183,6 +184,19 @@ class CORE_EXPORT QgsStyle : public QObject LabelSettingsEntity, //!< Label settings }; + /** + * Adds an \a entity to the style, with the specified \a name. Ownership is not transferred. + * + * If \a update is TRUE then the style database is updated automatically as a result. + * + * Returns TRUE if the add operation was successful. + * + * \note Adding an entity with the name of existing one replaces the existing one automatically. + * + * \since QGIS 3.10 + */ + bool addEntity( const QString &name, const QgsStyleEntityInterface *entity, bool update = false ); + /** * Adds a symbol to style and takes symbol's ownership *