QGIS API Documentation  3.19.0-Master (c9e5875a2b)
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 : [email protected]
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 
23 
25 {
26  return QStringLiteral( "CheckBox" );
27 }
28 
29 QString 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 = value.isNull();
48  bool boolValue = false;
49  QString textValue = QgsApplication::nullRepresentation();
50 
51  const QVariant::Type fieldType = layer->fields().at( fieldIndex ).type();
52  if ( fieldType == QVariant::Bool )
53  {
54  boolValue = value.toBool();
55  textValue = boolValue ? QObject::tr( "true" ) : QObject::tr( "false" );
56  }
57  else
58  {
59  if ( !value.canConvert<QString>() )
60  {
61  isNull = true;
62  textValue = QObject::tr( "(invalid)" );
63  }
64  else
65  {
66  textValue = value.toString();
67  if ( config.contains( QStringLiteral( "CheckedState" ) ) && textValue == config[ QStringLiteral( "CheckedState" ) ].toString() )
68  {
69  boolValue = true;
70  }
71  else if ( config.contains( QStringLiteral( "UncheckedState" ) ) && textValue == config[ QStringLiteral( "UncheckedState" ) ].toString() )
72  {
73  boolValue = false;
74  }
75  else
76  {
77  isNull = true;
78  textValue = QStringLiteral( "(%1)" ).arg( textValue );
79  }
80  }
81  }
82 
83  if ( isNull )
84  {
85  return textValue;
86  }
87 
88  const TextDisplayMethod displayMethod = static_cast< TextDisplayMethod >( config.value( QStringLiteral( "TextDisplayMethod" ), QStringLiteral( "0" ) ).toInt() );
89  switch ( displayMethod )
90  {
92  if ( boolValue )
93  return QObject::tr( "true" );
94  else
95  return QObject::tr( "false" );
96 
98  return textValue;
99  }
100  return QString();
101 }
static QString nullRepresentation()
This string is 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.
QVariant::Type type
Definition: qgsfield.h:58
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
Represents a vector layer which manages a vector based data sets.
QgsFields fields() const FINAL
Returns the list of fields of this layer.