Skip to content

Commit 9006d58

Browse files
committedApr 17, 2018
[processing] Accept strings of form "(1,2") for point parameters too
Since the help text above a point parameter indicates the acceptable format is "(x, y)", it can be confusing for users to know whether the brackets should be entered here. So be forgiving and accept strings with or without brackets. Refs https://gis.stackexchange.com/questions/278765/how-do-i-enter-start-point-in-shortest-path-algorithm-in-qgis-3-0-1 (cherry-picked from bf0c4f)
1 parent 655633a commit 9006d58

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed
 

‎src/core/processing/qgsprocessingparameters.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ QgsRectangle QgsProcessingParameters::parameterAsExtent( const QgsProcessingPara
533533
if ( rectText.isEmpty() && !layer )
534534
return QgsRectangle();
535535

536-
QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
536+
QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
537537
QRegularExpressionMatch match = rx.match( rectText );
538538
if ( match.hasMatch() )
539539
{
@@ -624,7 +624,7 @@ QgsGeometry QgsProcessingParameters::parameterAsExtentGeometry( const QgsProcess
624624

625625
if ( !rectText.isEmpty() )
626626
{
627-
QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
627+
QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
628628
QRegularExpressionMatch match = rx.match( rectText );
629629
if ( match.hasMatch() )
630630
{
@@ -702,7 +702,7 @@ QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsExtentCrs( cons
702702
}
703703
}
704704

705-
QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
705+
QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
706706

707707
QString valueAsString = parameterAsString( definition, parameters, context );
708708
QRegularExpressionMatch match = rx.match( valueAsString );
@@ -760,7 +760,7 @@ QgsPointXY QgsProcessingParameters::parameterAsPoint( const QgsProcessingParamet
760760
if ( pointText.isEmpty() )
761761
return QgsPointXY();
762762

763-
QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
763+
QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
764764

765765
QString valueAsString = parameterAsString( definition, parameters, context );
766766
QRegularExpressionMatch match = rx.match( valueAsString );
@@ -808,7 +808,7 @@ QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsPointCrs( const
808808
}
809809
}
810810

811-
QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
811+
QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
812812

813813
QString valueAsString = parameterAsString( definition, parameters, context );
814814
QRegularExpressionMatch match = rx.match( valueAsString );
@@ -1457,7 +1457,7 @@ bool QgsProcessingParameterExtent::checkValueIsAcceptable( const QVariant &input
14571457
return true;
14581458
}
14591459

1460-
QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
1460+
QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
14611461
QRegularExpressionMatch match = rx.match( input.toString() );
14621462
if ( match.hasMatch() )
14631463
{
@@ -1549,7 +1549,7 @@ bool QgsProcessingParameterPoint::checkValueIsAcceptable( const QVariant &input,
15491549
return mFlags & FlagOptional;
15501550
}
15511551

1552-
QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
1552+
QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
15531553

15541554
QRegularExpressionMatch match = rx.match( input.toString() );
15551555
if ( match.hasMatch() )

‎tests/src/analysis/testqgsprocessing.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,16 +2362,25 @@ void TestQgsProcessing::parameterPoint()
23622362
QVERIFY( !def->checkValueIsAcceptable( true ) );
23632363
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
23642364
QVERIFY( def->checkValueIsAcceptable( "1.1,2" ) );
2365+
QVERIFY( def->checkValueIsAcceptable( "(1.1,2)" ) );
23652366
QVERIFY( def->checkValueIsAcceptable( " 1.1, 2 " ) );
2367+
QVERIFY( def->checkValueIsAcceptable( " ( 1.1, 2 ) " ) );
23662368
QVERIFY( def->checkValueIsAcceptable( "-1.1,2" ) );
23672369
QVERIFY( def->checkValueIsAcceptable( "1.1,-2" ) );
23682370
QVERIFY( def->checkValueIsAcceptable( "-1.1,-2" ) );
2371+
QVERIFY( def->checkValueIsAcceptable( "(-1.1,-2)" ) );
23692372
QVERIFY( def->checkValueIsAcceptable( "1.1,2[EPSG:4326]" ) );
23702373
QVERIFY( def->checkValueIsAcceptable( "1.1,2 [EPSG:4326]" ) );
2374+
QVERIFY( def->checkValueIsAcceptable( "(1.1,2 [EPSG:4326] )" ) );
23712375
QVERIFY( def->checkValueIsAcceptable( " -1.1, -2 [EPSG:4326] " ) );
2376+
QVERIFY( def->checkValueIsAcceptable( " ( -1.1, -2 [EPSG:4326] ) " ) );
23722377
QVERIFY( !def->checkValueIsAcceptable( "1.1,a" ) );
2378+
QVERIFY( !def->checkValueIsAcceptable( "(1.1,a)" ) );
23732379
QVERIFY( !def->checkValueIsAcceptable( "layer12312312" ) );
2380+
QVERIFY( !def->checkValueIsAcceptable( "(layer12312312)" ) );
23742381
QVERIFY( !def->checkValueIsAcceptable( "" ) );
2382+
QVERIFY( !def->checkValueIsAcceptable( "()" ) );
2383+
QVERIFY( !def->checkValueIsAcceptable( " ( ) " ) );
23752384
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
23762385
QVERIFY( def->checkValueIsAcceptable( QgsPointXY( 1, 2 ) ) );
23772386
QVERIFY( def->checkValueIsAcceptable( QgsReferencedPointXY( QgsPointXY( 1, 2 ), QgsCoordinateReferenceSystem( "EPSG:4326" ) ) ) );
@@ -2389,6 +2398,17 @@ void TestQgsProcessing::parameterPoint()
23892398
QGSCOMPARENEAR( point.x(), 1.1, 0.001 );
23902399
QGSCOMPARENEAR( point.y(), 2.2, 0.001 );
23912400

2401+
// with optional brackets
2402+
params.insert( "non_optional", QString( "(1.1,2.2)" ) );
2403+
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context );
2404+
QGSCOMPARENEAR( point.x(), 1.1, 0.001 );
2405+
QGSCOMPARENEAR( point.y(), 2.2, 0.001 );
2406+
2407+
params.insert( "non_optional", QString( " ( -1.1 ,-2.2 ) " ) );
2408+
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context );
2409+
QGSCOMPARENEAR( point.x(), -1.1, 0.001 );
2410+
QGSCOMPARENEAR( point.y(), -2.2, 0.001 );
2411+
23922412
// with CRS as string
23932413
params.insert( "non_optional", QString( "1.1,2.2[EPSG:4326]" ) );
23942414
QCOMPARE( QgsProcessingParameters::parameterAsPointCrs( def.get(), params, context ).authid(), QStringLiteral( "EPSG:4326" ) );
@@ -2401,12 +2421,23 @@ void TestQgsProcessing::parameterPoint()
24012421
QGSCOMPARENEAR( point.x(), 122451, 100 );
24022422
QGSCOMPARENEAR( point.y(), 244963, 100 );
24032423

2424+
params.insert( "non_optional", QString( " ( 1.1,2.2 [EPSG:4326] ) " ) );
2425+
QCOMPARE( QgsProcessingParameters::parameterAsPointCrs( def.get(), params, context ).authid(), QStringLiteral( "EPSG:4326" ) );
2426+
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:3785" ) );
2427+
QGSCOMPARENEAR( point.x(), 122451, 100 );
2428+
QGSCOMPARENEAR( point.y(), 244963, 100 );
2429+
24042430
// nonsense string
24052431
params.insert( "non_optional", QString( "i'm not a crs, and nothing you can do will make me one" ) );
24062432
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context );
24072433
QCOMPARE( point.x(), 0.0 );
24082434
QCOMPARE( point.y(), 0.0 );
24092435

2436+
params.insert( "non_optional", QString( " ( ) " ) );
2437+
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context );
2438+
QCOMPARE( point.x(), 0.0 );
2439+
QCOMPARE( point.y(), 0.0 );
2440+
24102441
// QgsPointXY
24112442
params.insert( "non_optional", QgsPointXY( 11.1, 12.2 ) );
24122443
point = QgsProcessingParameters::parameterAsPoint( def.get(), params, context );

0 commit comments

Comments
 (0)
Please sign in to comment.