Skip to content

Commit 660867c

Browse files
committed
Explicitly pass QgsProject object when dealing with expression contexts
Continued effort to reduce number of uses of QgsProject as singleton...
1 parent af5eb6a commit 660867c

64 files changed

Lines changed: 241 additions & 289 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎doc/api_break.dox‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,12 @@ QgsExpression::Function {#qgis_api_break_3_0_QgsExpression_Function}
887887
- `QgsExpression::Function::helptext()` has been renamed to `helpText()`
888888

889889

890+
QgsExpressionContextUtils {#qgis_api_break_3_0_QgsExpressionContextUtils}
891+
-------------------------
892+
893+
- projectScope(), setProjectVariable() and setProjectVariables() require pointer to QgsProject as the first argument.
894+
895+
890896
QgsFeature {#qgis_api_break_3_0_QgsFeature}
891897
----------
892898

‎python/core/qgsexpressioncontext.sip‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ class QgsExpressionContext
217217

218218
QgsExpressionContext();
219219

220+
/** Initializes the context with given list of scopes.
221+
* Ownership of the scopes is transferred to the stack.
222+
* @note added in QGIS 3.0
223+
*/
224+
explicit QgsExpressionContext( const QList<QgsExpressionContextScope*>& scopes /Transfer/ );
225+
220226
/** Copy constructor
221227
*/
222228
QgsExpressionContext( const QgsExpressionContext& other );
@@ -359,6 +365,14 @@ class QgsExpressionContext
359365
*/
360366
void appendScope( QgsExpressionContextScope* scope /Transfer/ );
361367

368+
/** Appends a list of scopes to the end of the context. This scopes will override
369+
* any matching variables or functions provided by existing scopes within the
370+
* context. Ownership of the scopes is transferred to the stack.
371+
* @param scopes scopes to append to context
372+
* @note added in QGIS 3.0
373+
*/
374+
void appendScopes( const QList<QgsExpressionContextScope*>& scopes /Transfer/ );
375+
362376
/**
363377
* Removes the last scope from the expression context and return it.
364378
*/
@@ -484,15 +498,20 @@ class QgsExpressionContextUtils
484498
static QgsExpressionContextScope* globalScope() /Factory/;
485499
static void setGlobalVariable( const QString& name, const QVariant& value );
486500
static void setGlobalVariables( const QVariantMap& variables );
487-
static QgsExpressionContextScope* projectScope() /Factory/;
488-
static void setProjectVariable( const QString& name, const QVariant& value );
489-
static void setProjectVariables( const QVariantMap& variables );
501+
static QgsExpressionContextScope* projectScope( const QgsProject* project ) /Factory/;
502+
static void setProjectVariable( QgsProject* project, const QString& name, const QVariant& value );
503+
static void setProjectVariables( QgsProject* project, const QVariantMap& variables );
490504

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

510+
/** Creates a list of three scopes: global, layer's project and layer.
511+
* @note added in QGIS 3.0
512+
*/
513+
static QList<QgsExpressionContextScope*> globalProjectLayerScopes( const QgsMapLayer* layer ) /Factory/;
514+
496515
/** Sets a layer context variable. This variable will be contained within scopes retrieved via
497516
* layerScope().
498517
* @param layer map layer

‎python/gui/symbology-ng/qgssymbolwidgetcontext.sip‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,11 @@
5757
* @see setAdditionalExpressionContextScopes()
5858
*/
5959
QList< QgsExpressionContextScope > additionalExpressionContextScopes() const;
60+
61+
/** Returns list of scopes: global, project, atlas, map, layer.
62+
* Ownership is transferred to the caller.
63+
* @note added in QGIS 3.0
64+
*/
65+
QList<QgsExpressionContextScope*> globalProjectAtlasMapLayerScopes( const QgsMapLayer* layer ) const /Factory/;
6066
};
6167

‎src/app/composer/qgscompositionwidget.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ void QgsCompositionWidget::updateVariables()
205205
{
206206
QgsExpressionContext context;
207207
context << QgsExpressionContextUtils::globalScope()
208-
<< QgsExpressionContextUtils::projectScope()
208+
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
209209
<< QgsExpressionContextUtils::compositionScope( mComposition );
210210
mVariableEditor->setContext( &context );
211211
mVariableEditor->setEditableScopeIndex( 2 );

‎src/app/qgsattributetabledialog.cpp‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ QgsExpressionContext QgsAttributeTableDialog::createExpressionContext() const
5454
{
5555
QgsExpressionContext expContext;
5656
expContext << QgsExpressionContextUtils::globalScope()
57-
<< QgsExpressionContextUtils::projectScope();
57+
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() );
5858

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

475475
int rownum = 1;
476476

477-
QgsExpressionContext context;
478-
context << QgsExpressionContextUtils::globalScope()
479-
<< QgsExpressionContextUtils::projectScope()
480-
<< QgsExpressionContextUtils::layerScope( layer );
477+
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) );
481478

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

@@ -583,10 +580,7 @@ void QgsAttributeTableDialog::filterColumnChanged( QObject* filterAction )
583580
void QgsAttributeTableDialog::filterExpressionBuilder()
584581
{
585582
// Show expression builder
586-
QgsExpressionContext context;
587-
context << QgsExpressionContextUtils::globalScope()
588-
<< QgsExpressionContextUtils::projectScope()
589-
<< QgsExpressionContextUtils::layerScope( mLayer );
583+
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
590584

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

968-
QgsExpressionContext context;
969-
context << QgsExpressionContextUtils::globalScope()
970-
<< QgsExpressionContextUtils::projectScope()
971-
<< QgsExpressionContextUtils::layerScope( mLayer );
962+
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
972963

973964
if ( !filterExpression.prepare( &context ) )
974965
{

‎src/app/qgsdiagramproperties.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ QgsExpressionContext QgsDiagramProperties::createExpressionContext() const
4747
{
4848
QgsExpressionContext expContext;
4949
expContext << QgsExpressionContextUtils::globalScope()
50-
<< QgsExpressionContextUtils::projectScope()
50+
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
5151
<< QgsExpressionContextUtils::atlasScope( nullptr )
5252
<< QgsExpressionContextUtils::mapSettingsScope( mMapCanvas->mapSettings() )
5353
<< QgsExpressionContextUtils::layerScope( mLayer );
@@ -571,7 +571,7 @@ void QgsDiagramProperties::on_mFindMaximumValueButton_clicked()
571571
QgsExpression exp( sizeFieldNameOrExp );
572572
QgsExpressionContext context;
573573
context << QgsExpressionContextUtils::globalScope()
574-
<< QgsExpressionContextUtils::projectScope()
574+
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
575575
<< QgsExpressionContextUtils::mapSettingsScope( mMapCanvas->mapSettings() )
576576
<< QgsExpressionContextUtils::layerScope( mLayer );
577577

@@ -859,7 +859,7 @@ QString QgsDiagramProperties::showExpressionBuilder( const QString& initialExpre
859859
{
860860
QgsExpressionContext context;
861861
context << QgsExpressionContextUtils::globalScope()
862-
<< QgsExpressionContextUtils::projectScope()
862+
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
863863
<< QgsExpressionContextUtils::atlasScope( nullptr )
864864
<< QgsExpressionContextUtils::mapSettingsScope( mMapCanvas->mapSettings() )
865865
<< QgsExpressionContextUtils::layerScope( mLayer );

‎src/app/qgsfieldcalculator.cpp‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer* vl, QWidget* parent )
3939
return;
4040

4141

42-
QgsExpressionContext expContext;
43-
expContext << QgsExpressionContextUtils::globalScope()
44-
<< QgsExpressionContextUtils::projectScope()
45-
<< QgsExpressionContextUtils::layerScope( mVectorLayer );
42+
QgsExpressionContext expContext( QgsExpressionContextUtils::globalProjectLayerScopes( mVectorLayer ) );
4643

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

171-
QgsExpressionContext expContext;
172-
expContext << QgsExpressionContextUtils::globalScope()
173-
<< QgsExpressionContextUtils::projectScope()
174-
<< QgsExpressionContextUtils::layerScope( mVectorLayer );
168+
QgsExpressionContext expContext( QgsExpressionContextUtils::globalProjectLayerScopes( mVectorLayer ) );
175169

176170
if ( !exp.prepare( &expContext ) )
177171
{

‎src/app/qgsfieldsproperties.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ void QgsFieldsProperties::updateExpression()
841841

842842
QgsExpressionContext context;
843843
context << QgsExpressionContextUtils::globalScope()
844-
<< QgsExpressionContextUtils::projectScope();
844+
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() );
845845

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

‎src/app/qgsidentifyresultsdialog.cpp‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,7 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
587587
if ( !featureLabeled )
588588
{
589589
featItem->setText( 0, tr( "Title" ) );
590-
QgsExpressionContext context;
591-
context << QgsExpressionContextUtils::globalScope()
592-
<< QgsExpressionContextUtils::projectScope()
593-
<< QgsExpressionContextUtils::layerScope( vlayer );
590+
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( vlayer ) );
594591
context.setFeature( f );
595592

596593
QString value = QgsExpression( vlayer->displayExpression() ).evaluate( &context ).toString();

‎src/app/qgslabelinggui.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ QgsExpressionContext QgsLabelingGui::createExpressionContext() const
2626
{
2727
QgsExpressionContext expContext;
2828
expContext << QgsExpressionContextUtils::globalScope()
29-
<< QgsExpressionContextUtils::projectScope()
29+
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
3030
<< QgsExpressionContextUtils::atlasScope( nullptr )
3131
<< QgsExpressionContextUtils::mapSettingsScope( QgisApp::instance()->mapCanvas()->mapSettings() );
3232

0 commit comments

Comments
 (0)