QGIS API Documentation 3.41.0-Master (d5b93354e9c)
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 "qgsvectorlayer.h"
19#include "qgsvariantutils.h"
20
21const QString QgsValueMapFieldFormatter::NULL_VALUE = QStringLiteral( "{2839923C-8B7D-419E-B84B-CA2FE9B80EC7}" );
22
24{
25 setFlags( flags() | QgsFieldFormatter::CanProvideAvailableValues );
26}
27
29{
30 return QStringLiteral( "ValueMap" );
31}
32
33QString QgsValueMapFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
34{
35 Q_UNUSED( cache )
36
37 QString valueInternalText;
38 if ( QgsVariantUtils::isNull( value ) )
39 valueInternalText = NULL_VALUE;
40 else
41 valueInternalText = value.toString();
42
43 const QVariant v = config.value( QStringLiteral( "map" ) );
44 const QVariantList list = v.toList();
45 if ( !list.empty() )
46 {
47 for ( const QVariant &item : list )
48 {
49 const QVariantMap map = item.toMap();
50 // no built-in Qt way to check if a map contains a value, so iterate through each value
51 for ( auto it = map.constBegin(); it != map.constEnd(); ++it )
52 {
53 if ( it.value().toString() == valueInternalText )
54 return it.key();
55 }
56 }
57 return QStringLiteral( "(%1)" ).arg( layer->fields().at( fieldIndex ).displayString( value ) );
58 }
59 else
60 {
61 // old style config
62 const QVariantMap map = v.toMap();
63 return map.key( valueInternalText, QVariant( QStringLiteral( "(%1)" ).arg( layer->fields().at( fieldIndex ).displayString( value ) ) ).toString() );
64 }
65}
66
67QVariant QgsValueMapFieldFormatter::sortValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
68{
69 return representValue( layer, fieldIndex, config, cache, value );
70}
71
72QVariantList QgsValueMapFieldFormatter::availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext &context ) const
73{
74 Q_UNUSED( context )
75
76 QVariantList values;
77 const QList<QVariant> valueList = config.value( QStringLiteral( "map" ) ).toList();
78 for ( const QVariant &item : valueList )
79 {
80 values.append( item.toMap().constBegin().value() );
81 if ( values.count() == countLimit )
82 break;
83 }
84
85 return values;
86}
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:317
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 data sets.