Skip to content

Commit

Permalink
Added methods to search expression nodes and drop RegExp
Browse files Browse the repository at this point in the history
- nodes() return the list of all nodes
- findNodes<T> returns a list of the nodes matching the class

Also drops the regexp for finding form attrs in the value-relation
expressions.

Other minor fixes as suggested in the PR review.
  • Loading branch information
elpaso committed May 15, 2018
1 parent 1e046a1 commit 673fac0
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 57 deletions.
3 changes: 2 additions & 1 deletion python/core/auto_generated/expression/qgsexpression.sip.in
Expand Up @@ -180,11 +180,12 @@ which is determined at runtime.

QSet<QString> referencedFunctions() const;
%Docstring
Return a list of all functions which are used in this expression.
Return a list of the names of all functions which are used in this expression.

.. versionadded:: 3.2
%End


QSet<int> referencedAttributeIndexes( const QgsFields &fields ) const;
%Docstring
Return a list of field name indexes obtained from the provided fields.
Expand Down
Expand Up @@ -57,8 +57,6 @@ Returns the node the operator will operate upon.

virtual QSet<QString> referencedFunctions() const;

virtual bool needsGeometry() const;

virtual QgsExpressionNode *clone() const /Factory/;


Expand Down Expand Up @@ -158,7 +156,6 @@ Returns the node to the right of the operator.

virtual QSet<QString> referencedFunctions() const;


virtual bool needsGeometry() const;

virtual QgsExpressionNode *clone() const /Factory/;
Expand Down Expand Up @@ -231,8 +228,6 @@ Returns the list of nodes to search for matching values within.

virtual QSet<QString> referencedFunctions() const;

virtual bool needsGeometry() const;

virtual QgsExpressionNode *clone() const /Factory/;

virtual bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const;
Expand Down Expand Up @@ -284,7 +279,6 @@ Returns a list of arguments specified for the function.

virtual QSet<QString> referencedFunctions() const;

virtual bool needsGeometry() const;

virtual QgsExpressionNode *clone() const /Factory/;

Expand Down Expand Up @@ -334,7 +328,6 @@ The value of the literal.

virtual QSet<QString> referencedFunctions() const;

virtual bool needsGeometry() const;

virtual QgsExpressionNode *clone() const /Factory/;

Expand Down Expand Up @@ -481,6 +474,7 @@ The ELSE expression used for the condition.

virtual QSet<QString> referencedFunctions() const;


virtual bool needsGeometry() const;

virtual QgsExpressionNode *clone() const /Factory/;
Expand Down
Expand Up @@ -59,11 +59,7 @@ Constructor for QgsValueRelationFieldFormatter.

static QStringList valueToStringList( const QVariant &value );
%Docstring
Utility to convert an array or a string representation of and array ``value`` to a string list

:param value: The value to be converted

:return: A string list
Utility to convert an array or a string representation of an array ``value`` to a string list

.. versionadded:: 3.2
%End
Expand Down Expand Up @@ -120,13 +116,11 @@ Return a list of variables required by the form context ``expression``
%Docstring
Check whether the ``feature`` has all values required by the ``expression``

@return True if the expression can be used
:return: True if the expression can be used

.. versionadded:: 3.2
%End

static QString FORM_SCOPE_FUNCTIONS_RE;

};


Expand Down
11 changes: 11 additions & 0 deletions src/core/expression/qgsexpression.cpp
Expand Up @@ -956,3 +956,14 @@ bool QgsExpression::isField() const
{
return d->mRootNode && d->mRootNode->nodeType() == QgsExpressionNode::ntColumnRef;
}

QList<const QgsExpressionNode *> QgsExpression::nodes() const
{
if ( !d->mRootNode )
return QList<const QgsExpressionNode *>();

return d->mRootNode->nodes();
}



35 changes: 33 additions & 2 deletions src/core/expression/qgsexpression.h
Expand Up @@ -29,6 +29,7 @@
#include "qgis.h"
#include "qgsunittypes.h"
#include "qgsinterval.h"
#include "qgsexpressionnode.h"

class QgsFeature;
class QgsGeometry;
Expand All @@ -41,7 +42,6 @@ class QgsDistanceArea;
class QDomElement;
class QgsExpressionContext;
class QgsExpressionPrivate;
class QgsExpressionNode;
class QgsExpressionFunction;

/**
Expand Down Expand Up @@ -259,12 +259,43 @@ class CORE_EXPORT QgsExpression
QSet<QString> referencedVariables() const;

/**
* Return a list of all functions which are used in this expression.
* Return a list of the names of all functions which are used in this expression.
*
* \since QGIS 3.2
*/
QSet<QString> referencedFunctions() const;

#ifndef SIP_RUN

/**
* Return a list of all nodes which are used in this expression
*
* \note not available in Python bindings
* \since QGIS 3.2
*/
QList<const QgsExpressionNode *> nodes( ) const;

/**
* Return a list of all nodes of the given class which are used in this expression
*
* \note not available in Python bindings
* \since QGIS 3.2
*/
template <class T>
QList<const T *> findNodes( ) const
{
QList<const T *> lst;
const QList<const QgsExpressionNode *> allNodes( nodes() );
for ( const auto &node : allNodes )
{
const T *n = dynamic_cast<const T *>( node );
if ( n )
lst << n;
}
return lst;
}
#endif

/**
* Return a list of field name indexes obtained from the provided fields.
*
Expand Down
8 changes: 8 additions & 0 deletions src/core/expression/qgsexpressionnode.h
Expand Up @@ -227,6 +227,14 @@ class CORE_EXPORT QgsExpressionNode SIP_ABSTRACT
*/
virtual QSet<QString> referencedFunctions() const = 0;

/**
* Return a list of all nodes which are used in this expression.
*
* \note not available in Python bindings
* \since QGIS 3.2
*/
virtual QList<const QgsExpressionNode *> nodes( ) const = 0; SIP_SKIP

/**
* Abstract virtual method which returns if the geometry is required to evaluate
* this expression.
Expand Down
69 changes: 69 additions & 0 deletions src/core/expression/qgsexpressionnodeimpl.cpp
Expand Up @@ -142,6 +142,14 @@ QSet<QString> QgsExpressionNodeUnaryOperator::referencedFunctions() const
return mOperand->referencedFunctions();
}

QList<const QgsExpressionNode *> QgsExpressionNodeUnaryOperator::nodes() const
{
QList<const QgsExpressionNode *> lst;
lst.append( this );
lst += mOperand->nodes();
return lst;
}

bool QgsExpressionNodeUnaryOperator::needsGeometry() const
{
return mOperand->needsGeometry();
Expand Down Expand Up @@ -730,6 +738,14 @@ QSet<QString> QgsExpressionNodeBinaryOperator::referencedFunctions() const
return mOpLeft->referencedFunctions() + mOpRight->referencedFunctions();
}

QList<const QgsExpressionNode *> QgsExpressionNodeBinaryOperator::nodes() const
{
QList<const QgsExpressionNode *> lst;
lst << this;
lst += mOpLeft->nodes() + mOpRight->nodes();
return lst;
}

bool QgsExpressionNodeBinaryOperator::needsGeometry() const
{
return mOpLeft->needsGeometry() || mOpRight->needsGeometry();
Expand Down Expand Up @@ -1005,6 +1021,21 @@ QSet<QString> QgsExpressionNodeFunction::referencedFunctions() const
return functions;
}

QList<const QgsExpressionNode *> QgsExpressionNodeFunction::nodes() const
{
QList<const QgsExpressionNode *> lst;
lst << this;
if ( !mArgs )
return lst;

const QList< QgsExpressionNode * > nodeList = mArgs->list();
for ( QgsExpressionNode *n : nodeList )
{
lst += n->nodes();
}
return lst;
}

bool QgsExpressionNodeFunction::needsGeometry() const
{
bool needs = QgsExpression::QgsExpression::Functions()[mFnIndex]->usesGeometry( this );
Expand Down Expand Up @@ -1160,6 +1191,13 @@ QSet<QString> QgsExpressionNodeLiteral::referencedFunctions() const
return QSet<QString>();
}

QList<const QgsExpressionNode *> QgsExpressionNodeLiteral::nodes() const
{
QList<const QgsExpressionNode *> lst;
lst << this;
return lst;
}

bool QgsExpressionNodeLiteral::needsGeometry() const
{
return false;
Expand Down Expand Up @@ -1252,6 +1290,13 @@ QSet<QString> QgsExpressionNodeColumnRef::referencedFunctions() const
return QSet<QString>();
}

QList<const QgsExpressionNode *> QgsExpressionNodeColumnRef::nodes() const
{
QList<const QgsExpressionNode *> result;
result << this;
return result;
}

bool QgsExpressionNodeColumnRef::needsGeometry() const
{
return false;
Expand Down Expand Up @@ -1389,6 +1434,21 @@ QSet<QString> QgsExpressionNodeCondition::referencedFunctions() const
return lst;
}

QList<const QgsExpressionNode *> QgsExpressionNodeCondition::nodes() const
{
QList<const QgsExpressionNode *> lst;
lst << this;
for ( WhenThen *cond : mConditions )
{
lst += cond->mWhenExp->nodes() + cond->mThenExp->nodes();
}

if ( mElseExp )
lst += mElseExp->nodes();

return lst;
}

bool QgsExpressionNodeCondition::needsGeometry() const
{
for ( WhenThen *cond : mConditions )
Expand Down Expand Up @@ -1451,7 +1511,16 @@ QSet<QString> QgsExpressionNodeInOperator::referencedFunctions() const
for ( const QgsExpressionNode *n : nodeList )
lst.unite( n->referencedFunctions() );
return lst;
}

QList<const QgsExpressionNode *> QgsExpressionNodeInOperator::nodes() const
{
QList<const QgsExpressionNode *> lst;
lst << this;
const QList< QgsExpressionNode * > nodeList = mList->list();
for ( const QgsExpressionNode *n : nodeList )
lst += n->nodes();
return lst;
}

QgsExpressionNodeCondition::WhenThen::WhenThen( QgsExpressionNode *whenExp, QgsExpressionNode *thenExp )
Expand Down
12 changes: 12 additions & 0 deletions src/core/expression/qgsexpressionnodeimpl.h
Expand Up @@ -65,6 +65,7 @@ class CORE_EXPORT QgsExpressionNodeUnaryOperator : public QgsExpressionNode
QSet<QString> referencedColumns() const override;
QSet<QString> referencedVariables() const override;
QSet<QString> referencedFunctions() const override;
QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
bool needsGeometry() const override;
QgsExpressionNode *clone() const override SIP_FACTORY;

Expand Down Expand Up @@ -164,6 +165,7 @@ class CORE_EXPORT QgsExpressionNodeBinaryOperator : public QgsExpressionNode
QSet<QString> referencedColumns() const override;
QSet<QString> referencedVariables() const override;
QSet<QString> referencedFunctions() const override;
QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP

bool needsGeometry() const override;
QgsExpressionNode *clone() const override SIP_FACTORY;
Expand Down Expand Up @@ -245,6 +247,7 @@ class CORE_EXPORT QgsExpressionNodeInOperator : public QgsExpressionNode
QSet<QString> referencedColumns() const override;
QSet<QString> referencedVariables() const override;
QSet<QString> referencedFunctions() const override;
QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
bool needsGeometry() const override;
QgsExpressionNode *clone() const override SIP_FACTORY;
bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
Expand Down Expand Up @@ -289,6 +292,8 @@ class CORE_EXPORT QgsExpressionNodeFunction : public QgsExpressionNode
QSet<QString> referencedColumns() const override;
QSet<QString> referencedVariables() const override;
QSet<QString> referencedFunctions() const override;

QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
bool needsGeometry() const override;
QgsExpressionNode *clone() const override SIP_FACTORY;
bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
Expand Down Expand Up @@ -327,6 +332,8 @@ class CORE_EXPORT QgsExpressionNodeLiteral : public QgsExpressionNode
QSet<QString> referencedColumns() const override;
QSet<QString> referencedVariables() const override;
QSet<QString> referencedFunctions() const override;

QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP
bool needsGeometry() const override;
QgsExpressionNode *clone() const override SIP_FACTORY;
bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
Expand Down Expand Up @@ -363,6 +370,8 @@ class CORE_EXPORT QgsExpressionNodeColumnRef : public QgsExpressionNode
QSet<QString> referencedColumns() const override;
QSet<QString> referencedVariables() const override;
QSet<QString> referencedFunctions() const override;
QList<const QgsExpressionNode *> nodes( ) const override; SIP_SKIP

bool needsGeometry() const override;

QgsExpressionNode *clone() const override SIP_FACTORY;
Expand Down Expand Up @@ -464,6 +473,9 @@ class CORE_EXPORT QgsExpressionNodeCondition : public QgsExpressionNode
QSet<QString> referencedColumns() const override;
QSet<QString> referencedVariables() const override;
QSet<QString> referencedFunctions() const override;

QList<const QgsExpressionNode *> nodes() const override; SIP_SKIP

bool needsGeometry() const override;
QgsExpressionNode *clone() const override SIP_FACTORY;
bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const override;
Expand Down

0 comments on commit 673fac0

Please sign in to comment.