Skip to content

Commit 1a90ec1

Browse files
committedApr 2, 2018
Fix value map field formatter doesn't work
The configuration format was changed without updating the unit tests to flag this failure. (cherry-picked from 4f16e01)
1 parent 9b8441e commit 1a90ec1

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
@@ -40,6 +40,7 @@ def test_representValue(self):
4040
fieldFormatter = QgsValueMapFieldFormatter()
4141

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

0 commit comments

Comments
 (0)
Please sign in to comment.