Skip to content
Permalink
Browse files

[api] Add QgsMapLayerStyleManager::copyStylesFrom

  • Loading branch information
m-kuhn committed Jun 3, 2020
1 parent 6c5ac93 commit 68574d9835d0769dcb4c0ea27bc2c232d6b55f27
@@ -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:
@@ -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 ) );
}
}
@@ -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 );
@@ -39,6 +39,7 @@ class TestQgsMapLayerStyleManager : public QObject
void testStyle();
void testReadWrite();
void testSwitchingStyles();
void testCopyStyles();

private:
QgsVectorLayer *mVL = nullptr;
@@ -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.
You can’t perform that action at this time.