Skip to content
Permalink
Browse files

implement filtering of categories in reading/writing

  • Loading branch information
3nids authored and nyalldawson committed Sep 14, 2018
1 parent 51e9be0 commit 448724133696618c4ffea04f7698ac9ca0f08a69
@@ -38,7 +38,7 @@ only possible to read or write layer's current style.
Fields,
Forms,
Actions,
Tooltips,
MapTips,
Diagrams,
AttributeTable,
Rendering,
@@ -246,10 +246,12 @@ bool QgsMeshLayer::readSymbology( const QDomNode &node, QString &errorMessage,
QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories )
{
Q_UNUSED( errorMessage );
Q_UNUSED( context );
Q_UNUSED( categories );
// TODO: implement categories for raster layer

QDomElement elem = node.toElement();

readCommonStyle( elem, context, categories );

QDomElement elemRendererSettings = elem.firstChildElement( "mesh-renderer-settings" );
if ( !elemRendererSettings.isNull() )
mRendererSettings.readXml( elemRendererSettings );
@@ -261,10 +263,12 @@ bool QgsMeshLayer::writeSymbology( QDomNode &node, QDomDocument &doc, QString &e
const QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories ) const
{
Q_UNUSED( errorMessage );
Q_UNUSED( context );
Q_UNUSED( categories );
// TODO: implement categories for raster layer

QDomElement elem = node.toElement();

writeCommonStyle( elem, doc, context, categories );

QDomElement elemRendererSettings = mRendererSettings.writeXml( doc );
elem.appendChild( elemRendererSettings );

@@ -559,12 +559,15 @@ void QgsMapLayer::writeCommonStyle( QDomElement &layerElement, QDomDocument &doc
layerElement.setAttribute( QStringLiteral( "minScale" ), QString::number( minimumScale() ) );
}

if ( m3DRenderer )
if ( categories.testFlag( QgsMapLayerStyle::Symbology ) )
{
QDomElement renderer3DElem = document.createElement( QStringLiteral( "renderer-3d" ) );
renderer3DElem.setAttribute( QStringLiteral( "type" ), m3DRenderer->type() );
m3DRenderer->writeXml( renderer3DElem, context );
layerElement.appendChild( renderer3DElem );
if ( m3DRenderer )
{
QDomElement renderer3DElem = document.createElement( QStringLiteral( "renderer-3d" ) );
renderer3DElem.setAttribute( QStringLiteral( "type" ), m3DRenderer->type() );
m3DRenderer->writeXml( renderer3DElem, context );
layerElement.appendChild( renderer3DElem );
}
}

if ( categories.testFlag( QgsMapLayerStyle::LayerConfiguration ) )
@@ -1112,7 +1115,7 @@ void QgsMapLayer::exportNamedStyle( QDomDocument &doc, QString &errorMsg, QgsRea
myRootNode.setAttribute( QStringLiteral( "version" ), Qgis::QGIS_VERSION );
myDocument.appendChild( myRootNode );

if ( !writeSymbology( myRootNode, myDocument, errorMsg, QgsReadWriteContext(), categories ) ) // TODO: support relative paths in QML?
if ( !writeSymbology( myRootNode, myDocument, errorMsg, context, categories ) ) // TODO: support relative paths in QML?
{
errorMsg = QObject::tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
return;
@@ -1563,6 +1566,7 @@ bool QgsMapLayer::readStyle( const QDomNode &node, QString &errorMessage, QgsRea
Q_UNUSED( node );
Q_UNUSED( errorMessage );
Q_UNUSED( context );
Q_UNUSED( categories );
return false;
}

@@ -1573,61 +1577,74 @@ bool QgsMapLayer::writeStyle( QDomNode &node, QDomDocument &doc, QString &errorM
Q_UNUSED( doc );
Q_UNUSED( errorMessage );
Q_UNUSED( context );
Q_UNUSED( categories );
return false;
}

void QgsMapLayer::readCommonStyle( const QDomElement &layerElement, const QgsReadWriteContext &context,
QgsMapLayerStyle::StyleCategories categories )
{
QgsAbstract3DRenderer *r3D = nullptr;
QDomElement renderer3DElem = layerElement.firstChildElement( QStringLiteral( "renderer-3d" ) );
if ( !renderer3DElem.isNull() )
if ( categories.testFlag( QgsMapLayerStyle::Symbology ) )
{
QString type3D = renderer3DElem.attribute( QStringLiteral( "type" ) );
Qgs3DRendererAbstractMetadata *meta3D = QgsApplication::renderer3DRegistry()->rendererMetadata( type3D );
if ( meta3D )
QgsAbstract3DRenderer *r3D = nullptr;
QDomElement renderer3DElem = layerElement.firstChildElement( QStringLiteral( "renderer-3d" ) );
if ( !renderer3DElem.isNull() )
{
r3D = meta3D->createRenderer( renderer3DElem, context );
QString type3D = renderer3DElem.attribute( QStringLiteral( "type" ) );
Qgs3DRendererAbstractMetadata *meta3D = QgsApplication::renderer3DRegistry()->rendererMetadata( type3D );
if ( meta3D )
{
r3D = meta3D->createRenderer( renderer3DElem, context );
}
}
setRenderer3D( r3D );
}
setRenderer3D( r3D );

// read custom properties before passing reading further to a subclass, so that
// the subclass can also read custom properties
readCustomProperties( layerElement );

// use scale dependent visibility flag
setScaleBasedVisibility( layerElement.attribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ) ).toInt() == 1 );
if ( layerElement.hasAttribute( QStringLiteral( "minimumScale" ) ) )
if ( categories.testFlag( QgsMapLayerStyle::CustomProperties ) )
{
// older element, when scales were reversed
setMaximumScale( layerElement.attribute( QStringLiteral( "minimumScale" ) ).toDouble() );
setMinimumScale( layerElement.attribute( QStringLiteral( "maximumScale" ) ).toDouble() );
// read custom properties before passing reading further to a subclass, so that
// the subclass can also read custom properties
readCustomProperties( layerElement );
}
else

// use scale dependent visibility flag
if ( categories.testFlag( QgsMapLayerStyle::Rendering ) )
{
setMaximumScale( layerElement.attribute( QStringLiteral( "maxScale" ) ).toDouble() );
setMinimumScale( layerElement.attribute( QStringLiteral( "minScale" ) ).toDouble() );
setScaleBasedVisibility( layerElement.attribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ) ).toInt() == 1 );
if ( layerElement.hasAttribute( QStringLiteral( "minimumScale" ) ) )
{
// older element, when scales were reversed
setMaximumScale( layerElement.attribute( QStringLiteral( "minimumScale" ) ).toDouble() );
setMinimumScale( layerElement.attribute( QStringLiteral( "maximumScale" ) ).toDouble() );
}
else
{
setMaximumScale( layerElement.attribute( QStringLiteral( "maxScale" ) ).toDouble() );
setMinimumScale( layerElement.attribute( QStringLiteral( "minScale" ) ).toDouble() );
}
}

// flags
QDomElement flagsElem = layerElement.firstChildElement( QStringLiteral( "flags" ) );
QMetaEnum metaEnum = QMetaEnum::fromType<QgsMapLayer::LayerFlag>();
LayerFlags flags = mFlags;
for ( int idx = 0; idx < metaEnum.keyCount(); ++idx )
if ( categories.testFlag( QgsMapLayerStyle::LayerConfiguration ) )
{
const char *enumKey = metaEnum.key( idx );
QDomNode flagNode = flagsElem.namedItem( QString( enumKey ) );
if ( flagNode.isNull() )
continue;
bool flagValue = flagNode.toElement().text() == "1" ? true : false;
QgsMapLayer::LayerFlag enumValue = static_cast<QgsMapLayer::LayerFlag>( metaEnum.keyToValue( enumKey ) );
if ( flags.testFlag( enumValue ) && !flagValue )
flags &= ~enumValue;
else if ( !flags.testFlag( enumValue ) && flagValue )
flags |= enumValue;
// flags
QDomElement flagsElem = layerElement.firstChildElement( QStringLiteral( "flags" ) );
QMetaEnum metaEnum = QMetaEnum::fromType<QgsMapLayer::LayerFlag>();
LayerFlags flags = mFlags;
for ( int idx = 0; idx < metaEnum.keyCount(); ++idx )
{
const char *enumKey = metaEnum.key( idx );
QDomNode flagNode = flagsElem.namedItem( QString( enumKey ) );
if ( flagNode.isNull() )
continue;
bool flagValue = flagNode.toElement().text() == "1" ? true : false;
QgsMapLayer::LayerFlag enumValue = static_cast<QgsMapLayer::LayerFlag>( metaEnum.keyToValue( enumKey ) );
if ( flags.testFlag( enumValue ) && !flagValue )
flags &= ~enumValue;
else if ( !flags.testFlag( enumValue ) && flagValue )
flags |= enumValue;
}
setFlags( flags );
}
setFlags( flags );
}

QUndoStack *QgsMapLayer::undoStack()
@@ -48,19 +48,19 @@ class CORE_EXPORT QgsMapLayerStyle
*/
enum StyleCategory
{
LayerConfiguration = 1 << 0, //!< Flags,
LayerConfiguration = 1 << 0, //!< Flags, display expression, read-only
Symbology = 1 << 1,
Labels = 1 << 2,
Fields = 1 << 3, //!< Aliases, WMS/WFS, expressions, constraints, virtual fields
Forms = 1 << 4,
Actions = 1 << 5,
Tooltips = 1 << 6,
MapTips = 1 << 6,
Diagrams = 1 << 2,
AttributeTable = 7 << 8,
Rendering = 1 << 9, //!< Scale visibility, simplify method
Rendering = 1 << 9, //!< Scale visibility, simplify method, opacity
CustomProperties = 1 << 10,
All = LayerConfiguration | Symbology | Labels | Fields | Forms | Actions |
Tooltips | Diagrams | AttributeTable | Rendering | CustomProperties,
MapTips | Diagrams | AttributeTable | Rendering | CustomProperties,
};
Q_ENUM( StyleCategory )
Q_DECLARE_FLAGS( StyleCategories, StyleCategory )

0 comments on commit 4487241

Please sign in to comment.
You can’t perform that action at this time.