Skip to content

Commit

Permalink
Fixes #44692 : emit customPropertyChanged when read from XML
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 authored and nyalldawson committed Sep 29, 2021
1 parent ddd9c20 commit aeae9dc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -774,7 +774,17 @@ void QgsMapLayer::resolveReferences( QgsProject *project )

void QgsMapLayer::readCustomProperties( const QDomNode &layerNode, const QString &keyStartsWith )
{
const QgsObjectCustomProperties oldKeys = mCustomProperties;

mCustomProperties.readXml( layerNode, keyStartsWith );

for ( const QString key : mCustomProperties.keys() )
{
if ( !oldKeys.contains( key ) || mCustomProperties.value( key ) != oldKeys.value( key ) )
{
emit customPropertyChanged( key );
}
}
}

void QgsMapLayer::writeCustomProperties( QDomNode &layerNode, QDomDocument &doc ) const
Expand Down Expand Up @@ -1963,6 +1973,10 @@ void QgsMapLayer::setCustomProperty( const QString &key, const QVariant &value )
void QgsMapLayer::setCustomProperties( const QgsObjectCustomProperties &properties )
{
mCustomProperties = properties;
for ( const QString key : mCustomProperties.keys() )
{
emit customPropertyChanged( key );
}
}

const QgsObjectCustomProperties &QgsMapLayer::customProperties() const
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsmaplayer.h
Expand Up @@ -2111,6 +2111,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
QString mLegendPlaceholderImage;

friend class QgsVectorLayer;
friend class TestQgsMapLayer;
};

Q_DECLARE_METATYPE( QgsMapLayer * )
Expand Down
36 changes: 36 additions & 0 deletions tests/src/core/testqgsmaplayer.cpp
Expand Up @@ -29,6 +29,7 @@
#include "qgsvectorlayerref.h"
#include "qgsmaplayerlistutils.h"
#include "qgsmaplayerproxymodel.h"
#include "qgsxmlutils.h"

/**
* \ingroup UnitTests
Expand Down Expand Up @@ -67,6 +68,8 @@ class TestQgsMapLayer : public QObject

void customEnumFlagProperties();

void readCustomProperties();

private:
QgsVectorLayer *mpLayer = nullptr;
};
Expand Down Expand Up @@ -442,6 +445,39 @@ void TestQgsMapLayer::customEnumFlagProperties()
QCOMPARE( v5ss, QStringLiteral( "PointLayer|PolygonLayer" ) );
}

void TestQgsMapLayer::readCustomProperties()
{
std::unique_ptr<QgsVectorLayer> ml = std::make_unique<QgsVectorLayer>( QStringLiteral( "Point" ), QStringLiteral( "name" ), QStringLiteral( "memory" ) );

// assign to inexisting property
ml->setCustomProperty( QStringLiteral( "my_property_one" ), 42 );
ml->setCustomProperty( QStringLiteral( "my_property_two" ), QStringLiteral( "test2" ) );
ml->setCustomProperty( QStringLiteral( "my_property_three" ), QStringLiteral( "test3" ) );

QMap<QString, QVariant> map;
map[ "my_property_one" ] = 51;
map[ "my_property_two" ] = QStringLiteral( "test2 different" );
map[ "my_property_three" ] = QStringLiteral( "test3" );

QDomDocument doc( QStringLiteral( "qgis" ) );
QDomElement rootNode = doc.createElement( QStringLiteral( "qgis" ) );
QDomElement propsElement = doc.createElement( QStringLiteral( "customproperties" ) );
propsElement.appendChild( QgsXmlUtils::writeVariant( map, doc ) );
rootNode.appendChild( propsElement );

const QSignalSpy spy( ml.get(), &QgsMapLayer::customPropertyChanged );
ml->readCustomProperties( rootNode, "group" );

const QgsObjectCustomProperties &props = ml->customProperties();
QCOMPARE( props.value( QStringLiteral( "my_property_one" ) ), 51 );
QCOMPARE( props.value( QStringLiteral( "my_property_two" ) ), QStringLiteral( "test2 different" ) );
QCOMPARE( props.value( QStringLiteral( "my_property_three" ) ), QStringLiteral( "test3" ) );

QCOMPARE( spy.count(), 2 );
QCOMPARE( spy.at( 0 ).at( 0 ), "my_property_one" );
QCOMPARE( spy.at( 1 ).at( 0 ), "my_property_two" );

}

QGSTEST_MAIN( TestQgsMapLayer )
#include "testqgsmaplayer.moc"

0 comments on commit aeae9dc

Please sign in to comment.