QGIS API Documentation 3.27.0-Master (f261cc1f8b)
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
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
20const QString QgsValueMapFieldFormatter::NULL_VALUE = QStringLiteral( "{2839923C-8B7D-419E-B84B-CA2FE9B80EC7}" );
21
23{
25}
26
28{
29 return QStringLiteral( "ValueMap" );
30}
31
32QString 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
66QVariant 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
71QVariantList 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}
A context for field formatter containing information like the project.
Flags flags() const
Returns the flags.
@ CanProvideAvailableValues
Can provide possible values.
void setFlags(const Flags &flags)
Sets the flags.
QString displayString(const QVariant &v) const
Formats string for display.
Definition: qgsfield.cpp:255
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Definition: qgsfields.cpp:163
QString id() const override
Returns a unique id for this field formatter.
QgsValueMapFieldFormatter()
Default constructor of field formatter for a value map field.
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.
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.
static const QString NULL_VALUE
Will be saved in the configuration when a value is NULL.
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...
Represents a vector layer which manages a vector based data sets.
QgsFields fields() const FINAL
Returns the list of fields of this layer.