Skip to content
Permalink
Browse files

Add method to search variables in text with expressions

  • Loading branch information
pblottiere committed Apr 26, 2018
1 parent 201f108 commit 4168c05c3e01c726b76e928f3cafe4b6b942948f
@@ -369,6 +369,15 @@ Additional substitutions can be passed through the substitutionMap parameter
and area conversion

.. versionadded:: 2.12
%End

static QSet<QString> referencedVariables( const QString &text );
%Docstring
This function returns variables in each expression between [% and %].

:param text: The source string in which variables should be searched.

.. versionadded:: 3.2
%End

static double evaluateToDouble( const QString &text, const double fallbackValue );
@@ -477,6 +477,29 @@ QString QgsExpression::replaceExpressionText( const QString &action, const QgsEx
return expr_action;
}

QSet<QString> QgsExpression::referencedVariables( const QString &text )
{
QSet<QString> variables;
int index = 0;
while ( index < text.size() )
{
QRegExp rx = QRegExp( "\\[%([^\\]]+)%\\]" );

int pos = rx.indexIn( text, index );
if ( pos < 0 )
break;

int start = index;
index = pos + rx.matchedLength();
QString to_replace = rx.cap( 1 ).trimmed();

QgsExpression exp( to_replace );
variables.unite( exp.referencedVariables() );
}

return variables;
}

double QgsExpression::evaluateToDouble( const QString &text, const double fallbackValue )
{
bool ok;
@@ -400,6 +400,15 @@ class CORE_EXPORT QgsExpression
static QString replaceExpressionText( const QString &action, const QgsExpressionContext *context,
const QgsDistanceArea *distanceArea = nullptr );

/**
* This function returns variables in each expression between [% and %].
*
* \param text The source string in which variables should be searched.
*
* \since QGIS 3.2
*/
static QSet<QString> referencedVariables( const QString &text );

/**
* Attempts to evaluate a text string as an expression to a resultant double
* value.

0 comments on commit 4168c05

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