Skip to content

Commit 4f16e01

Browse files
committedMar 26, 2018
Fix value map field formatter doesn't work
The configuration format was changed without updating the unit tests to flag this failure.
1 parent ce34646 commit 4f16e01

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed
 

‎src/core/fieldformatter/qgsvaluemapfieldformatter.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,28 @@ QString QgsValueMapFieldFormatter::representValue( QgsVectorLayer *layer, int fi
3434
else
3535
valueInternalText = value.toString();
3636

37-
QVariantMap map = config.value( QStringLiteral( "map" ) ).toMap();
38-
39-
return map.key( valueInternalText, QVariant( QStringLiteral( "(%1)" ).arg( layer->fields().at( fieldIndex ).displayString( value ) ) ).toString() );
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+
}
4059
}
4160

4261
QVariant QgsValueMapFieldFormatter::sortValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const

‎tests/src/python/test_qgsfieldformatters.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def test_representValue(self):
3939
fieldFormatter = QgsValueMapFieldFormatter()
4040

4141
# Tests with different value types occurring in the value map
42+
# old style config (pre 3.0)
4243
config = {'map': {'two': '2', 'twoandhalf': '2.5', 'NULL text': 'NULL',
4344
'nothing': self.VALUEMAP_NULL_TEXT}}
4445
self.assertEqual(fieldFormatter.representValue(layer, 0, config, None, 2), 'two')
@@ -48,6 +49,20 @@ def test_representValue(self):
4849
self.assertEqual(fieldFormatter.representValue(layer, 3, config, None, None), 'nothing')
4950
self.assertEqual(fieldFormatter.representValue(layer, 4, config, None, None), 'nothing')
5051
self.assertEqual(fieldFormatter.representValue(layer, 5, config, None, None), 'nothing')
52+
53+
# new style config (post 3.0)
54+
config = {'map': [{'two': '2'},
55+
{'twoandhalf': '2.5'},
56+
{'NULL text': 'NULL'},
57+
{'nothing': self.VALUEMAP_NULL_TEXT}]}
58+
self.assertEqual(fieldFormatter.representValue(layer, 0, config, None, 2), 'two')
59+
self.assertEqual(fieldFormatter.representValue(layer, 1, config, None, 2.5), 'twoandhalf')
60+
self.assertEqual(fieldFormatter.representValue(layer, 2, config, None, 'NULL'), 'NULL text')
61+
# Tests with null values of different types, if value map contains null
62+
self.assertEqual(fieldFormatter.representValue(layer, 3, config, None, None), 'nothing')
63+
self.assertEqual(fieldFormatter.representValue(layer, 4, config, None, None), 'nothing')
64+
self.assertEqual(fieldFormatter.representValue(layer, 5, config, None, None), 'nothing')
65+
5166
# Tests with fallback display for different value types
5267
config = {}
5368
self.assertEqual(fieldFormatter.representValue(layer, 0, config, None, 2), '(2)')

0 commit comments

Comments
 (0)
Please sign in to comment.