Skip to content

Commit

Permalink
server getfeatureinfo: delegate value representation to editor widgets (
Browse files Browse the repository at this point in the history
fixes #12210)
  • Loading branch information
jef-n committed Feb 16, 2015
1 parent 6e77350 commit ceceebd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 82 deletions.
95 changes: 14 additions & 81 deletions src/server/qgswmsserver.cpp
Expand Up @@ -45,6 +45,7 @@
#include "qgspaintenginehack.h"
#include "qgsogcutils.h"
#include "qgsfeature.h"
#include "qgseditorwidgetregistry.h"

#include <QImage>
#include <QPainter>
Expand Down Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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<QString, QVariant>::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 QString( "(%1)" ).arg( attributeVal );
return value;
}
else if ( type == "ValueRelation" )
{
QgsEditorWidgetConfig cfg( vl->editorWidgetV2Config( idx ) );
QgsVectorLayer* layer = qobject_cast<QgsVectorLayer*>( 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;
else
return QString( "(%1)" ).arg( attributeVal );
}

int QgsWMSServer::getImageQuality() const
Expand Down
1 change: 0 additions & 1 deletion src/server/qgswmsserver.h
Expand Up @@ -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;
Expand Down

0 comments on commit ceceebd

Please sign in to comment.