Skip to content

Commit

Permalink
Raster label precision: round to power of 10
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso authored and nyalldawson committed Oct 6, 2020
1 parent 0d4dfc4 commit f6507d3
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 43 deletions.
42 changes: 42 additions & 0 deletions src/gui/qgsguiutils.cpp
Expand Up @@ -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 );
}

}

//
Expand Down
9 changes: 9 additions & 0 deletions src/gui/qgsguiutils.h
Expand Up @@ -20,6 +20,7 @@
#include <QWidget>
#include <QStringList>
#include "qgis_gui.h"
#include "qgis.h"

#define SIP_NO_FILE

Expand Down Expand Up @@ -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 );

}

/**
Expand Down
24 changes: 17 additions & 7 deletions src/gui/raster/qgscolorrampshaderwidget.cpp
Expand Up @@ -31,6 +31,7 @@
#include "qgscolordialog.h"
#include "qgsrasterrendererutils.h"
#include "qgsfileutils.h"
#include "qgsguiutils.h"

#include <QCursor>
#include <QPushButton>
Expand Down Expand Up @@ -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(); } );
}
Expand Down Expand Up @@ -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;
Expand Down
43 changes: 7 additions & 36 deletions src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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 );
}
2 changes: 2 additions & 0 deletions src/providers/postgres/raster/qgspostgresrasterutils.h
Expand Up @@ -16,8 +16,10 @@
#ifndef QGSPOSTGRESRASTERUTILS_H
#define QGSPOSTGRESRASTERUTILS_H

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


//! Raster utility functions
struct QgsPostgresRasterUtils
{
Expand Down

0 comments on commit f6507d3

Please sign in to comment.