Skip to content
Permalink
Browse files

[Server][WFS] Use editor widget setup for DateTime fields

The user can define with the editor widget the data type of the field with more accuracy than the data provider.
In the case of DateTime, this data can be stored in a text field or the field is DateTime by default because the data provider does not identify Date or Time.

QGIS Server in the WFS case can use this config to provide well formed data and description.
  • Loading branch information
rldhont committed Sep 26, 2018
1 parent cfddac5 commit 37c1e2373fd0181003c1cd3d1df173a38f1489d9
@@ -45,6 +45,7 @@ INCLUDE_DIRECTORIES(
../../../core/raster
../../../core/symbology
../../../core/layertree
../../../core/fieldformatter
../..
..
.
@@ -30,6 +30,9 @@
#include "qgsvectordataprovider.h"
#include "qgsmapserviceexception.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsfieldformatterregistry.h"
#include "qgsfieldformatter.h"
#include "qgsdatetimefieldformatter.h"

#include <QStringList>

@@ -307,6 +310,20 @@ namespace QgsWfs
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "string" ) );
}

const QgsEditorWidgetSetup setup = field.editorWidgetSetup();
if ( setup.type() == QStringLiteral( "DateTime" ) )
{
QgsDateTimeFieldFormatter fieldFormatter;
const QVariantMap config = setup.config();
const QString fieldFormat = config.value( QStringLiteral( "field_format" ), fieldFormatter.defaultFormat( field.type() ) ).toString();
if ( fieldFormat == QStringLiteral( "yyyy-MM-dd" ) )
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "date" ) );
else if ( fieldFormat == QStringLiteral( "HH:mm:ss" ) )
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "time" ) );
else
attElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "dateTime" ) );
}

sequenceElem.appendChild( attElem );

QString alias = field.alias();
@@ -22,6 +22,9 @@
#include "qgswfsutils.h"
#include "qgsserverprojectutils.h"
#include "qgsfields.h"
#include "qgsfieldformatterregistry.h"
#include "qgsfieldformatter.h"
#include "qgsdatetimefieldformatter.h"
#include "qgsexpression.h"
#include "qgsgeometry.h"
#include "qgsmaplayer.h"
@@ -62,7 +65,7 @@ namespace QgsWfs

QString createFeatureGeoJSON( QgsFeature *feat, const createFeatureParams &params );

QString encodeValueToText( const QVariant &value );
QString encodeValueToText( const QVariant &value, const QgsEditorWidgetSetup &setup );

QDomElement createFeatureGML2( QgsFeature *feat, QDomDocument &doc, const createFeatureParams &params, const QgsProject *project );

@@ -1337,10 +1340,12 @@ namespace QgsWfs
{
continue;
}
QString attributeName = fields.at( idx ).name();
const QgsField field = fields.at( idx );
const QgsEditorWidgetSetup setup = field.editorWidgetSetup();
QString attributeName = field.name();

QDomElement fieldElem = doc.createElement( "qgs:" + attributeName.replace( ' ', '_' ).replace( cleanTagNameRegExp, QString() ) );
QDomText fieldText = doc.createTextNode( encodeValueToText( featureAttributes[idx] ) );
QDomText fieldText = doc.createTextNode( encodeValueToText( featureAttributes[idx], setup ) );
fieldElem.appendChild( fieldText );
typeNameElement.appendChild( fieldElem );
}
@@ -1432,22 +1437,37 @@ namespace QgsWfs
{
continue;
}
QString attributeName = fields.at( idx ).name();
const QgsField field = fields.at( idx );
const QgsEditorWidgetSetup setup = field.editorWidgetSetup();
QString attributeName = field.name();

QDomElement fieldElem = doc.createElement( "qgs:" + attributeName.replace( ' ', '_' ).replace( cleanTagNameRegExp, QString() ) );
QDomText fieldText = doc.createTextNode( encodeValueToText( featureAttributes[idx] ) );
QDomText fieldText = doc.createTextNode( encodeValueToText( featureAttributes[idx], setup ) );
fieldElem.appendChild( fieldText );
typeNameElement.appendChild( fieldElem );
}

return featureElement;
}

QString encodeValueToText( const QVariant &value )
QString encodeValueToText( const QVariant &value, const QgsEditorWidgetSetup &setup )
{
if ( value.isNull() )
return QStringLiteral( "null" );

if ( setup.type() == QStringLiteral( "DateTime" ) )
{
QgsDateTimeFieldFormatter fieldFormatter;
const QVariantMap config = setup.config();
const QString fieldFormat = config.value( QStringLiteral( "field_format" ), fieldFormatter.defaultFormat( value.type() ) ).toString();
QDateTime date = value.toDateTime();

if ( date.isValid() )
{
return date.toString( fieldFormat );
}
}

switch ( value.type() )
{
case QVariant::Int:

0 comments on commit 37c1e23

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