QGIS API Documentation 4.1.0-Master (60fea48833c)
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
27
29{
30 return u"percentage"_s;
31}
32
34{
35 return QObject::tr( "Percentage" );
36}
37
42
44{
45 switch ( mInputValues )
46 {
48 return 50.1234;
49
51 return 0.501234;
52 }
53 return 50.1234; // no warnings
54}
55
56QString QgsPercentageNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
57{
58 switch ( mInputValues )
59 {
61 break;
62
64 value *= 100;
65 break;
66 }
67
68 return QgsBasicNumericFormat::formatDouble( value, context ) + context.percent();
69}
70
75
77{
78 auto res = std::make_unique< QgsPercentageNumericFormat >();
79 res->setConfiguration( configuration, context );
80 res->mInputValues = static_cast< InputValues >( configuration.value( u"input_values"_s, static_cast< int >( ValuesArePercentage ) ).toInt() );
81 res->setRoundingType( QgsBasicNumericFormat::DecimalPlaces );
82 return res.release();
83}
84
86{
87 QVariantMap res = QgsBasicNumericFormat::configuration( context );
88 res.insert( u"input_values"_s, static_cast< int >( mInputValues ) );
89 return res;
90}
91
96
@ 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.