|
| 1 | +/*************************************************************************** |
| 2 | + qgsrangefieldformatter.cpp - QgsRangeFieldFormatter |
| 3 | +
|
| 4 | + --------------------- |
| 5 | + begin : 01/02/2018 |
| 6 | + copyright : (C) 2018 by Alessandro Pasotti |
| 7 | + email : elpaso at itopen dot it |
| 8 | + *************************************************************************** |
| 9 | + * * |
| 10 | + * This program is free software; you can redistribute it and/or modify * |
| 11 | + * it under the terms of the GNU General Public License as published by * |
| 12 | + * the Free Software Foundation; either version 2 of the License, or * |
| 13 | + * (at your option) any later version. * |
| 14 | + * * |
| 15 | + ***************************************************************************/ |
| 16 | + |
| 17 | +#include <QLocale> |
| 18 | + |
| 19 | +#include "qgsrangefieldformatter.h" |
| 20 | + |
| 21 | +#include "qgssettings.h" |
| 22 | +#include "qgsfield.h" |
| 23 | +#include "qgsvectorlayer.h" |
| 24 | + |
| 25 | + |
| 26 | +QString QgsRangeFieldFormatter::id() const |
| 27 | +{ |
| 28 | + return QStringLiteral( "Range" ); |
| 29 | +} |
| 30 | + |
| 31 | +QString QgsRangeFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const |
| 32 | +{ |
| 33 | + Q_UNUSED( cache ) |
| 34 | + Q_UNUSED( config ) |
| 35 | + |
| 36 | + if ( value.isNull() ) |
| 37 | + { |
| 38 | + return QgsApplication::nullRepresentation(); |
| 39 | + } |
| 40 | + |
| 41 | + QString result; |
| 42 | + |
| 43 | + // Prepare locale |
| 44 | + std::function<QLocale()> f_locale = [ ] |
| 45 | + { |
| 46 | + QLocale locale( QgsApplication::instance()->locale() ); |
| 47 | + QLocale::NumberOptions options( locale.numberOptions() ); |
| 48 | + options |= QLocale::NumberOption::OmitGroupSeparator; |
| 49 | + locale.setNumberOptions( options ); |
| 50 | + return locale; |
| 51 | + }; |
| 52 | + |
| 53 | + const QgsField field = layer->fields().at( fieldIndex ); |
| 54 | + |
| 55 | + if ( field.type() == QVariant::Double && |
| 56 | + config.contains( QStringLiteral( "Precision" ) ) && |
| 57 | + value.isValid( ) ) |
| 58 | + { |
| 59 | + bool ok; |
| 60 | + double val( value.toDouble( &ok ) ); |
| 61 | + if ( ok ) |
| 62 | + { |
| 63 | + int precision( config[ QStringLiteral( "Precision" ) ].toInt( &ok ) ); |
| 64 | + if ( ok ) |
| 65 | + { |
| 66 | + // TODO: make the format configurable! |
| 67 | + result = f_locale().toString( val, 'f', precision ); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + else if ( field.type() == QVariant::Int && |
| 72 | + value.isValid( ) ) |
| 73 | + { |
| 74 | + bool ok; |
| 75 | + double val( value.toInt( &ok ) ); |
| 76 | + if ( ok ) |
| 77 | + { |
| 78 | + result = f_locale().toString( val ); |
| 79 | + } |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + result = value.toString(); |
| 84 | + } |
| 85 | + return result; |
| 86 | +} |
0 commit comments