QGIS API Documentation  3.2.0-Bonn (bc43194)
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 {
24  return QStringLiteral( "ValueMap" );
25 }
26 
27 QString QgsValueMapFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
28 {
29  Q_UNUSED( cache )
30 
31  QString valueInternalText;
32  if ( value.isNull() )
33  valueInternalText = NULL_VALUE;
34  else
35  valueInternalText = value.toString();
36 
37  const QVariant v = config.value( QStringLiteral( "map" ) );
38  const QVariantList list = v.toList();
39  if ( !list.empty() )
40  {
41  for ( const QVariant &item : list )
42  {
43  const QVariantMap map = item.toMap();
44  // no built-in Qt way to check if a map contains a value, so iterate through each value
45  for ( auto it = map.constBegin(); it != map.constEnd(); ++it )
46  {
47  if ( it.value().toString() == valueInternalText )
48  return it.key();
49  }
50  }
51  return QStringLiteral( "(%1)" ).arg( layer->fields().at( fieldIndex ).displayString( value ) );
52  }
53  else
54  {
55  // old style config
56  QVariantMap map = v.toMap();
57  return map.key( valueInternalText, QVariant( QStringLiteral( "(%1)" ).arg( layer->fields().at( fieldIndex ).displayString( value ) ) ).toString() );
58  }
59 }
60 
61 QVariant QgsValueMapFieldFormatter::sortValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
62 {
63  return representValue( layer, fieldIndex, config, cache, value );
64 }
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:145
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.
QString id() const override
Returns a unique id for this field formatter.
QgsFields fields() const override
Returns the list of fields of this layer.
QString displayString(const QVariant &v) const
Formats string for display.
Definition: qgsfield.cpp:205
Represents a vector layer which manages a vector based data sets.
static const QString NULL_VALUE
Will be saved in the configuration when a value is NULL.
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...