QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 = qgis::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 }
QgsPercentageNumericFormat::sortKey
int sortKey() override
Returns a sorting key value, where formats with a lower sort key will be shown earlier in lists.
Definition: qgspercentagenumericformat.cpp:35
QgsReadWriteContext
Definition: qgsreadwritecontext.h:34
qgis.h
QgsPercentageNumericFormat::InputValues
InputValues
Input value format, which specifies the format of the incoming values.
Definition: qgspercentagenumericformat.h:46
QgsNumericFormat
Definition: qgsnumericformat.h:217
QgsPercentageNumericFormat::visibleName
QString visibleName() const override
Returns the translated, user-visible name for this format.
Definition: qgspercentagenumericformat.cpp:30
QgsPercentageNumericFormat::create
QgsNumericFormat * create(const QVariantMap &configuration, const QgsReadWriteContext &context) const override
Creates a new copy of the format, using the supplied configuration.
Definition: qgspercentagenumericformat.cpp:73
QgsPercentageNumericFormat::inputValues
InputValues inputValues() const
Returns the format of the incoming values.
Definition: qgspercentagenumericformat.cpp:89
QgsNumericFormatContext::percent
QChar percent() const
Returns the percent character.
Definition: qgsnumericformat.h:91
QgsPercentageNumericFormat::suggestSampleValue
double suggestSampleValue() const override
Returns a suggested sample value which nicely represents the current format configuration.
Definition: qgspercentagenumericformat.cpp:40
QgsPercentageNumericFormat::clone
QgsNumericFormat * clone() const override
Clones the format, returning a new object.
Definition: qgspercentagenumericformat.cpp:68
QgsPercentageNumericFormat::ValuesArePercentage
@ ValuesArePercentage
Incoming values are percentage values (e.g. 50 for 50%)
Definition: qgspercentagenumericformat.h:61
QgsBasicNumericFormat::configuration
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
Definition: qgsbasicnumericformat.cpp:143
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
QgsPercentageNumericFormat::setInputValues
void setInputValues(InputValues format)
Sets the format of the incoming values.
Definition: qgspercentagenumericformat.cpp:94
QgsPercentageNumericFormat::id
QString id() const override
Returns a unique id for this numeric format.
Definition: qgspercentagenumericformat.cpp:25
QgsPercentageNumericFormat::formatDouble
QString formatDouble(double value, const QgsNumericFormatContext &context) const override
Returns a formatted string representation of a numeric double value.
Definition: qgspercentagenumericformat.cpp:53
qgspercentagenumericformat.h
QgsBasicNumericFormat::DecimalPlaces
@ DecimalPlaces
Maximum number of decimal places.
Definition: qgsbasicnumericformat.h:66
QgsPercentageNumericFormat::ValuesAreFractions
@ ValuesAreFractions
Incoming values are numeric fractions (e.g. 0.5 for 50%)
Definition: qgspercentagenumericformat.h:62
QgsNumericFormatContext
Definition: qgsnumericformat.h:34
QgsNumericFormat::sortKey
virtual int sortKey()
Returns a sorting key value, where formats with a lower sort key will be shown earlier in lists.
Definition: qgsnumericformat.cpp:35
QgsPercentageNumericFormat::QgsPercentageNumericFormat
QgsPercentageNumericFormat()
Default constructor.
Definition: qgspercentagenumericformat.cpp:21
QgsPercentageNumericFormat::configuration
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
Definition: qgspercentagenumericformat.cpp:82