Add a generic method for adding style entities to a QgsStyle

This commit is contained in:
Nyall Dawson 2019-07-03 14:58:23 +10:00
parent 590d1aae9d
commit 4f5f6b5607
3 changed files with 57 additions and 0 deletions

View File

@ -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

View File

@ -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 )

View File

@ -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
*