QGIS API Documentation
4.3.0-Master (8c22baaae17)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
fieldformatter
qgsrangefieldformatter.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
qgsrangefieldformatter.cpp - QgsRangeFieldFormatter
3
4
---------------------
5
begin : 01/02/2018
6
copyright : (C) 2018 by Alessandro Pasotti
7
email : elpaso at itopen dot it
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 "
qgsrangefieldformatter.h
"
18
19
#include "
qgsapplication.h
"
20
#include "
qgsfield.h
"
21
#include "
qgssettings.h
"
22
#include "
qgsvariantutils.h
"
23
#include "
qgsvectorlayer.h
"
24
25
#include <QLocale>
26
#include <QString>
27
28
using namespace
Qt::StringLiterals;
29
30
QString
QgsRangeFieldFormatter::id
()
const
31
{
32
return
u
"Range"
_s;
33
}
34
35
QString
QgsRangeFieldFormatter::representValue
(
QgsVectorLayer
*layer,
int
fieldIndex,
const
QVariantMap &config,
const
QVariant &cache,
const
QVariant &value )
const
36
{
37
Q_UNUSED( cache )
38
Q_UNUSED( config )
39
40
if
(
QgsVariantUtils::isNull
( value ) )
41
{
42
return
QgsApplication::nullRepresentation
();
43
}
44
45
QString result;
46
47
const
QgsField
field = layer->
fields
().
at
( fieldIndex );
48
49
if
( field.
type
() == QMetaType::Type::Double && config.contains( u
"Precision"
_s ) && value.isValid() )
50
{
51
bool
ok;
52
const
double
val( value.toDouble( &ok ) );
53
if
( ok )
54
{
55
const
int
precision( config[u
"Precision"
_s].toInt( &ok ) );
56
if
( ok )
57
{
58
// TODO: make the format configurable!
59
result = QLocale().toString( val,
'f'
, precision );
60
}
61
}
62
}
63
else
if
( ( field.
type
() == QMetaType::Type::Int ) && value.isValid() )
64
{
65
bool
ok;
66
const
double
val( value.toInt( &ok ) );
67
if
( ok )
68
{
69
result = QLocale().toString( val,
'f'
, 0 );
70
}
71
}
72
else
if
( ( field.
type
() == QMetaType::Type::LongLong ) && value.isValid() )
73
{
74
bool
ok;
75
const
double
val( value.toLongLong( &ok ) );
76
if
( ok )
77
{
78
result = QLocale().toString( val,
'f'
, 0 );
79
}
80
}
81
else
82
{
83
result = value.toString();
84
}
85
return
result;
86
}
QgsApplication::nullRepresentation
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
Definition
qgsapplication.cpp:2227
QgsField
Encapsulate a field in an attribute table or data source.
Definition
qgsfield.h:56
QgsField::type
QMetaType::Type type
Definition
qgsfield.h:63
QgsFields::at
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Definition
qgsfields.cpp:209
QgsRangeFieldFormatter::id
QString id() const override
Returns a unique id for this field formatter.
Definition
qgsrangefieldformatter.cpp:30
QgsRangeFieldFormatter::representValue
QString representValue(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value) const override
Create a pretty String representation of the value.
Definition
qgsrangefieldformatter.cpp:35
QgsVariantUtils::isNull
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
Definition
qgsvariantutils.cpp:176
QgsVectorLayer
Represents a vector layer which manages a vector based dataset.
Definition
qgsvectorlayer.h:414
QgsVectorLayer::fields
QgsFields fields
Definition
qgsvectorlayer.h:422
qgsapplication.h
qgsfield.h
qgsrangefieldformatter.h
qgssettings.h
qgsvariantutils.h
qgsvectorlayer.h
Generated on
for QGIS API Documentation by
1.17.0