QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #include "qgis.h"
19 
20 
22  : mPrefix( QStringLiteral( "$" ) )
23 {
25  setShowTrailingZeros( true );
26 }
27 
29 {
30  return QStringLiteral( "currency" );
31 }
32 
34 {
35  return QObject::tr( "Currency" );
36 }
37 
39 {
40  return DEFAULT_SORT_KEY;
41 }
42 
44 {
45  return 1234.56;
46 }
47 
48 QString QgsCurrencyNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
49 {
50  const QString res = QgsBasicNumericFormat::formatDouble( value, context );
51  if ( value < 0 || ( value > 0 && showPlusSign() ) )
52  return res.at( 0 ) + mPrefix + res.mid( 1 ) + mSuffix;
53  else
54  return mPrefix + res + mSuffix;
55 }
56 
58 {
59  return new QgsCurrencyNumericFormat( *this );
60 }
61 
62 QgsNumericFormat *QgsCurrencyNumericFormat::create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const
63 {
64  std::unique_ptr< QgsCurrencyNumericFormat > res = std::make_unique< QgsCurrencyNumericFormat >();
65  res->setConfiguration( configuration, context );
66  res->mPrefix = configuration.value( QStringLiteral( "prefix" ), QStringLiteral( "$" ) ).toString();
67  res->mSuffix = configuration.value( QStringLiteral( "suffix" ), QString() ).toString();
68 
69  // override base class default for number of decimal places -- we want to default to 2, showing trailing zeros
70  res->setNumberDecimalPlaces( configuration.value( QStringLiteral( "decimals" ), 2 ).toInt() );
71  res->setShowTrailingZeros( configuration.value( QStringLiteral( "show_trailing_zeros" ), true ).toBool() );
72  res->setRoundingType( QgsBasicNumericFormat::DecimalPlaces );
73 
74  return res.release();
75 }
76 
78 {
79  QVariantMap res = QgsBasicNumericFormat::configuration( context );
80  res.insert( QStringLiteral( "prefix" ), mPrefix );
81  res.insert( QStringLiteral( "suffix" ), mSuffix );
82  return res;
83 }
84 
86 {
87  return mPrefix;
88 }
89 
90 void QgsCurrencyNumericFormat::setPrefix( const QString &prefix )
91 {
92  mPrefix = prefix;
93 }
94 
96 {
97  return mSuffix;
98 }
99 
100 void QgsCurrencyNumericFormat::setSuffix( const QString &suffix )
101 {
102  mSuffix = suffix;
103 }
QgsCurrencyNumericFormat::visibleName
QString visibleName() const override
Returns the translated, user-visible name for this format.
Definition: qgscurrencynumericformat.cpp:33
QgsCurrencyNumericFormat::id
QString id() const override
Returns a unique id for this numeric format.
Definition: qgscurrencynumericformat.cpp:28
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsCurrencyNumericFormat::suggestSampleValue
double suggestSampleValue() const override
Returns a suggested sample value which nicely represents the current format configuration.
Definition: qgscurrencynumericformat.cpp:43
QgsBasicNumericFormat::setNumberDecimalPlaces
virtual void setNumberDecimalPlaces(int places)
Sets the maximum number of decimal places to show.
Definition: qgsbasicnumericformat.cpp:179
qgis.h
QgsNumericFormat
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
Definition: qgsnumericformat.h:259
qgscurrencynumericformat.h
QgsBasicNumericFormat::showPlusSign
bool showPlusSign() const
Returns true if a leading plus sign will be shown for positive values.
Definition: qgsbasicnumericformat.cpp:194
QgsCurrencyNumericFormat::configuration
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
Definition: qgscurrencynumericformat.cpp:77
QgsCurrencyNumericFormat::suffix
QString suffix() const
Returns the currency suffix, e.g.
Definition: qgscurrencynumericformat.cpp:95
QgsCurrencyNumericFormat::clone
QgsNumericFormat * clone() const override
Clones the format, returning a new object.
Definition: qgscurrencynumericformat.cpp:57
QgsCurrencyNumericFormat::setPrefix
void setPrefix(const QString &prefix)
Sets the currency prefix, e.g.
Definition: qgscurrencynumericformat.cpp:90
QgsCurrencyNumericFormat::QgsCurrencyNumericFormat
QgsCurrencyNumericFormat()
Default constructor.
Definition: qgscurrencynumericformat.cpp:21
QgsBasicNumericFormat::configuration
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
Definition: qgsbasicnumericformat.cpp:150
QgsBasicNumericFormat::formatDouble
QString formatDouble(double value, const QgsNumericFormatContext &context) const override
Returns a formatted string representation of a numeric double value.
Definition: qgsbasicnumericformat.cpp:59
QgsCurrencyNumericFormat::sortKey
int sortKey() override
Returns a sorting key value, where formats with a lower sort key will be shown earlier in lists.
Definition: qgscurrencynumericformat.cpp:38
QgsCurrencyNumericFormat::create
QgsNumericFormat * create(const QVariantMap &configuration, const QgsReadWriteContext &context) const override
Creates a new copy of the format, using the supplied configuration.
Definition: qgscurrencynumericformat.cpp:62
QgsCurrencyNumericFormat::setSuffix
void setSuffix(const QString &suffix)
Sets the currency suffix, e.g.
Definition: qgscurrencynumericformat.cpp:100
QgsCurrencyNumericFormat::formatDouble
QString formatDouble(double value, const QgsNumericFormatContext &context) const override
Returns a formatted string representation of a numeric double value.
Definition: qgscurrencynumericformat.cpp:48
QgsBasicNumericFormat::setShowTrailingZeros
void setShowTrailingZeros(bool show)
Sets whether trailing zeros will be shown (up to the specified numberDecimalPlaces()).
Definition: qgsbasicnumericformat.cpp:209
QgsNumericFormat::DEFAULT_SORT_KEY
static constexpr int DEFAULT_SORT_KEY
Definition: qgsnumericformat.h:354
QgsBasicNumericFormat::DecimalPlaces
@ DecimalPlaces
Maximum number of decimal places.
Definition: qgsbasicnumericformat.h:66
QgsCurrencyNumericFormat::prefix
QString prefix() const
Returns the currency prefix, e.g.
Definition: qgscurrencynumericformat.cpp:85
QgsNumericFormatContext
A context for numeric formats.
Definition: qgsnumericformat.h:34