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