Skip to content
Permalink
Browse files
When refining a rule based renderer rule by categories, copy the
category label as each rule label instead of the raw expression
  • Loading branch information
nyalldawson committed Jun 23, 2021
1 parent cc33a73 commit d29c6ad2c695a68cad9b7f15a6a664313d7f15aa
Showing with 13 additions and 4 deletions.
  1. +4 −3 src/core/symbology/qgsrulebasedrenderer.cpp
  2. +9 −1 tests/src/python/test_qgsrulebasedrenderer.py
@@ -1221,8 +1221,9 @@ void QgsRuleBasedRenderer::refineRuleCategories( QgsRuleBasedRenderer::Rule *ini
value = QString::number( cat.value().toDouble(), 'f', 4 );
else
value = QgsExpression::quotedString( cat.value().toString() );
QString filter = QStringLiteral( "%1 = %2" ).arg( attr, value );
QString label = filter;
const QString filter = QStringLiteral( "%1 = %2" ).arg( attr, value );
const QString label = !cat.label().isEmpty() ? cat.label() :
cat.value().isValid() ? value : QString();
initialRule->appendChild( new Rule( cat.symbol()->clone(), 0, 0, filter, label ) );
}
}
@@ -1254,7 +1255,7 @@ void QgsRuleBasedRenderer::refineRuleRanges( QgsRuleBasedRenderer::Rule *initial
QString::number( rng.lowerValue(), 'f', 4 ),
QString::number( rng.upperValue(), 'f', 4 ) );
firstRange = false;
QString label = rng.label().isEmpty() ? filter : rng.label();
QString label = rng.label().isEmpty() ? filter : rng.label();
initialRule->appendChild( new Rule( rng.symbol()->clone(), 0, 0, filter, label ) );
}
}
@@ -201,12 +201,16 @@ def testRefineWithCategories(self):
# First, try with a field based category (id)
cats = []
cats.append(QgsRendererCategory(1, QgsMarkerSymbol(), "id 1"))
cats.append(QgsRendererCategory(2, QgsMarkerSymbol(), "id 2"))
cats.append(QgsRendererCategory(2, QgsMarkerSymbol(), ''))
cats.append(QgsRendererCategory(None, QgsMarkerSymbol(), ''))
c = QgsCategorizedSymbolRenderer("id", cats)

QgsRuleBasedRenderer.refineRuleCategories(self.r2, c)
self.assertEqual(self.r2.children()[0].filterExpression(), '"id" = 1')
self.assertEqual(self.r2.children()[1].filterExpression(), '"id" = 2')
self.assertEqual(self.r2.children()[0].label(), 'id 1')
self.assertEqual(self.r2.children()[1].label(), '2')
self.assertEqual(self.r2.children()[2].label(), '')

# Next try with an expression based category
cats = []
@@ -217,6 +221,8 @@ def testRefineWithCategories(self):
QgsRuleBasedRenderer.refineRuleCategories(self.r1, c)
self.assertEqual(self.r1.children()[0].filterExpression(), 'id + 1 = 1')
self.assertEqual(self.r1.children()[1].filterExpression(), 'id + 1 = 2')
self.assertEqual(self.r1.children()[0].label(), 'result 1')
self.assertEqual(self.r1.children()[1].label(), 'result 2')

# Last try with an expression which is just a quoted field name
cats = []
@@ -227,6 +233,8 @@ def testRefineWithCategories(self):
QgsRuleBasedRenderer.refineRuleCategories(self.r3, c)
self.assertEqual(self.r3.children()[0].filterExpression(), '"id" = 1')
self.assertEqual(self.r3.children()[1].filterExpression(), '"id" = 2')
self.assertEqual(self.r3.children()[0].label(), 'result 1')
self.assertEqual(self.r3.children()[1].label(), 'result 2')

def testRefineWithRanges(self):
# Test refining rule with ranges (refs #10815)

0 comments on commit d29c6ad

Please sign in to comment.