Skip to content
Permalink
Browse files

Merge pull request #6185 from elpaso/bugfix-17878-range-widgets-nulls

[bugfix] Fix minimum values for range widgets
  • Loading branch information
elpaso committed Jan 29, 2018
2 parents d512611 + 741dddb commit a420d0410af0bb785b0ab059eaf8266ee7f57e89
@@ -22,17 +22,30 @@ QgsRangeConfigDlg::QgsRangeConfigDlg( QgsVectorLayer *vl, int fieldIdx, QWidget
{
setupUi( this );

minimumSpinBox->setMinimum( std::numeric_limits<int>::min() );
minimumSpinBox->setMinimum( std::numeric_limits<int>::lowest() );
minimumSpinBox->setMaximum( std::numeric_limits<int>::max() );
minimumSpinBox->setValue( std::numeric_limits<int>::min() );
minimumSpinBox->setValue( std::numeric_limits<int>::lowest() );

maximumSpinBox->setMinimum( std::numeric_limits<int>::min() );
maximumSpinBox->setMinimum( std::numeric_limits<int>::lowest() );
maximumSpinBox->setMaximum( std::numeric_limits<int>::max() );
maximumSpinBox->setValue( std::numeric_limits<int>::max() );

stepSpinBox->setMaximum( std::numeric_limits<int>::max() );
stepSpinBox->setValue( 1 );

minimumDoubleSpinBox->setMinimum( std::numeric_limits<double>::lowest() );
minimumDoubleSpinBox->setMaximum( std::numeric_limits<double>::max() );
minimumDoubleSpinBox->setValue( std::numeric_limits<double>::min() );

maximumDoubleSpinBox->setMinimum( std::numeric_limits<double>::lowest() );
maximumDoubleSpinBox->setMaximum( std::numeric_limits<double>::max() );
maximumDoubleSpinBox->setValue( std::numeric_limits<double>::max() );

// Use integer here:
stepDoubleSpinBox->setMaximum( std::numeric_limits<int>::max() );
stepDoubleSpinBox->setValue( 1 );


QString text;

switch ( vl->fields().at( fieldIdx ).type() )
@@ -44,9 +57,9 @@ QgsRangeConfigDlg::QgsRangeConfigDlg( QgsVectorLayer *vl, int fieldIdx, QWidget
rangeStackedWidget->setCurrentIndex( vl->fields().at( fieldIdx ).type() == QVariant::Double ? 1 : 0 );

rangeWidget->clear();
rangeWidget->addItem( tr( "Editable" ), "SpinBox" );
rangeWidget->addItem( tr( "Slider" ), "Slider" );
rangeWidget->addItem( tr( "Dial" ), "Dial" );
rangeWidget->addItem( tr( "Editable" ), QStringLiteral( "SpinBox" ) );
rangeWidget->addItem( tr( "Slider" ), QStringLiteral( "Slider" ) );
rangeWidget->addItem( tr( "Dial" ), QStringLiteral( "Dial" ) );

QVariant min = vl->minimumValue( fieldIdx );
QVariant max = vl->maximumValue( fieldIdx );
@@ -111,11 +124,11 @@ QVariantMap QgsRangeConfigDlg::config()

void QgsRangeConfigDlg::setConfig( const QVariantMap &config )
{
minimumDoubleSpinBox->setValue( config.value( QStringLiteral( "Min" ), -std::numeric_limits<double>::max() ).toDouble() );
maximumDoubleSpinBox->setValue( config.value( QStringLiteral( "Max" ), std::numeric_limits<double>::max() ).toDouble() );
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>::min() ).toInt() );
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() );

@@ -64,7 +64,7 @@ template<class T>
static void setupIntEditor( const QVariant &min, const QVariant &max, const QVariant &step, T *slider, QgsRangeWidgetWrapper *wrapper )
{
// must use a template function because those methods are overloaded and not inherited by some classes
slider->setMinimum( min.isValid() ? min.toInt() : std::numeric_limits<int>::min() );
slider->setMinimum( min.isValid() ? min.toInt() : std::numeric_limits<int>::lowest() );
slider->setMaximum( max.isValid() ? max.toInt() : std::numeric_limits<int>::max() );
slider->setSingleStep( step.isValid() ? step.toInt() : 1 );
QObject::connect( slider, SIGNAL( valueChanged( int ) ), wrapper, SLOT( emitValueChanged() ) );
@@ -95,27 +95,35 @@ void QgsRangeWidgetWrapper::initWidget( QWidget *editor )
mDoubleSpinBox->setDecimals( layer()->fields().at( fieldIdx() ).precision() );
}

double minval = min.toDouble();
double stepval = step.toDouble();
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();

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 )
{
minval -= 10 ^ -precision;
decr = std::pow( 10, -precision );
}
else
{
minval -= stepval;
decr = stepval;
}
minval -= decr;
// Note: call setMinimum here or setValue won't work
mDoubleSpinBox->setMinimum( minval );
mDoubleSpinBox->setValue( minval );
mDoubleSpinBox->setSpecialValueText( QgsApplication::nullRepresentation() );
}
mDoubleSpinBox->setMinimum( min.isValid() ? min.toDouble() : std::numeric_limits<double>::min() );
mDoubleSpinBox->setMaximum( max.isValid() ? max.toDouble() : std::numeric_limits<double>::max() );
mDoubleSpinBox->setSingleStep( step.isValid() ? step.toDouble() : 1.0 );
mDoubleSpinBox->setMinimum( minval );
mDoubleSpinBox->setMaximum( maxval );
mDoubleSpinBox->setSingleStep( stepval );
if ( config( QStringLiteral( "Suffix" ) ).isValid() )
mDoubleSpinBox->setSuffix( config( QStringLiteral( "Suffix" ) ).toString() );

@@ -29,6 +29,7 @@ class QSlider;
class QDial;
class QgsSlider;
class QgsDial;
class TestQgsRangeWidgetWrapper;

/**
* \ingroup gui
@@ -75,6 +76,8 @@ class GUI_EXPORT QgsRangeWidgetWrapper : public QgsEditorWidgetWrapper
QDial *mDial = nullptr;
QgsSlider *mQgsSlider = nullptr;
QgsDial *mQgsDial = nullptr;

friend class TestQgsRangeWidgetWrapper;
};

#endif // QGSRANGEWIDGETWRAPPER_H
@@ -76,7 +76,7 @@
<item row="2" column="0">
<widget class="QStackedWidget" name="rangeStackedWidget">
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<widget class="QWidget" name="intPage">
<layout class="QFormLayout" name="formLayout">
@@ -91,8 +91,7 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="minimumSpinBox">
</widget>
<widget class="QSpinBox" name="minimumSpinBox"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="maximumLabel">
@@ -102,8 +101,7 @@
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="maximumSpinBox">
</widget>
<widget class="QSpinBox" name="maximumSpinBox"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="stepLabel">
@@ -113,8 +111,7 @@
</widget>
</item>
<item row="2" column="1">
<widget class="QSpinBox" name="stepSpinBox">
</widget>
<widget class="QSpinBox" name="stepSpinBox"/>
</item>
</layout>
</widget>
@@ -145,36 +142,13 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QDoubleSpinBox" name="minimumDoubleSpinBox">
<property name="minimum">
<double>-1.79769e+308</double>
</property>
<property name="maximum">
<double>1.79769e+308</double>
</property>
<property name="value">
<double>-1.79769e+308</double>
</property>
</widget>
<widget class="QDoubleSpinBox" name="minimumDoubleSpinBox"/>
</item>
<item row="1" column="1">
<widget class="QDoubleSpinBox" name="maximumDoubleSpinBox">
<property name="minimum">
<double>-1.79769e+308</double>
</property>
<property name="maximum">
<double>1.79769e+308</double>
</property>
<property name="value">
<double>1.79769e+308</double>
</property>
</widget>
<widget class="QDoubleSpinBox" name="maximumDoubleSpinBox"/>
</item>
<item row="2" column="1">
<widget class="QDoubleSpinBox" name="stepDoubleSpinBox">
<property name="maximum">
<double>1.79769e+308</double>
</property>
<property name="value">
<double>1.000000000000000</double>
</property>
@@ -126,6 +126,7 @@ ADD_QGIS_TEST(qgsguitest testqgsgui.cpp)
ADD_QGIS_TEST(rubberbandtest testqgsrubberband.cpp)
ADD_QGIS_TEST(scalecombobox testqgsscalecombobox.cpp)
ADD_QGIS_TEST(scalerangewidget testqgsscalerangewidget.cpp)
ADD_QGIS_TEST(rangewidgetwrapper testqgsrangewidgetwrapper.cpp)
ADD_QGIS_TEST(spinbox testqgsspinbox.cpp)
ADD_QGIS_TEST(sqlcomposerdialog testqgssqlcomposerdialog.cpp)
ADD_QGIS_TEST(editorwidgetregistrytest testqgseditorwidgetregistry.cpp)

1 comment on commit a420d04

@nirvn

This comment has been minimized.

Copy link
Contributor

@nirvn nirvn commented on a420d04 Jan 29, 2018

Oups, forgot the [needs-docs] tag here, apologies.

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