Skip to content

Commit 73293e8

Browse files
authored
Merge pull request #6209 from elpaso/double-range-precision
[bugfix] Allow precision config for doubles in range widget
2 parents bb6e275 + 63d063f commit 73293e8

File tree

4 files changed

+47
-32
lines changed

4 files changed

+47
-32
lines changed

src/gui/editorwidgets/qgsrangeconfigdlg.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ QgsRangeConfigDlg::QgsRangeConfigDlg( QgsVectorLayer *vl, int fieldIdx, QWidget
4848

4949
QString text;
5050

51-
switch ( vl->fields().at( fieldIdx ).type() )
51+
QVariant::Type fieldType( vl->fields().at( fieldIdx ).type() );
52+
53+
switch ( fieldType )
5254
{
5355
case QVariant::Int:
5456
case QVariant::LongLong:
@@ -73,10 +75,16 @@ QgsRangeConfigDlg::QgsRangeConfigDlg( QgsVectorLayer *vl, int fieldIdx, QWidget
7375
break;
7476
}
7577

78+
// Hide precision for integer types
79+
if ( fieldType != QVariant::Double )
80+
{
81+
precisionSpinBox->hide();
82+
precisionLabel->hide();
83+
}
84+
7685
valuesLabel->setText( text );
7786

7887
connect( rangeWidget, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRangeConfigDlg::rangeWidgetChanged );
79-
8088
connect( minimumSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
8189
connect( maximumSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
8290
connect( stepSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
@@ -113,6 +121,7 @@ QVariantMap QgsRangeConfigDlg::config()
113121

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

117126
if ( !suffixLineEdit->text().isEmpty() )
118127
{
@@ -127,16 +136,13 @@ void QgsRangeConfigDlg::setConfig( const QVariantMap &config )
127136
minimumDoubleSpinBox->setValue( config.value( QStringLiteral( "Min" ), std::numeric_limits<double>::lowest() ).toDouble( ) );
128137
maximumDoubleSpinBox->setValue( config.value( QStringLiteral( "Max" ), std::numeric_limits<double>::max() ).toDouble( ) );
129138
stepDoubleSpinBox->setValue( config.value( QStringLiteral( "Step" ), 1.0 ).toDouble() );
130-
131139
minimumSpinBox->setValue( config.value( QStringLiteral( "Min" ), std::numeric_limits<int>::lowest() ).toInt() );
132140
maximumSpinBox->setValue( config.value( QStringLiteral( "Max" ), std::numeric_limits<int>::max() ).toInt() );
133141
stepSpinBox->setValue( config.value( QStringLiteral( "Step" ), 1 ).toInt() );
134-
135142
rangeWidget->setCurrentIndex( rangeWidget->findData( config.value( QStringLiteral( "Style" ), "SpinBox" ) ) );
136-
137143
suffixLineEdit->setText( config.value( QStringLiteral( "Suffix" ) ).toString() );
138-
139144
allowNullCheckBox->setChecked( config.value( QStringLiteral( "AllowNull" ), true ).toBool() );
145+
precisionSpinBox->setValue( config.value( QStringLiteral( "Precision" ), layer()->fields().at( field() ).precision() ).toInt( ) );
140146
}
141147

142148
void QgsRangeConfigDlg::rangeWidgetChanged( int index )

src/gui/editorwidgets/qgsrangewidgetwrapper.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,29 @@ void QgsRangeWidgetWrapper::initWidget( QWidget *editor )
8585
QVariant min( config( QStringLiteral( "Min" ) ) );
8686
QVariant max( config( QStringLiteral( "Max" ) ) );
8787
QVariant step( config( QStringLiteral( "Step" ) ) );
88+
QVariant precision( config( QStringLiteral( "Precision" ) ) );
8889

8990
if ( mDoubleSpinBox )
9091
{
91-
// set the precision if field is integer
92-
int precision = layer()->fields().at( fieldIdx() ).precision();
93-
if ( precision > 0 )
94-
{
95-
mDoubleSpinBox->setDecimals( layer()->fields().at( fieldIdx() ).precision() );
96-
}
97-
98-
QgsDoubleSpinBox *qgsWidget = dynamic_cast<QgsDoubleSpinBox *>( mDoubleSpinBox );
99-
10092
double stepval = step.isValid() ? step.toDouble() : 1.0;
10193
double minval = min.isValid() ? min.toDouble() : std::numeric_limits<double>::lowest();
10294
double maxval = max.isValid() ? max.toDouble() : std::numeric_limits<double>::max();
95+
int precisionval = precision.isValid() ? precision.toInt() : layer()->fields().at( fieldIdx() ).precision();
96+
97+
mDoubleSpinBox->setDecimals( precisionval );
98+
99+
QgsDoubleSpinBox *qgsWidget = dynamic_cast<QgsDoubleSpinBox *>( mDoubleSpinBox );
100+
103101

104102
if ( qgsWidget )
105103
qgsWidget->setShowClearButton( allowNull );
106104
// Make room for null value: lower the minimum to allow for NULL special values
107105
if ( allowNull )
108106
{
109107
double decr;
110-
if ( precision > 0 )
108+
if ( precisionval > 0 )
111109
{
112-
decr = std::pow( 10, -precision );
110+
decr = std::pow( 10, -precisionval );
113111
}
114112
else
115113
{

src/ui/editorwidgets/qgsrangeconfigdlgbase.ui

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@
4444
</property>
4545
</widget>
4646
</item>
47+
<item row="1" column="0">
48+
<widget class="QLabel" name="precisionLabel">
49+
<property name="text">
50+
<string>Precision</string>
51+
</property>
52+
</widget>
53+
</item>
54+
<item row="1" column="1">
55+
<widget class="QSpinBox" name="precisionSpinBox">
56+
<property name="toolTip">
57+
<string>Number of decimal places</string>
58+
</property>
59+
<property name="value">
60+
<number>4</number>
61+
</property>
62+
</widget>
63+
</item>
4764
</layout>
4865
</widget>
4966
</item>

tests/src/gui/testqgsrangewidgetwrapper.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ void TestQgsRangeWidgetWrapper::init()
7474
// add fields
7575
QList<QgsField> fields;
7676
fields.append( QgsField( "id", QVariant::Int ) );
77-
// 9 precision
77+
// precision = 9
7878
QgsField dfield( "number", QVariant::Double );
7979
dfield.setPrecision( 9 );
8080
fields.append( dfield );
81-
// default precision
81+
// default precision = 0
8282
QgsField dfield2( "number_def", QVariant::Double );
8383
fields.append( dfield2 );
8484
vl->dataProvider()->addAttributes( fields );
@@ -137,17 +137,15 @@ void TestQgsRangeWidgetWrapper::test_setDoubleRange()
137137
widget2->setFeature( vl->getFeature( 1 ) );
138138
QCOMPARE( vl->fields().at( 1 ).precision(), 9 );
139139
// Default is 0 !!! for double, really ?
140-
// btw if it is 0 the decimals property in the spinbox is left to the default value of 2
141140
QCOMPARE( vl->fields().at( 2 ).precision(), 0 );
142141
QCOMPARE( editor->decimals(), vl->fields().at( 1 ).precision() );
143142
QCOMPARE( editor->decimals(), 9 );
144-
// This fails: QCOMPARE( editor2->decimals(), vl->fields().at( 2 ).precision() );
145-
QCOMPARE( editor2->decimals(), 2 ); // Default spinbox decimals is 2
143+
QCOMPARE( editor2->decimals(), vl->fields().at( 2 ).precision() );
146144
QCOMPARE( editor->valueFromText( feat.attribute( 1 ).toString() ), 123.123456789 );
147145
QCOMPARE( feat.attribute( 1 ).toString(), QStringLiteral( "123.123456789" ) );
148146
QCOMPARE( editor2->valueFromText( feat.attribute( 1 ).toString() ), 123.123456789 );
149147
QCOMPARE( editor->value( ), 123.123456789 );
150-
QCOMPARE( editor2->value( ), 123.12 );
148+
QCOMPARE( editor2->value( ), 123.0 );
151149
QCOMPARE( editor->minimum( ), std::numeric_limits<double>::lowest() );
152150
QCOMPARE( editor2->minimum( ), std::numeric_limits<double>::lowest() );
153151
QCOMPARE( editor->maximum( ), std::numeric_limits<double>::max() );
@@ -161,7 +159,7 @@ void TestQgsRangeWidgetWrapper::test_setDoubleRange()
161159
widget->setFeature( vl->getFeature( 3 ) );
162160
widget2->setFeature( vl->getFeature( 3 ) );
163161
QCOMPARE( editor->value( ), -123.123456789 );
164-
QCOMPARE( editor2->value( ), -123.12 );
162+
QCOMPARE( editor2->value( ), -123.0 );
165163
}
166164

167165
void TestQgsRangeWidgetWrapper::test_setDoubleSmallerRange()
@@ -189,11 +187,9 @@ void TestQgsRangeWidgetWrapper::test_setDoubleSmallerRange()
189187

190188
QCOMPARE( vl->fields().at( 1 ).precision(), 9 );
191189
// Default is 0 !!! for double, really ?
192-
// btw if it is 0 the decimals property in the spinbox is left to the default value of 2
193190
QCOMPARE( vl->fields().at( 2 ).precision(), 0 );
194191
QCOMPARE( editor->decimals(), vl->fields().at( 1 ).precision() );
195-
// This fails: QCOMPARE( editor2->decimals(), vl->fields().at( 2 ).precision() );
196-
QCOMPARE( editor2->decimals(), 2 ); // Default spinbox decimals is 2
192+
QCOMPARE( editor2->decimals(), vl->fields().at( 2 ).precision() );
197193
// value was changed to the maximum (not NULL) accepted value
198194
QCOMPARE( editor->value( ), 100.0 );
199195
// value was changed to the maximum (not NULL) accepted value
@@ -250,13 +246,11 @@ void TestQgsRangeWidgetWrapper::test_setDoubleLimits()
250246

251247
QCOMPARE( vl->fields().at( 1 ).precision(), 9 );
252248
// Default is 0 !!! for double, really ?
253-
// btw if it is 0 the decimals property in the spinbox is left to the default value of 2
254249
QCOMPARE( vl->fields().at( 2 ).precision(), 0 );
255250
QCOMPARE( editor->decimals(), vl->fields().at( 1 ).precision() );
256-
// This fails: QCOMPARE( editor2->decimals(), vl->fields().at( 2 ).precision() );
257-
QCOMPARE( editor2->decimals(), 2 ); // Default spinbox decimals is 2
251+
QCOMPARE( editor2->decimals(), vl->fields().at( 2 ).precision() );
258252
QCOMPARE( editor->value( ), 123.123456789 );
259-
QCOMPARE( editor2->value( ), 123.12 );
253+
QCOMPARE( editor2->value( ), 123.0 );
260254

261255
// NULL, NULL
262256
widget->setFeature( vl->getFeature( 2 ) );
@@ -269,7 +263,7 @@ void TestQgsRangeWidgetWrapper::test_setDoubleLimits()
269263
widget2->setFeature( vl->getFeature( 3 ) );
270264
// value was changed to the minimum
271265
QCOMPARE( editor->value( ), -123.123456789 );
272-
QCOMPARE( editor2->value( ), -123.12 );
266+
QCOMPARE( editor2->value( ), -123.0 );
273267

274268
}
275269

0 commit comments

Comments
 (0)