Skip to content
Permalink
Browse files

Added methods to search expression nodes and drop RegExp

- 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 673fac0c888d54f4e384173ca308fe2972e9d5d7
@@ -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.
@@ -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/;


@@ -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/;
@@ -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;
@@ -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/;

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

virtual QSet<QString> referencedFunctions() const;

virtual bool needsGeometry() const;

virtual QgsExpressionNode *clone() const /Factory/;

@@ -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/;
@@ -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
@@ -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;

};


@@ -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();
}



@@ -29,6 +29,7 @@
#include "qgis.h"
#include "qgsunittypes.h"
#include "qgsinterval.h"
#include "qgsexpressionnode.h"

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

/**
@@ -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.
*
@@ -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.
@@ -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();
@@ -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();
@@ -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 );
@@ -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;
@@ -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;
@@ -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 )
@@ -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 )
@@ -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;

@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;

0 comments on commit 673fac0

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