Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
Showing
6 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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; | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters