Skip to content

Commit

Permalink
use configuration flags to save WMS/WFS from vector layer
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Sep 14, 2020
1 parent 19d4b52 commit 4fd940a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 50 deletions.
9 changes: 5 additions & 4 deletions python/core/auto_generated/qgsvectorlayer.sip.in
Expand Up @@ -1968,22 +1968,22 @@ Convenience function that returns the attribute alias if defined or the field na
Returns a map of field name to attribute alias
%End

QSet<QString> excludeAttributesWms() const;
QSet<QString> excludeAttributesWms() /Deprecated/ const;
%Docstring
A set of attributes that are not advertised in WMS requests with QGIS server.
%End

void setExcludeAttributesWms( const QSet<QString> &att );
void setExcludeAttributesWms( const QSet<QString> &att ) /Deprecated/;
%Docstring
A set of attributes that are not advertised in WMS requests with QGIS server.
%End

QSet<QString> excludeAttributesWfs() const;
QSet<QString> excludeAttributesWfs() /Deprecated/ const;
%Docstring
A set of attributes that are not advertised in WFS requests with QGIS server.
%End

void setExcludeAttributesWfs( const QSet<QString> &att );
void setExcludeAttributesWfs( const QSet<QString> &att ) /Deprecated/;
%Docstring
A set of attributes that are not advertised in WFS requests with QGIS server.
%End
Expand Down Expand Up @@ -2294,6 +2294,7 @@ can also be set. Setting an empty expression will clear any existing expression




void setEditorWidgetSetup( int index, const QgsEditorWidgetSetup &setup );
%Docstring
\copydoc editorWidgetSetup
Expand Down
66 changes: 24 additions & 42 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -274,8 +274,6 @@ QgsVectorLayer *QgsVectorLayer::clone() const
layer->setMapTipTemplate( mapTipTemplate() );
layer->setReadOnly( isReadOnly() );
layer->selectByIds( selectedFeatureIds() );
layer->setExcludeAttributesWms( excludeAttributesWms() );
layer->setExcludeAttributesWfs( excludeAttributesWfs() );
layer->setAttributeTableConfig( attributeTableConfig() );
layer->setFeatureBlendMode( featureBlendMode() );
layer->setOpacity( opacity() );
Expand Down Expand Up @@ -2306,26 +2304,25 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes

updateFields();

// Legacy reading for QGIS 3.14 and older projects
//Attributes excluded from WMS and WFS
mExcludeAttributesWMS.clear();
QDomNode excludeWMSNode = layerNode.namedItem( QStringLiteral( "excludeAttributesWMS" ) );
if ( !excludeWMSNode.isNull() )
const QList<QPair<QString, QgsField::ConfigurationFlag>> legacyConfig
{
QDomNodeList attributeNodeList = excludeWMSNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
for ( int i = 0; i < attributeNodeList.size(); ++i )
{
mExcludeAttributesWMS.insert( attributeNodeList.at( i ).toElement().text() );
}
}

mExcludeAttributesWFS.clear();
QDomNode excludeWFSNode = layerNode.namedItem( QStringLiteral( "excludeAttributesWFS" ) );
if ( !excludeWFSNode.isNull() )
qMakePair( QStringLiteral( "excludeAttributesWMS" ), QgsField::ConfigurationFlag::Wms ),
qMakePair( QStringLiteral( "excludeAttributesWFS" ), QgsField::ConfigurationFlag::Wfs )
};
for ( const auto &config : legacyConfig )
{
QDomNodeList attributeNodeList = excludeWFSNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
for ( int i = 0; i < attributeNodeList.size(); ++i )
QDomNode excludeNode = layerNode.namedItem( config.first );
if ( !excludeNode.isNull() )
{
mExcludeAttributesWFS.insert( attributeNodeList.at( i ).toElement().text() );
QDomNodeList attributeNodeList = excludeNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
for ( int i = 0; i < attributeNodeList.size(); ++i )
{
QString fieldName = attributeNodeList.at( i ).toElement().text();
int index = mFields.indexFromName( fieldName );
setFieldConfigurationFlag( index, config.second, false );
}
}
}

Expand Down Expand Up @@ -2660,30 +2657,6 @@ bool QgsVectorLayer::writeSymbology( QDomNode &node, QDomDocument &doc, QString
}
node.appendChild( aliasElem );

//exclude attributes WMS
QDomElement excludeWMSElem = doc.createElement( QStringLiteral( "excludeAttributesWMS" ) );
QSet<QString>::const_iterator attWMSIt = mExcludeAttributesWMS.constBegin();
for ( ; attWMSIt != mExcludeAttributesWMS.constEnd(); ++attWMSIt )
{
QDomElement attrElem = doc.createElement( QStringLiteral( "attribute" ) );
QDomText attrText = doc.createTextNode( *attWMSIt );
attrElem.appendChild( attrText );
excludeWMSElem.appendChild( attrElem );
}
node.appendChild( excludeWMSElem );

//exclude attributes WFS
QDomElement excludeWFSElem = doc.createElement( QStringLiteral( "excludeAttributesWFS" ) );
QSet<QString>::const_iterator attWFSIt = mExcludeAttributesWFS.constBegin();
for ( ; attWFSIt != mExcludeAttributesWFS.constEnd(); ++attWFSIt )
{
QDomElement attrElem = doc.createElement( QStringLiteral( "attribute" ) );
QDomText attrText = doc.createTextNode( *attWFSIt );
attrElem.appendChild( attrText );
excludeWFSElem.appendChild( attrElem );
}
node.appendChild( excludeWFSElem );

//default expressions
QDomElement defaultsElem = doc.createElement( QStringLiteral( "defaults" ) );
for ( const QgsField &field : qgis::as_const( mFields ) )
Expand Down Expand Up @@ -5493,6 +5466,15 @@ void QgsVectorLayer::setFieldConfigurationFlags( int index, QgsField::Configurat
updateFields();
}

void QgsVectorLayer::setFieldConfigurationFlag( int index, QgsField::ConfigurationFlag flag, bool active )
{
if ( index < 0 || index >= mFields.count() )
return;
QgsField::ConfigurationFlags flags = mFields.at( index ).configurationFlags();
flags.setFlag( flag, active );
setFieldConfigurationFlags( index, flags );
}

QgsField::ConfigurationFlags QgsVectorLayer::fieldConfigurationFlags( int index ) const
{

Expand Down
14 changes: 10 additions & 4 deletions src/core/qgsvectorlayer.h
Expand Up @@ -1840,22 +1840,22 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
/**
* A set of attributes that are not advertised in WMS requests with QGIS server.
*/
QSet<QString> excludeAttributesWms() const { return mExcludeAttributesWMS; }
Q_DECL_DEPRECATED QSet<QString> excludeAttributesWms() SIP_DEPRECATED const { return mExcludeAttributesWMS; }

/**
* A set of attributes that are not advertised in WMS requests with QGIS server.
*/
void setExcludeAttributesWms( const QSet<QString> &att ) { mExcludeAttributesWMS = att; }
Q_DECL_DEPRECATED void setExcludeAttributesWms( const QSet<QString> &att ) SIP_DEPRECATED { mExcludeAttributesWMS = att; }

/**
* A set of attributes that are not advertised in WFS requests with QGIS server.
*/
QSet<QString> excludeAttributesWfs() const { return mExcludeAttributesWFS; }
Q_DECL_DEPRECATED QSet<QString> excludeAttributesWfs() SIP_DEPRECATED const { return mExcludeAttributesWFS; }

/**
* A set of attributes that are not advertised in WFS requests with QGIS server.
*/
void setExcludeAttributesWfs( const QSet<QString> &att ) { mExcludeAttributesWFS = att; }
Q_DECL_DEPRECATED void setExcludeAttributesWfs( const QSet<QString> &att ) SIP_DEPRECATED { mExcludeAttributesWFS = att; }

/**
* Deletes an attribute field (but does not commit it).
Expand Down Expand Up @@ -2122,6 +2122,12 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
*/
void setFieldConfigurationFlags( int index, QgsField::ConfigurationFlags flags ) SIP_SKIP;

/**
* Sets the given configuration \a flag for the field at given \a index to be \a active or not.
* \since QGIS 3.16
*/
void setFieldConfigurationFlag( int index, QgsField::ConfigurationFlag flag, bool active ) SIP_SKIP;

/**
* Returns the configuration flags of the field at given index
* \see QgsField::ConfigurationFlag
Expand Down

0 comments on commit 4fd940a

Please sign in to comment.