Skip to content

Commit

Permalink
Explicitly pass QgsProject object when dealing with expression contexts
Browse files Browse the repository at this point in the history
Continued effort to reduce number of uses of QgsProject as singleton...
  • Loading branch information
wonder-sk committed Jan 6, 2017
1 parent af5eb6a commit 660867c
Show file tree
Hide file tree
Showing 64 changed files with 241 additions and 289 deletions.
6 changes: 6 additions & 0 deletions doc/api_break.dox
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,12 @@ QgsExpression::Function {#qgis_api_break_3_0_QgsExpression_Function}
- `QgsExpression::Function::helptext()` has been renamed to `helpText()`


QgsExpressionContextUtils {#qgis_api_break_3_0_QgsExpressionContextUtils}
-------------------------

- projectScope(), setProjectVariable() and setProjectVariables() require pointer to QgsProject as the first argument.


QgsFeature {#qgis_api_break_3_0_QgsFeature}
----------

Expand Down
25 changes: 22 additions & 3 deletions python/core/qgsexpressioncontext.sip
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ class QgsExpressionContext

QgsExpressionContext();

/** Initializes the context with given list of scopes.
* Ownership of the scopes is transferred to the stack.
* @note added in QGIS 3.0
*/
explicit QgsExpressionContext( const QList<QgsExpressionContextScope*>& scopes /Transfer/ );

/** Copy constructor
*/
QgsExpressionContext( const QgsExpressionContext& other );
Expand Down Expand Up @@ -359,6 +365,14 @@ class QgsExpressionContext
*/
void appendScope( QgsExpressionContextScope* scope /Transfer/ );

/** Appends a list of scopes to the end of the context. This scopes will override
* any matching variables or functions provided by existing scopes within the
* context. Ownership of the scopes is transferred to the stack.
* @param scopes scopes to append to context
* @note added in QGIS 3.0
*/
void appendScopes( const QList<QgsExpressionContextScope*>& scopes /Transfer/ );

/**
* Removes the last scope from the expression context and return it.
*/
Expand Down Expand Up @@ -484,15 +498,20 @@ class QgsExpressionContextUtils
static QgsExpressionContextScope* globalScope() /Factory/;
static void setGlobalVariable( const QString& name, const QVariant& value );
static void setGlobalVariables( const QVariantMap& variables );
static QgsExpressionContextScope* projectScope() /Factory/;
static void setProjectVariable( const QString& name, const QVariant& value );
static void setProjectVariables( const QVariantMap& variables );
static QgsExpressionContextScope* projectScope( const QgsProject* project ) /Factory/;
static void setProjectVariable( QgsProject* project, const QString& name, const QVariant& value );
static void setProjectVariables( QgsProject* project, const QVariantMap& variables );

/** Creates a new scope which contains variables and functions relating to a QgsMapLayer.
* For instance, layer name, id and fields.
*/
static QgsExpressionContextScope* layerScope( const QgsMapLayer *layer ) /Factory/;

/** Creates a list of three scopes: global, layer's project and layer.
* @note added in QGIS 3.0
*/
static QList<QgsExpressionContextScope*> globalProjectLayerScopes( const QgsMapLayer* layer ) /Factory/;

/** Sets a layer context variable. This variable will be contained within scopes retrieved via
* layerScope().
* @param layer map layer
Expand Down
6 changes: 6 additions & 0 deletions python/gui/symbology-ng/qgssymbolwidgetcontext.sip
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,11 @@
* @see setAdditionalExpressionContextScopes()
*/
QList< QgsExpressionContextScope > additionalExpressionContextScopes() const;

/** Returns list of scopes: global, project, atlas, map, layer.
* Ownership is transferred to the caller.
* @note added in QGIS 3.0
*/
QList<QgsExpressionContextScope*> globalProjectAtlasMapLayerScopes( const QgsMapLayer* layer ) const /Factory/;
};

2 changes: 1 addition & 1 deletion src/app/composer/qgscompositionwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ void QgsCompositionWidget::updateVariables()
{
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::compositionScope( mComposition );
mVariableEditor->setContext( &context );
mVariableEditor->setEditableScopeIndex( 2 );
Expand Down
17 changes: 4 additions & 13 deletions src/app/qgsattributetabledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ QgsExpressionContext QgsAttributeTableDialog::createExpressionContext() const
{
QgsExpressionContext expContext;
expContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope();
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() );

if ( mLayer )
expContext << QgsExpressionContextUtils::layerScope( mLayer );
Expand Down Expand Up @@ -474,10 +474,7 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer* layer, const

int rownum = 1;

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( layer );
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) );

QgsField fld = layer->fields().at( fieldindex );

Expand Down Expand Up @@ -583,10 +580,7 @@ void QgsAttributeTableDialog::filterColumnChanged( QObject* filterAction )
void QgsAttributeTableDialog::filterExpressionBuilder()
{
// Show expression builder
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mLayer );
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );

QgsExpressionBuilderDialog dlg( mLayer, mFilterQuery->text(), this, QStringLiteral( "generic" ), context );
dlg.setWindowTitle( tr( "Expression based filter" ) );
Expand Down Expand Up @@ -965,10 +959,7 @@ void QgsAttributeTableDialog::setFilterExpression( const QString& filterString,
return;
}

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mLayer );
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );

if ( !filterExpression.prepare( &context ) )
{
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgsdiagramproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ QgsExpressionContext QgsDiagramProperties::createExpressionContext() const
{
QgsExpressionContext expContext;
expContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::atlasScope( nullptr )
<< QgsExpressionContextUtils::mapSettingsScope( mMapCanvas->mapSettings() )
<< QgsExpressionContextUtils::layerScope( mLayer );
Expand Down Expand Up @@ -571,7 +571,7 @@ void QgsDiagramProperties::on_mFindMaximumValueButton_clicked()
QgsExpression exp( sizeFieldNameOrExp );
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::mapSettingsScope( mMapCanvas->mapSettings() )
<< QgsExpressionContextUtils::layerScope( mLayer );

Expand Down Expand Up @@ -859,7 +859,7 @@ QString QgsDiagramProperties::showExpressionBuilder( const QString& initialExpre
{
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::atlasScope( nullptr )
<< QgsExpressionContextUtils::mapSettingsScope( mMapCanvas->mapSettings() )
<< QgsExpressionContextUtils::layerScope( mLayer );
Expand Down
10 changes: 2 additions & 8 deletions src/app/qgsfieldcalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer* vl, QWidget* parent )
return;


QgsExpressionContext expContext;
expContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mVectorLayer );
QgsExpressionContext expContext( QgsExpressionContextUtils::globalProjectLayerScopes( mVectorLayer ) );

expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "row_number" ), 1, true ) );
expContext.setHighlightedVariables( QStringList() << QStringLiteral( "row_number" ) );
Expand Down Expand Up @@ -168,10 +165,7 @@ void QgsFieldCalculator::accept()
exp.setDistanceUnits( QgsProject::instance()->distanceUnits() );
exp.setAreaUnits( QgsProject::instance()->areaUnits() );

QgsExpressionContext expContext;
expContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mVectorLayer );
QgsExpressionContext expContext( QgsExpressionContextUtils::globalProjectLayerScopes( mVectorLayer ) );

if ( !exp.prepare( &expContext ) )
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsfieldsproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ void QgsFieldsProperties::updateExpression()

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope();
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() );

QgsExpressionBuilderDialog dlg( mLayer, exp, nullptr, QStringLiteral( "generic" ), context );

Expand Down
5 changes: 1 addition & 4 deletions src/app/qgsidentifyresultsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,7 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
if ( !featureLabeled )
{
featItem->setText( 0, tr( "Title" ) );
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( vlayer );
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( vlayer ) );
context.setFeature( f );

QString value = QgsExpression( vlayer->displayExpression() ).evaluate( &context ).toString();
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgslabelinggui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ QgsExpressionContext QgsLabelingGui::createExpressionContext() const
{
QgsExpressionContext expContext;
expContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::atlasScope( nullptr )
<< QgsExpressionContextUtils::mapSettingsScope( QgisApp::instance()->mapCanvas()->mapSettings() );

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgslabelpropertydialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ void QgsLabelPropertyDialog::setDataDefinedValues( const QgsPalLayerSettings &la

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::atlasScope( nullptr )
<< QgsExpressionContextUtils::mapSettingsScope( QgisApp::instance()->mapCanvas()->mapSettings() )
<< QgsExpressionContextUtils::layerScope( vlayer );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptoolfeatureaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ bool QgsMapToolFeatureAction::doAction( QgsVectorLayer *layer, int x, int y )
// define custom substitutions: layer id and clicked coords
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::mapSettingsScope( mCanvas->mapSettings() );
QgsExpressionContextScope* actionScope = new QgsExpressionContextScope();
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "click_x" ), point.x(), true ) );
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgspointmarkeritem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "qgssymbol.h"
#include "qgsmapcanvas.h"
#include "qgsmapsettings.h"
#include "qgsproject.h"
#include <QPainter>
#include <cmath>

Expand All @@ -31,7 +32,7 @@ QgsRenderContext QgsPointMarkerItem::renderContext( QPainter* painter )
{
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::atlasScope( nullptr );
if ( mMapCanvas )
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsprojectproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa

// Variables editor
mVariableEditor->context()->appendScope( QgsExpressionContextUtils::globalScope() );
mVariableEditor->context()->appendScope( QgsExpressionContextUtils::projectScope() );
mVariableEditor->context()->appendScope( QgsExpressionContextUtils::projectScope( QgsProject::instance() ) );
mVariableEditor->reloadContext();
mVariableEditor->setEditableScopeIndex( 1 );

Expand Down
53 changes: 25 additions & 28 deletions src/app/qgsrulebasedlabelingwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "qgsfeatureiterator.h"
#include "qgslabelinggui.h"
#include "qgsmapcanvas.h"
#include "qgsproject.h"
#include "qgsrulebasedlabeling.h"
#include "qgsvectorlayer.h"
#include "qgsvectorlayerlabeling.h"
Expand All @@ -27,6 +28,27 @@
#include <QClipboard>
#include <QMessageBox>


static QList<QgsExpressionContextScope*> _globalProjectAtlasMapLayerScopes( QgsMapCanvas* mapCanvas, const QgsMapLayer* layer )
{
QList<QgsExpressionContextScope*> scopes;
scopes << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::atlasScope( nullptr );
if ( mapCanvas )
{
scopes << QgsExpressionContextUtils::mapSettingsScope( mapCanvas->mapSettings() )
<< new QgsExpressionContextScope( mapCanvas->expressionContextScope() );
}
else
{
scopes << QgsExpressionContextUtils::mapSettingsScope( QgsMapSettings() );
}
scopes << QgsExpressionContextUtils::layerScope( layer );
return scopes;
}


QgsRuleBasedLabelingWidget::QgsRuleBasedLabelingWidget( QgsVectorLayer* layer, QgsMapCanvas* canvas, QWidget* parent )
: QgsPanelWidget( parent )
, mLayer( layer )
Expand Down Expand Up @@ -643,20 +665,7 @@ void QgsLabelingRulePropsWidget::testFilter()
return;
}

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::atlasScope( nullptr );
if ( mMapCanvas )
{
context << QgsExpressionContextUtils::mapSettingsScope( mMapCanvas->mapSettings() )
<< new QgsExpressionContextScope( mMapCanvas->expressionContextScope() );
}
else
{
context << QgsExpressionContextUtils::mapSettingsScope( QgsMapSettings() );
}
context << QgsExpressionContextUtils::layerScope( mLayer );
QgsExpressionContext context( _globalProjectAtlasMapLayerScopes( mMapCanvas, mLayer ) );

if ( !filter.prepare( &context ) )
{
Expand Down Expand Up @@ -686,22 +695,10 @@ void QgsLabelingRulePropsWidget::testFilter()
QMessageBox::information( this, tr( "Filter" ), tr( "Filter returned %n feature(s)", "number of filtered features", count ) );
}


void QgsLabelingRulePropsWidget::buildExpression()
{
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::atlasScope( nullptr );
if ( mMapCanvas )
{
context << QgsExpressionContextUtils::mapSettingsScope( mMapCanvas->mapSettings() )
<< new QgsExpressionContextScope( mMapCanvas->expressionContextScope() );
}
else
{
context << QgsExpressionContextUtils::mapSettingsScope( QgsMapSettings() );
}
context << QgsExpressionContextUtils::layerScope( mLayer );
QgsExpressionContext context( _globalProjectAtlasMapLayerScopes( mMapCanvas, mLayer ) );

QgsExpressionBuilderDialog dlg( mLayer, editFilter->text(), this, QStringLiteral( "generic" ), context );

Expand Down
5 changes: 1 addition & 4 deletions src/app/qgsselectbyformdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ void QgsSelectByFormDialog::zoomToFeatures( const QString& filter )
{
QgsFeatureIds ids;

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mLayer );
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );

QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( filter )
.setExpressionContext( context )
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsstatisticalsummarydockwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ QgsExpressionContext QgsStatisticalSummaryDockWidget::createExpressionContext()
{
QgsExpressionContext expContext;
expContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::mapSettingsScope( QgisApp::instance()->mapCanvas()->mapSettings() )
<< QgsExpressionContextUtils::layerScope( mLayer );

Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsvectorlayerproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
connect( mOptionsStackedWidget, SIGNAL( currentChanged( int ) ), this, SLOT( mOptionsStackedWidget_CurrentChanged( int ) ) );

mContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::atlasScope( nullptr )
<< QgsExpressionContextUtils::mapSettingsScope( QgisApp::instance()->mapCanvas()->mapSettings() )
<< QgsExpressionContextUtils::layerScope( mLayer );
Expand Down Expand Up @@ -1333,7 +1333,7 @@ void QgsVectorLayerProperties::updateVariableEditor()
QgsExpressionContext context;
mVariableEditor->setContext( &context );
mVariableEditor->context()->appendScope( QgsExpressionContextUtils::globalScope() );
mVariableEditor->context()->appendScope( QgsExpressionContextUtils::projectScope() );
mVariableEditor->context()->appendScope( QgsExpressionContextUtils::projectScope( QgsProject::instance() ) );
mVariableEditor->context()->appendScope( QgsExpressionContextUtils::layerScope( mLayer ) );
mVariableEditor->reloadContext();
mVariableEditor->setEditableScopeIndex( 2 );
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgsatlascomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ QgsExpressionContext QgsAtlasComposition::createExpressionContext()
{
QgsExpressionContext expressionContext;
expressionContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope();
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() );
if ( mComposition )
expressionContext << QgsExpressionContextUtils::compositionScope( mComposition );

Expand Down
Loading

0 comments on commit 660867c

Please sign in to comment.