Skip to content

Commit ee9fabb

Browse files
committedMay 16, 2017
Fix for style loading and clean up of used labeling custom properties
1 parent 56309ef commit ee9fabb

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed
 

‎python/core/qgsmaplayer.sip

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,14 @@ Invoked by QgsProject.read().
448448
:rtype: bool
449449
%End
450450

451+
QStringList customPropertyKeys() const;
452+
%Docstring
453+
Returns list of all keys within custom properties. Properties are stored in a map and saved in project file.
454+
.. seealso:: customProperty()
455+
.. versionadded:: 3.0
456+
:rtype: list of str
457+
%End
458+
451459
void setCustomProperty( const QString &key, const QVariant &value );
452460
%Docstring
453461
Set a custom property for layer. Properties are stored in a map and saved in project file.

‎src/core/qgsmaplayer.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ bool QgsMapLayer::writeLayerXml( QDomElement &layerElement, QDomDocument &docume
788788

789789
return writeXml( layerElement, document, context );
790790

791-
} // bool QgsMapLayer::writeXml
791+
}
792792

793793

794794
bool QgsMapLayer::writeXml( QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context ) const
@@ -1573,6 +1573,11 @@ QUndoStack *QgsMapLayer::undoStackStyles()
15731573
}
15741574

15751575

1576+
QStringList QgsMapLayer::customPropertyKeys() const
1577+
{
1578+
return mCustomProperties.keys();
1579+
}
1580+
15761581
void QgsMapLayer::setCustomProperty( const QString &key, const QVariant &value )
15771582
{
15781583
mCustomProperties.setValue( key, value );

‎src/core/qgsmaplayer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,12 @@ class CORE_EXPORT QgsMapLayer : public QObject
427427
*/
428428
bool writeLayerXml( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context ) const;
429429

430+
/** Returns list of all keys within custom properties. Properties are stored in a map and saved in project file.
431+
* \see customProperty()
432+
* \since QGIS 3.0
433+
*/
434+
QStringList customPropertyKeys() const;
435+
430436
/** Set a custom property for layer. Properties are stored in a map and saved in project file.
431437
* \see customProperty()
432438
* \see removeCustomProperty()

‎src/core/qgsvectorlayer.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,11 @@ bool QgsVectorLayer::readStyle( const QDomNode &node, QString &errorMessage, con
17691769
if ( labelingElement.isNull() ||
17701770
( labelingElement.attribute( "type" ) == "simple" && labelingElement.firstChildElement( QStringLiteral( "settings" ) ).isNull() ) )
17711771
{
1772+
// make sure we have custom properties for labeling for 2.x projects
1773+
// (custom properties should be already loaded when reading the whole layer from XML,
1774+
// but when reading style, custom properties are not read)
1775+
readCustomProperties( node, QStringLiteral( "labeling" ) );
1776+
17721777
// support for pre-QGIS 3 labeling configurations written in custom properties
17731778
labeling = readLabelingFromCustomProperties();
17741779
}
@@ -1811,9 +1816,6 @@ bool QgsVectorLayer::readStyle( const QDomNode &node, QString &errorMessage, con
18111816
mSimplifyMethod.setForceLocalOptimization( e.attribute( QStringLiteral( "simplifyLocal" ), QStringLiteral( "1" ) ).toInt() );
18121817
mSimplifyMethod.setMaximumScale( e.attribute( QStringLiteral( "simplifyMaxScale" ), QStringLiteral( "1" ) ).toFloat() );
18131818

1814-
//also restore custom properties (for labeling-ng)
1815-
readCustomProperties( node, QStringLiteral( "labeling" ) );
1816-
18171819
//diagram renderer and diagram layer settings
18181820
delete mDiagramRenderer;
18191821
mDiagramRenderer = nullptr;
@@ -4225,17 +4227,24 @@ QgsEditorWidgetSetup QgsVectorLayer::editorWidgetSetup( int index ) const
42254227
QgsAbstractVectorLayerLabeling *QgsVectorLayer::readLabelingFromCustomProperties()
42264228
{
42274229
QgsAbstractVectorLayerLabeling *labeling = nullptr;
4228-
if ( customProperty( QStringLiteral( "labeling" ) ).toString() == QLatin1String( "pal" ) &&
4229-
customProperty( QStringLiteral( "labeling/enabled" ), QVariant( false ) ).toBool() == true )
4230+
if ( customProperty( QStringLiteral( "labeling" ) ).toString() == QLatin1String( "pal" ) )
42304231
{
4231-
// try to load from custom properties
4232-
QgsPalLayerSettings settings;
4233-
settings.readFromLayerCustomProperties( this );
4234-
labeling = new QgsVectorLayerSimpleLabeling( settings );
4235-
}
4232+
if ( customProperty( QStringLiteral( "labeling/enabled" ), QVariant( false ) ).toBool() == true )
4233+
{
4234+
// try to load from custom properties
4235+
QgsPalLayerSettings settings;
4236+
settings.readFromLayerCustomProperties( this );
4237+
labeling = new QgsVectorLayerSimpleLabeling( settings );
4238+
}
42364239

4237-
// also clear old-style labeling config
4238-
removeCustomProperty( QStringLiteral( "labeling" ) );
4240+
// also clear old-style labeling config
4241+
removeCustomProperty( QStringLiteral( "labeling" ) );
4242+
Q_FOREACH ( const QString &key, customPropertyKeys() )
4243+
{
4244+
if ( key.startsWith( "labeling/" ) )
4245+
removeCustomProperty( key );
4246+
}
4247+
}
42394248

42404249
return labeling;
42414250
}

0 commit comments

Comments
 (0)
Please sign in to comment.