QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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"
20#include "qgsfeature.h"
21#include "qgspointlocator.h"
23#include "qgsmeshdataprovider.h"
24
25#include <QString>
26#include <QVariantMap>
27
29class QgsProject;
30class QgsLayout;
31class QgsSymbol;
32class QgsLayoutAtlas;
33class QgsLayoutItem;
34class QgsMapSettings;
36class QgsProcessingModelAlgorithm;
39
49{
50 public:
51
57 static QgsExpressionContextScope *globalScope() SIP_FACTORY;
58
65 static QgsExpressionContextScope *formScope( const QgsFeature &formFeature = QgsFeature( ), const QString &formMode = QString() ) SIP_FACTORY;
66
73 static QgsExpressionContextScope *parentFormScope( const QgsFeature &formFeature = QgsFeature( ), const QString &formMode = QString() ) SIP_FACTORY;
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 QgsExpressionContextScope *projectScope( const QgsProject *project ) SIP_FACTORY;
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 QgsExpressionContextScope *layerScope( const QgsMapLayer *layer ) SIP_FACTORY;
151
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
183 static QgsExpressionContextScope *mapSettingsScope( const QgsMapSettings &mapSettings ) SIP_FACTORY;
184
191 static QgsExpressionContextScope *mapToolCaptureScope( const QList<QgsPointLocator::Match> &matches ) SIP_FACTORY;
192
200 static QgsExpressionContextScope *mapLayerPositionScope( const QgsPointXY &position ) SIP_FACTORY;
201
208 static QgsExpressionContextScope *updateSymbolScope( const QgsSymbol *symbol, QgsExpressionContextScope *symbolScope = nullptr );
209
215 static QgsExpressionContextScope *layoutScope( const QgsLayout *layout ) SIP_FACTORY;
216
227 static void setLayoutVariable( QgsLayout *layout, const QString &name, const QVariant &value );
228
238 static void setLayoutVariables( QgsLayout *layout, const QVariantMap &variables );
239
245 static QgsExpressionContextScope *atlasScope( const QgsLayoutAtlas *atlas ) SIP_FACTORY;
246
254 static QgsExpressionContextScope *layoutItemScope( const QgsLayoutItem *item ) SIP_FACTORY;
255
264 static void setLayoutItemVariable( QgsLayoutItem *item, const QString &name, const QVariant &value );
265
273 static void setLayoutItemVariables( QgsLayoutItem *item, const QVariantMap &variables );
274
281 static QgsExpressionContextScope *multiFrameScope( const QgsLayoutMultiFrame *frame ) SIP_FACTORY;
282
291 static void setLayoutMultiFrameVariable( QgsLayoutMultiFrame *frame, const QString &name, const QVariant &value );
292
300 static void setLayoutMultiFrameVariables( QgsLayoutMultiFrame *frame, const QVariantMap &variables );
301
307 static QgsExpressionContext createFeatureBasedContext( const QgsFeature &feature, const QgsFields &fields );
308
315 static QgsExpressionContextScope *processingAlgorithmScope( const QgsProcessingAlgorithm *algorithm, const QVariantMap &parameters, QgsProcessingContext &context ) SIP_FACTORY;
316
323 static QgsExpressionContextScope *processingModelAlgorithmScope( const QgsProcessingModelAlgorithm *model, const QVariantMap &parameters, QgsProcessingContext &context ) SIP_FACTORY;
324
329 static QgsExpressionContextScope *notificationScope( const QString &message = QString() ) SIP_FACTORY;
330
334 static void registerContextFunctions();
335
341
342 private:
343
344 class GetLayerVisibility : public QgsScopedExpressionFunction
345 {
346 public:
347 GetLayerVisibility( const QList<QgsMapLayer *> &layers, double scale = 0 );
348 QVariant func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * ) override;
349 QgsScopedExpressionFunction *clone() const override;
350
351 private:
352 GetLayerVisibility();
353
354 QList< QPointer< QgsMapLayer > > mLayers;
355 QMap< QPointer< QgsMapLayer >, QPair< double, double > > mScaleBasedVisibilityDetails;
356 double mScale = 0.0;
357
358 };
359
360 friend class QgsLayoutItemMap; // needs access to GetLayerVisibility
361
362};
363
365#ifndef SIP_RUN
366class LoadLayerFunction : public QgsScopedExpressionFunction
367{
368 public:
369 LoadLayerFunction()
370 : QgsScopedExpressionFunction( QStringLiteral( "load_layer" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "uri" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "provider" ) ), QStringLiteral( "Map Layers" ) )
371 {}
372
373 QVariant func( const QVariantList &, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * ) override;
374 bool isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
375
376 QgsScopedExpressionFunction *clone() const override;
377
378};
379#endif
381
382#ifndef SIP_RUN
383
391{
392 public:
393
403 : mContext( context )
404 {
405 mContext.appendScope( scope );
406 }
407
409 {
410 delete mContext.popScope();
411 }
412
413 private:
414 QgsExpressionContext &mContext;
415};
416#endif
417
418#endif // QGSEXPRESSIONCONTEXTUTILS_H
RAII class to pop scope from an expression context on destruction.
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 * meshExpressionScope(QgsMesh::ElementType elementType)
Creates a new scope which contains functions relating to mesh layer element elementType.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsExpressionContextScope * popScope()
Removes the last scope from the expression context and return it.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
A abstract base class for defining QgsExpression functions.
An expression node for expression functions.
Class for 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:56
Container of fields for a vector layer.
Definition qgsfields.h:45
Class used to render QgsLayout as an atlas, by iterating over the features from an associated vector ...
Layout graphical items for displaying a map.
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:74
The QgsMapSettings class contains configuration for rendering of the map.
A class to represent a 2D point.
Definition qgspointxy.h:59
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:107
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:94
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:76
ElementType
Defines type of mesh elements.