|
41 | 41 | #include "qgssymbol.h" |
42 | 42 | #include "qgssymbollayer.h" |
43 | 43 | #include "qgssymbollayerutils.h" |
| 44 | +#include "qgsvariantutils.h" |
44 | 45 | #include "qgsvectorlayer.h" |
45 | 46 |
|
46 | 47 | #include <QDomDocument> |
@@ -1050,122 +1051,7 @@ QgsLegendSymbolList QgsCategorizedSymbolRenderer::baseLegendSymbolItems() const |
1050 | 1051 |
|
1051 | 1052 | QString QgsCategorizedSymbolRenderer::displayString( const QVariant &v, int precision ) |
1052 | 1053 | { |
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 ); |
1169 | 1055 | } |
1170 | 1056 |
|
1171 | 1057 | QgsLegendSymbolList QgsCategorizedSymbolRenderer::legendSymbolItems() const |
@@ -1672,7 +1558,7 @@ QgsCategoryList QgsCategorizedSymbolRenderer::createCategories( const QList<QVar |
1672 | 1558 | if ( !QgsVariantUtils::isNull( value ) ) |
1673 | 1559 | { |
1674 | 1560 | const int fieldIdx = fields.lookupField( attributeName ); |
1675 | | - QString categoryName = displayString( value ); |
| 1561 | + QString categoryName = QgsVariantUtils::displayString( value ); |
1676 | 1562 | if ( fieldIdx != -1 ) |
1677 | 1563 | { |
1678 | 1564 | const QgsField field = fields.at( fieldIdx ); |
|
0 commit comments