Skip to content

Commit f68dcc2

Browse files
committedNov 29, 2018
[processing] Allow parameter metadata to set the number of decimal places
to show in numeric/distance widgets E.g. to only show 2 decimal places: # only show two decimal places in parameter's widgets, not 6: param.setMetadata( {'widget_wrapper': { 'decimals': 2 } }) (cherry picked from commit d72c4d0)
1 parent 2e9f4a3 commit f68dcc2

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed
 

‎python/core/auto_generated/processing/qgsprocessingparameters.sip.in

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,17 @@ class QgsProcessingParameterNumber : QgsProcessingParameterDefinition
13641364
%Docstring
13651365
A numeric parameter for processing algorithms.
13661366

1367+
For numeric parameters with a dataType() of Double, the number of decimals places
1368+
shown in the parameter's widget can be specified by setting the parameter's metadata. For example:
1369+
1370+
.. code-block:: python
1371+
1372+
param = QgsProcessingParameterNumber( 'VAL', 'Threshold', type=QgsProcessingParameter.Double)
1373+
# only show two decimal places in parameter's widgets, not 6:
1374+
param.setMetadata( {'widget_wrapper':
1375+
{ 'decimals': 2 }
1376+
})
1377+
13671378
.. versionadded:: 3.0
13681379
%End
13691380

@@ -1463,6 +1474,17 @@ class QgsProcessingParameterDistance : QgsProcessingParameterNumber
14631474
A double numeric parameter for distance values. Linked to a source layer or CRS parameter
14641475
to determine what units the distance values are in.
14651476

1477+
The number of decimals places shown in a distance parameter's widget can be specified by
1478+
setting the parameter's metadata. For example:
1479+
1480+
.. code-block:: python
1481+
1482+
param = QgsProcessingParameterDistance( 'VAL', 'Threshold')
1483+
# only show two decimal places in parameter's widgets, not 6:
1484+
param.setMetadata( {'widget_wrapper':
1485+
{ 'decimals': 2 }
1486+
})
1487+
14661488
.. versionadded:: 3.2
14671489
%End
14681490

‎src/core/processing/qgsprocessingparameters.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,19 @@ class CORE_EXPORT QgsProcessingParameterMultipleLayers : public QgsProcessingPar
13591359
* \class QgsProcessingParameterNumber
13601360
* \ingroup core
13611361
* A numeric parameter for processing algorithms.
1362-
* \since QGIS 3.0
1362+
*
1363+
* For numeric parameters with a dataType() of Double, the number of decimals places
1364+
* shown in the parameter's widget can be specified by setting the parameter's metadata. For example:
1365+
*
1366+
* * \code{.py}
1367+
* param = QgsProcessingParameterNumber( 'VAL', 'Threshold', type=QgsProcessingParameter.Double)
1368+
* # only show two decimal places in parameter's widgets, not 6:
1369+
* param.setMetadata( {'widget_wrapper':
1370+
* { 'decimals': 2 }
1371+
* })
1372+
* \endcode
1373+
*
1374+
* \since QGIS 3.0
13631375
*/
13641376
class CORE_EXPORT QgsProcessingParameterNumber : public QgsProcessingParameterDefinition
13651377
{
@@ -1449,7 +1461,19 @@ class CORE_EXPORT QgsProcessingParameterNumber : public QgsProcessingParameterDe
14491461
* \ingroup core
14501462
* A double numeric parameter for distance values. Linked to a source layer or CRS parameter
14511463
* to determine what units the distance values are in.
1452-
* \since QGIS 3.2
1464+
*
1465+
* The number of decimals places shown in a distance parameter's widget can be specified by
1466+
* setting the parameter's metadata. For example:
1467+
*
1468+
* * \code{.py}
1469+
* param = QgsProcessingParameterDistance( 'VAL', 'Threshold')
1470+
* # only show two decimal places in parameter's widgets, not 6:
1471+
* param.setMetadata( {'widget_wrapper':
1472+
* { 'decimals': 2 }
1473+
* })
1474+
* \endcode
1475+
*
1476+
* \since QGIS 3.2
14531477
*/
14541478
class CORE_EXPORT QgsProcessingParameterDistance : public QgsProcessingParameterNumber
14551479
{

‎src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ QgsProcessingNumericWidgetWrapper::QgsProcessingNumericWidgetWrapper( const QgsP
420420
QWidget *QgsProcessingNumericWidgetWrapper::createWidget()
421421
{
422422
const QgsProcessingParameterNumber *numberDef = static_cast< const QgsProcessingParameterNumber * >( parameterDefinition() );
423+
const QVariantMap metadata = numberDef->metadata();
424+
const int decimals = metadata.value( QStringLiteral( "widget_wrapper" ) ).toMap().value( QStringLiteral( "decimals" ), 6 ).toInt();
423425
switch ( type() )
424426
{
425427
case QgsProcessingGui::Standard:
@@ -433,13 +435,14 @@ QWidget *QgsProcessingNumericWidgetWrapper::createWidget()
433435
case QgsProcessingParameterNumber::Double:
434436
mDoubleSpinBox = new QgsDoubleSpinBox();
435437
mDoubleSpinBox->setExpressionsEnabled( true );
436-
mDoubleSpinBox->setDecimals( 6 );
438+
mDoubleSpinBox->setDecimals( decimals );
437439

438440
// guess reasonable step value for double spin boxes
439441
if ( !qgsDoubleNear( numberDef->maximum(), std::numeric_limits<double>::max() ) &&
440442
!qgsDoubleNear( numberDef->minimum(), std::numeric_limits<double>::lowest() + 1 ) )
441443
{
442-
const double singleStep = calculateStep( numberDef->minimum(), numberDef->maximum() );
444+
double singleStep = calculateStep( numberDef->minimum(), numberDef->maximum() );
445+
singleStep = std::max( singleStep, std::pow( 10, -decimals ) );
443446
mDoubleSpinBox->setSingleStep( singleStep );
444447
}
445448

‎tests/src/gui/testprocessinggui.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,19 @@ void TestProcessingGui::testNumericWrapperDouble()
10051005
QCOMPARE( wrapperOptionalDefault.parameterValue().toDouble(), 5.0 );
10061006

10071007
delete w;
1008+
1009+
// with decimals
1010+
QgsProcessingParameterNumber paramDecimals( QStringLiteral( "num" ), QStringLiteral( "num" ), QgsProcessingParameterNumber::Double, QVariant(), true, 1, 1.02 );
1011+
QVariantMap metadata;
1012+
QVariantMap wrapperMetadata;
1013+
wrapperMetadata.insert( QStringLiteral( "decimals" ), 2 );
1014+
metadata.insert( QStringLiteral( "widget_wrapper" ), wrapperMetadata );
1015+
paramDecimals.setMetadata( metadata );
1016+
QgsProcessingNumericWidgetWrapper wrapperDecimals( &paramDecimals, type );
1017+
w = wrapperDecimals.createWrappedWidget( context );
1018+
QCOMPARE( static_cast< QgsDoubleSpinBox * >( wrapperDecimals.wrappedWidget() )->decimals(), 2 );
1019+
QCOMPARE( static_cast< QgsDoubleSpinBox * >( wrapperDecimals.wrappedWidget() )->singleStep(), 0.01 ); // single step should never be less than set number of decimals
1020+
delete w;
10081021
};
10091022

10101023
// standard wrapper
@@ -1280,6 +1293,19 @@ void TestProcessingGui::testDistanceWrapper()
12801293

12811294
delete w;
12821295

1296+
// with decimals
1297+
QgsProcessingParameterDistance paramDecimals( QStringLiteral( "num" ), QStringLiteral( "num" ), QVariant(), QString(), true, 1, 1.02 );
1298+
QVariantMap metadata;
1299+
QVariantMap wrapperMetadata;
1300+
wrapperMetadata.insert( QStringLiteral( "decimals" ), 2 );
1301+
metadata.insert( QStringLiteral( "widget_wrapper" ), wrapperMetadata );
1302+
paramDecimals.setMetadata( metadata );
1303+
QgsProcessingDistanceWidgetWrapper wrapperDecimals( &paramDecimals, QgsProcessingGui::Standard );
1304+
w = wrapperDecimals.createWrappedWidget( context );
1305+
QCOMPARE( wrapperDecimals.mDoubleSpinBox->decimals(), 2 );
1306+
QCOMPARE( wrapperDecimals.mDoubleSpinBox->singleStep(), 0.01 ); // single step should never be less than set number of decimals
1307+
delete w;
1308+
12831309
// batch wrapper
12841310
QgsProcessingDistanceWidgetWrapper wrapperB( &param, QgsProcessingGui::Batch );
12851311

0 commit comments

Comments
 (0)