Skip to content

Commit

Permalink
[bugfix] Range formatter for doubles and ints
Browse files Browse the repository at this point in the history
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 8ae65b3
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -473,6 +473,7 @@ SET(QGIS_CORE_SRCS
3d/qgs3drendererregistry.cpp 3d/qgs3drendererregistry.cpp
3d/qgsabstract3drenderer.cpp 3d/qgsabstract3drenderer.cpp


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


fieldformatter/qgsrangefieldformatter.h
fieldformatter/qgsdatetimefieldformatter.h fieldformatter/qgsdatetimefieldformatter.h
fieldformatter/qgsfallbackfieldformatter.h fieldformatter/qgsfallbackfieldformatter.h
fieldformatter/qgskeyvaluefieldformatter.h fieldformatter/qgskeyvaluefieldformatter.h
Expand Down
84 changes: 84 additions & 0 deletions src/core/fieldformatter/qgsrangefieldformatter.cpp
@@ -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;
}
43 changes: 43 additions & 0 deletions src/core/fieldformatter/qgsrangefieldformatter.h
@@ -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
2 changes: 2 additions & 0 deletions src/core/qgsfieldformatterregistry.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgsrelationreferencefieldformatter.h" #include "qgsrelationreferencefieldformatter.h"
#include "qgskeyvaluefieldformatter.h" #include "qgskeyvaluefieldformatter.h"
#include "qgslistfieldformatter.h" #include "qgslistfieldformatter.h"
#include "qgsrangefieldformatter.h"
#include "qgsfallbackfieldformatter.h" #include "qgsfallbackfieldformatter.h"




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


mFallbackFieldFormatter = new QgsFallbackFieldFormatter(); mFallbackFieldFormatter = new QgsFallbackFieldFormatter();
} }
Expand Down
3 changes: 3 additions & 0 deletions src/gui/editorwidgets/qgsdoublespinbox.cpp
Expand Up @@ -31,7 +31,10 @@ QgsDoubleSpinBox::QgsDoubleSpinBox( QWidget *parent )
{ {
mLineEdit = new QgsSpinBoxLineEdit(); mLineEdit = new QgsSpinBoxLineEdit();


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


QSize msz = minimumSizeHint(); QSize msz = minimumSizeHint();
setMinimumSize( msz.width() + CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2, setMinimumSize( msz.width() + CLEAR_ICON_SIZE + 9 + frameWidth() * 2 + 2,
Expand Down
2 changes: 1 addition & 1 deletion src/gui/editorwidgets/qgsspinbox.cpp
Expand Up @@ -30,7 +30,7 @@ QgsSpinBox::QgsSpinBox( QWidget *parent )
: QSpinBox( parent ) : QSpinBox( parent )
{ {
mLineEdit = new QgsSpinBoxLineEdit(); mLineEdit = new QgsSpinBoxLineEdit();

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


QSize msz = minimumSizeHint(); QSize msz = minimumSizeHint();
Expand Down

0 comments on commit 8ae65b3

Please sign in to comment.