Skip to content
Permalink
Browse files

Raster label precision: round to power of 10

  • Loading branch information
elpaso authored and nyalldawson committed Oct 6, 2020
1 parent 0d4dfc4 commit f6507d341279f481c1c06c88e722582b3498f4e6
@@ -275,6 +275,48 @@ namespace QgsGuiUtils
}
return QSize( adjustedSize, adjustedSize );
}

QString displayValue( const Qgis::DataType rasterDataType, const double value )
{

int precision { 9 };
switch ( rasterDataType )
{
case Qgis::DataType::Int16:
case Qgis::DataType::UInt16:
{
precision = 5;
break;
}
case Qgis::DataType::Int32:
case Qgis::DataType::UInt32:
{
precision = 10;
break;
}
case Qgis::DataType::Byte:
{
precision = 3;
break;
}
case Qgis::DataType::Float32:
{
precision = 9;
break;
}
case Qgis::DataType::Float64:
{
precision = 17;
break;
}
default:
{
precision = 9;
}
}
return QLocale().toString( value, 'f', precision );
}

}

//
@@ -20,6 +20,7 @@
#include <QWidget>
#include <QStringList>
#include "qgis_gui.h"
#include "qgis.h"

#define SIP_NO_FILE

@@ -189,6 +190,14 @@ namespace QgsGuiUtils
* \since QGIS 3.8
*/
QSize GUI_EXPORT panelIconSize( QSize size );

/**
* Returns a localized string representation of the \a value with the appropriate number of
* decimals supported by the \a rasterDataType.
* \since QGIS 3.16
*/
QString displayValue( const Qgis::DataType rasterDataType, const double value );

}

/**
@@ -31,6 +31,7 @@
#include "qgscolordialog.h"
#include "qgsrasterrendererutils.h"
#include "qgsfileutils.h"
#include "qgsguiutils.h"

#include <QCursor>
#include <QPushButton>
@@ -101,10 +102,10 @@ QgsColorRampShaderWidget::QgsColorRampShaderWidget( QWidget *parent )
connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsColorRampShaderWidget::applyColorRamp );
connect( mNumberOfEntriesSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsColorRampShaderWidget::classify );
connect( mClipCheckBox, &QAbstractButton::toggled, this, &QgsColorRampShaderWidget::widgetChanged );
connect( mLabelPrecisionRawData, &QCheckBox::toggled, this, [ = ]
connect( mLabelPrecisionUseDefault, &QCheckBox::toggled, this, [ = ]
{
autoLabel();
mPrecisionSpinBox->setEnabled( ! mLabelPrecisionRawData->isChecked() );
mPrecisionSpinBox->setEnabled( ! mLabelPrecisionUseDefault->isChecked() );
} );
connect( mPrecisionSpinBox, qgis::overload<int>::of( &QSpinBox::valueChanged ), this, [ = ]( int ) { autoLabel(); } );
}
@@ -174,14 +175,23 @@ void QgsColorRampShaderWidget::autoLabel()
QString label;
int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();

auto applyPrecision = [ = ]( QString value )
auto applyPrecision = [ = ]( const QString & value )
{
// TODO: rounding to powers of 10
if ( ! mLabelPrecisionRawData->isChecked() )
double val { QLocale().toDouble( value ) };
if ( ! mLabelPrecisionUseDefault->isChecked() )
{
value = QLocale().toString( QLocale().toDouble( value ), 'f', mPrecisionSpinBox->value() );
if ( mPrecisionSpinBox->value() < 0 )
{
const double factor { std::pow( 10, - mPrecisionSpinBox->value() ) };
val = static_cast<qlonglong>( val / factor ) * factor;
return QLocale().toString( val, 'f', 0 );
}
return QLocale().toString( val, 'f', mPrecisionSpinBox->value() );
}
else
{
return QgsGuiUtils::displayValue( mRasterDataProvider->dataType( mBand ), val );
}
return value;
};

QTreeWidgetItem *currentItem = nullptr;
@@ -25,6 +25,7 @@
#include "qgstreewidgetitem.h"
#include "qgssettings.h"
#include "qgsmapcanvas.h"
#include "qgsguiutils.h"

// for color ramps - todo add rasterStyle and refactor raster vs. vector ramps
#include "qgsstyle.h"
@@ -280,43 +281,13 @@ void QgsSingleBandPseudoColorRendererWidget::minMaxModified()

QString QgsSingleBandPseudoColorRendererWidget::displayValue( const double value )
{
int precision { 9 };
if ( mRasterLayer->dataProvider() )
{
switch ( mRasterLayer->dataProvider()->dataType( mBandComboBox->currentBand() ) )
{
case Qgis::DataType::Int16:
case Qgis::DataType::UInt16:
{
precision = 5;
break;
}
case Qgis::DataType::Int32:
case Qgis::DataType::UInt32:
{
precision = 10;
break;
}
case Qgis::DataType::Byte:
{
precision = 3;
break;
}
case Qgis::DataType::Float32:
{
precision = 9;
break;
}
case Qgis::DataType::Float64:
{
precision = 17;
break;
}
default:
{
precision = 9;
}
}
return QgsGuiUtils::displayValue( mRasterLayer->dataProvider()->dataType( mBandComboBox->currentBand() ), value );
}
else
{
// Use QLocale default
return QLocale().toString( value, 'g' );
}
return QLocale().toString( value, 'g', precision );
}
@@ -16,8 +16,10 @@
#ifndef QGSPOSTGRESRASTERUTILS_H
#define QGSPOSTGRESRASTERUTILS_H

#include "qgis.h"
#include <QVariantMap>


//! Raster utility functions
struct QgsPostgresRasterUtils
{

0 comments on commit f6507d3

Please sign in to comment.
You can’t perform that action at this time.