@@ -781,6 +781,8 @@ QString QgsIdentifyResultsDialog::representValue( QgsVectorLayer *vlayer, const
781
781
return fieldFormatter->representValue ( vlayer, idx, setup.config (), cache, value );
782
782
}
783
783
784
+
785
+ // Raster variant of addFeature
784
786
void QgsIdentifyResultsDialog::addFeature ( QgsRasterLayer *layer,
785
787
const QString &label,
786
788
const QMap<QString, QString> &attributes,
@@ -856,19 +858,71 @@ void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
856
858
if ( i >= fields.count () )
857
859
continue ;
858
860
859
- const auto value { attrs.at ( i ).toString () };
860
- QTreeWidgetItem *attrItem = new QTreeWidgetItem ( QStringList () << QString::number ( i ) << value );
861
- attrItem->setData ( 0 , Qt::DisplayRole, fields.at ( i ).name () );
862
- attrItem->setData ( 1 , Qt::DisplayRole, attrs.at ( i ) );
861
+ // We have no vector layer here (can't use the formatters), let's guess the format from the QVariant type
862
+ const auto value { attrs.at ( i ) };
863
+ auto formattedValue { value.toString () };
864
+ bool isString = false ;
865
+ if ( value.isValid ( ) )
866
+ {
867
+ if ( value.type () == QVariant::Double )
868
+ {
869
+ bool ok;
870
+ double val ( value.toDouble ( &ok ) );
871
+ if ( ok )
872
+ {
873
+ // Precision is not set, let's guess it from the
874
+ // standard conversion to string
875
+ const auto strVal { value.toString () };
876
+ int dotPosition { strVal.indexOf ( ' .' ) };
877
+ int precision;
878
+ if ( dotPosition < 0 )
879
+ {
880
+ precision = 0 ;
881
+ }
882
+ else
883
+ {
884
+ precision = strVal.length () - dotPosition - 1 ;
885
+ }
886
+ formattedValue = QLocale ().toString ( val, ' f' , precision );
887
+ }
888
+ }
889
+ else if ( value.type () == QVariant::Int )
890
+ {
891
+ bool ok;
892
+ double val ( value.toInt ( &ok ) );
893
+ if ( ok )
894
+ {
895
+ formattedValue = QLocale ().toString ( val, ' f' , 0 );
896
+ }
897
+ }
898
+ else if ( value.type () == QVariant::LongLong )
899
+ {
900
+ bool ok;
901
+ double val ( value.toLongLong ( &ok ) );
902
+ if ( ok )
903
+ {
904
+ formattedValue = QLocale ().toString ( val, ' f' , 0 );
905
+ }
906
+ }
907
+ else
908
+ {
909
+ isString = true ;
910
+ }
911
+ }
912
+ QTreeWidgetItem *attrItem = new QTreeWidgetItem ( { fields.at ( i ).name (), formattedValue } );
863
913
featItem->addChild ( attrItem );
864
- bool foundLinks = false ;
865
- const auto links { QgsStringUtils::insertLinks ( value, &foundLinks ) };
866
- if ( foundLinks )
914
+ // If not numeric, convert links
915
+ if ( isString )
867
916
{
868
- auto valueLabel { qgis::make_unique<QLabel>( links ) };
869
- attrItem->setText ( 1 , QString ( ) );
870
- valueLabel->setOpenExternalLinks ( true );
871
- lstResults->setItemWidget ( attrItem, 1 , valueLabel.release () );
917
+ bool foundLinks = false ;
918
+ const auto links { QgsStringUtils::insertLinks ( formattedValue, &foundLinks ) };
919
+ if ( foundLinks )
920
+ {
921
+ auto valueLabel { qgis::make_unique<QLabel>( links ) };
922
+ attrItem->setText ( 1 , QString ( ) );
923
+ valueLabel->setOpenExternalLinks ( true );
924
+ lstResults->setItemWidget ( attrItem, 1 , valueLabel.release () );
925
+ }
872
926
}
873
927
}
874
928
}
0 commit comments