QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgscurrencynumericformat.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscurrencynumericformat.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 "qgis.h"
20
21#include <QString>
22
23using namespace Qt::StringLiterals;
24
31
33{
34 return u"currency"_s;
35}
36
38{
39 return QObject::tr( "Currency" );
40}
41
46
48{
49 return 1234.56;
50}
51
52QString QgsCurrencyNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
53{
54 const QString res = QgsBasicNumericFormat::formatDouble( value, context );
55 if ( value < 0 || ( value > 0 && showPlusSign() ) )
56 return res.at( 0 ) + mPrefix + res.mid( 1 ) + mSuffix;
57 else
58 return mPrefix + res + mSuffix;
59}
60
65
67{
68 auto res = std::make_unique< QgsCurrencyNumericFormat >();
69 res->setConfiguration( configuration, context );
70 res->mPrefix = configuration.value( u"prefix"_s, u"$"_s ).toString();
71 res->mSuffix = configuration.value( u"suffix"_s, QString() ).toString();
72
73 // override base class default for number of decimal places -- we want to default to 2, showing trailing zeros
74 res->setNumberDecimalPlaces( configuration.value( u"decimals"_s, 2 ).toInt() );
75 res->setShowTrailingZeros( configuration.value( u"show_trailing_zeros"_s, true ).toBool() );
76 res->setRoundingType( QgsBasicNumericFormat::DecimalPlaces );
77
78 return res.release();
79}
80
82{
83 QVariantMap res = QgsBasicNumericFormat::configuration( context );
84 res.insert( u"prefix"_s, mPrefix );
85 res.insert( u"suffix"_s, mSuffix );
86 return res;
87}
88
90{
91 return mPrefix;
92}
93
95{
96 mPrefix = prefix;
97}
98
100{
101 return mSuffix;
102}
103
105{
106 mSuffix = suffix;
107}
@ DecimalPlaces
Maximum number of decimal places.
void setShowTrailingZeros(bool show)
Sets whether trailing zeros will be shown (up to the specified numberDecimalPlaces()).
bool showPlusSign() const
Returns true if 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.
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
QgsNumericFormat * create(const QVariantMap &configuration, const QgsReadWriteContext &context) const override
Creates a new copy of the format, using the supplied configuration.
QString prefix() const
Returns the currency prefix, e.g.
QString visibleName() const override
Returns the translated, user-visible name for this format.
void setSuffix(const QString &suffix)
Sets the currency suffix, e.g.
void setPrefix(const QString &prefix)
Sets the currency prefix, e.g.
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
int sortKey() override
Returns a sorting key value, where formats with a lower sort key will be shown earlier in lists.
QString id() const override
Returns a unique id for this numeric format.
QString formatDouble(double value, const QgsNumericFormatContext &context) const override
Returns a formatted string representation of a numeric double value.
double suggestSampleValue() const override
Returns a suggested sample value which nicely represents the current format configuration.
QgsCurrencyNumericFormat()
Default constructor.
QgsNumericFormat * clone() const override
Clones the format, returning a new object.
QString suffix() const
Returns the currency suffix, e.g.
A context for numeric formats.
static constexpr int DEFAULT_SORT_KEY
QgsNumericFormat()=default
A container for the context for various read/write operations on objects.