Skip to content
Permalink
Browse files

Port virtual fields to expression contexts

  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent d654227 commit cf3cf19124e0a72a69a3be58ddd369c7f34ba7bf
Showing with 15 additions and 2 deletions.
  1. +10 −2 src/core/qgsvectorlayerfeatureiterator.cpp
  2. +5 −0 src/core/qgsvectorlayerfeatureiterator.h
@@ -22,9 +22,11 @@
#include "qgsvectorlayereditbuffer.h"
#include "qgsvectorlayer.h"
#include "qgsvectorlayerjoinbuffer.h"
#include "qgsexpressioncontext.h"

QgsVectorLayerFeatureSource::QgsVectorLayerFeatureSource( QgsVectorLayer *layer )
{
mLayer = layer;
mProviderFeatureSource = layer->dataProvider()->featureSource();
mFields = layer->fields();
mJoinBuffer = layer->mJoinBuffer->clone();
@@ -502,6 +504,11 @@ void QgsVectorLayerFeatureIterator::prepareExpressions()
{
const QList<QgsExpressionFieldBuffer::ExpressionField> exps = mSource->mExpressionFieldBuffer->expressions();

mExpressionContext.reset( new QgsExpressionContext() );
mExpressionContext->appendScope( QgsExpressionContextUtils::globalScope() );
mExpressionContext->appendScope( QgsExpressionContextUtils::projectScope() );
mExpressionContext->setFields( mSource->mFields );

for ( int i = 0; i < mSource->mFields.count(); i++ )
{
if ( mSource->mFields.fieldOrigin( i ) == QgsFields::OriginExpression )
@@ -512,7 +519,7 @@ void QgsVectorLayerFeatureIterator::prepareExpressions()
{
int oi = mSource->mFields.fieldOriginIndex( i );
QgsExpression* exp = new QgsExpression( exps[oi].expression );
exp->prepare( mSource->mFields );
exp->prepare( mExpressionContext.data() );
mExpressionFieldInfo.insert( i, exp );

if ( mRequest.flags() & QgsFeatureRequest::SubsetOfAttributes )
@@ -572,7 +579,8 @@ void QgsVectorLayerFeatureIterator::addVirtualAttributes( QgsFeature& f )
for ( ; it != mExpressionFieldInfo.constEnd(); ++it )
{
QgsExpression* exp = it.value();
QVariant val = exp->evaluate( f );
mExpressionContext->setFeature( f );
QVariant val = exp->evaluate( mExpressionContext.data() );
mSource->mFields.at( it.key() ).convertCompatible( val );;
f.setAttribute( it.key(), val );
}
@@ -26,6 +26,7 @@ class QgsVectorLayer;
class QgsVectorLayerEditBuffer;
class QgsVectorLayerJoinBuffer;
struct QgsVectorJoinInfo;
class QgsExpressionContext;

class QgsVectorLayerFeatureIterator;

@@ -42,6 +43,8 @@ class QgsVectorLayerFeatureSource : public QgsAbstractFeatureSource

protected:

QgsVectorLayer* mLayer;

QgsAbstractFeatureSource* mProviderFeatureSource;

QgsVectorLayerJoinBuffer* mJoinBuffer;
@@ -158,6 +161,8 @@ class CORE_EXPORT QgsVectorLayerFeatureIterator : public QgsAbstractFeatureItera
//! optional object to locally simplify edited (changed or added) geometries fetched by this feature iterator
QgsAbstractGeometrySimplifier* mEditGeometrySimplifier;

QScopedPointer<QgsExpressionContext> mExpressionContext;

//! returns whether the iterator supports simplify geometries on provider side
virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const override;
};

0 comments on commit cf3cf19

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