Skip to content

Commit 0d4dfc4

Browse files
elpasonyalldawson
authored andcommittedOct 6, 2020
Raster style label precision
1 parent 982e4ea commit 0d4dfc4

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed
 

‎src/gui/raster/qgscolorrampshaderwidget.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ QgsColorRampShaderWidget::QgsColorRampShaderWidget( QWidget *parent )
101101
connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsColorRampShaderWidget::applyColorRamp );
102102
connect( mNumberOfEntriesSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsColorRampShaderWidget::classify );
103103
connect( mClipCheckBox, &QAbstractButton::toggled, this, &QgsColorRampShaderWidget::widgetChanged );
104+
connect( mLabelPrecisionRawData, &QCheckBox::toggled, this, [ = ]
105+
{
106+
autoLabel();
107+
mPrecisionSpinBox->setEnabled( ! mLabelPrecisionRawData->isChecked() );
108+
} );
109+
connect( mPrecisionSpinBox, qgis::overload<int>::of( &QSpinBox::valueChanged ), this, [ = ]( int ) { autoLabel(); } );
104110
}
105111

106112
void QgsColorRampShaderWidget::initializeForUseWithRasterLayer()
@@ -167,6 +173,17 @@ void QgsColorRampShaderWidget::autoLabel()
167173
QString unit = mUnitLineEdit->text();
168174
QString label;
169175
int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
176+
177+
auto applyPrecision = [ = ]( QString value )
178+
{
179+
// TODO: rounding to powers of 10
180+
if ( ! mLabelPrecisionRawData->isChecked() )
181+
{
182+
value = QLocale().toString( QLocale().toDouble( value ), 'f', mPrecisionSpinBox->value() );
183+
}
184+
return value;
185+
};
186+
170187
QTreeWidgetItem *currentItem = nullptr;
171188
for ( int i = 0; i < topLevelItemCount; ++i )
172189
{
@@ -181,20 +198,20 @@ void QgsColorRampShaderWidget::autoLabel()
181198
{
182199
if ( i == 0 )
183200
{
184-
label = "<= " + currentItem->text( ValueColumn ) + unit;
201+
label = "<= " + applyPrecision( currentItem->text( ValueColumn ) ) + unit;
185202
}
186203
else if ( QLocale().toDouble( currentItem->text( ValueColumn ) ) == std::numeric_limits<double>::infinity() )
187204
{
188-
label = "> " + mColormapTreeWidget->topLevelItem( i - 1 )->text( ValueColumn ) + unit;
205+
label = "> " + applyPrecision( mColormapTreeWidget->topLevelItem( i - 1 )->text( ValueColumn ) ) + unit;
189206
}
190207
else
191208
{
192-
label = mColormapTreeWidget->topLevelItem( i - 1 )->text( ValueColumn ) + " - " + currentItem->text( ValueColumn ) + unit;
209+
label = applyPrecision( mColormapTreeWidget->topLevelItem( i - 1 )->text( ValueColumn ) ) + " - " + applyPrecision( currentItem->text( ValueColumn ) ) + unit;
193210
}
194211
}
195212
else
196213
{
197-
label = currentItem->text( ValueColumn ) + unit;
214+
label = applyPrecision( currentItem->text( ValueColumn ) ) + unit;
198215
}
199216

200217
if ( currentItem->text( LabelColumn ).isEmpty() || currentItem->text( LabelColumn ) == label || currentItem->foreground( LabelColumn ).color() == QColor( Qt::gray ) )

0 commit comments

Comments
 (0)
Please sign in to comment.