Skip to content
Permalink
Browse files

Explicitly pass QgsProject object when dealing with expression contexts

Continued effort to reduce number of uses of QgsProject as singleton...
  • Loading branch information
wonder-sk committed Jan 6, 2017
1 parent af5eb6a commit 660867cb4e8a2d2229a4bbd035fb05039be90b74
Showing with 241 additions and 289 deletions.
  1. +6 −0 doc/api_break.dox
  2. +22 −3 python/core/qgsexpressioncontext.sip
  3. +6 −0 python/gui/symbology-ng/qgssymbolwidgetcontext.sip
  4. +1 −1 src/app/composer/qgscompositionwidget.cpp
  5. +4 −13 src/app/qgsattributetabledialog.cpp
  6. +3 −3 src/app/qgsdiagramproperties.cpp
  7. +2 −8 src/app/qgsfieldcalculator.cpp
  8. +1 −1 src/app/qgsfieldsproperties.cpp
  9. +1 −4 src/app/qgsidentifyresultsdialog.cpp
  10. +1 −1 src/app/qgslabelinggui.cpp
  11. +1 −1 src/app/qgslabelpropertydialog.cpp
  12. +1 −1 src/app/qgsmaptoolfeatureaction.cpp
  13. +2 −1 src/app/qgspointmarkeritem.cpp
  14. +1 −1 src/app/qgsprojectproperties.cpp
  15. +25 −28 src/app/qgsrulebasedlabelingwidget.cpp
  16. +1 −4 src/app/qgsselectbyformdialog.cpp
  17. +1 −1 src/app/qgsstatisticalsummarydockwidget.cpp
  18. +2 −2 src/app/qgsvectorlayerproperties.cpp
  19. +1 −1 src/core/composer/qgsatlascomposition.cpp
  20. +2 −1 src/core/composer/qgscomposerobject.cpp
  21. +1 −1 src/core/composer/qgscomposition.cpp
  22. +1 −4 src/core/dxf/qgsdxfexport.cpp
  23. +1 −4 src/core/fieldformatter/qgsrelationreferencefieldformatter.cpp
  24. +1 −1 src/core/qgsactionmanager.cpp
  25. +1 −1 src/core/qgsexpression.cpp
  26. +32 −8 src/core/qgsexpressioncontext.cpp
  27. +27 −4 src/core/qgsexpressioncontext.h
  28. +1 −1 src/core/qgsproject.cpp
  29. +1 −4 src/core/qgsvectorfilewriter.cpp
  30. +5 −20 src/core/qgsvectorlayer.cpp
  31. +1 −1 src/core/qgsvectorlayerfeatureiterator.cpp
  32. +1 −4 src/core/qgsvectorlayerutils.cpp
  33. +1 −3 src/gui/attributetable/qgsattributetablefiltermodel.cpp
  34. +1 −3 src/gui/attributetable/qgsattributetablemodel.cpp
  35. +2 −8 src/gui/attributetable/qgsdualview.cpp
  36. +2 −8 src/gui/attributetable/qgsfeaturelistmodel.cpp
  37. +1 −4 src/gui/attributetable/qgsfieldconditionalformatwidget.cpp
  38. +2 −8 src/gui/editorwidgets/qgsrelationreferencewidget.cpp
  39. +1 −4 src/gui/editorwidgets/qgsvaluerelationconfigdlg.cpp
  40. +2 −1 src/gui/qgsexpressionlineedit.cpp
  41. +2 −8 src/gui/qgsexpressionselectiondialog.cpp
  42. +1 −1 src/gui/qgsfieldexpressionwidget.cpp
  43. +1 −4 src/gui/qgshtmlannotationitem.cpp
  44. +1 −1 src/gui/qgsmapcanvas.cpp
  45. +1 −4 src/gui/qgsmaptip.cpp
  46. +1 −4 src/gui/qgssearchquerybuilder.cpp
  47. +2 −2 src/gui/symbology-ng/qgscategorizedsymbolrendererwidget.cpp
  48. +1 −1 src/gui/symbology-ng/qgsgraduatedsymbolrendererwidget.cpp
  49. +1 −1 src/gui/symbology-ng/qgsheatmaprendererwidget.cpp
  50. +2 −1 src/gui/symbology-ng/qgslayerpropertieswidget.cpp
  51. +2 −1 src/gui/symbology-ng/qgsrendererwidget.cpp
  52. +7 −42 src/gui/symbology-ng/qgsrulebasedrendererwidget.cpp
  53. +2 −2 src/gui/symbology-ng/qgssizescalewidget.cpp
  54. +1 −16 src/gui/symbology-ng/qgssymbollayerwidget.cpp
  55. +1 −16 src/gui/symbology-ng/qgssymbolslistwidget.cpp
  56. +21 −0 src/gui/symbology-ng/qgssymbolwidgetcontext.cpp
  57. +6 −0 src/gui/symbology-ng/qgssymbolwidgetcontext.h
  58. +2 −1 src/providers/delimitedtext/qgsdelimitedtextfeatureiterator.cpp
  59. +2 −1 src/providers/memory/qgsmemoryfeatureiterator.cpp
  60. +1 −1 src/providers/virtual/qgsvirtuallayersqlitemodule.cpp
  61. +1 −1 src/server/qgswfsserver.cpp
  62. +2 −4 src/server/qgswmsserver.cpp
  63. +10 −9 tests/src/core/testqgsexpressioncontext.cpp
  64. +1 −1 tests/src/python/test_qgsvectorlayer.py
@@ -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}
----------

@@ -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 );
@@ -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.
*/
@@ -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
@@ -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/;
};

@@ -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 );
@@ -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 );
@@ -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 );

@@ -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" ) );
@@ -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 ) )
{
@@ -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 );
@@ -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 );

@@ -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 );
@@ -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" ) );
@@ -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 ) )
{
@@ -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 );

@@ -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();
@@ -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() );

@@ -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 );
@@ -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 ) );
@@ -17,6 +17,7 @@
#include "qgssymbol.h"
#include "qgsmapcanvas.h"
#include "qgsmapsettings.h"
#include "qgsproject.h"
#include <QPainter>
#include <cmath>

@@ -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 )
{
@@ -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 );

@@ -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"
@@ -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 )
@@ -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 ) )
{
@@ -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 );

@@ -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 )
@@ -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 );

@@ -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 );
@@ -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 );
@@ -718,7 +718,7 @@ QgsExpressionContext QgsAtlasComposition::createExpressionContext()
{
QgsExpressionContext expressionContext;
expressionContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope();
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() );
if ( mComposition )
expressionContext << QgsExpressionContextUtils::compositionScope( mComposition );

0 comments on commit 660867c

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