QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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  }
56  else
57  {
58  if ( !value.canConvert<QString>() )
59  {
60  isNull = true;
61  textValue = QObject::tr( "(invalid)" );
62  }
63  else
64  {
65  if ( config.contains( QStringLiteral( "CheckedState" ) ) && value.toString() == config[ QStringLiteral( "CheckedState" ) ].toString() )
66  {
67  boolValue = true;
68  }
69  else if ( config.contains( QStringLiteral( "UncheckedState" ) ) && value.toString() == config[ QStringLiteral( "UncheckedState" ) ].toString() )
70  {
71  boolValue = false;
72  }
73  else
74  {
75  isNull = true;
76  textValue = QStringLiteral( "(%1)" ).arg( value.toString() );
77  }
78  }
79  }
80 
81  if ( isNull )
82  {
83  return textValue;
84  }
85  if ( boolValue )
86  return QObject::tr( "true" );
87  else
88  return QObject::tr( "false" );
89 }
qgsapplication.h
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3280
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:29
QgsApplication::nullRepresentation
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
Definition: qgsapplication.cpp:1802
qgsvectorlayer.h
QgsCheckBoxFieldFormatter::id
QString id() const override
Returns a unique id for this field formatter.
Definition: qgscheckboxfieldformatter.cpp:24
QgsVectorLayer
Definition: qgsvectorlayer.h:385
qgscheckboxfieldformatter.h
QgsFields::at
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
QgsField::type
QVariant::Type type
Definition: qgsfield.h:57