Skip to content
Permalink
Browse files
Fix for style loading and clean up of used labeling custom properties
  • Loading branch information
wonder-sk committed May 16, 2017
1 parent 56309ef commit ee9fabbec1ef71c116bc5e5321bcffa07a677beb
Showing with 41 additions and 13 deletions.
  1. +8 −0 python/core/qgsmaplayer.sip
  2. +6 −1 src/core/qgsmaplayer.cpp
  3. +6 −0 src/core/qgsmaplayer.h
  4. +21 −12 src/core/qgsvectorlayer.cpp
@@ -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.
@@ -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
@@ -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 );
@@ -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()
@@ -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();
}
@@ -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;
@@ -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.