From 6c73cfcd457d9179302a2156459c625b320ea004 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 12 May 2022 16:56:19 +1000 Subject: [PATCH] Add name to QgsStyle --- .../auto_generated/symbology/qgsstyle.sip.in | 18 ++++++++++++++++++ src/core/symbology/qgsstyle.cpp | 11 +++++++++++ src/core/symbology/qgsstyle.h | 18 ++++++++++++++++++ tests/src/core/testqgsstyle.cpp | 8 ++++++++ 4 files changed, 55 insertions(+) diff --git a/python/core/auto_generated/symbology/qgsstyle.sip.in b/python/core/auto_generated/symbology/qgsstyle.sip.in index 86034140f97..3c80d3270e4 100644 --- a/python/core/auto_generated/symbology/qgsstyle.sip.in +++ b/python/core/auto_generated/symbology/qgsstyle.sip.in @@ -99,6 +99,24 @@ Constructor for QgsStyle. Symbol3DEntity, }; + QString name() const; +%Docstring +Returns the name of the style. + +.. seealso:: :py:func:`setName` + +.. versionadded:: 3.26 +%End + + void setName( const QString &name ); +%Docstring +Sets the ``name`` of the style. + +.. seealso:: :py:func:`name` + +.. versionadded:: 3.26 +%End + 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. diff --git a/src/core/symbology/qgsstyle.cpp b/src/core/symbology/qgsstyle.cpp index 4938515e4bd..df4eaadaa32 100644 --- a/src/core/symbology/qgsstyle.cpp +++ b/src/core/symbology/qgsstyle.cpp @@ -96,6 +96,16 @@ QgsStyle::~QgsStyle() clear(); } +void QgsStyle::setName( const QString &name ) +{ + mName = name; +} + +QString QgsStyle::name() const +{ + return mName; +} + bool QgsStyle::addEntity( const QString &name, const QgsStyleEntityInterface *entity, bool update ) { switch ( entity->type() ) @@ -158,6 +168,7 @@ QgsStyle *QgsStyle::defaultStyle() // static sDefaultStyle->upgradeIfRequired(); } } + sDefaultStyle->setName( QObject::tr( "Default" ) ); } return sDefaultStyle; } diff --git a/src/core/symbology/qgsstyle.h b/src/core/symbology/qgsstyle.h index cb2434a3dcb..d6138d5b2aa 100644 --- a/src/core/symbology/qgsstyle.h +++ b/src/core/symbology/qgsstyle.h @@ -187,6 +187,22 @@ class CORE_EXPORT QgsStyle : public QObject Symbol3DEntity, //!< 3D symbol entity (since QGIS 3.14) }; + /** + * Returns the name of the style. + * + * \see setName() + * \since QGIS 3.26 + */ + QString name() const; + + /** + * Sets the \a name of the style. + * + * \see name() + * \since QGIS 3.26 + */ + void setName( const QString &name ); + /** * Adds an \a entity to the style, with the specified \a name. Ownership is not transferred. * @@ -1078,6 +1094,8 @@ class CORE_EXPORT QgsStyle : public QObject private: + QString mName; + QgsSymbolMap mSymbols; QgsVectorColorRampMap mColorRamps; QgsTextFormatMap mTextFormats; diff --git a/tests/src/core/testqgsstyle.cpp b/tests/src/core/testqgsstyle.cpp index 80203729f42..45dfae01676 100644 --- a/tests/src/core/testqgsstyle.cpp +++ b/tests/src/core/testqgsstyle.cpp @@ -104,6 +104,7 @@ class TestStyle : public QObject void cleanup() {}// will be called after every testfunction. // void initStyles(); + void testProperties(); void testCreateSymbols(); void testCreateColorRamps(); void testCreateTextFormats(); @@ -195,6 +196,13 @@ void TestStyle::cleanupTestCase() } } +void TestStyle::testProperties() +{ + QgsStyle s; + s.setName( QStringLiteral( "my name" ) ); + QCOMPARE( s.name(), QStringLiteral( "my name" ) ); +} + void TestStyle::testCreateSymbols() { // add some symbols to favorites