QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
numericformats
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
17
#include "
qgspercentagenumericformat.h
"
18
19
#include "
qgis.h
"
20
21
#include <QString>
22
23
using namespace
Qt::StringLiterals;
24
25
QgsPercentageNumericFormat::QgsPercentageNumericFormat
()
26
{}
27
28
QString
QgsPercentageNumericFormat::id
()
const
29
{
30
return
u
"percentage"
_s;
31
}
32
33
QString
QgsPercentageNumericFormat::visibleName
()
const
34
{
35
return
QObject::tr(
"Percentage"
);
36
}
37
38
int
QgsPercentageNumericFormat::sortKey
()
39
{
40
return
DEFAULT_SORT_KEY
;
41
}
42
43
double
QgsPercentageNumericFormat::suggestSampleValue
()
const
44
{
45
switch
( mInputValues )
46
{
47
case
ValuesArePercentage
:
48
return
50.1234;
49
50
case
ValuesAreFractions
:
51
return
0.501234;
52
}
53
return
50.1234;
// no warnings
54
}
55
56
QString
QgsPercentageNumericFormat::formatDouble
(
double
value,
const
QgsNumericFormatContext
&context )
const
57
{
58
switch
( mInputValues )
59
{
60
case
ValuesArePercentage
:
61
break
;
62
63
case
ValuesAreFractions
:
64
value *= 100;
65
break
;
66
}
67
68
return
QgsBasicNumericFormat::formatDouble
( value, context ) + context.
percent
();
69
}
70
71
QgsNumericFormat
*
QgsPercentageNumericFormat::clone
()
const
72
{
73
return
new
QgsPercentageNumericFormat
( *
this
);
74
}
75
76
QgsNumericFormat
*
QgsPercentageNumericFormat::create
(
const
QVariantMap &
configuration
,
const
QgsReadWriteContext
&context )
const
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
85
QVariantMap
QgsPercentageNumericFormat::configuration
(
const
QgsReadWriteContext
&context )
const
86
{
87
QVariantMap res =
QgsBasicNumericFormat::configuration
( context );
88
res.insert( u
"input_values"
_s,
static_cast<
int
>
( mInputValues ) );
89
return
res;
90
}
91
92
QgsPercentageNumericFormat::InputValues
QgsPercentageNumericFormat::inputValues
()
const
93
{
94
return
mInputValues;
95
}
96
97
void
QgsPercentageNumericFormat::setInputValues
(
InputValues
inputValues
)
98
{
99
mInputValues =
inputValues
;
100
}
QgsBasicNumericFormat::DecimalPlaces
@ DecimalPlaces
Maximum number of decimal places.
Definition
qgsbasicnumericformat.h:40
QgsBasicNumericFormat::formatDouble
QString formatDouble(double value, const QgsNumericFormatContext &context) const override
Returns a formatted string representation of a numeric double value.
Definition
qgsbasicnumericformat.cpp:67
QgsBasicNumericFormat::configuration
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
Definition
qgsbasicnumericformat.cpp:156
QgsNumericFormatContext
A context for numeric formats.
Definition
qgsnumericformat.h:35
QgsNumericFormatContext::percent
QChar percent() const
Returns the percent character.
Definition
qgsnumericformat.h:79
QgsNumericFormat::DEFAULT_SORT_KEY
static constexpr int DEFAULT_SORT_KEY
Definition
qgsnumericformat.h:323
QgsNumericFormat::QgsNumericFormat
QgsNumericFormat()=default
QgsPercentageNumericFormat::id
QString id() const override
Returns a unique id for this numeric format.
Definition
qgspercentagenumericformat.cpp:28
QgsPercentageNumericFormat::inputValues
InputValues inputValues() const
Returns the format of the incoming values.
Definition
qgspercentagenumericformat.cpp:92
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:76
QgsPercentageNumericFormat::clone
QgsNumericFormat * clone() const override
Clones the format, returning a new object.
Definition
qgspercentagenumericformat.cpp:71
QgsPercentageNumericFormat::visibleName
QString visibleName() const override
Returns the translated, user-visible name for this format.
Definition
qgspercentagenumericformat.cpp:33
QgsPercentageNumericFormat::QgsPercentageNumericFormat
QgsPercentageNumericFormat()
Default constructor.
Definition
qgspercentagenumericformat.cpp:25
QgsPercentageNumericFormat::setInputValues
void setInputValues(InputValues format)
Sets the format of the incoming values.
Definition
qgspercentagenumericformat.cpp: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:38
QgsPercentageNumericFormat::suggestSampleValue
double suggestSampleValue() const override
Returns a suggested sample value which nicely represents the current format configuration.
Definition
qgspercentagenumericformat.cpp:43
QgsPercentageNumericFormat::configuration
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
Definition
qgspercentagenumericformat.cpp:85
QgsPercentageNumericFormat::InputValues
InputValues
Input value format, which specifies the format of the incoming values.
Definition
qgspercentagenumericformat.h:33
QgsPercentageNumericFormat::ValuesAreFractions
@ ValuesAreFractions
Incoming values are numeric fractions (e.g. 0.5 for 50%).
Definition
qgspercentagenumericformat.h:35
QgsPercentageNumericFormat::ValuesArePercentage
@ ValuesArePercentage
Incoming values are percentage values (e.g. 50 for 50%).
Definition
qgspercentagenumericformat.h:34
QgsPercentageNumericFormat::formatDouble
QString formatDouble(double value, const QgsNumericFormatContext &context) const override
Returns a formatted string representation of a numeric double value.
Definition
qgspercentagenumericformat.cpp:56
QgsReadWriteContext
A container for the context for various read/write operations on objects.
Definition
qgsreadwritecontext.h:38
qgis.h
qgspercentagenumericformat.h
Generated on
for QGIS API Documentation by
1.17.0