Skip to content

Commit ca3d2c0

Browse files
committedMay 10, 2018
expression help: support optional parameters and defaults
(cherry picked from commit 968b50f)
1 parent 2c90bf2 commit ca3d2c0

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed
 

‎resources/function_help/json/closest_point

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "closest_point",
33
"type": "function",
4-
"description": "Returns the point on geometry 1 that is closest to geometry 2.",
4+
"description": "Returns the point on geometry1 that is closest to geometry2.",
55
"arguments": [
6-
{"arg":"geometry 1","description":"geometry to find closest point on"},
7-
{"arg":"geometry 2","description":"geometry to find closest point to"}
6+
{"arg":"geometry1","description":"geometry to find closest point on"},
7+
{"arg":"geometry2","description":"geometry to find closest point to"}
88
],
99
"examples": [
1010
{

‎resources/function_help/json/shortest_line

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "shortest_line",
33
"type": "function",
4-
"description": "Returns the shortest line joining geometry 1 to geometry 2. The resultant line will start at geometry 1 and end at geometry 2.",
4+
"description": "Returns the shortest line joining geometry1 to geometry2. The resultant line will start at geometry1 and end at geometry2.",
55
"arguments": [
6-
{"arg":"geometry 1","description":"geometry to find shortest line from"},
7-
{"arg":"geometry 2","description":"geometry to find shortest line to"}
6+
{"arg":"geometry1","description":"geometry to find shortest line from"},
7+
{"arg":"geometry2","description":"geometry to find shortest line to"}
88
],
99
"examples": [
1010
{

‎scripts/process_function_template.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,14 @@ def quote(v):
8787

8888
if 'arguments' in v:
8989
for a in v['arguments']:
90-
cpp.write("\n << HelpArg( \"{0}\", tr( \"{1}\" ), {2}, {3} )".format(
90+
cpp.write("\n << HelpArg( \"{0}\", tr( \"{1}\" ), {2}, {3}, {4}, \"{5}\" )".format(
9191
a['arg'],
9292
a.get('description', ''),
9393
"true" if a.get('descOnly', False) else "false",
9494
"true" if a.get('syntaxOnly', False) else "false",
9595
"true" if a.get('optional', False) else "false",
96-
a.get('default', ''))
96+
a.get('default', '')
97+
)
9798
)
9899

99100
cpp.write(",\n /* variableLenArguments */ {0}".format(

‎src/core/expression/qgsexpression.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -548,20 +548,33 @@ QString QgsExpression::helpText( QString name )
548548
{
549549
helpContents += QStringLiteral( "<code><span class=\"functionname\">%1</span>" ).arg( name );
550550

551+
bool hasOptionalArgs = false;
552+
551553
if ( f.mType == tr( "function" ) && ( f.mName[0] != '$' || !v.mArguments.isEmpty() || v.mVariableLenArguments ) )
552554
{
553555
helpContents += '(';
554556

555557
QString delim;
556558
for ( const HelpArg &a : qgis::as_const( v.mArguments ) )
557559
{
558-
helpContents += delim;
559-
delim = QStringLiteral( ", " );
560560
if ( !a.mDescOnly )
561561
{
562-
helpContents += QStringLiteral( "<span class=\"argument %1\">%2%3</span>" ).arg( a.mOptional ? QStringLiteral( "optional" ) : QStringLiteral( "" ), a.mArg,
563-
a.mDefaultVal.isEmpty() ? QLatin1String( "" ) : '=' + a.mDefaultVal );
562+
if ( a.mOptional )
563+
{
564+
hasOptionalArgs = true;
565+
helpContents += QStringLiteral( "[" );
566+
}
567+
568+
helpContents += delim;
569+
helpContents += QStringLiteral( "<span class=\"argument\">%2%3</span>" ).arg(
570+
a.mArg,
571+
a.mDefaultVal.isEmpty() ? QLatin1String( "" ) : '=' + a.mDefaultVal
572+
);
573+
574+
if ( a.mOptional )
575+
helpContents += QStringLiteral( "]" );
564576
}
577+
delim = QStringLiteral( "," );
565578
}
566579

567580
if ( v.mVariableLenArguments )
@@ -573,6 +586,11 @@ QString QgsExpression::helpText( QString name )
573586
}
574587

575588
helpContents += QLatin1String( "</code>" );
589+
590+
if ( hasOptionalArgs )
591+
{
592+
helpContents += QLatin1String( "<br/><br/>" ) + tr( "[ ] marks optional components" );
593+
}
576594
}
577595

578596
if ( !v.mArguments.isEmpty() )

0 commit comments

Comments
 (0)
Please sign in to comment.