QGIS API Documentation 3.41.0-Master (3440c17df1d)
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
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 <QObject>
18
20#include "qgsvectorlayer.h"
21#include "qgsapplication.h"
22#include "qgsvariantutils.h"
23
25{
26 return QStringLiteral( "CheckBox" );
27}
28
29QString QgsCheckBoxFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
30{
31 Q_UNUSED( cache )
32
33 /*
34 This follows this logic:
35
36 if field type is bool:
37 NULL => nullRepresentation
38 true => tr("true")
39 false => tr("false")
40 else
41 if cannot convert to string (like json integer list) => (invalid)
42 if == checkedstate => tr("true")
43 if == uncheckedstate => tr("false")
44 else (value.toString)
45 */
46
47 bool isNull = QgsVariantUtils::isNull( value );
48 bool boolValue = false;
49 QString textValue = QgsApplication::nullRepresentation();
50
51 const QMetaType::Type fieldType = layer->fields().at( fieldIndex ).type();
52 if ( fieldType == QMetaType::Type::Bool )
53 {
54 if ( ! isNull )
55 {
56 boolValue = value.toBool();
57 textValue = boolValue ? QObject::tr( "true" ) : QObject::tr( "false" );
58 }
59 }
60 else
61 {
62 if ( !value.canConvert<QString>() )
63 {
64 isNull = true;
65 textValue = QObject::tr( "(invalid)" );
66 }
67 else
68 {
69 textValue = value.toString();
70 if ( config.contains( QStringLiteral( "CheckedState" ) ) && textValue == config[ QStringLiteral( "CheckedState" ) ].toString() )
71 {
72 boolValue = true;
73 }
74 else if ( config.contains( QStringLiteral( "UncheckedState" ) ) && textValue == config[ QStringLiteral( "UncheckedState" ) ].toString() )
75 {
76 boolValue = false;
77 }
78 else
79 {
80 isNull = true;
81 textValue = QStringLiteral( "(%1)" ).arg( textValue );
82 }
83 }
84 }
85
86 if ( isNull )
87 {
88 return textValue;
89 }
90
91 const TextDisplayMethod displayMethod = static_cast< TextDisplayMethod >( config.value( QStringLiteral( "TextDisplayMethod" ), QStringLiteral( "0" ) ).toInt() );
92 switch ( displayMethod )
93 {
95 if ( boolValue )
96 return QObject::tr( "true" );
97 else
98 return QObject::tr( "false" );
99
101 return textValue;
102 }
103 return QString();
104}
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:60
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 data sets.