QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsexpressioncontextutils.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsexpressioncontextutils.h
3  ---------------------------
4  Date : April 2015
5  Copyright : (C) 2015 by Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSEXPRESSIONCONTEXTUTILS_H
16 #define QGSEXPRESSIONCONTEXTUTILS_H
17 
18 #include "qgis_core.h"
19 #include "qgis_sip.h"
20 #include "qgsfeature.h"
21 #include "qgspointlocator.h"
22 #include "qgsexpressioncontext.h"
23 #include "qgsmeshdataprovider.h"
24 #include <QString>
25 #include <QVariantMap>
26 
28 class QgsProject;
29 class QgsLayout;
30 class QgsSymbol;
31 class QgsLayoutAtlas;
32 class QgsLayoutItem;
34 class QgsProcessingModelAlgorithm;
37 
46 class CORE_EXPORT QgsExpressionContextUtils
47 {
48  public:
49 
55  static QgsExpressionContextScope *globalScope() SIP_FACTORY;
56 
63  static QgsExpressionContextScope *formScope( const QgsFeature &formFeature = QgsFeature( ), const QString &formMode = QString() ) SIP_FACTORY;
64 
71  static QgsExpressionContextScope *parentFormScope( const QgsFeature &formFeature = QgsFeature( ), const QString &formMode = QString() ) SIP_FACTORY;
72 
82  static void setGlobalVariable( const QString &name, const QVariant &value );
83 
92  static void setGlobalVariables( const QVariantMap &variables );
93 
101  static void removeGlobalVariable( const QString &name );
102 
109  static QgsExpressionContextScope *projectScope( const QgsProject *project ) SIP_FACTORY;
110 
121  static void setProjectVariable( QgsProject *project, const QString &name, const QVariant &value );
122 
132  static void setProjectVariables( QgsProject *project, const QVariantMap &variables );
133 
142  static void removeProjectVariable( QgsProject *project, const QString &name );
143 
148  static QgsExpressionContextScope *layerScope( const QgsMapLayer *layer ) SIP_FACTORY;
149 
154  static QList<QgsExpressionContextScope *> globalProjectLayerScopes( const QgsMapLayer *layer ) SIP_FACTORY;
155 
165  static void setLayerVariable( QgsMapLayer *layer, const QString &name, const QVariant &value );
166 
175  static void setLayerVariables( QgsMapLayer *layer, const QVariantMap &variables );
176 
181  static QgsExpressionContextScope *mapSettingsScope( const QgsMapSettings &mapSettings ) SIP_FACTORY;
182 
189  static QgsExpressionContextScope *mapToolCaptureScope( const QList<QgsPointLocator::Match> &matches ) SIP_FACTORY;
190 
197  static QgsExpressionContextScope *updateSymbolScope( const QgsSymbol *symbol, QgsExpressionContextScope *symbolScope = nullptr );
198 
204  static QgsExpressionContextScope *layoutScope( const QgsLayout *layout ) SIP_FACTORY;
205 
216  static void setLayoutVariable( QgsLayout *layout, const QString &name, const QVariant &value );
217 
227  static void setLayoutVariables( QgsLayout *layout, const QVariantMap &variables );
228 
234  static QgsExpressionContextScope *atlasScope( const QgsLayoutAtlas *atlas ) SIP_FACTORY;
235 
243  static QgsExpressionContextScope *layoutItemScope( const QgsLayoutItem *item ) SIP_FACTORY;
244 
253  static void setLayoutItemVariable( QgsLayoutItem *item, const QString &name, const QVariant &value );
254 
262  static void setLayoutItemVariables( QgsLayoutItem *item, const QVariantMap &variables );
263 
270  static QgsExpressionContextScope *multiFrameScope( const QgsLayoutMultiFrame *frame ) SIP_FACTORY;
271 
280  static void setLayoutMultiFrameVariable( QgsLayoutMultiFrame *frame, const QString &name, const QVariant &value );
281 
289  static void setLayoutMultiFrameVariables( QgsLayoutMultiFrame *frame, const QVariantMap &variables );
290 
296  static QgsExpressionContext createFeatureBasedContext( const QgsFeature &feature, const QgsFields &fields );
297 
304  static QgsExpressionContextScope *processingAlgorithmScope( const QgsProcessingAlgorithm *algorithm, const QVariantMap &parameters, QgsProcessingContext &context ) SIP_FACTORY;
305 
312  static QgsExpressionContextScope *processingModelAlgorithmScope( const QgsProcessingModelAlgorithm *model, const QVariantMap &parameters, QgsProcessingContext &context ) SIP_FACTORY;
313 
318  static QgsExpressionContextScope *notificationScope( const QString &message = QString() ) SIP_FACTORY;
319 
323  static void registerContextFunctions();
324 
329  static QgsExpressionContextScope *meshExpressionScope( QgsMesh::ElementType elementType ) SIP_FACTORY;
330 
331  private:
332 
333  class GetLayerVisibility : public QgsScopedExpressionFunction
334  {
335  public:
336  GetLayerVisibility( const QList<QgsMapLayer *> &layers, double scale = 0 );
337  QVariant func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * ) override;
338  QgsScopedExpressionFunction *clone() const override;
339 
340  private:
341  GetLayerVisibility();
342 
343  QList< QPointer< QgsMapLayer > > mLayers;
344  QMap< QPointer< QgsMapLayer >, QPair< double, double > > mScaleBasedVisibilityDetails;
345  double mScale = 0.0;
346 
347  };
348 
349  friend class QgsLayoutItemMap; // needs access to GetLayerVisibility
350 
351 };
352 
353 #ifndef SIP_RUN
354 
362 {
363  public:
364 
374  : mContext( context )
375  {
376  mContext.appendScope( scope );
377  }
378 
380  {
381  delete mContext.popScope();
382  }
383 
384  private:
385  QgsExpressionContext &mContext;
386 };
387 #endif
388 
389 #endif // QGSEXPRESSIONCONTEXTUTILS_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsExpressionContextScopePopper
RAII class to pop scope from an expression context on destruction.
Definition: qgsexpressioncontextutils.h:361
QgsExpressionContext::popScope
QgsExpressionContextScope * popScope()
Removes the last scope from the expression context and return it.
Definition: qgsexpressioncontext.cpp:504
algorithm
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
qgsfeature.h
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:92
QgsLayoutMultiFrame
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
Definition: qgslayoutmultiframe.h:48
QgsExpressionContextUtils
Contains utilities for working with QgsExpressionContext objects, including methods for creating scop...
Definition: qgsexpressioncontextutils.h:46
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:103
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
qgsexpressioncontext.h
QgsMesh::ElementType
ElementType
Defines type of mesh elements.
Definition: qgsmeshdataprovider.h:65
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsExpressionContextScopePopper::QgsExpressionContextScopePopper
QgsExpressionContextScopePopper(QgsExpressionContext &context, QgsExpressionContextScope *scope)
Constructor for QgsExpressionContextScopePopper.
Definition: qgsexpressioncontextutils.h:373
qgis_sip.h
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:112
QgsExpressionNodeFunction
An expression node for expression functions.
Definition: qgsexpressionnodeimpl.h:395
QgsExpressionContextScopePopper::~QgsExpressionContextScopePopper
~QgsExpressionContextScopePopper()
Definition: qgsexpressioncontextutils.h:379
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext....
Definition: qgsexpressioncontext.h:113
QgsExpressionContext::appendScope
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
Definition: qgsexpressioncontext.cpp:494
QgsLayoutItemMap
Layout graphical items for displaying a map.
Definition: qgslayoutitemmap.h:317
QgsProcessingAlgorithm
Abstract base class for processing algorithms.
Definition: qgsprocessingalgorithm.h:52
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
qgspointlocator.h
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map. The rendering itself is don...
Definition: qgsmapsettings.h:88
QgsScopedExpressionFunction
Expression function for use within a QgsExpressionContextScope. This differs from a standard QgsExpre...
Definition: qgsexpressioncontext.h:39
QgsLayoutAtlas
Class used to render QgsLayout as an atlas, by iterating over the features from an associated vector ...
Definition: qgslayoutatlas.h:41
qgsmeshdataprovider.h