QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsvaluemapfieldformatter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvaluemapfieldformatter.cpp - QgsValueMapFieldFormatter
3 
4  ---------------------
5  begin : 3.12.2016
6  copyright : (C) 2016 by Matthias Kuhn
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  ***************************************************************************/
17 
18 #include "qgsvectorlayer.h"
19 
20 const QString QgsValueMapFieldFormatter::NULL_VALUE = QStringLiteral( "{2839923C-8B7D-419E-B84B-CA2FE9B80EC7}" );
21 
23 {
25 }
26 
28 {
29  return QStringLiteral( "ValueMap" );
30 }
31 
32 QString QgsValueMapFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
33 {
34  Q_UNUSED( cache )
35 
36  QString valueInternalText;
37  if ( value.isNull() )
38  valueInternalText = NULL_VALUE;
39  else
40  valueInternalText = value.toString();
41 
42  const QVariant v = config.value( QStringLiteral( "map" ) );
43  const QVariantList list = v.toList();
44  if ( !list.empty() )
45  {
46  for ( const QVariant &item : list )
47  {
48  const QVariantMap map = item.toMap();
49  // no built-in Qt way to check if a map contains a value, so iterate through each value
50  for ( auto it = map.constBegin(); it != map.constEnd(); ++it )
51  {
52  if ( it.value().toString() == valueInternalText )
53  return it.key();
54  }
55  }
56  return QStringLiteral( "(%1)" ).arg( layer->fields().at( fieldIndex ).displayString( value ) );
57  }
58  else
59  {
60  // old style config
61  const QVariantMap map = v.toMap();
62  return map.key( valueInternalText, QVariant( QStringLiteral( "(%1)" ).arg( layer->fields().at( fieldIndex ).displayString( value ) ) ).toString() );
63  }
64 }
65 
66 QVariant QgsValueMapFieldFormatter::sortValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
67 {
68  return representValue( layer, fieldIndex, config, cache, value );
69 }
70 
71 QVariantList QgsValueMapFieldFormatter::availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext &context ) const
72 {
73  Q_UNUSED( context )
74 
75  QVariantList values;
76  const QList<QVariant> valueList = config.value( QStringLiteral( "map" ) ).toList();
77  for ( const QVariant &item : valueList )
78  {
79  values.append( item.toMap().constBegin().value() );
80  if ( values.count() == countLimit )
81  break;
82  }
83 
84  return values;
85 }
QgsFieldFormatter::flags
Flags flags() const
Returns the flags.
Definition: qgsfieldformatter.h:170
QgsValueMapFieldFormatter::NULL_VALUE
static const QString NULL_VALUE
Will be saved in the configuration when a value is NULL.
Definition: qgsvaluemapfieldformatter.h:62
QgsField::displayString
QString displayString(const QVariant &v) const
Formats string for display.
Definition: qgsfield.cpp:255
QgsFieldFormatterContext
A context for field formatter containing information like the project.
Definition: qgsfieldformatter.h:33
QgsFieldFormatter::setFlags
void setFlags(const Flags &flags)
Sets the flags.
Definition: qgsfieldformatter.cpp:95
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3436
QgsValueMapFieldFormatter::sortValue
QVariant sortValue(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value) const override
If the default sort order should be overwritten for this widget, you can transform the value in here.
Definition: qgsvaluemapfieldformatter.cpp:66
qgsvaluemapfieldformatter.h
QgsValueMapFieldFormatter::id
QString id() const override
Returns a unique id for this field formatter.
Definition: qgsvaluemapfieldformatter.cpp:27
QgsValueMapFieldFormatter::availableValues
QVariantList availableValues(const QVariantMap &config, int countLimit, const QgsFieldFormatterContext &context) const override
Returns a list of the values that would be possible to select with this widget type On a RelationRefe...
Definition: qgsvaluemapfieldformatter.cpp:71
qgsvectorlayer.h
QgsValueMapFieldFormatter::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: qgsvaluemapfieldformatter.cpp:32
QgsFieldFormatter::CanProvideAvailableValues
@ CanProvideAvailableValues
Can provide possible values.
Definition: qgsfieldformatter.h:90
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsValueMapFieldFormatter::QgsValueMapFieldFormatter
QgsValueMapFieldFormatter()
Default constructor of field formatter for a value map field.
Definition: qgsvaluemapfieldformatter.cpp:22
QgsFields::at
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Definition: qgsfields.cpp:163