Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Open heart surgery on expression context for processing sources
  • Loading branch information
m-kuhn committed Nov 24, 2017
1 parent aaf70de commit 916c9c4
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 22 deletions.
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -41,6 +41,7 @@
%Include qgserror.sip
%Include qgsexpressioncontext.sip
%Include qgsexpressioncontextgenerator.sip
%Include qgsexpressioncontextscopegenerator.sip
%Include qgsexpressionfieldbuffer.sip
%Include qgsfeaturefilterprovider.sip
%Include qgsfeatureiterator.sip
Expand Down
7 changes: 3 additions & 4 deletions python/core/processing/qgsprocessingutils.sip
Expand Up @@ -274,11 +274,10 @@ class QgsProcessingFeatureSource : QgsFeatureSource
virtual QVariant maximumValue( int fieldIndex ) const;


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

};
Expand Down
42 changes: 42 additions & 0 deletions python/core/qgsexpressioncontextscopegenerator.sip
@@ -0,0 +1,42 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsexpressioncontextscopegenerator.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/



class QgsExpressionContextScopeGenerator
{
%Docstring
Abstract interface for generating an expression context scope.

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsexpressioncontextscopegenerator.h"
%End
public:

virtual QgsExpressionContextScope *createExpressionContextScope() const = 0 /Factory/;
%Docstring
This method needs to be reimplemented in all classes which implement this interface
and return an expression context scope.

.. versionadded:: 3.0
:rtype: QgsExpressionContextScope
%End

virtual ~QgsExpressionContextScopeGenerator();
};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsexpressioncontextscopegenerator.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
5 changes: 4 additions & 1 deletion python/core/qgsvectorlayer.sip
Expand Up @@ -17,7 +17,7 @@ typedef QList<int> QgsAttributeList;
typedef QSet<int> QgsAttributeIds;


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


virtual QgsExpressionContextScope *createExpressionContextScope() const /Factory/;


QgsEditFormConfig editFormConfig() const;
%Docstring
Get the configuration of the form used to represent this vector layer.
Expand Down
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -855,6 +855,7 @@ SET(QGIS_CORE_HDRS
qgsexception.h
qgsexpressioncontext.h
qgsexpressioncontextgenerator.h
qgsexpressioncontextscopegenerator.h
qgsexpressionfieldbuffer.h
qgsfeaturefilterprovider.h
qgsfeatureiterator.h
Expand Down
4 changes: 2 additions & 2 deletions src/core/processing/qgsprocessingalgorithm.cpp
Expand Up @@ -132,8 +132,8 @@ QgsExpressionContext QgsProcessingAlgorithm::createExpressionContext( const QVar
// If there's a source capable of generating a context scope, use it
if ( source )
{
const auto &scopes = source->createExpressionContext( context ).takeScopes();
for ( QgsExpressionContextScope *scope : scopes )
QgsExpressionContextScope *scope = source->createExpressionContextScope();
if ( scope )
c << scope;
}
else if ( c.scopeCount() == 0 )
Expand Down
17 changes: 6 additions & 11 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -26,6 +26,7 @@
#include "qgsprocessingparameters.h"
#include "qgsprocessingalgorithm.h"
#include "qgsvectorlayerfeatureiterator.h"
#include "qgsexpressioncontextscopegenerator.h"

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

QgsExpressionContext QgsProcessingFeatureSource::createExpressionContext( const QgsProcessingContext &context ) const
QgsExpressionContextScope *QgsProcessingFeatureSource::createExpressionContextScope() const
{
QgsExpressionContext expressionContext;
QgsExpressionContextGenerator *generator = dynamic_cast<QgsExpressionContextGenerator *>( mSource );
QgsExpressionContextScope *expressionContextScope = nullptr;
QgsExpressionContextScopeGenerator *generator = dynamic_cast<QgsExpressionContextScopeGenerator *>( mSource );
if ( generator )
{
expressionContext = generator->createExpressionContext();
expressionContextScope = generator->createExpressionContextScope();
}
else
{
expressionContext
<< QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope( context.project() );
}
return expressionContext;
return expressionContextScope;
}
5 changes: 2 additions & 3 deletions src/core/processing/qgsprocessingutils.h
Expand Up @@ -320,10 +320,9 @@ class CORE_EXPORT QgsProcessingFeatureSource : public QgsFeatureSource
QVariant maximumValue( int fieldIndex ) const override;

/**
* Returns an expression context scope suitable for this source or a default global/project
* context if nothing more specific can be created.
* Returns an expression context scope suitable for this source.
*/
QgsExpressionContext createExpressionContext( const QgsProcessingContext &context ) const;
QgsExpressionContextScope *createExpressionContextScope() const SIP_FACTORY;

private:

Expand Down
43 changes: 43 additions & 0 deletions src/core/qgsexpressioncontextscopegenerator.h
@@ -0,0 +1,43 @@
/***************************************************************************
qgsexpressioncontextscopegenerator.h - QgsExpressionContextScopeGenerator
---------------------
begin : 24.11.2017
copyright : (C) 2017 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSEXPRESSIONCONTEXTSCOPEGENERATOR_H
#define QGSEXPRESSIONCONTEXTSCOPEGENERATOR_H

#include "qgsexpressioncontext.h"

/**
* \ingroup core
* Abstract interface for generating an expression context scope.
*
* \since QGIS 3.0
*/

class CORE_EXPORT QgsExpressionContextScopeGenerator
{
public:

/**
* This method needs to be reimplemented in all classes which implement this interface
* and return an expression context scope.
*
* \since QGIS 3.0
*/
virtual QgsExpressionContextScope *createExpressionContextScope() const = 0 SIP_FACTORY;

virtual ~QgsExpressionContextScopeGenerator() = default;
};

#endif // QGSEXPRESSIONCONTEXTGENERATOR_H
5 changes: 5 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -3975,6 +3975,11 @@ QgsExpressionContext QgsVectorLayer::createExpressionContext() const
return QgsExpressionContext( QgsExpressionContextUtils::globalProjectLayerScopes( this ) );
}

QgsExpressionContextScope *QgsVectorLayer::createExpressionContextScope() const
{
return QgsExpressionContextUtils::layerScope( this );
}

void QgsVectorLayer::setDiagramLayerSettings( const QgsDiagramLayerSettings &s )
{
if ( !mDiagramLayerSettings )
Expand Down
5 changes: 4 additions & 1 deletion src/core/qgsvectorlayer.h
Expand Up @@ -40,6 +40,7 @@
#include "qgsaggregatecalculator.h"
#include "qgsfeatureiterator.h"
#include "qgsexpressioncontextgenerator.h"
#include "qgsexpressioncontextscopegenerator.h"

class QPainter;
class QImage;
Expand Down Expand Up @@ -350,7 +351,7 @@ typedef QSet<int> QgsAttributeIds;
* TODO QGIS3: Remove virtual from non-inherited methods (like isModified)
* \see QgsVectorLayerUtils()
*/
class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionContextGenerator, public QgsFeatureSink, public QgsFeatureSource
class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionContextGenerator, public QgsExpressionContextScopeGenerator, public QgsFeatureSink, public QgsFeatureSource
{
Q_OBJECT

Expand Down Expand Up @@ -1741,6 +1742,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte

QgsExpressionContext createExpressionContext() const override;

QgsExpressionContextScope *createExpressionContextScope() const override SIP_FACTORY;

/**
* Get the configuration of the form used to represent this vector layer.
* This is a writable configuration that can directly be changed in place.
Expand Down

0 comments on commit 916c9c4

Please sign in to comment.