QGIS API Documentation 4.3.0-Master (91d4a333d5d)
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:
57 static std::unique_ptr<QgsExpressionContextScope> globalScope();
58
65 static std::unique_ptr<QgsExpressionContextScope> formScope( const QgsFeature &formFeature = QgsFeature(), const QString &formMode = QString() );
66
73 static std::unique_ptr<QgsExpressionContextScope> parentFormScope( const QgsFeature &formFeature = QgsFeature(), const QString &formMode = QString() );
74
84 static void setGlobalVariable( const QString &name, const QVariant &value );
85
94 static void setGlobalVariables( const QVariantMap &variables );
95
103 static void removeGlobalVariable( const QString &name );
104
111 static std::unique_ptr<QgsExpressionContextScope> projectScope( const QgsProject *project );
112
123 static void setProjectVariable( QgsProject *project, const QString &name, const QVariant &value );
124
134 static void setProjectVariables( QgsProject *project, const QVariantMap &variables );
135
144 static void removeProjectVariable( QgsProject *project, const QString &name );
145
150 static std::unique_ptr<QgsExpressionContextScope> layerScope( const QgsMapLayer *layer );
151
155 static std::vector<std::unique_ptr<QgsExpressionContextScope>> globalProjectLayerScopes( const QgsMapLayer *layer );
156
166 static void setLayerVariable( QgsMapLayer *layer, const QString &name, const QVariant &value );
167
176 static void setLayerVariables( QgsMapLayer *layer, const QVariantMap &variables );
177
182 static std::unique_ptr<QgsExpressionContextScope> mapSettingsScope( const QgsMapSettings &mapSettings );
183
189 static std::unique_ptr<QgsExpressionContextScope> mapToolCaptureScope( const QList<QgsPointLocator::Match> &matches );
190
198 static std::unique_ptr<QgsExpressionContextScope> mapLayerPositionScope( const QgsPointXY &position );
199
205 static QgsExpressionContextScope *updateSymbolScope( const QgsSymbol *symbol, QgsExpressionContextScope *symbolScope = nullptr );
206
211 static std::unique_ptr<QgsExpressionContextScope> layoutScope( const QgsLayout *layout );
212
222 static void setLayoutVariable( QgsLayout *layout, const QString &name, const QVariant &value );
223
232 static void setLayoutVariables( QgsLayout *layout, const QVariantMap &variables );
233
239 static std::unique_ptr<QgsExpressionContextScope> atlasScope( const QgsLayoutAtlas *atlas );
240
247 static std::unique_ptr<QgsExpressionContextScope> layoutItemScope( const QgsLayoutItem *item );
248
256 static void setLayoutItemVariable( QgsLayoutItem *item, const QString &name, const QVariant &value );
257
264 static void setLayoutItemVariables( QgsLayoutItem *item, const QVariantMap &variables );
265
272 static std::unique_ptr<QgsExpressionContextScope> multiFrameScope( const QgsLayoutMultiFrame *frame );
273
282 static void setLayoutMultiFrameVariable( QgsLayoutMultiFrame *frame, const QString &name, const QVariant &value );
283
291 static void setLayoutMultiFrameVariables( QgsLayoutMultiFrame *frame, const QVariantMap &variables );
292
298 static QgsExpressionContext createFeatureBasedContext( const QgsFeature &feature, const QgsFields &fields );
299
306 static std::unique_ptr<QgsExpressionContextScope> processingAlgorithmScope( const QgsProcessingAlgorithm *algorithm, const QVariantMap &parameters, QgsProcessingContext &context );
307
314 static std::unique_ptr<QgsExpressionContextScope> processingModelAlgorithmScope( const QgsProcessingModelAlgorithm *model, const QVariantMap &parameters, QgsProcessingContext &context );
315
320 static std::unique_ptr<QgsExpressionContextScope> notificationScope( const QString &message = QString() );
321
330
335 static std::unique_ptr<QgsExpressionContextScope> meshExpressionScope( QgsMesh::ElementType elementType );
336
337 private:
338 class GetLayerVisibility : public QgsScopedExpressionFunction
339 {
340 public:
341 GetLayerVisibility( const QList<QgsMapLayer *> &layers, double scale = 0 );
342 QVariant func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * ) override;
343 QgsScopedExpressionFunction *clone() const override;
344
345 private:
346 GetLayerVisibility();
347
348 QList< QPointer< QgsMapLayer > > mLayers;
349 QMap< QPointer< QgsMapLayer >, QPair< double, double > > mScaleBasedVisibilityDetails;
350 double mScale = 0.0;
351 };
352
353 friend class QgsLayoutItemMap; // needs access to GetLayerVisibility
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#endif
372
373#ifndef SIP_RUN
374
382{
383 public:
393 : mContext( context )
394 {
395 mContext.appendScope( scope );
396 }
397
398 ~QgsExpressionContextScopePopper() { delete mContext.popScope(); }
399
400 private:
401 QgsExpressionContext &mContext;
402};
403#endif
404
405#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 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 std::unique_ptr< QgsExpressionContextScope > globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
static QgsExpressionContextScope * updateSymbolScope(const QgsSymbol *symbol, QgsExpressionContextScope *symbolScope=nullptr)
Updates a symbol scope related to a QgsSymbol to an expression context.
static std::unique_ptr< QgsExpressionContextScope > mapLayerPositionScope(const QgsPointXY &position)
Sets the expression context variables which are available for expressions triggered by moving the mou...
static void setProjectVariables(QgsProject *project, const QVariantMap &variables)
Sets all project context variables.
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 std::unique_ptr< QgsExpressionContextScope > layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
static void setLayerVariables(QgsMapLayer *layer, const QVariantMap &variables)
Sets all layer context variables.
static std::unique_ptr< QgsExpressionContextScope > mapToolCaptureScope(const QList< QgsPointLocator::Match > &matches)
Sets the expression context variables which are available for expressions triggered by a map tool cap...
static void setGlobalVariables(const QVariantMap &variables)
Sets all global context variables.
static std::unique_ptr< 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 void registerContextFunctions(QgsExpression::SharedFunctionPtrList &functions)
Registers all known core functions provided by QgsExpressionContextScope objects.
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 std::unique_ptr< QgsExpressionContextScope > notificationScope(const QString &message=QString())
Creates a new scope which contains variables and functions relating to provider notifications.
static std::vector< std::unique_ptr< QgsExpressionContextScope > > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
static std::unique_ptr< QgsExpressionContextScope > layoutItemScope(const QgsLayoutItem *item)
Creates a new scope which contains variables and functions relating to a QgsLayoutItem.
static std::unique_ptr< 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 void setLayoutVariable(QgsLayout *layout, const QString &name, const QVariant &value)
Sets a layout context variable.
static std::unique_ptr< QgsExpressionContextScope > meshExpressionScope(QgsMesh::ElementType elementType)
Creates a new scope which contains functions relating to mesh layer element elementType.
static void setLayoutItemVariable(QgsLayoutItem *item, const QString &name, const QVariant &value)
Sets a layout item context variable, with the given name and value.
static std::unique_ptr< QgsExpressionContextScope > layoutScope(const QgsLayout *layout)
Creates a new scope which contains variables and functions relating to a QgsLayout layout.
static std::unique_ptr< QgsExpressionContextScope > multiFrameScope(const QgsLayoutMultiFrame *frame)
Creates a new scope which contains variables and functions relating to a QgsLayoutMultiFrame.
static void setLayoutVariables(QgsLayout *layout, const QVariantMap &variables)
Sets all layout context variables.
static void removeGlobalVariable(const QString &name)
Remove a global context variable.
static std::unique_ptr< QgsExpressionContextScope > mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object.
static void setGlobalVariable(const QString &name, const QVariant &value)
Sets a global context variable.
static std::unique_ptr< 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 void setProjectVariable(QgsProject *project, const QString &name, const QVariant &value)
Sets a project context variable.
static std::unique_ptr< 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 std::unique_ptr< QgsExpressionContextScope > atlasScope(const QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
static std::unique_ptr< QgsExpressionContextScope > projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
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.
A thread-safe container of shared pointers to registered 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:45
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:51
Base class for all map layer types.
Definition qgsmaplayer.h:84
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:114
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:227
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_SKIP
Definition qgis_sip.h:138
ElementType
Defines type of mesh elements.