QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgspercentagenumericformat.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspercentagenumericformat.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 {
23 }
24 
26 {
27  return QStringLiteral( "percentage" );
28 }
29 
31 {
32  return QObject::tr( "Percentage" );
33 }
34 
36 {
38 }
39 
41 {
42  switch ( mInputValues )
43  {
45  return 50.1234;
46 
47  case ValuesAreFractions:
48  return 0.501234;
49  }
50  return 50.1234; // no warnings
51 }
52 
53 QString QgsPercentageNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
54 {
55  switch ( mInputValues )
56  {
58  break;
59 
60  case ValuesAreFractions:
61  value *= 100;
62  break;
63  }
64 
65  return QgsBasicNumericFormat::formatDouble( value, context ) + context.percent();
66 }
67 
69 {
70  return new QgsPercentageNumericFormat( *this );
71 }
72 
73 QgsNumericFormat *QgsPercentageNumericFormat::create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const
74 {
75  std::unique_ptr< QgsPercentageNumericFormat > res = std::make_unique< QgsPercentageNumericFormat >();
76  res->setConfiguration( configuration, context );
77  res->mInputValues = static_cast< InputValues >( configuration.value( QStringLiteral( "input_values" ), static_cast< int >( ValuesArePercentage ) ).toInt() );
78  res->setRoundingType( QgsBasicNumericFormat::DecimalPlaces );
79  return res.release();
80 }
81 
83 {
84  QVariantMap res = QgsBasicNumericFormat::configuration( context );
85  res.insert( QStringLiteral( "input_values" ), static_cast< int >( mInputValues ) );
86  return res;
87 }
88 
90 {
91  return mInputValues;
92 }
93 
95 {
96  mInputValues = inputValues;
97 }
@ DecimalPlaces
Maximum number of decimal places.
QString formatDouble(double value, const QgsNumericFormatContext &context) const override
Returns a formatted string representation of a numeric double value.
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
A context for numeric formats.
QChar percent() const
Returns the percent character.
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.
QString id() const override
Returns a unique id for this numeric format.
InputValues inputValues() const
Returns the format of the incoming values.
QgsNumericFormat * create(const QVariantMap &configuration, const QgsReadWriteContext &context) const override
Creates a new copy of the format, using the supplied configuration.
QgsNumericFormat * clone() const override
Clones the format, returning a new object.
QString visibleName() const override
Returns the translated, user-visible name for this format.
QgsPercentageNumericFormat()
Default constructor.
void setInputValues(InputValues format)
Sets the format of the incoming values.
int sortKey() override
Returns a sorting key value, where formats with a lower sort key will be shown earlier in lists.
double suggestSampleValue() const override
Returns a suggested sample value which nicely represents the current format configuration.
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
InputValues
Input value format, which specifies the format of the incoming values.
@ ValuesAreFractions
Incoming values are numeric fractions (e.g. 0.5 for 50%)
@ ValuesArePercentage
Incoming values are percentage values (e.g. 50 for 50%)
QString formatDouble(double value, const QgsNumericFormatContext &context) const override
Returns a formatted string representation of a numeric double value.
The class is used as a container of context for various read/write operations on other objects.