also consider non boolean fields

This commit is contained in:
Denis Rouzaud 2019-09-24 07:53:11 +02:00
parent be4bd42a2c
commit 0caa93c401
3 changed files with 36 additions and 11 deletions

View File

@ -29,7 +29,11 @@ This is an abstract base class and will always need to be subclassed.
#include "qgsfieldformatter.h"
%End
public:
QgsFieldFormatter();
%Docstring
Default constructor
%End
virtual ~QgsFieldFormatter();

View File

@ -18,6 +18,7 @@
#include "qgscheckboxfieldformatter.h"
#include "qgsapplication.h"
#include "qgsvectorlayer.h"
QString QgsCheckBoxFieldFormatter::id() const
@ -27,21 +28,37 @@ QString QgsCheckBoxFieldFormatter::id() const
QString QgsCheckBoxFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
{
Q_UNUSED( layer )
Q_UNUSED( fieldIndex )
Q_UNUSED( cache )
Q_UNUSED( config )
if ( value.isNull() || !value.canConvert<bool>() )
enum BoolValue {_FALSE, _TRUE, _NULL};
BoolValue boolValue = _NULL;
const QVariant::Type fieldType = layer->fields().at( fieldIndex ).type();
if ( fieldType == QVariant::Bool )
{
return QgsApplication::nullRepresentation();
if ( value.toBool() )
boolValue = _TRUE;
else
boolValue = _FALSE;
}
else
{
if ( config.contains( QStringLiteral( "CheckedState" ) ) && value == config[ QStringLiteral( "CheckedState" ) ] )
boolValue = _TRUE;
else if ( config.contains( QStringLiteral( "UncheckedState" ) ) && value == config[ QStringLiteral( "UncheckedState" ) ] )
boolValue = _FALSE;
else
boolValue = _NULL;
}
const bool boolValue = value.toBool();
if ( boolValue )
return QObject::tr( "true" );
else
return QObject::tr( "false" );
switch ( boolValue )
{
case _NULL:
return QgsApplication::nullRepresentation();
case _TRUE:
return QObject::tr( "true" );
case _FALSE:
return QObject::tr( "false" );
}
}

View File

@ -40,6 +40,10 @@ class QgsVectorLayer;
class CORE_EXPORT QgsFieldFormatter
{
public:
/**
* Default constructor
*/
QgsFieldFormatter() = default;
virtual ~QgsFieldFormatter() = default;