QGIS API Documentation
4.3.0-Master (52aa6503bed)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
fieldformatter
qgscheckboxfieldformatter.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
qgscheckboxfieldformatter.cpp - QgsCheckBoxFieldFormatter
3
4
---------------------
5
begin : 23.09.2019
6
copyright : (C) 2019 by Denis Rouzaud
7
email : denis@opengis.ch
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 "
qgscheckboxfieldformatter.h
"
18
19
#include "
qgsapplication.h
"
20
#include "
qgsvariantutils.h
"
21
#include "
qgsvectorlayer.h
"
22
23
#include <QObject>
24
#include <QString>
25
26
using namespace
Qt::StringLiterals;
27
28
QString
QgsCheckBoxFieldFormatter::id
()
const
29
{
30
return
u
"CheckBox"
_s;
31
}
32
33
QString
QgsCheckBoxFieldFormatter::representValue
(
QgsVectorLayer
*layer,
int
fieldIndex,
const
QVariantMap &config,
const
QVariant &cache,
const
QVariant &value )
const
34
{
35
Q_UNUSED( cache )
36
37
/*
38
This follows this logic:
39
40
if field type is bool:
41
NULL => nullRepresentation
42
true => tr("true")
43
false => tr("false")
44
else
45
if cannot convert to string (like json integer list) => (invalid)
46
if == checkedstate => tr("true")
47
if == uncheckedstate => tr("false")
48
else (value.toString)
49
*/
50
51
bool
isNull =
QgsVariantUtils::isNull
( value );
52
bool
boolValue =
false
;
53
QString textValue =
QgsApplication::nullRepresentation
();
54
55
const
QMetaType::Type fieldType = layer->
fields
().
at
( fieldIndex ).
type
();
56
if
( fieldType == QMetaType::Type::Bool )
57
{
58
if
( !isNull )
59
{
60
boolValue = value.toBool();
61
textValue = boolValue ? QObject::tr(
"true"
) : QObject::tr(
"false"
);
62
}
63
}
64
else
65
{
66
if
( !value.canConvert<QString>() )
67
{
68
isNull =
true
;
69
textValue = QObject::tr(
"(invalid)"
);
70
}
71
else
72
{
73
textValue = value.toString();
74
if
( config.contains( u
"CheckedState"
_s ) && textValue == config[u
"CheckedState"
_s].toString() )
75
{
76
boolValue =
true
;
77
}
78
else
if
( config.contains( u
"UncheckedState"
_s ) && textValue == config[u
"UncheckedState"
_s].toString() )
79
{
80
boolValue =
false
;
81
}
82
else
83
{
84
isNull =
true
;
85
textValue = u
"(%1)"
_s.arg( textValue );
86
}
87
}
88
}
89
90
if
( isNull )
91
{
92
return
textValue;
93
}
94
95
const
TextDisplayMethod
displayMethod =
static_cast<
TextDisplayMethod
>
( config.value( u
"TextDisplayMethod"
_s, u
"0"
_s ).toInt() );
96
switch
( displayMethod )
97
{
98
case
QgsCheckBoxFieldFormatter::ShowTrueFalse
:
99
if
( boolValue )
100
return
QObject::tr(
"true"
);
101
else
102
return
QObject::tr(
"false"
);
103
104
case
QgsCheckBoxFieldFormatter::ShowStoredValues
:
105
return
textValue;
106
}
107
return
QString();
108
}
QgsApplication::nullRepresentation
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
Definition
qgsapplication.cpp:2227
QgsCheckBoxFieldFormatter::TextDisplayMethod
TextDisplayMethod
Method to use when displaying the checkbox values as plain text.
Definition
qgscheckboxfieldformatter.h:37
QgsCheckBoxFieldFormatter::ShowTrueFalse
@ ShowTrueFalse
Shows "True" or "False" strings.
Definition
qgscheckboxfieldformatter.h:38
QgsCheckBoxFieldFormatter::ShowStoredValues
@ ShowStoredValues
Shows actual stored field value.
Definition
qgscheckboxfieldformatter.h:39
QgsCheckBoxFieldFormatter::id
QString id() const override
Returns a unique id for this field formatter.
Definition
qgscheckboxfieldformatter.cpp:28
QgsCheckBoxFieldFormatter::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
qgscheckboxfieldformatter.cpp:33
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
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
qgscheckboxfieldformatter.h
qgsvariantutils.h
qgsvectorlayer.h
Generated on
for QGIS API Documentation by
1.17.0