QGIS API Documentation 4.3.0-Master (d3b565c628d)
Loading...
Searching...
No Matches
qgsenumerationfieldformatter.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsenumerationfieldformatter.cpp
3 ---------------------
4 begin : July 2026
5 copyright : (C) 2026 by Stefanos Natsis
6 email : uclaros at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
16
17#include "qgsapplication.h"
18#include "qgsvariantutils.h"
19#include "qgsvectorlayer.h"
20
21#include <QString>
22
23using namespace Qt::StringLiterals;
24
27
29{
30 return u"Enumeration"_s;
31}
32
33QString QgsEnumerationFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
34{
35 Q_UNUSED( layer )
36 Q_UNUSED( fieldIndex )
37 Q_UNUSED( config )
38
39 if ( QgsVariantUtils::isNull( value ) )
40 {
42 }
43
44 const QMap<QString, QString> map( cache.value<QMap<QString, QString>>() );
45
46 return map.value( value.toString() );
47}
48
49QVariant QgsEnumerationFieldFormatter::sortValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
50{
51 return representValue( layer, fieldIndex, config, cache, value );
52}
53
54QVariant QgsEnumerationFieldFormatter::createCache( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config ) const
55{
56 Q_UNUSED( config )
57
58 const QList<QPair<QString, QString>> values( layer->dataProvider()->codedValues( fieldIndex ) );
59 QMap<QString, QString> map;
60 for ( const QPair<QString, QString> &pair : values )
61 {
62 map[pair.first] = pair.second;
63 }
64
65 return QVariant::fromValue( map );
66}
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
QgsEnumerationFieldFormatter()
Default constructor of field formatter provider supplied enumerated values.
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.
QVariant createCache(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config) const override
Create a cache for a given field.
QString id() const override
Returns a unique id for this field formatter.
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 bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
virtual QList< QPair< QString, QString > > codedValues(int index) const
Returns the possible enum or coded values of an attribute in key-value pairs.
Represents a vector layer which manages a vector based dataset.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.