Skip to content

Commit

Permalink
Setup loaded layer store for processing exp contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 9, 2023
1 parent e670ed3 commit b468a18
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/core/processing/qgsprocessingcontext.cpp
Expand Up @@ -31,6 +31,7 @@ QgsProcessingContext::QgsProcessingContext()
mFeedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( feature.id() ) );
};
mTransformErrorCallback = callback;
mExpressionContext.setLoadedLayerStore( &tempLayerStore );
}

QgsProcessingContext::~QgsProcessingContext()
Expand All @@ -41,6 +42,13 @@ QgsProcessingContext::~QgsProcessingContext()
}
}

void QgsProcessingContext::setExpressionContext( const QgsExpressionContext &context )
{
mExpressionContext = context;
// any layers temporarily loaded by expressions should use the same temporary layer store as this context
mExpressionContext.setLoadedLayerStore( &tempLayerStore );
}

void QgsProcessingContext::setLayersToLoadOnCompletion( const QMap<QString, QgsProcessingContext::LayerDetails> &layers )
{
for ( auto it = mLayersToLoadOnCompletion.constBegin(); it != mLayersToLoadOnCompletion.constEnd(); ++it )
Expand Down
3 changes: 2 additions & 1 deletion src/core/processing/qgsprocessingcontext.h
Expand Up @@ -88,6 +88,7 @@ class CORE_EXPORT QgsProcessingContext
mProject = other.mProject;
mTransformContext = other.mTransformContext;
mExpressionContext = other.mExpressionContext;
mExpressionContext.setLoadedLayerStore( &tempLayerStore );
mInvalidGeometryCallback = other.mInvalidGeometryCallback;
mUseDefaultInvalidGeometryCallback = other.mUseDefaultInvalidGeometryCallback;
mInvalidGeometryCheck = other.mInvalidGeometryCheck;
Expand Down Expand Up @@ -156,7 +157,7 @@ class CORE_EXPORT QgsProcessingContext
/**
* Sets the expression \a context.
*/
void setExpressionContext( const QgsExpressionContext &context ) { mExpressionContext = context; }
void setExpressionContext( const QgsExpressionContext &context );

/**
* Returns the coordinate transform context.
Expand Down
11 changes: 11 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -1228,6 +1228,8 @@ void TestQgsProcessing::context()
context.setInvalidGeometryCheck( QgsFeatureRequest::GeometrySkipInvalid );
QCOMPARE( context.invalidGeometryCheck(), QgsFeatureRequest::GeometrySkipInvalid );

QCOMPARE( context.expressionContext().loadedLayerStore(), context.temporaryLayerStore() );

QgsVectorLayer *vector = new QgsVectorLayer( "Polygon", "vector", "memory" );
context.temporaryLayerStore()->addMapLayer( vector );
QCOMPARE( context.temporaryLayerStore()->mapLayer( vector->id() ), vector );
Expand All @@ -1241,6 +1243,15 @@ void TestQgsProcessing::context()
QCOMPARE( static_cast< int >( context2.logLevel() ), static_cast< int >( QgsProcessingContext::Verbose ) );
// layers from temporaryLayerStore must not be copied by copyThreadSafeSettings
QVERIFY( context2.temporaryLayerStore()->mapLayers().isEmpty() );
QCOMPARE( context2.expressionContext().loadedLayerStore(), context2.temporaryLayerStore() );

QgsExpressionContext expContext;
QgsExpressionContextScope *scope = new QgsExpressionContextScope();
scope->setVariable( QStringLiteral( "var" ), 5 );
expContext.appendScope( scope );
context2.setExpressionContext( expContext );
QCOMPARE( context2.expressionContext().variable( QStringLiteral( "var" ) ).toInt(), 5 );
QCOMPARE( context2.expressionContext().loadedLayerStore(), context2.temporaryLayerStore() );

// layers to load on completion
QgsVectorLayer *v1 = new QgsVectorLayer( "Polygon", "V1", "memory" );
Expand Down

0 comments on commit b468a18

Please sign in to comment.