Skip to content

Commit

Permalink
Fix for style loading and clean up of used labeling custom properties
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed May 16, 2017
1 parent 56309ef commit ee9fabb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
8 changes: 8 additions & 0 deletions python/core/qgsmaplayer.sip
Expand Up @@ -448,6 +448,14 @@ Invoked by QgsProject.read().
:rtype: bool
%End

QStringList customPropertyKeys() const;
%Docstring
Returns list of all keys within custom properties. Properties are stored in a map and saved in project file.
.. seealso:: customProperty()
.. versionadded:: 3.0
:rtype: list of str
%End

void setCustomProperty( const QString &key, const QVariant &value );
%Docstring
Set a custom property for layer. Properties are stored in a map and saved in project file.
Expand Down
7 changes: 6 additions & 1 deletion src/core/qgsmaplayer.cpp
Expand Up @@ -788,7 +788,7 @@ bool QgsMapLayer::writeLayerXml( QDomElement &layerElement, QDomDocument &docume

return writeXml( layerElement, document, context );

} // bool QgsMapLayer::writeXml
}


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


QStringList QgsMapLayer::customPropertyKeys() const
{
return mCustomProperties.keys();
}

void QgsMapLayer::setCustomProperty( const QString &key, const QVariant &value )
{
mCustomProperties.setValue( key, value );
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsmaplayer.h
Expand Up @@ -427,6 +427,12 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
bool writeLayerXml( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context ) const;

/** Returns list of all keys within custom properties. Properties are stored in a map and saved in project file.
* \see customProperty()
* \since QGIS 3.0
*/
QStringList customPropertyKeys() const;

/** Set a custom property for layer. Properties are stored in a map and saved in project file.
* \see customProperty()
* \see removeCustomProperty()
Expand Down
33 changes: 21 additions & 12 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -1769,6 +1769,11 @@ bool QgsVectorLayer::readStyle( const QDomNode &node, QString &errorMessage, con
if ( labelingElement.isNull() ||
( labelingElement.attribute( "type" ) == "simple" && labelingElement.firstChildElement( QStringLiteral( "settings" ) ).isNull() ) )
{
// make sure we have custom properties for labeling for 2.x projects
// (custom properties should be already loaded when reading the whole layer from XML,
// but when reading style, custom properties are not read)
readCustomProperties( node, QStringLiteral( "labeling" ) );

// support for pre-QGIS 3 labeling configurations written in custom properties
labeling = readLabelingFromCustomProperties();
}
Expand Down Expand Up @@ -1811,9 +1816,6 @@ bool QgsVectorLayer::readStyle( const QDomNode &node, QString &errorMessage, con
mSimplifyMethod.setForceLocalOptimization( e.attribute( QStringLiteral( "simplifyLocal" ), QStringLiteral( "1" ) ).toInt() );
mSimplifyMethod.setMaximumScale( e.attribute( QStringLiteral( "simplifyMaxScale" ), QStringLiteral( "1" ) ).toFloat() );

//also restore custom properties (for labeling-ng)
readCustomProperties( node, QStringLiteral( "labeling" ) );

//diagram renderer and diagram layer settings
delete mDiagramRenderer;
mDiagramRenderer = nullptr;
Expand Down Expand Up @@ -4225,17 +4227,24 @@ QgsEditorWidgetSetup QgsVectorLayer::editorWidgetSetup( int index ) const
QgsAbstractVectorLayerLabeling *QgsVectorLayer::readLabelingFromCustomProperties()
{
QgsAbstractVectorLayerLabeling *labeling = nullptr;
if ( customProperty( QStringLiteral( "labeling" ) ).toString() == QLatin1String( "pal" ) &&
customProperty( QStringLiteral( "labeling/enabled" ), QVariant( false ) ).toBool() == true )
if ( customProperty( QStringLiteral( "labeling" ) ).toString() == QLatin1String( "pal" ) )
{
// try to load from custom properties
QgsPalLayerSettings settings;
settings.readFromLayerCustomProperties( this );
labeling = new QgsVectorLayerSimpleLabeling( settings );
}
if ( customProperty( QStringLiteral( "labeling/enabled" ), QVariant( false ) ).toBool() == true )
{
// try to load from custom properties
QgsPalLayerSettings settings;
settings.readFromLayerCustomProperties( this );
labeling = new QgsVectorLayerSimpleLabeling( settings );
}

// also clear old-style labeling config
removeCustomProperty( QStringLiteral( "labeling" ) );
// also clear old-style labeling config
removeCustomProperty( QStringLiteral( "labeling" ) );
Q_FOREACH ( const QString &key, customPropertyKeys() )
{
if ( key.startsWith( "labeling/" ) )
removeCustomProperty( key );
}
}

return labeling;
}

0 comments on commit ee9fabb

Please sign in to comment.