QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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"
21#include "qgsfeature.h"
22#include "qgsmeshdataprovider.h"
23#include "qgspointlocator.h"
24
25#include <QString>
26#include <QVariantMap>
27
28using namespace Qt::StringLiterals;
29
31class QgsProject;
32class QgsLayout;
33class QgsSymbol;
34class QgsLayoutAtlas;
35class QgsLayoutItem;
36class QgsMapSettings;
38class QgsProcessingModelAlgorithm;
41
48
50{
51 public:
52
59
66 static QgsExpressionContextScope *formScope( const QgsFeature &formFeature = QgsFeature( ), const QString &formMode = QString() ) SIP_FACTORY;
67
74 static QgsExpressionContextScope *parentFormScope( const QgsFeature &formFeature = QgsFeature( ), const QString &formMode = QString() ) SIP_FACTORY;
75
85 static void setGlobalVariable( const QString &name, const QVariant &value );
86
95 static void setGlobalVariables( const QVariantMap &variables );
96
104 static void removeGlobalVariable( const QString &name );
105
113
124 static void setProjectVariable( QgsProject *project, const QString &name, const QVariant &value );
125
135 static void setProjectVariables( QgsProject *project, const QVariantMap &variables );
136
145 static void removeProjectVariable( QgsProject *project, const QString &name );
146
152
156 static QList<QgsExpressionContextScope *> globalProjectLayerScopes( const QgsMapLayer *layer ) SIP_FACTORY;
157
167 static void setLayerVariable( QgsMapLayer *layer, const QString &name, const QVariant &value );
168
177 static void setLayerVariables( QgsMapLayer *layer, const QVariantMap &variables );
178
184
190 static QgsExpressionContextScope *mapToolCaptureScope( const QList<QgsPointLocator::Match> &matches ) SIP_FACTORY;
191
200
206 static QgsExpressionContextScope *updateSymbolScope( const QgsSymbol *symbol, QgsExpressionContextScope *symbolScope = nullptr );
207
213
223 static void setLayoutVariable( QgsLayout *layout, const QString &name, const QVariant &value );
224
233 static void setLayoutVariables( QgsLayout *layout, const QVariantMap &variables );
234
241
249
257 static void setLayoutItemVariable( QgsLayoutItem *item, const QString &name, const QVariant &value );
258
265 static void setLayoutItemVariables( QgsLayoutItem *item, const QVariantMap &variables );
266
274
283 static void setLayoutMultiFrameVariable( QgsLayoutMultiFrame *frame, const QString &name, const QVariant &value );
284
292 static void setLayoutMultiFrameVariables( QgsLayoutMultiFrame *frame, const QVariantMap &variables );
293
299 static QgsExpressionContext createFeatureBasedContext( const QgsFeature &feature, const QgsFields &fields );
300
308
315 static QgsExpressionContextScope *processingModelAlgorithmScope( const QgsProcessingModelAlgorithm *model, const QVariantMap &parameters, QgsProcessingContext &context ) SIP_FACTORY;
316
321 static QgsExpressionContextScope *notificationScope( const QString &message = QString() ) SIP_FACTORY;
322
326 static void registerContextFunctions();
327
333
334 private:
335
336 class GetLayerVisibility : public QgsScopedExpressionFunction
337 {
338 public:
339 GetLayerVisibility( const QList<QgsMapLayer *> &layers, double scale = 0 );
340 QVariant func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * ) override;
341 QgsScopedExpressionFunction *clone() const override;
342
343 private:
344 GetLayerVisibility();
345
346 QList< QPointer< QgsMapLayer > > mLayers;
347 QMap< QPointer< QgsMapLayer >, QPair< double, double > > mScaleBasedVisibilityDetails;
348 double mScale = 0.0;
349
350 };
351
352 friend class QgsLayoutItemMap; // needs access to GetLayerVisibility
353
354};
355
357#ifndef SIP_RUN
358class LoadLayerFunction : public QgsScopedExpressionFunction
359{
360 public:
361 LoadLayerFunction()
362 : QgsScopedExpressionFunction( u"load_layer"_s, QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( u"uri"_s ) << QgsExpressionFunction::Parameter( u"provider"_s ), u"Map Layers"_s )
363 {}
364
365 QVariant func( const QVariantList &, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * ) override;
366 bool isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
367
368 QgsScopedExpressionFunction *clone() const override;
369
370};
371#endif
373
374#ifndef SIP_RUN
375
383{
384 public:
385
395 : mContext( context )
396 {
397 mContext.appendScope( scope );
398 }
399
401 {
402 delete mContext.popScope();
403 }
404
405 private:
406 QgsExpressionContext &mContext;
407};
408#endif
409
410#endif // QGSEXPRESSIONCONTEXTUTILS_H
QgsExpressionContextScopePopper(QgsExpressionContext &context, QgsExpressionContextScope *scope)
Constructor for QgsExpressionContextScopePopper.
Single scope for storing variables and functions for use within a QgsExpressionContext.
Contains utilities for working with QgsExpressionContext objects, including methods for creating scop...
static QgsExpressionContextScope * layoutItemScope(const QgsLayoutItem *item)
Creates a new scope which contains variables and functions relating to a QgsLayoutItem.
static void setLayoutMultiFrameVariable(QgsLayoutMultiFrame *frame, const QString &name, const QVariant &value)
Sets a layout multi frame context variable, with the given name and value.
static void setLayerVariable(QgsMapLayer *layer, const QString &name, const QVariant &value)
Sets a layer context variable.
static QgsExpressionContextScope * processingModelAlgorithmScope(const QgsProcessingModelAlgorithm *model, const QVariantMap &parameters, QgsProcessingContext &context)
Creates a new scope which contains variables and functions relating to a processing model algorithm,...
static QgsExpressionContextScope * updateSymbolScope(const QgsSymbol *symbol, QgsExpressionContextScope *symbolScope=nullptr)
Updates a symbol scope related to a QgsSymbol to an expression context.
static void setProjectVariables(QgsProject *project, const QVariantMap &variables)
Sets all project context variables.
static QgsExpressionContextScope * layoutScope(const QgsLayout *layout)
Creates a new scope which contains variables and functions relating to a QgsLayout layout.
static QgsExpressionContext createFeatureBasedContext(const QgsFeature &feature, const QgsFields &fields)
Helper function for creating an expression context which contains just a feature and fields collectio...
static void removeProjectVariable(QgsProject *project, const QString &name)
Remove project context variable.
static void setLayerVariables(QgsMapLayer *layer, const QVariantMap &variables)
Sets all layer context variables.
static void setGlobalVariables(const QVariantMap &variables)
Sets all global context variables.
static void setLayoutItemVariables(QgsLayoutItem *item, const QVariantMap &variables)
Sets all layout item context variables for an item.
static void setLayoutMultiFrameVariables(QgsLayoutMultiFrame *frame, const QVariantMap &variables)
Sets all layout multiframe context variables for an frame.
static QgsExpressionContextScope * processingAlgorithmScope(const QgsProcessingAlgorithm *algorithm, const QVariantMap &parameters, QgsProcessingContext &context)
Creates a new scope which contains variables and functions relating to a processing algorithm,...
static QgsExpressionContextScope * notificationScope(const QString &message=QString())
Creates a new scope which contains variables and functions relating to provider notifications.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static QgsExpressionContextScope * parentFormScope(const QgsFeature &formFeature=QgsFeature(), const QString &formMode=QString())
Creates a new scope which contains functions and variables from the current parent attribute form/tab...
static void setLayoutVariable(QgsLayout *layout, const QString &name, const QVariant &value)
Sets a layout context variable.
static QgsExpressionContextScope * formScope(const QgsFeature &formFeature=QgsFeature(), const QString &formMode=QString())
Creates a new scope which contains functions and variables from the current attribute form/table form...
static QgsExpressionContextScope * multiFrameScope(const QgsLayoutMultiFrame *frame)
Creates a new scope which contains variables and functions relating to a QgsLayoutMultiFrame.
static void setLayoutItemVariable(QgsLayoutItem *item, const QString &name, const QVariant &value)
Sets a layout item context variable, with the given name and value.
static QgsExpressionContextScope * mapToolCaptureScope(const QList< QgsPointLocator::Match > &matches)
Sets the expression context variables which are available for expressions triggered by a map tool cap...
static QgsExpressionContextScope * atlasScope(const QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
static void registerContextFunctions()
Registers all known core functions provided by QgsExpressionContextScope objects.
static QgsExpressionContextScope * mapLayerPositionScope(const QgsPointXY &position)
Sets the expression context variables which are available for expressions triggered by moving the mou...
static void setLayoutVariables(QgsLayout *layout, const QVariantMap &variables)
Sets all layout context variables.
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object.
static void removeGlobalVariable(const QString &name)
Remove a global context variable.
static void setGlobalVariable(const QString &name, const QVariant &value)
Sets a global context variable.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
static QgsExpressionContextScope * meshExpressionScope(QgsMesh::ElementType elementType)
Creates a new scope which contains functions relating to mesh layer element elementType.
static void setProjectVariable(QgsProject *project, const QString &name, const QVariant &value)
Sets a project context variable.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
An abstract base class for defining QgsExpression functions.
An expression node for expression functions.
Handles parsing and evaluation of expressions (formerly called "search strings").
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
Container of fields for a vector layer.
Definition qgsfields.h:46
Used to render QgsLayout as an atlas, by iterating over the features from an associated vector layer.
Base class for graphical items within a QgsLayout.
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:50
Base class for all map layer types.
Definition qgsmaplayer.h:83
Contains configuration for rendering maps.
Represents a 2D point.
Definition qgspointxy.h:62
Abstract base class for processing algorithms.
Contains information about the context in which a processing algorithm is executed.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:112
Expression function for use within a QgsExpressionContextScope.
virtual QgsScopedExpressionFunction * clone() const =0
Returns a clone of the function.
bool isStatic(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context) const override
Will be called during prepare to determine if the function is static.
QVariant func(const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node) override=0
Returns result of evaluating the function.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:231
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
#define SIP_FACTORY
Definition qgis_sip.h:84
ElementType
Defines type of mesh elements.