QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 {
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 }
@ 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.
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
virtual int sortKey()
Returns a sorting key value, where formats with a lower sort key will be shown earlier in lists.
The class is used as a container of context for various read/write operations on other objects.