Skip to content

Commit 916c9c4

Browse files
committed
Open heart surgery on expression context for processing sources
1 parent aaf70de commit 916c9c4

11 files changed

+113
-22
lines changed

python/core/core_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
%Include qgserror.sip
4242
%Include qgsexpressioncontext.sip
4343
%Include qgsexpressioncontextgenerator.sip
44+
%Include qgsexpressioncontextscopegenerator.sip
4445
%Include qgsexpressionfieldbuffer.sip
4546
%Include qgsfeaturefilterprovider.sip
4647
%Include qgsfeatureiterator.sip

python/core/processing/qgsprocessingutils.sip

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,10 @@ class QgsProcessingFeatureSource : QgsFeatureSource
274274
virtual QVariant maximumValue( int fieldIndex ) const;
275275

276276

277-
QgsExpressionContext createExpressionContext( const QgsProcessingContext &context ) const;
277+
QgsExpressionContextScope *createExpressionContextScope() const /Factory/;
278278
%Docstring
279-
Returns an expression context scope suitable for this source or a default global/project
280-
context if nothing more specific can be created.
281-
:rtype: QgsExpressionContext
279+
Returns an expression context scope suitable for this source.
280+
:rtype: QgsExpressionContextScope
282281
%End
283282

284283
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/qgsexpressioncontextscopegenerator.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
class QgsExpressionContextScopeGenerator
12+
{
13+
%Docstring
14+
Abstract interface for generating an expression context scope.
15+
16+
.. versionadded:: 3.0
17+
%End
18+
19+
%TypeHeaderCode
20+
#include "qgsexpressioncontextscopegenerator.h"
21+
%End
22+
public:
23+
24+
virtual QgsExpressionContextScope *createExpressionContextScope() const = 0 /Factory/;
25+
%Docstring
26+
This method needs to be reimplemented in all classes which implement this interface
27+
and return an expression context scope.
28+
29+
.. versionadded:: 3.0
30+
:rtype: QgsExpressionContextScope
31+
%End
32+
33+
virtual ~QgsExpressionContextScopeGenerator();
34+
};
35+
36+
/************************************************************************
37+
* This file has been generated automatically from *
38+
* *
39+
* src/core/qgsexpressioncontextscopegenerator.h *
40+
* *
41+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
42+
************************************************************************/

python/core/qgsvectorlayer.sip

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ typedef QList<int> QgsAttributeList;
1717
typedef QSet<int> QgsAttributeIds;
1818

1919

20-
class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator, QgsFeatureSink, QgsFeatureSource
20+
class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator, QgsExpressionContextScopeGenerator, QgsFeatureSink, QgsFeatureSource
2121
{
2222
%Docstring
2323
Represents a vector layer which manages a vector based data sets.
@@ -1783,6 +1783,9 @@ Returns the current blending mode for features
17831783
virtual QgsExpressionContext createExpressionContext() const;
17841784

17851785

1786+
virtual QgsExpressionContextScope *createExpressionContextScope() const /Factory/;
1787+
1788+
17861789
QgsEditFormConfig editFormConfig() const;
17871790
%Docstring
17881791
Get the configuration of the form used to represent this vector layer.

src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@ SET(QGIS_CORE_HDRS
855855
qgsexception.h
856856
qgsexpressioncontext.h
857857
qgsexpressioncontextgenerator.h
858+
qgsexpressioncontextscopegenerator.h
858859
qgsexpressionfieldbuffer.h
859860
qgsfeaturefilterprovider.h
860861
qgsfeatureiterator.h

src/core/processing/qgsprocessingalgorithm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ QgsExpressionContext QgsProcessingAlgorithm::createExpressionContext( const QVar
132132
// If there's a source capable of generating a context scope, use it
133133
if ( source )
134134
{
135-
const auto &scopes = source->createExpressionContext( context ).takeScopes();
136-
for ( QgsExpressionContextScope *scope : scopes )
135+
QgsExpressionContextScope *scope = source->createExpressionContextScope();
136+
if ( scope )
137137
c << scope;
138138
}
139139
else if ( c.scopeCount() == 0 )

src/core/processing/qgsprocessingutils.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "qgsprocessingparameters.h"
2727
#include "qgsprocessingalgorithm.h"
2828
#include "qgsvectorlayerfeatureiterator.h"
29+
#include "qgsexpressioncontextscopegenerator.h"
2930

3031
QList<QgsRasterLayer *> QgsProcessingUtils::compatibleRasterLayers( QgsProject *project, bool sort )
3132
{
@@ -721,19 +722,13 @@ QVariant QgsProcessingFeatureSource::maximumValue( int fieldIndex ) const
721722
return mSource->maximumValue( fieldIndex );
722723
}
723724

724-
QgsExpressionContext QgsProcessingFeatureSource::createExpressionContext( const QgsProcessingContext &context ) const
725+
QgsExpressionContextScope *QgsProcessingFeatureSource::createExpressionContextScope() const
725726
{
726-
QgsExpressionContext expressionContext;
727-
QgsExpressionContextGenerator *generator = dynamic_cast<QgsExpressionContextGenerator *>( mSource );
727+
QgsExpressionContextScope *expressionContextScope = nullptr;
728+
QgsExpressionContextScopeGenerator *generator = dynamic_cast<QgsExpressionContextScopeGenerator *>( mSource );
728729
if ( generator )
729730
{
730-
expressionContext = generator->createExpressionContext();
731+
expressionContextScope = generator->createExpressionContextScope();
731732
}
732-
else
733-
{
734-
expressionContext
735-
<< QgsExpressionContextUtils::globalScope()
736-
<< QgsExpressionContextUtils::projectScope( context.project() );
737-
}
738-
return expressionContext;
733+
return expressionContextScope;
739734
}

src/core/processing/qgsprocessingutils.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,9 @@ class CORE_EXPORT QgsProcessingFeatureSource : public QgsFeatureSource
320320
QVariant maximumValue( int fieldIndex ) const override;
321321

322322
/**
323-
* Returns an expression context scope suitable for this source or a default global/project
324-
* context if nothing more specific can be created.
323+
* Returns an expression context scope suitable for this source.
325324
*/
326-
QgsExpressionContext createExpressionContext( const QgsProcessingContext &context ) const;
325+
QgsExpressionContextScope *createExpressionContextScope() const SIP_FACTORY;
327326

328327
private:
329328

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/***************************************************************************
2+
qgsexpressioncontextscopegenerator.h - QgsExpressionContextScopeGenerator
3+
4+
---------------------
5+
begin : 24.11.2017
6+
copyright : (C) 2017 by Matthias Kuhn
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#ifndef QGSEXPRESSIONCONTEXTSCOPEGENERATOR_H
17+
#define QGSEXPRESSIONCONTEXTSCOPEGENERATOR_H
18+
19+
#include "qgsexpressioncontext.h"
20+
21+
/**
22+
* \ingroup core
23+
* Abstract interface for generating an expression context scope.
24+
*
25+
* \since QGIS 3.0
26+
*/
27+
28+
class CORE_EXPORT QgsExpressionContextScopeGenerator
29+
{
30+
public:
31+
32+
/**
33+
* This method needs to be reimplemented in all classes which implement this interface
34+
* and return an expression context scope.
35+
*
36+
* \since QGIS 3.0
37+
*/
38+
virtual QgsExpressionContextScope *createExpressionContextScope() const = 0 SIP_FACTORY;
39+
40+
virtual ~QgsExpressionContextScopeGenerator() = default;
41+
};
42+
43+
#endif // QGSEXPRESSIONCONTEXTGENERATOR_H

src/core/qgsvectorlayer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3975,6 +3975,11 @@ QgsExpressionContext QgsVectorLayer::createExpressionContext() const
39753975
return QgsExpressionContext( QgsExpressionContextUtils::globalProjectLayerScopes( this ) );
39763976
}
39773977

3978+
QgsExpressionContextScope *QgsVectorLayer::createExpressionContextScope() const
3979+
{
3980+
return QgsExpressionContextUtils::layerScope( this );
3981+
}
3982+
39783983
void QgsVectorLayer::setDiagramLayerSettings( const QgsDiagramLayerSettings &s )
39793984
{
39803985
if ( !mDiagramLayerSettings )

src/core/qgsvectorlayer.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "qgsaggregatecalculator.h"
4141
#include "qgsfeatureiterator.h"
4242
#include "qgsexpressioncontextgenerator.h"
43+
#include "qgsexpressioncontextscopegenerator.h"
4344

4445
class QPainter;
4546
class QImage;
@@ -350,7 +351,7 @@ typedef QSet<int> QgsAttributeIds;
350351
* TODO QGIS3: Remove virtual from non-inherited methods (like isModified)
351352
* \see QgsVectorLayerUtils()
352353
*/
353-
class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionContextGenerator, public QgsFeatureSink, public QgsFeatureSource
354+
class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionContextGenerator, public QgsExpressionContextScopeGenerator, public QgsFeatureSink, public QgsFeatureSource
354355
{
355356
Q_OBJECT
356357

@@ -1741,6 +1742,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
17411742

17421743
QgsExpressionContext createExpressionContext() const override;
17431744

1745+
QgsExpressionContextScope *createExpressionContextScope() const override SIP_FACTORY;
1746+
17441747
/**
17451748
* Get the configuration of the form used to represent this vector layer.
17461749
* This is a writable configuration that can directly be changed in place.

0 commit comments

Comments
 (0)