QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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
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 "qgsapplication.h"
20#include "qgsvariantutils.h"
21#include "qgsvectorlayer.h"
22
23#include <QObject>
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
29{
30 return u"CheckBox"_s;
31}
32
33QString 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 {
99 if ( boolValue )
100 return QObject::tr( "true" );
101 else
102 return QObject::tr( "false" );
103
105 return textValue;
106 }
107 return QString();
108}
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
TextDisplayMethod
Method to use when displaying the checkbox values as plain text.
@ ShowTrueFalse
Shows "True" or "False" strings.
@ ShowStoredValues
Shows actual stored field value.
QString id() const override
Returns a unique id for this field formatter.
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.
QMetaType::Type type
Definition qgsfield.h:63
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
Represents a vector layer which manages a vector based dataset.