Skip to content
Permalink
Browse files
Improve color conversion if color is a part of an expression and add …
…unit test for it
  • Loading branch information
mhugent authored and nyalldawson committed Dec 23, 2021
1 parent 2018b40 commit cdac05edb6e3febdafe9106a66e56dd96f9fbe8b
Showing with 15 additions and 20 deletions.
  1. +13 −15 src/core/vectortile/qgsmapboxglstyleconverter.cpp
  2. +1 −5 src/core/vectortile/qgsmapboxglstyleconverter.h
  3. +1 −0 tests/src/python/test_qgsmapboxglconverter.py
@@ -2655,7 +2655,7 @@ QString QgsMapBoxGlStyleConverter::parseColorExpression( const QVariant &colorEx
{
return parseExpression( colorExpression.toList(), context, true );
}
return parseValue( colorExpression, context );
return parseValue( colorExpression, context, true );
}

QColor QgsMapBoxGlStyleConverter::parseColor( const QVariant &color, QgsMapBoxGlStyleConversionContext &context )
@@ -2910,9 +2910,9 @@ QString QgsMapBoxGlStyleConverter::parseExpression( const QVariantList &expressi
caseString += QStringLiteral( "WHEN (%1) " ).arg( QgsExpression::createFieldEqualityExpression( attribute, expression.at( i ) ) );
}

caseString += QStringLiteral( "THEN %1 " ).arg( colorExpected ? parseValueCheckColor( expression.at( i + 1 ), context ) : parseValue( expression.at( i + 1 ), context ) );
caseString += QStringLiteral( "THEN %1 " ).arg( parseValue( expression.at( i + 1 ), context, colorExpected ) );
}
caseString += QStringLiteral( "ELSE %1 END" ).arg( colorExpected ? parseValueCheckColor( expression.last(), context ) : parseValue( expression.last(), context ) );
caseString += QStringLiteral( "ELSE %1 END" ).arg( parseValue( expression.last(), context, colorExpected ) );
return caseString;
}
}
@@ -3143,17 +3143,25 @@ QString QgsMapBoxGlStyleConverter::retrieveSpriteAsBase64( const QVariant &value
return spritePath;
}

QString QgsMapBoxGlStyleConverter::parseValue( const QVariant &value, QgsMapBoxGlStyleConversionContext &context )
QString QgsMapBoxGlStyleConverter::parseValue( const QVariant &value, QgsMapBoxGlStyleConversionContext &context, bool colorExpected )
{
QColor c;
switch ( value.type() )
{
case QVariant::List:
case QVariant::StringList:
return parseExpression( value.toList(), context );
return parseExpression( value.toList(), context, colorExpected );

case QVariant::Bool:
case QVariant::String:
if ( colorExpected )
{
QColor c = parseColor( value, context );
if ( c.isValid() )
{
return parseValue( c, context );
}
}
return QgsExpression::quotedValue( value );

case QVariant::Int:
@@ -3171,16 +3179,6 @@ QString QgsMapBoxGlStyleConverter::parseValue( const QVariant &value, QgsMapBoxG
return QString();
}

QString QgsMapBoxGlStyleConverter::parseValueCheckColor( const QVariant &value, QgsMapBoxGlStyleConversionContext &context )
{
QColor c = parseColor( value, context );
if ( c.isValid() )
{
return parseValue( c, context );
}
return parseValue( value, context );
}

QString QgsMapBoxGlStyleConverter::parseKey( const QVariant &value, QgsMapBoxGlStyleConversionContext &context )
{
if ( value.toString() == QLatin1String( "$type" ) )
@@ -544,12 +544,8 @@ class CORE_EXPORT QgsMapBoxGlStyleConverter
QgsMapBoxGlStyleConverter( const QgsMapBoxGlStyleConverter &other );
#endif

static QString parseValue( const QVariant &value, QgsMapBoxGlStyleConversionContext &context );
static QString parseValue( const QVariant &value, QgsMapBoxGlStyleConversionContext &context, bool colorExpected = false );

/**
* Checks if value is a color before calling parseValue
*/
static QString parseValueCheckColor( const QVariant &value, QgsMapBoxGlStyleConversionContext &context );
static QString parseKey( const QVariant &value, QgsMapBoxGlStyleConversionContext &context );

/**
@@ -359,6 +359,7 @@ def testParseExpression(self):
self.assertEqual(QgsMapBoxGlStyleConverter.parseExpression(["%", 100, 20],
conversion_context),
'''100 % 20''')
self.assertEqual(QgsMapBoxGlStyleConverter.parseExpression(["match", ["get", "subclass"], "funicular", "rgba(243,243,246,0)", "rgb(243,243,246)"], conversion_context, True), '''CASE WHEN ("subclass" = 'funicular') THEN color_rgba(243,243,246,0) ELSE color_rgba(243,243,246,255) END''')

def testConvertLabels(self):
context = QgsMapBoxGlStyleConversionContext()

0 comments on commit cdac05e

Please sign in to comment.