Skip to content

Commit 11daa8c

Browse files
authored
Merge pull request qgis#63862 from ptitjano/variant-utils-display-string
Move displayString from qgscategorizedsymbolrenderer to qgsvariantutils
2 parents 3f59bf3 + 01f2f60 commit 11daa8c

File tree

13 files changed

+248
-124
lines changed

13 files changed

+248
-124
lines changed

python/PyQt6/core/auto_additions/qgsvariantutils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
QgsVariantUtils.typeToDisplayString = staticmethod(QgsVariantUtils.typeToDisplayString)
44
QgsVariantUtils.isNull = staticmethod(QgsVariantUtils.isNull)
55
QgsVariantUtils.isNumericType = staticmethod(QgsVariantUtils.isNumericType)
6+
QgsVariantUtils.displayString = staticmethod(QgsVariantUtils.displayString)
67
except (NameError, AttributeError):
78
pass

python/PyQt6/core/auto_generated/qgsvariantutils.sip.in

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ Returns ``True`` if the specified ``metaType`` is a numeric type.
6868

6969

7070

71+
static QString displayString( const QVariant &variant, int precision = -1 );
72+
%Docstring
73+
Returns a localized representation of ``value`` with the given
74+
``precision``, if precision is -1 then precision is guessed from the
75+
default QVariant.toString output.
76+
77+
The current localization settings are applied.
78+
79+
For strings, the input text is returned unchanged. For lists, a single
80+
string is generated with elements separated by ‘;’.
81+
82+
.. note::
83+
84+
Precision is ignored for integers.
85+
86+
.. versionadded:: 4.0
87+
%End
7188
};
7289

7390
/************************************************************************

python/PyQt6/core/auto_generated/symbology/qgscategorizedsymbolrenderer.sip.in

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ respective field configuration.
501501
.. versionadded:: 3.6
502502
%End
503503

504-
static QString displayString( const QVariant &value, int precision = -1 );
504+
static QString displayString( const QVariant &value, int precision = -1 ) /Deprecated="Since 4.0. Use QgsVariantUtils.displayString() instead."/;
505505
%Docstring
506506
Returns a localized representation of ``value`` with the given
507507
``precision``, if precision is -1 then precision is guessed from the
@@ -512,6 +512,10 @@ default QVariant.toString output.
512512
Precision is ignored for integers.
513513

514514
.. versionadded:: 3.22.1
515+
516+
.. deprecated:: 4.0
517+
518+
Use :py:func:`QgsVariantUtils.displayString()` instead.
515519
%End
516520

517521

python/core/auto_additions/qgsvariantutils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
QgsVariantUtils.typeToDisplayString = staticmethod(QgsVariantUtils.typeToDisplayString)
44
QgsVariantUtils.isNull = staticmethod(QgsVariantUtils.isNull)
55
QgsVariantUtils.isNumericType = staticmethod(QgsVariantUtils.isNumericType)
6+
QgsVariantUtils.displayString = staticmethod(QgsVariantUtils.displayString)
67
except (NameError, AttributeError):
78
pass

python/core/auto_generated/qgsvariantutils.sip.in

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ Returns ``True`` if the specified ``metaType`` is a numeric type.
6868

6969

7070

71+
static QString displayString( const QVariant &variant, int precision = -1 );
72+
%Docstring
73+
Returns a localized representation of ``value`` with the given
74+
``precision``, if precision is -1 then precision is guessed from the
75+
default QVariant.toString output.
76+
77+
The current localization settings are applied.
78+
79+
For strings, the input text is returned unchanged. For lists, a single
80+
string is generated with elements separated by ‘;’.
81+
82+
.. note::
83+
84+
Precision is ignored for integers.
85+
86+
.. versionadded:: 4.0
87+
%End
7188
};
7289

7390
/************************************************************************

python/core/auto_generated/symbology/qgscategorizedsymbolrenderer.sip.in

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ respective field configuration.
501501
.. versionadded:: 3.6
502502
%End
503503

504-
static QString displayString( const QVariant &value, int precision = -1 );
504+
static QString displayString( const QVariant &value, int precision = -1 ) /Deprecated="Since 4.0. Use QgsVariantUtils.displayString() instead."/;
505505
%Docstring
506506
Returns a localized representation of ``value`` with the given
507507
``precision``, if precision is -1 then precision is guessed from the
@@ -512,6 +512,10 @@ default QVariant.toString output.
512512
Precision is ignored for integers.
513513

514514
.. versionadded:: 3.22.1
515+
516+
.. deprecated:: 4.0
517+
518+
Use :py:func:`QgsVariantUtils.displayString()` instead.
515519
%End
516520

517521

src/core/qgsvariantutils.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgslogger.h"
1919
#include "qgsunsetattributevalue.h"
20+
#include "qgsapplication.h"
2021

2122
#include <QBitArray>
2223
#include <QBitmap>
@@ -633,3 +634,111 @@ QVariant QgsVariantUtils::createNullVariant( QMetaType::Type metaType )
633634
#endif
634635

635636
}
637+
638+
QString QgsVariantUtils::displayString( const QVariant &variant, int precision )
639+
{
640+
641+
auto _displayString = [ ]( const QVariant & variant, int precision ) -> QString
642+
{
643+
644+
if ( QgsVariantUtils::isNull( variant ) )
645+
{
646+
return QgsApplication::nullRepresentation();
647+
}
648+
649+
// Special treatment for numeric types if group separator is set or decimalPoint is not a dot
650+
if ( variant.userType() == QMetaType::Type::Double )
651+
{
652+
// Locales with decimal point != '.' or that require group separator: use QLocale
653+
if ( QLocale().decimalPoint() != '.' ||
654+
!( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
655+
{
656+
if ( precision > 0 )
657+
{
658+
if ( -1 < variant.toDouble() && variant.toDouble() < 1 )
659+
{
660+
return QLocale().toString( variant.toDouble(), 'g', precision );
661+
}
662+
else
663+
{
664+
return QLocale().toString( variant.toDouble(), 'f', precision );
665+
}
666+
}
667+
else
668+
{
669+
// Precision is not set, let's guess it from the
670+
// standard conversion to string
671+
const QString str( variant.toString() );
672+
const int dotPosition( static_cast<int>( str.indexOf( '.' ) ) );
673+
int precision;
674+
if ( dotPosition < 0 && str.indexOf( 'e', 0, Qt::CaseInsensitive ) < 0 )
675+
{
676+
precision = 0;
677+
return QLocale().toString( variant.toDouble(), 'f', precision );
678+
}
679+
else
680+
{
681+
if ( dotPosition < 0 ) precision = 0;
682+
else precision = static_cast<int>( str.length() ) - dotPosition - 1;
683+
684+
if ( -1 < variant.toDouble() && variant.toDouble() < 1 )
685+
{
686+
return QLocale().toString( variant.toDouble(), 'g', precision );
687+
}
688+
else
689+
{
690+
return QLocale().toString( variant.toDouble(), 'f', precision );
691+
}
692+
}
693+
}
694+
}
695+
// Default for doubles with precision
696+
else if ( precision > 0 )
697+
{
698+
if ( -1 < variant.toDouble() && variant.toDouble() < 1 )
699+
{
700+
return QString::number( variant.toDouble(), 'g', precision );
701+
}
702+
else
703+
{
704+
return QString::number( variant.toDouble(), 'f', precision );
705+
}
706+
}
707+
}
708+
// Other numeric types than doubles
709+
else if ( QgsVariantUtils::isNumericType( static_cast< QMetaType::Type >( variant.userType() ) ) &&
710+
!( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
711+
{
712+
bool ok;
713+
const qlonglong converted( variant.toLongLong( &ok ) );
714+
if ( ok )
715+
return QLocale().toString( converted );
716+
}
717+
else if ( variant.userType() == QMetaType::Type::QByteArray )
718+
{
719+
return QObject::tr( "BLOB" );
720+
}
721+
722+
// Fallback if special rules do not apply
723+
return variant.toString();
724+
};
725+
726+
if ( variant.userType() == QMetaType::Type::QStringList || variant.userType() == QMetaType::Type::QVariantList )
727+
{
728+
QString result;
729+
const QVariantList list = variant.toList();
730+
for ( const QVariant &var : list )
731+
{
732+
if ( !result.isEmpty() )
733+
{
734+
result.append( ';' );
735+
}
736+
result.append( _displayString( var, precision ) );
737+
}
738+
return result;
739+
}
740+
else
741+
{
742+
return _displayString( variant, precision );
743+
}
744+
}

src/core/qgsvariantutils.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ class CORE_EXPORT QgsVariantUtils
9999
*/
100100
static QVariant createNullVariant( QMetaType::Type metaType ) SIP_SKIP;
101101

102+
/**
103+
* Returns a localized representation of \a value with the given \a precision,
104+
* if precision is -1 then precision is guessed from the default QVariant::toString
105+
* output.
106+
*
107+
* The current localization settings are applied.
108+
*
109+
* For strings, the input text is returned unchanged.
110+
* For lists, a single string is generated with elements separated by ‘;’.
111+
*
112+
* \note Precision is ignored for integers.
113+
*
114+
* \since QGIS 4.0
115+
*/
116+
static QString displayString( const QVariant &variant, int precision = -1 );
102117
};
103118

104119
#endif // QGSVARIANTUTILS_H

src/core/symbology/qgscategorizedsymbolrenderer.cpp

Lines changed: 3 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "qgssymbol.h"
4242
#include "qgssymbollayer.h"
4343
#include "qgssymbollayerutils.h"
44+
#include "qgsvariantutils.h"
4445
#include "qgsvectorlayer.h"
4546

4647
#include <QDomDocument>
@@ -1050,122 +1051,7 @@ QgsLegendSymbolList QgsCategorizedSymbolRenderer::baseLegendSymbolItems() const
10501051

10511052
QString QgsCategorizedSymbolRenderer::displayString( const QVariant &v, int precision )
10521053
{
1053-
1054-
auto _displayString = [ ]( const QVariant & v, int precision ) -> QString
1055-
{
1056-
1057-
if ( QgsVariantUtils::isNull( v ) )
1058-
{
1059-
return QgsApplication::nullRepresentation();
1060-
}
1061-
1062-
const bool isNumeric {v.userType() == QMetaType::Type::Double || v.userType() == QMetaType::Type::Int || v.userType() == QMetaType::Type::UInt || v.userType() == QMetaType::Type::LongLong || v.userType() == QMetaType::Type::ULongLong};
1063-
1064-
// Special treatment for numeric types if group separator is set or decimalPoint is not a dot
1065-
if ( v.userType() == QMetaType::Type::Double )
1066-
{
1067-
// if value doesn't contain a double (a default value expression for instance),
1068-
// apply no transformation
1069-
bool ok;
1070-
v.toDouble( &ok );
1071-
if ( !ok )
1072-
return v.toString();
1073-
1074-
// Locales with decimal point != '.' or that require group separator: use QLocale
1075-
if ( QLocale().decimalPoint() != '.' ||
1076-
!( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
1077-
{
1078-
if ( precision > 0 )
1079-
{
1080-
if ( -1 < v.toDouble() && v.toDouble() < 1 )
1081-
{
1082-
return QLocale().toString( v.toDouble(), 'g', precision );
1083-
}
1084-
else
1085-
{
1086-
return QLocale().toString( v.toDouble(), 'f', precision );
1087-
}
1088-
}
1089-
else
1090-
{
1091-
// Precision is not set, let's guess it from the
1092-
// standard conversion to string
1093-
const QString s( v.toString() );
1094-
const int dotPosition( s.indexOf( '.' ) );
1095-
int precision;
1096-
if ( dotPosition < 0 && s.indexOf( 'e' ) < 0 )
1097-
{
1098-
precision = 0;
1099-
return QLocale().toString( v.toDouble(), 'f', precision );
1100-
}
1101-
else
1102-
{
1103-
if ( dotPosition < 0 ) precision = 0;
1104-
else precision = s.length() - dotPosition - 1;
1105-
1106-
if ( -1 < v.toDouble() && v.toDouble() < 1 )
1107-
{
1108-
return QLocale().toString( v.toDouble(), 'g', precision );
1109-
}
1110-
else
1111-
{
1112-
return QLocale().toString( v.toDouble(), 'f', precision );
1113-
}
1114-
}
1115-
}
1116-
}
1117-
// Default for doubles with precision
1118-
else if ( precision > 0 )
1119-
{
1120-
if ( -1 < v.toDouble() && v.toDouble() < 1 )
1121-
{
1122-
return QString::number( v.toDouble(), 'g', precision );
1123-
}
1124-
else
1125-
{
1126-
return QString::number( v.toDouble(), 'f', precision );
1127-
}
1128-
}
1129-
}
1130-
// Other numeric types than doubles
1131-
else if ( isNumeric &&
1132-
!( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
1133-
{
1134-
bool ok;
1135-
const qlonglong converted( v.toLongLong( &ok ) );
1136-
if ( ok )
1137-
return QLocale().toString( converted );
1138-
}
1139-
else if ( v.userType() == QMetaType::Type::QByteArray )
1140-
{
1141-
return QObject::tr( "BLOB" );
1142-
}
1143-
1144-
// Fallback if special rules do not apply
1145-
return v.toString();
1146-
};
1147-
1148-
if ( v.userType() == QMetaType::Type::QStringList || v.userType() == QMetaType::Type::QVariantList )
1149-
{
1150-
// Note that this code is never hit because the joining of lists (merged categories) happens
1151-
// in data(); I'm leaving this here anyway because it is tested and it may be useful for
1152-
// other purposes in the future.
1153-
QString result;
1154-
const QVariantList list = v.toList();
1155-
for ( const QVariant &var : list )
1156-
{
1157-
if ( !result.isEmpty() )
1158-
{
1159-
result.append( ';' );
1160-
}
1161-
result.append( _displayString( var, precision ) );
1162-
}
1163-
return result;
1164-
}
1165-
else
1166-
{
1167-
return _displayString( v, precision );
1168-
}
1054+
return QgsVariantUtils::displayString( v, precision );
11691055
}
11701056

11711057
QgsLegendSymbolList QgsCategorizedSymbolRenderer::legendSymbolItems() const
@@ -1672,7 +1558,7 @@ QgsCategoryList QgsCategorizedSymbolRenderer::createCategories( const QList<QVar
16721558
if ( !QgsVariantUtils::isNull( value ) )
16731559
{
16741560
const int fieldIdx = fields.lookupField( attributeName );
1675-
QString categoryName = displayString( value );
1561+
QString categoryName = QgsVariantUtils::displayString( value );
16761562
if ( fieldIdx != -1 )
16771563
{
16781564
const QgsField field = fields.at( fieldIdx );

src/core/symbology/qgscategorizedsymbolrenderer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,9 @@ class CORE_EXPORT QgsCategorizedSymbolRenderer : public QgsFeatureRenderer
469469
* \note Precision is ignored for integers.
470470
*
471471
* \since QGIS 3.22.1
472+
* \deprecated QGIS 4.0. Use QgsVariantUtils::displayString() instead.
472473
*/
473-
static QString displayString( const QVariant &value, int precision = -1 );
474+
Q_DECL_DEPRECATED static QString displayString( const QVariant &value, int precision = -1 ) SIP_DEPRECATED;
474475

475476

476477
protected:

0 commit comments

Comments
 (0)