QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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 "qgsvariantutils.h"
19#include "qgsvectorlayer.h"
20
21#include <QString>
22
23using namespace Qt::StringLiterals;
24
25const QString QgsValueMapFieldFormatter::NULL_VALUE = u"{2839923C-8B7D-419E-B84B-CA2FE9B80EC7}"_s;
26
28{
29 setFlags( flags() | QgsFieldFormatter::CanProvideAvailableValues );
30}
31
33{
34 return u"ValueMap"_s;
35}
36
37QString QgsValueMapFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
38{
39 Q_UNUSED( cache )
40
41 QString valueInternalText;
42 if ( QgsVariantUtils::isNull( value ) )
43 valueInternalText = NULL_VALUE;
44 else
45 valueInternalText = value.toString();
46
47 const QVariant v = config.value( u"map"_s );
48 const QVariantList list = v.toList();
49 if ( !list.empty() )
50 {
51 for ( const QVariant &item : list )
52 {
53 const QVariantMap map = item.toMap();
54 // no built-in Qt way to check if a map contains a value, so iterate through each value
55 for ( auto it = map.constBegin(); it != map.constEnd(); ++it )
56 {
57 if ( it.value().toString() == valueInternalText )
58 return it.key();
59 }
60 }
61 return u"(%1)"_s.arg( layer->fields().at( fieldIndex ).displayString( value ) );
62 }
63 else
64 {
65 // old style config
66 const QVariantMap map = v.toMap();
67 return map.key( valueInternalText, QVariant( u"(%1)"_s.arg( layer->fields().at( fieldIndex ).displayString( value ) ) ).toString() );
68 }
69}
70
71QVariant QgsValueMapFieldFormatter::sortValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
72{
73 return representValue( layer, fieldIndex, config, cache, value );
74}
75
76QVariantList QgsValueMapFieldFormatter::availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext &context ) const
77{
78 Q_UNUSED( context )
79
80 QVariantList values;
81 const QList<QVariant> valueList = config.value( u"map"_s ).toList();
82 for ( const QVariant &item : valueList )
83 {
84 values.append( item.toMap().constBegin().value() );
85 if ( values.count() == countLimit )
86 break;
87 }
88
89 return values;
90}
A context for field formatter containing information like the project.
Flags flags() const
Returns the flags.
void setFlags(const Flags &flags)
Sets the flags.
QString displayString(const QVariant &v) const
Formats string for display.
Definition qgsfield.cpp:323
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
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...
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.