QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsbasicnumericformat.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsbasicnumericformat.cpp
3 ----------------------------
4 begin : January 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7
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
18
19#include <iomanip>
20#include <iostream>
21#include <locale>
22#include <memory>
23
24#include "qgis.h"
25
27{
28 struct formatter : std::numpunct<wchar_t>
29 {
30 formatter( QChar thousands, bool showThousands, QChar decimal )
31 : mThousands( thousands.unicode() )
32 , mDecimal( decimal.unicode() )
33 , mShowThousands( showThousands )
34 {}
35 wchar_t do_decimal_point() const override { return mDecimal; }
36 wchar_t do_thousands_sep() const override { return mThousands; }
37 std::string do_grouping() const override { return mShowThousands ? "\3" : "\0"; }
38
39 wchar_t mThousands;
40 wchar_t mDecimal;
41 bool mShowThousands = true;
42 };
43}
44
48
50{
51 return QStringLiteral( "basic" );
52}
53
55{
56 return QObject::tr( "Number" );
57}
58
60{
61 return 1;
62}
63
64QString QgsBasicNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
65{
66 const QChar decimal = mDecimalSeparator.isNull() ? context.decimalSeparator() : mDecimalSeparator;
67 std::basic_stringstream<wchar_t> os;
68 os.imbue( std::locale( os.getloc(), new QgsBasicNumericFormat_ns::formatter( mThousandsSeparator.isNull() ? context.thousandsSeparator() : mThousandsSeparator,
69 mShowThousandsSeparator,
70 decimal ) ) );
71
72 if ( !mUseScientific )
73 {
74 switch ( mRoundingType )
75 {
76 case DecimalPlaces:
77 os << std::fixed << std::setprecision( mNumberDecimalPlaces );
78 if ( qgsDoubleNear( value, 0 ) )
79 os << 0.0;
80 else
81 os << value;
82
83 break;
84
86 {
87 if ( qgsDoubleNear( value, 0 ) )
88 {
89 os << std::fixed << std::setprecision( mNumberDecimalPlaces - 1 ) << 0.0;
90 }
91 else
92 {
93 // digits before decimal point
94 const int d = std::floor( std::log10( value < 0 ? -value : value ) ) + 1;
95 const double order = std::pow( 10.0, mNumberDecimalPlaces - d );
96 os << std::fixed << std::setprecision( std::max( mNumberDecimalPlaces - d, 0 ) ) << std::round( value * order ) / order;
97 }
98 break;
99 }
100 }
101 }
102 else
103 {
104 os << std::scientific << std::setprecision( mNumberDecimalPlaces );
105 if ( qgsDoubleNear( value, 0 ) )
106 os << 0.0;
107 else
108 os << value;
109 }
110
111 QString res = QString::fromStdWString( os.str() );
112
113 if ( mShowPlusSign && value > 0 )
114 res.prepend( context.positiveSign() );
115
116 if ( !mShowTrailingZeros && res.contains( decimal ) )
117 {
118 int trimPoint = res.length() - 1;
119 int ePoint = 0;
120 if ( mUseScientific )
121 {
122 while ( res.at( trimPoint ).toUpper() != context.exponential().toUpper() )
123 trimPoint--;
124 ePoint = trimPoint;
125 trimPoint--;
126 }
127
128 while ( res.at( trimPoint ) == context.zeroDigit() )
129 trimPoint--;
130
131 if ( res.at( trimPoint ) == decimal )
132 trimPoint--;
133
134 const QString original = res;
135 res.truncate( trimPoint + 1 );
136 if ( mUseScientific )
137 res += original.mid( ePoint );
138 }
139
140 return res;
141}
142
144{
145 return new QgsBasicNumericFormat( *this );
146}
147
149{
150 auto res = std::make_unique< QgsBasicNumericFormat >();
151 res->setConfiguration( configuration, context );
152 return res.release();
153}
154
156{
157 QVariantMap res;
158 res.insert( QStringLiteral( "decimals" ), mNumberDecimalPlaces );
159 res.insert( QStringLiteral( "show_thousand_separator" ), mShowThousandsSeparator );
160 res.insert( QStringLiteral( "show_plus" ), mShowPlusSign );
161 res.insert( QStringLiteral( "show_trailing_zeros" ), mShowTrailingZeros );
162 res.insert( QStringLiteral( "rounding_type" ), static_cast< int >( mRoundingType ) );
163 res.insert( QStringLiteral( "thousand_separator" ), mThousandsSeparator.isNull() ? QVariant() : QVariant::fromValue( mThousandsSeparator ) );
164 res.insert( QStringLiteral( "decimal_separator" ), mDecimalSeparator.isNull() ? QVariant() : QVariant::fromValue( mDecimalSeparator ) );
165 return res;
166}
167
169{
170 mNumberDecimalPlaces = configuration.value( QStringLiteral( "decimals" ), 6 ).toInt();
171 mShowThousandsSeparator = configuration.value( QStringLiteral( "show_thousand_separator" ), true ).toBool();
172 mShowPlusSign = configuration.value( QStringLiteral( "show_plus" ), false ).toBool();
173 mShowTrailingZeros = configuration.value( QStringLiteral( "show_trailing_zeros" ), false ).toBool();
174 mRoundingType = static_cast< RoundingType >( configuration.value( QStringLiteral( "rounding_type" ), static_cast< int >( DecimalPlaces ) ).toInt() );
175 mThousandsSeparator = configuration.value( QStringLiteral( "thousand_separator" ), QChar() ).toChar();
176 mDecimalSeparator = configuration.value( QStringLiteral( "decimal_separator" ), QChar() ).toChar();
177}
178
180{
181 return mNumberDecimalPlaces;
182}
183
188
190{
191 return mShowThousandsSeparator;
192}
193
198
200{
201 return mShowPlusSign;
202}
203
205{
206 mShowPlusSign = showPlusSign;
207}
208
210{
211 return mShowTrailingZeros;
212}
213
218
223
228
230{
231 return mThousandsSeparator;
232}
233
235{
236 mThousandsSeparator = character;
237}
238
240{
241 return mDecimalSeparator;
242}
243
245{
246 mDecimalSeparator = character;
247}
QgsNumericFormat * create(const QVariantMap &configuration, const QgsReadWriteContext &context) const override
Creates a new copy of the format, using the supplied configuration.
QChar thousandsSeparator() const
Returns any override for the thousands separator character.
RoundingType
Sets rounding type and behavior of the numberDecimalPlaces() setting.
@ DecimalPlaces
Maximum number of decimal places.
@ SignificantFigures
Maximum number of significant figures.
void setThousandsSeparator(QChar character)
Sets an override character for the thousands separator character.
QgsBasicNumericFormat()
Default constructor.
void setShowTrailingZeros(bool show)
Sets whether trailing zeros will be shown (up to the specified numberDecimalPlaces()).
void setShowThousandsSeparator(bool show)
Sets whether the thousands grouping separator will be shown.
bool showThousandsSeparator() const
Returns true if the thousands grouping separator will be shown.
QString id() const override
Returns a unique id for this numeric format.
virtual void setConfiguration(const QVariantMap &configuration, const QgsReadWriteContext &context)
Sets the format's configuration.
RoundingType roundingType() const
Returns the rounding type, which controls the behavior of the numberDecimalPlaces() setting.
QString visibleName() const override
Returns the translated, user-visible name for this format.
int sortKey() override
Returns a sorting key value, where formats with a lower sort key will be shown earlier in lists.
bool showTrailingZeros() const
Returns true if trailing zeros will be shown (up to the specified numberDecimalPlaces()).
void setRoundingType(RoundingType type)
Sets the rounding type, which controls the behavior of the numberDecimalPlaces() setting.
QgsNumericFormat * clone() const override
Clones the format, returning a new object.
bool showPlusSign() const
Returns true if a leading plus sign will be shown for positive values.
QChar decimalSeparator() const
Returns any override for the decimal separator character.
void setShowPlusSign(bool show)
Sets whether a leading plus sign will be shown for positive values.
QString formatDouble(double value, const QgsNumericFormatContext &context) const override
Returns a formatted string representation of a numeric double value.
virtual void setNumberDecimalPlaces(int places)
Sets the maximum number of decimal places to show.
int numberDecimalPlaces() const
Returns the maximum number of decimal places to show.
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
void setDecimalSeparator(QChar character)
Sets an override character for the decimal separator character.
A context for numeric formats.
QChar thousandsSeparator() const
Returns the thousands separator character.
QChar zeroDigit() const
Returns the zero digit character.
QChar exponential() const
Returns the exponential character.
QChar decimalSeparator() const
Returns the decimal separator character.
QChar positiveSign() const
Returns the positive sign character.
QgsNumericFormat()=default
A container for the context for various read/write operations on objects.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6607
std::string do_grouping() const override
formatter(QChar thousands, bool showThousands, QChar decimal)