-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6246 from elpaso/rangeformatter
[bugfix] Range formatter for doubles and ints
- Loading branch information
Showing
14 changed files
with
274 additions
and
2 deletions.
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/fieldformatter/qgsrangefieldformatter.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
class QgsRangeFieldFormatter : QgsFieldFormatter | ||
{ | ||
%Docstring | ||
Field formatter for a range (double) field with precision and locale | ||
|
||
.. versionadded:: 3.0 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgsrangefieldformatter.h" | ||
%End | ||
public: | ||
|
||
QgsRangeFieldFormatter(); | ||
%Docstring | ||
Default constructor of field formatter for a range (double)field. | ||
%End | ||
|
||
virtual QString id() const; | ||
|
||
|
||
virtual QString representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const; | ||
|
||
|
||
}; | ||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/fieldformatter/qgsrangefieldformatter.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ |
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
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/*************************************************************************** | ||
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; | ||
|
||
// Prepare locale | ||
std::function<QLocale()> f_locale = [ ] | ||
{ | ||
QLocale locale( QgsApplication::instance()->locale() ); | ||
QLocale::NumberOptions options( locale.numberOptions() ); | ||
options |= QLocale::NumberOption::OmitGroupSeparator; | ||
locale.setNumberOptions( options ); | ||
return locale; | ||
}; | ||
|
||
const QgsField field = layer->fields().at( fieldIndex ); | ||
|
||
if ( field.type() == QVariant::Double && | ||
config.contains( QStringLiteral( "Precision" ) ) && | ||
value.isValid( ) ) | ||
{ | ||
bool ok; | ||
double val( value.toDouble( &ok ) ); | ||
if ( ok ) | ||
{ | ||
int precision( config[ QStringLiteral( "Precision" ) ].toInt( &ok ) ); | ||
if ( ok ) | ||
{ | ||
// TODO: make the format configurable! | ||
result = f_locale().toString( val, 'f', precision ); | ||
} | ||
} | ||
} | ||
else if ( field.type() == QVariant::Int && | ||
value.isValid( ) ) | ||
{ | ||
bool ok; | ||
double val( value.toInt( &ok ) ); | ||
if ( ok ) | ||
{ | ||
result = f_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 | 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
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
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