QGIS API Documentation 3.99.0-Master (8e76e220402)
Loading...
Searching...
No Matches
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
19#include "qgis.h"
20
21#include <QString>
22
23using namespace Qt::StringLiterals;
24
28
30{
31 return u"percentage"_s;
32}
33
35{
36 return QObject::tr( "Percentage" );
37}
38
43
45{
46 switch ( mInputValues )
47 {
49 return 50.1234;
50
52 return 0.501234;
53 }
54 return 50.1234; // no warnings
55}
56
57QString QgsPercentageNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
58{
59 switch ( mInputValues )
60 {
62 break;
63
65 value *= 100;
66 break;
67 }
68
69 return QgsBasicNumericFormat::formatDouble( value, context ) + context.percent();
70}
71
76
78{
79 auto res = std::make_unique< QgsPercentageNumericFormat >();
80 res->setConfiguration( configuration, context );
81 res->mInputValues = static_cast< InputValues >( configuration.value( u"input_values"_s, static_cast< int >( ValuesArePercentage ) ).toInt() );
82 res->setRoundingType( QgsBasicNumericFormat::DecimalPlaces );
83 return res.release();
84}
85
87{
88 QVariantMap res = QgsBasicNumericFormat::configuration( context );
89 res.insert( u"input_values"_s, static_cast< int >( mInputValues ) );
90 return res;
91}
92
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.
static constexpr int DEFAULT_SORT_KEY
QgsNumericFormat()=default
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.
A container for the context for various read/write operations on objects.