Skip to content
Permalink
Browse files

Port iterators to expression contexts

  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent fdfb804 commit 3b6f5915e7c2b1bf3d1834c83edc43b9145566a5
@@ -33,7 +33,7 @@ class QgsFeatureRequest
//! construct a request with rectangle filter
explicit QgsFeatureRequest( const QgsRectangle& rect );
//! construct a request with a filter expression
explicit QgsFeatureRequest( const QgsExpression& expr );
explicit QgsFeatureRequest( const QgsExpression& expr, const QgsExpressionContext& context = QgsExpressionContext() );

FilterType filterType() const;

@@ -54,6 +54,18 @@ class QgsFeatureRequest
QgsFeatureRequest& setFilterExpression( const QString& expression );
QgsExpression* filterExpression() const;

/** Returns the expression context used to evaluate filter expressions.
* @note added in QGIS 2.12
* @see setExpressionContext
*/
QgsExpressionContext* expressionContext();

/** Sets the expression context used to evaluate filter expressions.
* @note added in QGIS 2.12
* @see expressionContext
*/
QgsFeatureRequest& setExpressionContext( const QgsExpressionContext& context );

/**
* Disables filter conditions.
* The spatial filter (filterRect) will be kept in place.
@@ -66,7 +66,8 @@ bool QgsAbstractFeatureIterator::nextFeatureFilterExpression( QgsFeature& f )
{
while ( fetchFeature( f ) )
{
if ( mRequest.filterExpression()->evaluate( f ).toBool() )
mRequest.expressionContext()->setFeature( f );
if ( mRequest.filterExpression()->evaluate( mRequest.expressionContext() ).toBool() )
return true;
}
return false;
@@ -47,10 +47,11 @@ QgsFeatureRequest::QgsFeatureRequest( const QgsRectangle& rect )
{
}

QgsFeatureRequest::QgsFeatureRequest( const QgsExpression& expr )
QgsFeatureRequest::QgsFeatureRequest( const QgsExpression& expr, const QgsExpressionContext &context )
: mFilter( FilterExpression )
, mFilterFid( -1 )
, mFilterExpression( new QgsExpression( expr.expression() ) )
, mExpressionContext( context )
, mFlags( 0 )
{
}
@@ -113,6 +114,12 @@ QgsFeatureRequest& QgsFeatureRequest::setFilterExpression( const QString& expres
return *this;
}

QgsFeatureRequest &QgsFeatureRequest::setExpressionContext( const QgsExpressionContext &context )
{
mExpressionContext = context;
return *this;
}

QgsFeatureRequest& QgsFeatureRequest::setFlags( QgsFeatureRequest::Flags flags )
{
mFlags = flags;
@@ -176,7 +183,8 @@ bool QgsFeatureRequest::acceptFeature( const QgsFeature& feature )
break;

case QgsFeatureRequest::FilterExpression:
if ( mFilterExpression->evaluate( feature ).toBool() )
mExpressionContext.setFeature( feature );
if ( mFilterExpression->evaluate( &mExpressionContext ).toBool() )
return true;
else
return false;
@@ -20,6 +20,7 @@
#include "qgsfeature.h"
#include "qgsrectangle.h"
#include "qgsexpression.h"
#include "qgsexpressioncontext.h"
#include "qgssimplifymethod.h"

#include <QList>
@@ -85,7 +86,7 @@ class CORE_EXPORT QgsFeatureRequest
//! construct a request with rectangle filter
explicit QgsFeatureRequest( const QgsRectangle& rect );
//! construct a request with a filter expression
explicit QgsFeatureRequest( const QgsExpression& expr );
explicit QgsFeatureRequest( const QgsExpression& expr, const QgsExpressionContext& context = QgsExpressionContext() );
//! copy constructor
QgsFeatureRequest( const QgsFeatureRequest& rh );

@@ -108,10 +109,33 @@ class CORE_EXPORT QgsFeatureRequest
QgsFeatureRequest& setFilterFids( QgsFeatureIds fids );
const QgsFeatureIds& filterFids() const { return mFilterFids; }

//! Set filter expression. {@see QgsExpression}
/** Set the filter expression. {@see QgsExpression}
* @param expression expression string
* @see filterExpression
* @see setExpressionContext
*/
QgsFeatureRequest& setFilterExpression( const QString& expression );

/** Returns the filter expression if set.
* @see setFilterExpression
* @see expressionContext
*/
QgsExpression* filterExpression() const { return mFilterExpression; }

/** Returns the expression context used to evaluate filter expressions.
* @note added in QGIS 2.12
* @see setExpressionContext
* @see filterExpression
*/
QgsExpressionContext* expressionContext() { return &mExpressionContext; }

/** Sets the expression context used to evaluate filter expressions.
* @note added in QGIS 2.12
* @see expressionContext
* @see setFilterExpression
*/
QgsFeatureRequest& setExpressionContext( const QgsExpressionContext& context );

/**
* Disables filter conditions.
* The spatial filter (filterRect) will be kept in place.
@@ -162,6 +186,7 @@ class CORE_EXPORT QgsFeatureRequest
QgsFeatureId mFilterFid;
QgsFeatureIds mFilterFids;
QgsExpression* mFilterExpression;
QgsExpressionContext mExpressionContext;
Flags mFlags;
QgsAttributeList mAttrs;
QgsSimplifyMethod mSimplifyMethod;
@@ -160,7 +160,8 @@ QgsVectorLayerFeatureIterator::QgsVectorLayerFeatureIterator( QgsVectorLayerFeat

if ( mRequest.filterType() == QgsFeatureRequest::FilterExpression )
{
mRequest.filterExpression()->prepare( mSource->mFields );
mRequest.expressionContext()->setFields( mSource->mFields );
mRequest.filterExpression()->prepare( mRequest.expressionContext() );
}
}

@@ -380,7 +381,8 @@ bool QgsVectorLayerFeatureIterator::fetchNextChangedAttributeFeature( QgsFeature
if ( mHasVirtualAttributes )
addVirtualAttributes( f );

if ( mRequest.filterExpression()->evaluate( &f ).toBool() )
mRequest.expressionContext()->setFeature( f );
if ( mRequest.filterExpression()->evaluate( mRequest.expressionContext() ).toBool() )
{
return true;
}
@@ -150,6 +150,7 @@ bool QgsVectorLayerRenderer::render()
if ( !rendererFilter.isEmpty() )
{
featureRequest.setFilterExpression( rendererFilter );
featureRequest.setExpressionContext( mContext.expressionContext() );
}

// enable the simplification of the geometries (Using the current map2pixel context) before send it to renderer engine.

0 comments on commit 3b6f591

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