Skip to content

Commit

Permalink
Fix issue where boolean and NULL values are not properly saved/loaded…
Browse files Browse the repository at this point in the history
… with categorized renderer
  • Loading branch information
Djedouas committed Feb 15, 2024
1 parent c971a16 commit ebcb3e7
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/core/symbology/qgscategorizedsymbolrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,18 @@ QgsFeatureRenderer *QgsCategorizedSymbolRenderer::create( QDomElement &element,
return val;
}
}
else if ( valueType == QLatin1String( "bool" ) )
{
if ( value.toLower() == QStringLiteral( "false" ) )
return false;
if ( value.toLower() == QStringLiteral( "true" ) )
return true;
}
else if ( valueType == QLatin1String( "NULL" ) )
{
// This is the default ("fallback") category
return QVariant();
}
return value;
};

Expand All @@ -751,7 +763,6 @@ QgsFeatureRenderer *QgsCategorizedSymbolRenderer::create( QDomElement &element,
if ( catElem.hasAttribute( QStringLiteral( "value" ) ) )
{
value = valueFromString( catElem.attribute( QStringLiteral( "value" ) ), catElem.attribute( QStringLiteral( "type" ), QString() ) ) ;

}
else
{
Expand Down Expand Up @@ -855,7 +866,7 @@ QDomElement QgsCategorizedSymbolRenderer::save( QDomDocument &doc, const QgsRead
rendererElem.setAttribute( QStringLiteral( "attr" ), mAttrName );

// String for type
// We just need string and three numeric types: double, ulong and long for unsigned, signed and float/double
// We just need string, bool, and three numeric types: double, ulong and long for unsigned, signed and float/double
const auto stringForType = []( const QVariant::Type type ) -> QString
{
if ( type == QVariant::Char || type == QVariant::Int || type == QVariant::LongLong )
Expand All @@ -870,6 +881,10 @@ QDomElement QgsCategorizedSymbolRenderer::save( QDomDocument &doc, const QgsRead
{
return QStringLiteral( "double" ) ;
}
else if ( type == QVariant::Bool )
{
return QStringLiteral( "bool" );
}
else // Default: string
{
return QStringLiteral( "string" );
Expand Down Expand Up @@ -903,8 +918,17 @@ QDomElement QgsCategorizedSymbolRenderer::save( QDomDocument &doc, const QgsRead
}
else
{
catElem.setAttribute( QStringLiteral( "value" ), cat.value().toString() );
catElem.setAttribute( QStringLiteral( "type" ), stringForType( cat.value().type() ) );
if ( QgsVariantUtils::isNull( cat.value() ) )
{
// We need to save NULL value as specific kind, it is the default ("fallback") category
catElem.setAttribute( QStringLiteral( "value" ), "NULL" );
catElem.setAttribute( QStringLiteral( "type" ), "NULL" );
}
else
{
catElem.setAttribute( QStringLiteral( "value" ), cat.value().toString() );
catElem.setAttribute( QStringLiteral( "type" ), stringForType( cat.value().type() ) );
}
}
catElem.setAttribute( QStringLiteral( "symbol" ), symbolName );
catElem.setAttribute( QStringLiteral( "label" ), cat.label() );
Expand Down Expand Up @@ -1149,7 +1173,7 @@ QSet<QString> QgsCategorizedSymbolRenderer::legendKeysForFeature( const QgsFeatu
// Numeric NULL cat value is stored as an empty string
if ( QgsVariantUtils::isNull( value ) && ( value.type() == QVariant::Double || value.type() == QVariant::Int ||
value.type() == QVariant::UInt || value.type() == QVariant::LongLong ||
value.type() == QVariant::ULongLong ) )
value.type() == QVariant::ULongLong || value.type() == QVariant::Bool ) )
{
match = cat.value().toString().isEmpty();
}
Expand Down

0 comments on commit ebcb3e7

Please sign in to comment.