Skip to content

Commit

Permalink
[api] Add QgsMapLayerStyleManager::copyStylesFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jun 3, 2020
1 parent 6c5ac93 commit 68574d9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
8 changes: 8 additions & 0 deletions python/core/auto_generated/qgsmaplayerstylemanager.sip.in
Expand Up @@ -141,6 +141,14 @@ Restore the original store after a call to setOverrideStyle()
Returns ``True`` if this is the default style

.. versionadded:: 3.0
%End

void copyStylesFrom( QgsMapLayerStyleManager *other );
%Docstring
Copies all styles from ``other``.
In case there is already a style with the same name it will be overwritten.

.. versionadded:: 3.14
%End

signals:
Expand Down
11 changes: 11 additions & 0 deletions src/core/qgsmaplayerstylemanager.cpp
Expand Up @@ -230,3 +230,14 @@ bool QgsMapLayerStyleManager::isDefault( const QString &styleName ) const
{
return styleName == defaultStyleName();
}

void QgsMapLayerStyleManager::copyStylesFrom( QgsMapLayerStyleManager *other )
{
const QStringList styleNames = other->mStyles.keys();

for ( const QString &styleName : styleNames )
{
mStyles.remove( styleName );
addStyle( styleName, other->style( styleName ) );
}
}
8 changes: 8 additions & 0 deletions src/core/qgsmaplayerstylemanager.h
Expand Up @@ -136,6 +136,14 @@ class CORE_EXPORT QgsMapLayerStyleManager : public QObject
*/
bool isDefault( const QString &styleName ) const;

/**
* Copies all styles from \a other.
* In case there is already a style with the same name it will be overwritten.
*
* \since QGIS 3.14
*/
void copyStylesFrom( QgsMapLayerStyleManager *other );

signals:
//! Emitted when a new style has been added
void styleAdded( const QString &name );
Expand Down
20 changes: 20 additions & 0 deletions tests/src/core/testqgsmaplayerstylemanager.cpp
Expand Up @@ -39,6 +39,7 @@ class TestQgsMapLayerStyleManager : public QObject
void testStyle();
void testReadWrite();
void testSwitchingStyles();
void testCopyStyles();

private:
QgsVectorLayer *mVL = nullptr;
Expand Down Expand Up @@ -200,6 +201,25 @@ void TestQgsMapLayerStyleManager::testSwitchingStyles()
QCOMPARE( _getVLColor( mVL ), QColor( Qt::blue ) );
}

void TestQgsMapLayerStyleManager::testCopyStyles()
{
std::unique_ptr<QgsVectorLayer> lines = qgis::make_unique<QgsVectorLayer>( QStringLiteral( "LineString" ), QStringLiteral( "Line Layer" ), QStringLiteral( "memory" ) );
std::unique_ptr<QgsVectorLayer> lines2 = qgis::make_unique<QgsVectorLayer>( QStringLiteral( "LineString" ), QStringLiteral( "Line Layer" ), QStringLiteral( "memory" ) );

QgsMapLayerStyleManager *sm = lines->styleManager();

sm->addStyleFromLayer( QStringLiteral( "style2" ) );

QgsMapLayerStyleManager *sm2 = lines2->styleManager();

sm2->copyStylesFrom( sm );
sm2->addStyleFromLayer( "style3" );

QVERIFY( sm2->styles().contains( "style2" ) );
QVERIFY( sm2->styles().contains( "style3" ) );
QVERIFY( sm2->styles().contains( "default" ) );
}


QGSTEST_MAIN( TestQgsMapLayerStyleManager )
#include "testqgsmaplayerstylemanager.moc"

0 comments on commit 68574d9

Please sign in to comment.