From ceceebdf9ba5c118de63c218bec6a35ef178c515 Mon Sep 17 00:00:00 2001 From: "Juergen E. Fischer" Date: Mon, 16 Feb 2015 21:13:40 +0100 Subject: [PATCH] server getfeatureinfo: delegate value representation to editor widgets (fixes #12210) --- src/server/qgswmsserver.cpp | 95 ++++++------------------------------- src/server/qgswmsserver.h | 1 - 2 files changed, 14 insertions(+), 82 deletions(-) diff --git a/src/server/qgswmsserver.cpp b/src/server/qgswmsserver.cpp index 9dc60ae5e32..7772255eb90 100644 --- a/src/server/qgswmsserver.cpp +++ b/src/server/qgswmsserver.cpp @@ -45,6 +45,7 @@ #include "qgspaintenginehack.h" #include "qgsogcutils.h" #include "qgsfeature.h" +#include "qgseditorwidgetregistry.h" #include #include @@ -2094,16 +2095,12 @@ int QgsWMSServer::featureInfoFromVectorLayer( QgsVectorLayer* layer, QDomElement attributeElement = infoDocument.createElement( "Attribute" ); attributeElement.setAttribute( "name", attributeName ); - - QString value; - if ( featureAttributes[i].isNull() ) - value = QSettings().value( "qgis/nullValue", "NULL" ).toString(); - else - { - value = replaceValueMapAndRelation( layer, i, QgsExpression::replaceExpressionText( featureAttributes[i].toString(), &feature, layer ) ); - } - - attributeElement.setAttribute( "value", value ); + attributeElement.setAttribute( "value", + replaceValueMapAndRelation( + layer, i, + QgsExpression::replaceExpressionText( featureAttributes[i].toString(), &feature, layer ) + ) + ); featureElement.appendChild( attributeElement ); } @@ -3051,82 +3048,18 @@ QDomElement QgsWMSServer::createFeatureGML( QString QgsWMSServer::replaceValueMapAndRelation( QgsVectorLayer* vl, int idx, const QString& attributeVal ) { - if ( !vl ) - { - return attributeVal; - } - - QString type = vl->editorWidgetV2( idx ); - if ( type == "ValueMap" ) + if ( QgsEditorWidgetFactory *factory = QgsEditorWidgetRegistry::instance()->factory( vl->editorWidgetV2( idx ) ) ) { QgsEditorWidgetConfig cfg( vl->editorWidgetV2Config( idx ) ); - QMap::const_iterator vmapIt = cfg.constBegin(); - for ( ; vmapIt != cfg.constEnd(); ++vmapIt ) + QString value( factory->representValue( vl, idx, cfg, QVariant(), attributeVal ) ); + if ( cfg.value( "AllowMulti" ).toBool() && value.startsWith( "{" ) && value.endsWith( "}" ) ) { - if ( vmapIt.key() == attributeVal ) - { - return vmapIt.value().toString(); - } + value = value.mid( 1, value.size() - 2 ); } + return value; + } + else return QString( "(%1)" ).arg( attributeVal ); - } - else if ( type == "ValueRelation" ) - { - QgsEditorWidgetConfig cfg( vl->editorWidgetV2Config( idx ) ); - QgsVectorLayer* layer = qobject_cast( QgsMapLayerRegistry::instance()->mapLayer( cfg.value( "Layer" ).toString() ) ); - if ( !layer ) - { - return QString( "(%1)" ).arg( attributeVal ); - } - - QString outputString; - QString valueString = attributeVal; - QStringList valueList = cfg.value( "AllowMulti" ).toBool() - ? valueString.remove( QChar( '{' ) ).remove( QChar( '}' ) ).split( "," ) - : QStringList( valueString ); - for ( int i = 0; i < valueList.size(); ++i ) - { - if ( i > 0 ) - { - outputString += ";"; - } - outputString += relationValue( - valueList.at( i ), - layer, - cfg.value( "Key" ).toString(), - cfg.value( "Value" ).toString() - ); - } - return outputString; - } - - return attributeVal; -} - -QString QgsWMSServer::relationValue( const QString& attributeVal, QgsVectorLayer* layer, const QString& key, const QString& value ) -{ - if ( !layer ) - { - return attributeVal; - } - - int keyId = layer->fieldNameIndex( key ); - int valueId = layer->fieldNameIndex( value ); - if ( keyId == -1 || valueId == -1 ) - { - return attributeVal; - } - - QgsFeatureIterator fIt = layer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setSubsetOfAttributes( QgsAttributeList() << keyId << valueId ) ); - QgsFeature f; - while ( fIt.nextFeature( f ) ) - { - if ( f.attribute( key ).toString() == attributeVal ) - { - return f.attribute( value ).toString(); - } - } - return attributeVal; } int QgsWMSServer::getImageQuality() const diff --git a/src/server/qgswmsserver.h b/src/server/qgswmsserver.h index 988c7cec464..0f0912e3de3 100644 --- a/src/server/qgswmsserver.h +++ b/src/server/qgswmsserver.h @@ -251,7 +251,6 @@ class QgsWMSServer: public QgsOWSServer /**Replaces attribute value with ValueRelation or ValueRelation if defined. Otherwise returns the original value*/ static QString replaceValueMapAndRelation( QgsVectorLayer* vl, int idx, const QString& attributeVal ); - static QString relationValue( const QString& attributeVal, QgsVectorLayer* layer, const QString& key, const QString& value ); /** Return the image quality to use for getMap request */ int getImageQuality() const;