Skip to content
Permalink
Browse files

[bugfix] Range formatter for doubles and ints

Double and ints are now formatted according
to chosen locale (in user settings) and precision
(from the widget field config)

Numbers are also right aligned in the form
and not only in the table.

IMHO a bugfix since the numbers looked very
different in the form and the table
when the system locale used decimal
separator and group separator which were not
the same than system locale.
  • Loading branch information
elpaso committed Feb 1, 2018
1 parent d653fb5 commit 8ae65b3c70291a19c440a76172ac02bdfcb27c86
@@ -473,6 +473,7 @@ SET(QGIS_CORE_SRCS
3d/qgs3drendererregistry.cpp
3d/qgsabstract3drenderer.cpp

fieldformatter/qgsrangefieldformatter.cpp
fieldformatter/qgsdatetimefieldformatter.cpp
fieldformatter/qgsfallbackfieldformatter.cpp
fieldformatter/qgskeyvaluefieldformatter.cpp
@@ -1110,6 +1111,7 @@ SET(QGIS_CORE_HDRS
3d/qgs3drendererregistry.h
3d/qgsabstract3drenderer.h

fieldformatter/qgsrangefieldformatter.h
fieldformatter/qgsdatetimefieldformatter.h
fieldformatter/qgsfallbackfieldformatter.h
fieldformatter/qgskeyvaluefieldformatter.h
@@ -0,0 +1,84 @@
/***************************************************************************
qgsrangefieldformatter.cpp - QgsRangeFieldFormatter
---------------------
begin : 01/02/2018
copyright : (C) 2018 by Alessandro Pasotti
email : elpaso at itopen dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include <QLocale>

#include "qgsrangefieldformatter.h"

#include "qgssettings.h"
#include "qgsfield.h"
#include "qgsvectorlayer.h"


QString QgsRangeFieldFormatter::id() const
{
return QStringLiteral( "Range" );
}

QString QgsRangeFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
{
Q_UNUSED( cache )
Q_UNUSED( config )

if ( value.isNull() )
{
return QgsApplication::nullRepresentation();
}

QString result;

const QgsField field = layer->fields().at( fieldIndex );

if ( field.type() == QVariant::Double &&
field.editorWidgetSetup().config().contains( QStringLiteral( "Precision" ) ) &&
value.isValid( ) )
{
bool ok;
double val( value.toDouble( &ok ) );
if ( ok )
{
int precision( field.editorWidgetSetup().config()[ QStringLiteral( "Precision" ) ].toInt( &ok ) );
if ( ok )
{
// TODO: make the format configurable!
QLocale locale( QgsApplication::instance()->locale() );
QLocale::NumberOptions options( locale.numberOptions() );
options |= QLocale::NumberOption::OmitGroupSeparator;
locale.setNumberOptions( options );
result = locale.toString( val, 'f', precision );
}
}
}
else if ( field.type() == QVariant::Int &&
value.isValid( ) )
{
bool ok;
double val( value.toInt( &ok ) );
if ( ok )
{
QLocale locale( QgsApplication::instance()->locale() );
QLocale::NumberOptions options( locale.numberOptions() );
options |= QLocale::NumberOption::OmitGroupSeparator;
locale.setNumberOptions( options );
result = locale.toString( val );
}
}
else
{
result = value.toString();
}
return result;
}
@@ -0,0 +1,43 @@
/***************************************************************************
qgsrangefieldformatter.h - QgsRangeFieldFormatter
---------------------
begin : 01/02/2018
copyright : (C) 2018 by Alessandro Pasotti
email : elpaso at itopen dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSRANGEFIELDFORMATTER_H
#define QGSRANGEFIELDFORMATTER_H

#include "qgis_core.h"
#include "qgsfieldformatter.h"

/**
* \ingroup core
* Field formatter for a range (double) field with precision and locale
*
* \since QGIS 3.0
*/
class CORE_EXPORT QgsRangeFieldFormatter : public QgsFieldFormatter
{
public:

/**
* Default constructor of field formatter for a range (double)field.
*/
QgsRangeFieldFormatter() = default;

QString id() const override;

QString representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const override;

};

#endif // QGSRANGEFIELDFORMATTER_H
@@ -22,6 +22,7 @@
#include "qgsrelationreferencefieldformatter.h"
#include "qgskeyvaluefieldformatter.h"
#include "qgslistfieldformatter.h"
#include "qgsrangefieldformatter.h"
#include "qgsfallbackfieldformatter.h"


@@ -34,6 +35,7 @@ QgsFieldFormatterRegistry::QgsFieldFormatterRegistry( QObject *parent )
addFieldFormatter( new QgsKeyValueFieldFormatter() );
addFieldFormatter( new QgsListFieldFormatter() );
addFieldFormatter( new QgsDateTimeFieldFormatter() );
addFieldFormatter( new QgsRangeFieldFormatter() );

mFallbackFieldFormatter = new QgsFallbackFieldFormatter();
}
@@ -31,7 +31,10 @@ QgsDoubleSpinBox::QgsDoubleSpinBox( QWidget *parent )
{
mLineEdit = new QgsSpinBoxLineEdit();

// By default, group separator is off
setLocale( QLocale( QgsApplication::locale( ) ) );
setLineEdit( mLineEdit );
mLineEdit->setAlignment( Qt::AlignRight );

QSize msz = minimumSizeHint();
setMinimumSize( msz.width() + CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
@@ -30,7 +30,7 @@ QgsSpinBox::QgsSpinBox( QWidget *parent )
: QSpinBox( parent )
{
mLineEdit = new QgsSpinBoxLineEdit();

mLineEdit->setAlignment( Qt::AlignRight );
setLineEdit( mLineEdit );

QSize msz = minimumSizeHint();

0 comments on commit 8ae65b3

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