Skip to content

Commit a884448

Browse files
committed
Respect number formatting in identify raster results
1 parent c87833c commit a884448

File tree

1 file changed

+65
-11
lines changed

1 file changed

+65
-11
lines changed

src/app/qgsidentifyresultsdialog.cpp

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,8 @@ QString QgsIdentifyResultsDialog::representValue( QgsVectorLayer *vlayer, const
781781
return fieldFormatter->representValue( vlayer, idx, setup.config(), cache, value );
782782
}
783783

784+
785+
// Raster variant of addFeature
784786
void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
785787
const QString &label,
786788
const QMap<QString, QString> &attributes,
@@ -856,19 +858,71 @@ void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
856858
if ( i >= fields.count() )
857859
continue;
858860

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 } );
863913
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 )
867916
{
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+
}
872926
}
873927
}
874928
}

0 commit comments

Comments
 (0)