Skip to content

Commit

Permalink
Remove checkbox for default precision
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso authored and nyalldawson committed Oct 6, 2020
1 parent ce3588b commit 37d8421
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 48 deletions.
38 changes: 18 additions & 20 deletions src/gui/qgsguiutils.cpp
Expand Up @@ -276,47 +276,45 @@ namespace QgsGuiUtils
return QSize( adjustedSize, adjustedSize );
}

QString displayValue( const Qgis::DataType rasterDataType, const double value )
QString displayValueWithMaximumDecimals( const Qgis::DataType rasterDataType, const double value )
{
const int precision { significantDigits( rasterDataType ) };
// Reduce
return QLocale().toString( value, 'f', precision );
}

int precision { 9 };
int significantDigits( const Qgis::DataType rasterDataType )
{
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:
case Qgis::DataType::CInt16:
case Qgis::DataType::CInt32:
case Qgis::DataType::ARGB32:
case Qgis::DataType::ARGB32_Premultiplied:
{
precision = 3;
break;
return 0;
}
case Qgis::DataType::Float32:
case Qgis::DataType::CFloat32:
{
precision = 9;
break;
return std::numeric_limits<float>::digits10 + 1;
}
case Qgis::DataType::Float64:
case Qgis::DataType::CFloat64:
{
precision = 17;
break;
return std::numeric_limits<double>::digits10 + 1;
}
default:
case Qgis::DataType::UnknownDataType:
{
precision = 9;
return std::numeric_limits<double>::digits10 + 1;
}
}
return QLocale().toString( value, 'f', precision );
}

}

//
Expand Down
11 changes: 10 additions & 1 deletion src/gui/qgsguiutils.h
Expand Up @@ -194,9 +194,18 @@ namespace QgsGuiUtils
/**
* Returns a localized string representation of the \a value with the appropriate number of
* decimals supported by the \a rasterDataType.
* Note that for floating point types the number of decimals may exceed the actual internal
* precision because the precision is always calculated on the mantissa and the conversion to
* string interprets the precision as decimal places.
* \since QGIS 3.16
*/
QString displayValue( const Qgis::DataType rasterDataType, const double value );
QString displayValueWithMaximumDecimals( const Qgis::DataType rasterDataType, const double value );

/**
* Returns the maximum number of significant digits a for the given \a rasterDataType.
* \since QGIS 3.16
*/
int significantDigits( const Qgis::DataType rasterDataType );

}

Expand Down
34 changes: 17 additions & 17 deletions src/gui/raster/qgscolorrampshaderwidget.cpp
Expand Up @@ -102,11 +102,6 @@ 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( mLabelPrecisionUseDefault, &QCheckBox::toggled, this, [ = ]
{
autoLabel();
mPrecisionSpinBox->setEnabled( ! mLabelPrecisionUseDefault->isChecked() );
} );
connect( mPrecisionSpinBox, qgis::overload<int>::of( &QSpinBox::valueChanged ), this, [ = ]( int ) { autoLabel(); } );
}

Expand All @@ -125,6 +120,18 @@ void QgsColorRampShaderWidget::setRasterDataProvider( QgsRasterDataProvider *dp
void QgsColorRampShaderWidget::setRasterBand( int band )
{
mBand = band;
// Set the maximum number of digits in the precision spin box
if ( mRasterDataProvider )
{
const int maxDigits { QgsGuiUtils::significantDigits( mRasterDataProvider->dataType( mBand ) ) };
// Get current value
const int currentPrecision { mPrecisionSpinBox->value() };
mPrecisionSpinBox->setMaximum( maxDigits );
if ( currentPrecision > maxDigits )
{
mPrecisionSpinBox->setValue( maxDigits );
}
}
}

void QgsColorRampShaderWidget::setExtent( const QgsRectangle &extent )
Expand Down Expand Up @@ -178,20 +185,13 @@ void QgsColorRampShaderWidget::autoLabel()
auto applyPrecision = [ = ]( const QString & value )
{
double val { QLocale().toDouble( value ) };
if ( ! mLabelPrecisionUseDefault->isChecked() )
{
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
if ( mPrecisionSpinBox->value() < 0 )
{
return QgsGuiUtils::displayValue( mRasterDataProvider->dataType( mBand ), val );
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() );
};

QTreeWidgetItem *currentItem = nullptr;
Expand Down
16 changes: 8 additions & 8 deletions src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp
Expand Up @@ -196,7 +196,7 @@ void QgsSingleBandPseudoColorRendererWidget::loadMinMax( int bandNo, double min,
}
else
{
whileBlocking( mMinLineEdit )->setText( displayValue( min ) );
whileBlocking( mMinLineEdit )->setText( displayValueWithMaxPrecision( min ) );
}

if ( std::isnan( max ) )
Expand All @@ -205,13 +205,13 @@ void QgsSingleBandPseudoColorRendererWidget::loadMinMax( int bandNo, double min,
}
else
{
whileBlocking( mMaxLineEdit )->setText( displayValue( max ) );
whileBlocking( mMaxLineEdit )->setText( displayValueWithMaxPrecision( max ) );
}

// We compare old min and new min as text because QString::number keeps a fixed number of significant
// digits (default 6) and so loaded min/max will always differ from current one, which triggers a
// classification, and wipe out every user modification (see https://github.com/qgis/QGIS/issues/36172)
if ( mMinLineEdit->text() != displayValue( min ) || mMaxLineEdit->text() != displayValue( max ) )
if ( mMinLineEdit->text() != displayValueWithMaxPrecision( min ) || mMaxLineEdit->text() != displayValueWithMaxPrecision( max ) )
{
whileBlocking( mColorRampShaderWidget )->setRasterBand( bandNo );
whileBlocking( mColorRampShaderWidget )->setMinimumMaximumAndClassify( min, max );
Expand All @@ -221,8 +221,8 @@ void QgsSingleBandPseudoColorRendererWidget::loadMinMax( int bandNo, double min,

void QgsSingleBandPseudoColorRendererWidget::loadMinMaxFromTree( double min, double max )
{
whileBlocking( mMinLineEdit )->setText( displayValue( min ) );
whileBlocking( mMaxLineEdit )->setText( displayValue( max ) );
whileBlocking( mMinLineEdit )->setText( displayValueWithMaxPrecision( min ) );
whileBlocking( mMaxLineEdit )->setText( displayValueWithMaxPrecision( max ) );
minMaxModified();
}

Expand All @@ -232,7 +232,7 @@ void QgsSingleBandPseudoColorRendererWidget::setLineEditValue( QLineEdit *lineEd
QString s;
if ( !std::isnan( value ) )
{
s = displayValue( value );
s = displayValueWithMaxPrecision( value );
}
lineEdit->setText( s );
}
Expand Down Expand Up @@ -279,11 +279,11 @@ void QgsSingleBandPseudoColorRendererWidget::minMaxModified()
mMinMaxWidget->userHasSetManualMinMaxValues();
}

QString QgsSingleBandPseudoColorRendererWidget::displayValue( const double value )
QString QgsSingleBandPseudoColorRendererWidget::displayValueWithMaxPrecision( const double value )
{
if ( mRasterLayer->dataProvider() )
{
return QgsGuiUtils::displayValue( mRasterLayer->dataProvider()->dataType( mBandComboBox->currentBand() ), value );
return QgsGuiUtils::displayValueWithMaximumDecimals( mRasterLayer->dataProvider()->dataType( mBandComboBox->currentBand() ), value );
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/gui/raster/qgssinglebandpseudocolorrendererwidget.h
Expand Up @@ -81,8 +81,8 @@ class GUI_EXPORT QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendere

void minMaxModified();

// Convert min/max to localized display value with correct precision
QString displayValue( const double value );
// Convert min/max to localized display value with maximum precision for the current data type
QString displayValueWithMaxPrecision( const double value );

};

Expand Down

0 comments on commit 37d8421

Please sign in to comment.