Skip to content

Commit ceceebd

Browse files
committedFeb 16, 2015
server getfeatureinfo: delegate value representation to editor widgets (fixes #12210)
1 parent 6e77350 commit ceceebd

File tree

2 files changed

+14
-82
lines changed

2 files changed

+14
-82
lines changed
 

‎src/server/qgswmsserver.cpp

Lines changed: 14 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "qgspaintenginehack.h"
4646
#include "qgsogcutils.h"
4747
#include "qgsfeature.h"
48+
#include "qgseditorwidgetregistry.h"
4849

4950
#include <QImage>
5051
#include <QPainter>
@@ -2094,16 +2095,12 @@ int QgsWMSServer::featureInfoFromVectorLayer( QgsVectorLayer* layer,
20942095

20952096
QDomElement attributeElement = infoDocument.createElement( "Attribute" );
20962097
attributeElement.setAttribute( "name", attributeName );
2097-
2098-
QString value;
2099-
if ( featureAttributes[i].isNull() )
2100-
value = QSettings().value( "qgis/nullValue", "NULL" ).toString();
2101-
else
2102-
{
2103-
value = replaceValueMapAndRelation( layer, i, QgsExpression::replaceExpressionText( featureAttributes[i].toString(), &feature, layer ) );
2104-
}
2105-
2106-
attributeElement.setAttribute( "value", value );
2098+
attributeElement.setAttribute( "value",
2099+
replaceValueMapAndRelation(
2100+
layer, i,
2101+
QgsExpression::replaceExpressionText( featureAttributes[i].toString(), &feature, layer )
2102+
)
2103+
);
21072104
featureElement.appendChild( attributeElement );
21082105
}
21092106

@@ -3051,82 +3048,18 @@ QDomElement QgsWMSServer::createFeatureGML(
30513048

30523049
QString QgsWMSServer::replaceValueMapAndRelation( QgsVectorLayer* vl, int idx, const QString& attributeVal )
30533050
{
3054-
if ( !vl )
3055-
{
3056-
return attributeVal;
3057-
}
3058-
3059-
QString type = vl->editorWidgetV2( idx );
3060-
if ( type == "ValueMap" )
3051+
if ( QgsEditorWidgetFactory *factory = QgsEditorWidgetRegistry::instance()->factory( vl->editorWidgetV2( idx ) ) )
30613052
{
30623053
QgsEditorWidgetConfig cfg( vl->editorWidgetV2Config( idx ) );
3063-
QMap<QString, QVariant>::const_iterator vmapIt = cfg.constBegin();
3064-
for ( ; vmapIt != cfg.constEnd(); ++vmapIt )
3054+
QString value( factory->representValue( vl, idx, cfg, QVariant(), attributeVal ) );
3055+
if ( cfg.value( "AllowMulti" ).toBool() && value.startsWith( "{" ) && value.endsWith( "}" ) )
30653056
{
3066-
if ( vmapIt.key() == attributeVal )
3067-
{
3068-
return vmapIt.value().toString();
3069-
}
3057+
value = value.mid( 1, value.size() - 2 );
30703058
}
3071-
return QString( "(%1)" ).arg( attributeVal );
3059+
return value;
30723060
}
3073-
else if ( type == "ValueRelation" )
3074-
{
3075-
QgsEditorWidgetConfig cfg( vl->editorWidgetV2Config( idx ) );
3076-
QgsVectorLayer* layer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( cfg.value( "Layer" ).toString() ) );
3077-
if ( !layer )
3078-
{
3079-
return QString( "(%1)" ).arg( attributeVal );
3080-
}
3081-
3082-
QString outputString;
3083-
QString valueString = attributeVal;
3084-
QStringList valueList = cfg.value( "AllowMulti" ).toBool()
3085-
? valueString.remove( QChar( '{' ) ).remove( QChar( '}' ) ).split( "," )
3086-
: QStringList( valueString );
3087-
for ( int i = 0; i < valueList.size(); ++i )
3088-
{
3089-
if ( i > 0 )
3090-
{
3091-
outputString += ";";
3092-
}
3093-
outputString += relationValue(
3094-
valueList.at( i ),
3095-
layer,
3096-
cfg.value( "Key" ).toString(),
3097-
cfg.value( "Value" ).toString()
3098-
);
3099-
}
3100-
return outputString;
3101-
}
3102-
3103-
return attributeVal;
3104-
}
3105-
3106-
QString QgsWMSServer::relationValue( const QString& attributeVal, QgsVectorLayer* layer, const QString& key, const QString& value )
3107-
{
3108-
if ( !layer )
3109-
{
3110-
return attributeVal;
3111-
}
3112-
3113-
int keyId = layer->fieldNameIndex( key );
3114-
int valueId = layer->fieldNameIndex( value );
3115-
if ( keyId == -1 || valueId == -1 )
3116-
{
3117-
return attributeVal;
3118-
}
3119-
3120-
QgsFeatureIterator fIt = layer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setSubsetOfAttributes( QgsAttributeList() << keyId << valueId ) );
3121-
QgsFeature f;
3122-
while ( fIt.nextFeature( f ) )
3123-
{
3124-
if ( f.attribute( key ).toString() == attributeVal )
3125-
{
3126-
return f.attribute( value ).toString();
3127-
}
3128-
}
3129-
return attributeVal;
3061+
else
3062+
return QString( "(%1)" ).arg( attributeVal );
31303063
}
31313064

31323065
int QgsWMSServer::getImageQuality() const

‎src/server/qgswmsserver.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ class QgsWMSServer: public QgsOWSServer
251251

252252
/**Replaces attribute value with ValueRelation or ValueRelation if defined. Otherwise returns the original value*/
253253
static QString replaceValueMapAndRelation( QgsVectorLayer* vl, int idx, const QString& attributeVal );
254-
static QString relationValue( const QString& attributeVal, QgsVectorLayer* layer, const QString& key, const QString& value );
255254

256255
/** Return the image quality to use for getMap request */
257256
int getImageQuality() const;

0 commit comments

Comments
 (0)
Please sign in to comment.