Skip to content

Commit

Permalink
[bugfix] Allow precision config for doubles in range widget
Browse files Browse the repository at this point in the history
Fixes #17878 followup for PR #6185

This commit adds a configuration option for precision to
double field types in range widget.
  • Loading branch information
elpaso committed Jan 29, 2018
1 parent 998c67d commit c50e1bf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
18 changes: 12 additions & 6 deletions src/gui/editorwidgets/qgsrangeconfigdlg.cpp
Expand Up @@ -48,7 +48,9 @@ QgsRangeConfigDlg::QgsRangeConfigDlg( QgsVectorLayer *vl, int fieldIdx, QWidget

QString text;

switch ( vl->fields().at( fieldIdx ).type() )
QVariant::Type fieldType( vl->fields().at( fieldIdx ).type() );

switch ( fieldType )
{
case QVariant::Int:
case QVariant::LongLong:
Expand All @@ -73,10 +75,16 @@ QgsRangeConfigDlg::QgsRangeConfigDlg( QgsVectorLayer *vl, int fieldIdx, QWidget
break;
}

// Hide precision for integer types
if ( fieldType != QVariant::Double )
{
precisionSpinBox->hide();
precisionLabel->hide();
}

valuesLabel->setText( text );

connect( rangeWidget, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRangeConfigDlg::rangeWidgetChanged );

connect( minimumSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
connect( maximumSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
connect( stepSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
Expand Down Expand Up @@ -113,6 +121,7 @@ QVariantMap QgsRangeConfigDlg::config()

cfg.insert( QStringLiteral( "Style" ), rangeWidget->currentData().toString() );
cfg.insert( QStringLiteral( "AllowNull" ), allowNullCheckBox->isChecked() );
cfg.insert( QStringLiteral( "Precision" ), precisionSpinBox->value() );

if ( !suffixLineEdit->text().isEmpty() )
{
Expand All @@ -127,16 +136,13 @@ void QgsRangeConfigDlg::setConfig( const QVariantMap &config )
minimumDoubleSpinBox->setValue( config.value( QStringLiteral( "Min" ), std::numeric_limits<double>::lowest() ).toDouble( ) );
maximumDoubleSpinBox->setValue( config.value( QStringLiteral( "Max" ), std::numeric_limits<double>::max() ).toDouble( ) );
stepDoubleSpinBox->setValue( config.value( QStringLiteral( "Step" ), 1.0 ).toDouble() );

minimumSpinBox->setValue( config.value( QStringLiteral( "Min" ), std::numeric_limits<int>::lowest() ).toInt() );
maximumSpinBox->setValue( config.value( QStringLiteral( "Max" ), std::numeric_limits<int>::max() ).toInt() );
stepSpinBox->setValue( config.value( QStringLiteral( "Step" ), 1 ).toInt() );

rangeWidget->setCurrentIndex( rangeWidget->findData( config.value( QStringLiteral( "Style" ), "SpinBox" ) ) );

suffixLineEdit->setText( config.value( QStringLiteral( "Suffix" ) ).toString() );

allowNullCheckBox->setChecked( config.value( QStringLiteral( "AllowNull" ), true ).toBool() );
precisionSpinBox->setValue( config.value( QStringLiteral( "Precision" ), layer()->fields().at( field() ).precision() ).toInt( ) );
}

void QgsRangeConfigDlg::rangeWidgetChanged( int index )
Expand Down
20 changes: 9 additions & 11 deletions src/gui/editorwidgets/qgsrangewidgetwrapper.cpp
Expand Up @@ -85,31 +85,29 @@ void QgsRangeWidgetWrapper::initWidget( QWidget *editor )
QVariant min( config( QStringLiteral( "Min" ) ) );
QVariant max( config( QStringLiteral( "Max" ) ) );
QVariant step( config( QStringLiteral( "Step" ) ) );
QVariant precision( config( QStringLiteral( "Precision" ) ) );

if ( mDoubleSpinBox )
{
// set the precision if field is integer
int precision = layer()->fields().at( fieldIdx() ).precision();
if ( precision > 0 )
{
mDoubleSpinBox->setDecimals( layer()->fields().at( fieldIdx() ).precision() );
}

QgsDoubleSpinBox *qgsWidget = dynamic_cast<QgsDoubleSpinBox *>( mDoubleSpinBox );

double stepval = step.isValid() ? step.toDouble() : 1.0;
double minval = min.isValid() ? min.toDouble() : std::numeric_limits<double>::lowest();
double maxval = max.isValid() ? max.toDouble() : std::numeric_limits<double>::max();
int precisionval = precision.isValid() ? precision.toInt() : 4;

mDoubleSpinBox->setDecimals( precisionval );

QgsDoubleSpinBox *qgsWidget = dynamic_cast<QgsDoubleSpinBox *>( mDoubleSpinBox );


if ( qgsWidget )
qgsWidget->setShowClearButton( allowNull );
// Make room for null value: lower the minimum to allow for NULL special values
if ( allowNull )
{
double decr;
if ( precision > 0 )
if ( precisionval > 0 )
{
decr = std::pow( 10, -precision );
decr = std::pow( 10, -precisionval );
}
else
{
Expand Down
17 changes: 17 additions & 0 deletions src/ui/editorwidgets/qgsrangeconfigdlgbase.ui
Expand Up @@ -44,6 +44,23 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="precisionLabel">
<property name="text">
<string>Precision</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="precisionSpinBox">
<property name="toolTip">
<string>Number of decimal places</string>
</property>
<property name="value">
<number>4</number>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit c50e1bf

Please sign in to comment.