mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-19 00:04:52 -04:00
Code dedup
This commit is contained in:
parent
66d562a336
commit
eb8eb59680
@ -4302,20 +4302,17 @@ bool QgsWithVariableExpressionFunction::isStatic( const QgsExpressionNodeFunctio
|
|||||||
if ( args->count() < 3 )
|
if ( args->count() < 3 )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
// We only need to check if the node evaluation is static, if both - name and value - are static.
|
||||||
if ( args->at( 0 )->isStatic( parent, context ) && args->at( 1 )->isStatic( parent, context ) )
|
if ( args->at( 0 )->isStatic( parent, context ) && args->at( 1 )->isStatic( parent, context ) )
|
||||||
{
|
{
|
||||||
QVariant name = args->at( 0 )->eval( parent, context );
|
QVariant name = args->at( 0 )->eval( parent, context );
|
||||||
QVariant value = args->at( 1 )->eval( parent, context );
|
QVariant value = args->at( 1 )->eval( parent, context );
|
||||||
|
|
||||||
QgsExpressionContextScope *scope = new QgsExpressionContextScope();
|
// Temporarily append a new scope to provide the variable
|
||||||
scope->setVariable( name.toString(), value );
|
appendTemporaryVariable( context, name.toString(), value );
|
||||||
|
if ( args->at( 2 )->isStatic( parent, context ) )
|
||||||
QgsExpressionContext *updatedContext = const_cast<QgsExpressionContext *>( context );
|
|
||||||
updatedContext->appendScope( scope );
|
|
||||||
|
|
||||||
if ( args->at( 2 )->isStatic( parent, updatedContext ) )
|
|
||||||
isStatic = true;
|
isStatic = true;
|
||||||
delete updatedContext->popScope();
|
popTemporaryVariable( context );
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -4332,15 +4329,13 @@ QVariant QgsWithVariableExpressionFunction::run( QgsExpressionNode::NodeList *ar
|
|||||||
QVariant name = args->at( 0 )->eval( parent, context );
|
QVariant name = args->at( 0 )->eval( parent, context );
|
||||||
QVariant value = args->at( 1 )->eval( parent, context );
|
QVariant value = args->at( 1 )->eval( parent, context );
|
||||||
|
|
||||||
QgsExpressionContextScope *scope = new QgsExpressionContextScope();
|
|
||||||
scope->setVariable( name.toString(), value );
|
|
||||||
|
|
||||||
QgsExpressionContext *updatedContext = const_cast<QgsExpressionContext *>( context );
|
QgsExpressionContext *updatedContext = const_cast<QgsExpressionContext *>( context );
|
||||||
if ( !context )
|
if ( !context )
|
||||||
updatedContext = new QgsExpressionContext();
|
updatedContext = new QgsExpressionContext();
|
||||||
updatedContext->appendScope( scope );
|
|
||||||
|
appendTemporaryVariable( updatedContext, name.toString(), value );
|
||||||
result = args->at( 2 )->eval( parent, updatedContext );
|
result = args->at( 2 )->eval( parent, updatedContext );
|
||||||
delete updatedContext->popScope();
|
popTemporaryVariable( updatedContext );
|
||||||
if ( !context )
|
if ( !context )
|
||||||
delete updatedContext;
|
delete updatedContext;
|
||||||
|
|
||||||
@ -4369,13 +4364,24 @@ bool QgsWithVariableExpressionFunction::prepare( const QgsExpressionNodeFunction
|
|||||||
QVariant name = args->at( 0 )->prepare( parent, context );
|
QVariant name = args->at( 0 )->prepare( parent, context );
|
||||||
QVariant value = args->at( 1 )->prepare( parent, context );
|
QVariant value = args->at( 1 )->prepare( parent, context );
|
||||||
|
|
||||||
QgsExpressionContextScope *scope = new QgsExpressionContextScope();
|
appendTemporaryVariable( context, name.toString(), value );
|
||||||
scope->setVariable( name.toString(), value );
|
args->at( 2 )->prepare( parent, context );
|
||||||
|
popTemporaryVariable( context );
|
||||||
QgsExpressionContext *updatedContext = const_cast<QgsExpressionContext *>( context );
|
|
||||||
updatedContext->appendScope( scope );
|
|
||||||
args->at( 2 )->prepare( parent, updatedContext );
|
|
||||||
delete updatedContext->popScope();
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QgsWithVariableExpressionFunction::popTemporaryVariable( const QgsExpressionContext *context ) const
|
||||||
|
{
|
||||||
|
QgsExpressionContext *updatedContext = const_cast<QgsExpressionContext *>( context );
|
||||||
|
delete updatedContext->popScope();
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsWithVariableExpressionFunction::appendTemporaryVariable( const QgsExpressionContext *context, const QString &name, const QVariant &value ) const
|
||||||
|
{
|
||||||
|
QgsExpressionContextScope *scope = new QgsExpressionContextScope();
|
||||||
|
scope->setVariable( name, value );
|
||||||
|
|
||||||
|
QgsExpressionContext *updatedContext = const_cast<QgsExpressionContext *>( context );
|
||||||
|
updatedContext->appendScope( scope );
|
||||||
|
}
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
class QgsExpressionNodeFunction;
|
class QgsExpressionNodeFunction;
|
||||||
class QgsExpression;
|
class QgsExpression;
|
||||||
class QgsExpressionContext;
|
class QgsExpressionContext;
|
||||||
|
class QgsExpressionContextScope;
|
||||||
|
|
||||||
/** \ingroup core
|
/** \ingroup core
|
||||||
* A abstract base class for defining QgsExpression functions.
|
* A abstract base class for defining QgsExpression functions.
|
||||||
@ -475,6 +476,18 @@ class QgsWithVariableExpressionFunction : public QgsExpressionFunction
|
|||||||
QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent ) override;
|
QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent ) override;
|
||||||
|
|
||||||
bool prepare( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
|
bool prepare( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append a scope with a single variable definition (``name``=``value``)
|
||||||
|
*/
|
||||||
|
void appendTemporaryVariable( const QgsExpressionContext *context, const QString &name, const QVariant &value ) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pop the temporary scope again
|
||||||
|
*/
|
||||||
|
void popTemporaryVariable( const QgsExpressionContext *context ) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user