22 #include "qgsprocessingmodelalgorithm.h" 38 for ( QVariantMap::const_iterator it = customVariables.constBegin(); it != customVariables.constEnd(); ++it )
70 if ( vars.remove( name ) )
80 GetNamedProjectColor(
const QgsProject *project )
88 QStringList colorStrings = project->
readListEntry( QStringLiteral(
"Palette" ), QStringLiteral(
"/Colors" ) );
89 QStringList colorLabels = project->
readListEntry( QStringLiteral(
"Palette" ), QStringLiteral(
"/Labels" ) );
93 for ( QStringList::iterator it = colorStrings.begin();
94 it != colorStrings.end(); ++it )
98 if ( colorLabels.length() > colorIndex )
100 label = colorLabels.at( colorIndex );
103 mColors.insert( label.toLower(), color );
110 QString colorName = values.at( 0 ).toString().toLower();
111 if ( mColors.contains( colorName ) )
113 return QStringLiteral(
"%1,%2,%3" ).arg( mColors.value( colorName ).red() ).arg( mColors.value( colorName ).green() ).arg( mColors.value( colorName ).blue() );
121 return new GetNamedProjectColor( mProject );
127 QHash< QString, QColor > mColors;
144 QString
id = values.at( 0 ).toString();
157 return new GetLayoutItemVariables( mLayout );
169 GetCurrentFormFieldValue( )
175 QString fieldName( values.at( 0 ).toString() );
177 if ( fieldName.isEmpty() || ! feat.isValid( ) )
181 return feat.attribute( fieldName ) ;
186 return new GetCurrentFormFieldValue( );
195 GetProcessingParameterValue(
const QVariantMap ¶ms )
202 return mParams.value( values.at( 0 ).toString() );
207 return new GetProcessingParameterValue( mParams );
212 const QVariantMap mParams;
222 scope->
addFunction( QStringLiteral(
"current_value" ),
new GetCurrentFormFieldValue( ) );
224 scope->
setVariable( QStringLiteral(
"current_feature" ), formFeature,
true );
225 scope->
setVariable( QStringLiteral(
"form_mode" ), formMode,
true );
238 QVariantMap::const_iterator it = vars.constBegin();
240 for ( ; it != vars.constEnd(); ++it )
246 QString projectFolder = QFileInfo( projectPath ).path();
247 QString projectFilename = QFileInfo( projectPath ).fileName();
248 QString projectBasename = project->
baseName();
272 QVariantMap keywords;
274 for (
auto it = metadataKeywords.constBegin(); it != metadataKeywords.constEnd(); ++it )
276 keywords.insert( it.key(), it.value() );
280 scope->
addFunction( QStringLiteral(
"project_color" ),
new GetNamedProjectColor( project ) );
291 vars.insert( name, value );
312 if ( vars.remove( name ) )
324 const QStringList variableNames = layer->
customProperty( QStringLiteral(
"variableNames" ) ).toStringList();
325 const QStringList variableValues = layer->
customProperty( QStringLiteral(
"variableValues" ) ).toStringList();
328 for (
const QString &variableName : variableNames )
330 if ( varIndex >= variableValues.length() )
335 QVariant varValue = variableValues.at( varIndex );
337 scope->
setVariable( variableName, varValue,
true );
360 QList<QgsExpressionContextScope *> scopes;
379 QStringList variableNames = layer->
customProperty( QStringLiteral(
"variableNames" ) ).toStringList();
380 QStringList variableValues = layer->
customProperty( QStringLiteral(
"variableValues" ) ).toStringList();
382 variableNames << name;
383 variableValues << value.toString();
394 QStringList variableNames;
395 QStringList variableValues;
397 QVariantMap::const_iterator it = variables.constBegin();
398 for ( ; it != variables.constEnd(); ++it )
400 variableNames << it.key();
401 variableValues << it.value().toString();
451 QVariantList layersIds;
453 const QList<QgsMapLayer *> layersInMap = mapSettings.
layers();
454 layersIds.reserve( layersInMap.count() );
455 layers.reserve( layersInMap.count() );
458 layersIds << layer->id();
471 scope->
addFunction( QStringLiteral(
"is_layer_visible" ),
new GetLayerVisibility( mapSettings.
layers() ) );
483 QVariantList matchList;
487 QVariantMap matchMap;
489 matchMap.insert( QStringLiteral(
"valid" ), match.isValid() );
490 matchMap.insert( QStringLiteral(
"layer" ), QVariant::fromValue<QgsWeakMapLayerPointer>(
QgsWeakMapLayerPointer( match.layer() ) ) );
491 matchMap.insert( QStringLiteral(
"feature_id" ), match.featureId() );
492 matchMap.insert( QStringLiteral(
"vertex_index" ), match.vertexIndex() );
493 matchMap.insert( QStringLiteral(
"distance" ), match.distance() );
495 matchList.append( matchMap );
514 angle = markerSymbol->
angle();
525 return scope.release();
528 const QStringList variableNames = layout->
customProperty( QStringLiteral(
"variableNames" ) ).toStringList();
529 const QStringList variableValues = layout->
customProperty( QStringLiteral(
"variableValues" ) ).toStringList();
533 for (
const QString &variableName : variableNames )
535 if ( varIndex >= variableValues.length() )
540 QVariant varValue = variableValues.at( varIndex );
542 scope->setVariable( variableName, varValue );
559 scope->addFunction( QStringLiteral(
"item_variables" ),
new GetLayoutItemVariables( layout ) );
571 scope->setFeature( atlasFeature );
577 return scope.release();
586 QStringList variableNames = layout->
customProperty( QStringLiteral(
"variableNames" ) ).toStringList();
587 QStringList variableValues = layout->
customProperty( QStringLiteral(
"variableValues" ) ).toStringList();
589 variableNames << name;
590 variableValues << value.toString();
601 QStringList variableNames;
602 QStringList variableValues;
604 QVariantMap::const_iterator it = variables.constBegin();
605 for ( ; it != variables.constEnd(); ++it )
607 variableNames << it.key();
608 variableValues << it.value().toString();
661 const QStringList variableNames = item->
customProperty( QStringLiteral(
"variableNames" ) ).toStringList();
662 const QStringList variableValues = item->
customProperty( QStringLiteral(
"variableValues" ) ).toStringList();
665 for (
const QString &variableName : variableNames )
667 if ( varIndex >= variableValues.length() )
672 QVariant varValue = variableValues.at( varIndex );
707 QStringList variableNames = item->
customProperty( QStringLiteral(
"variableNames" ) ).toStringList();
708 QStringList variableValues = item->
customProperty( QStringLiteral(
"variableValues" ) ).toStringList();
710 variableNames << name;
711 variableValues << value.toString();
722 QStringList variableNames;
723 QStringList variableValues;
725 QVariantMap::const_iterator it = variables.constBegin();
726 for ( ; it != variables.constEnd(); ++it )
728 variableNames << it.key();
729 variableValues << it.value().toString();
750 scope->addFunction( QStringLiteral(
"parameter" ),
new GetProcessingParameterValue( parameters ) );
753 return scope.release();
758 return scope.release();
765 if ( !model->sourceFilePath().isEmpty() )
767 modelPath = model->sourceFilePath();
776 const QString modelFolder = !modelPath.isEmpty() ? QFileInfo( modelPath ).path() : QString();
783 const QVariantMap customVariables = model->variables();
784 for (
auto it = customVariables.constBegin(); it != customVariables.constEnd(); ++it )
789 return modelScope.release();
796 return scope.release();
811 return mUsesGeometry;
817 return mReferencedColumns;
822 return allParamsStatic( node, parent, context );
829 QgsExpressionContextUtils::GetLayerVisibility::GetLayerVisibility(
const QList<QgsMapLayer *> &layers )
831 , mLayers( _qgis_listRawToQPointer( layers ) )
836 if ( mLayers.isEmpty() )
841 QgsMapLayer *layer = QgsExpressionUtils::getMapLayer( values.at( 0 ), parent );
844 return mLayers.contains( layer );
854 return new GetLayerVisibility( _qgis_listQPointerToRaw( mLayers ) );
bool isValid() const
Returns the validity of this feature.
Class for parsing and evaluation of expressions (formerly called "search strings").
static QString locale()
Returns the QGIS locale.
static QgsExpressionContextScope * updateSymbolScope(const QgsSymbol *symbol, QgsExpressionContextScope *symbolScope=nullptr)
Updates a symbol scope related to a QgsSymbol to an expression context.
static QgsExpressionContextScope * processingAlgorithmScope(const QgsProcessingAlgorithm *algorithm, const QVariantMap ¶meters, QgsProcessingContext &context)
Creates a new scope which contains variables and functions relating to a processing algorithm...
static void setLayoutItemVariable(QgsLayoutItem *item, const QString &name, const QVariant &value)
Sets a layout item context variable, with the given name and value.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Single variable definition for use within a QgsExpressionContextScope.
static void setGlobalVariable(const QString &name, const QVariant &value)
Sets a global context variable.
Base class for all map layer types.
static void setLayoutItemVariables(QgsLayoutItem *item, const QVariantMap &variables)
Sets all layout item context variables for an item.
static const QString QGIS_VERSION
Version string.
static void setLayerVariables(QgsMapLayer *layer, const QVariantMap &variables)
Sets all layer context variables.
QString id() const
Returns the unique ID for the algorithm, which is a combination of the algorithm provider's ID and th...
Base class for graphical items within a QgsLayout.
static Q_INVOKABLE QString toString(QgsUnitTypes::DistanceUnit unit)
Returns a translated string representing a distance unit.
static void setLayoutVariable(QgsLayout *layout, const QString &name, const QVariant &value)
Sets a layout context variable.
static void setCustomVariables(const QVariantMap &customVariables)
Custom expression variables for this application.
Abstract base class for all rendered symbols.
double rotation() const
Returns the rotation of the resulting map image, in degrees clockwise.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from the layout.
void addFunction(const QString &name, QgsScopedExpressionFunction *function)
Adds a function to the scope.
static void removeProjectVariable(QgsProject *project, const QString &name)
Remove project context variable.
static QgsExpressionContext createFeatureBasedContext(const QgsFeature &feature, const QgsFields &fields)
Helper function for creating an expression context which contains just a feature and fields collectio...
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for layer.
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the object.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from the object.
QString toProj4() const
Returns a Proj4 string representation of this CRS.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
static QVariantMap customVariables()
Custom expression variables for this application.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the scope.
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 * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
QList< QgsMapLayer * > layers() const
Gets list of layers for map rendering The layers are stored in the reverse order of how they are rend...
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
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 feat...
bool usesGeometry(const QgsExpressionNodeFunction *node) const override
Does this function use a geometry object.
static void setLayoutVariables(QgsLayout *layout, const QVariantMap &variables)
Sets all layout context variables.
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
static const int QGIS_VERSION_INT
Version number used for comparing versions using the "Check QGIS Version" function.
Abstract base class for processing algorithms.
QList< QgsExpressionFunction::Parameter > ParameterList
List of parameters, used for function definition.
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout's render context, which stores information relating to the current ...
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes takes output image size into accou...
QVariant variable(const QString &name) const
Fetches a matching variable from the context.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
A marker symbol type, for rendering Point and MultiPoint geometries.
static QString userFullName()
Returns the user's operating system login account full display name.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
QgsCoordinateReferenceSystem destinationCrs() const
returns CRS of destination coordinate reference system
QgsUnitTypes::DistanceUnit mapUnits() const
Gets units of map's geographical coordinates - used for scale calculation.
QgsLayout * layout() override
Returns the layout associated with the iterator.
The QgsMapSettings class contains configuration for rendering of the map.
void setVariable(const QString &name, const QVariant &value, bool isStatic=false)
Convenience method for setting a variable in the context scope by name name and value.
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 c
QSizeF toQSizeF() const
Converts the layout size to a QSizeF.
static QgsExpressionContextScope * layoutItemScope(const QgsLayoutItem *item)
Creates a new scope which contains variables and functions relating to a QgsLayoutItem.
bool isStatic(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context) const override
Will be called during prepare to determine if the function is static.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
QgsVectorLayer * layer() const
Returns the vector layer associated with the layout's context.
static const QString EXPR_SYMBOL_ANGLE
Inbuilt variable name for symbol angle variable.
QString ellipsoidAcronym() const
Returns the ellipsoid acronym for the ellipsoid used by the CRS.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
const QgsLayout * layout() const
Returns the layout the object is attached to.
QgsLayoutItemPage * page(int pageNumber)
Returns a specific page (by pageNumber) from the collection.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QgsUnitTypes::AreaUnit areaUnits() const
Convenience function to query default area measurement units for project.
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout's page collection, which stores and manages page items in the layout...
static void setProjectVariables(QgsProject *project, const QVariantMap &variables)
Sets all project context variables.
QgsProjectMetadata metadata
double scale() const
Returns the calculated map scale.
double dpi() const
Returns the dpi for outputting the layout.
double width() const
Returns the width of the rectangle.
static QgsExpressionContextScope * notificationScope(const QString &message=QString())
Creates a new scope which contains variables and functions relating to provider notifications.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static void removeGlobalVariable(const QString &name)
Remove a global context variable.
QgsCoordinateReferenceSystem crs
QVariantMap variablesToMap() const
Returns a map of variable name to value representing all the expression variables contained by the co...
Class used to render QgsLayout as an atlas, by iterating over the features from an associated vector ...
static const QString EXPR_SYMBOL_COLOR
Inbuilt variable name for symbol color variable.
QString description() const
Returns the descriptive name of the CRS, e.g., "WGS 84" or "GDA 94 / Vicgrid94".
static bool registerFunction(QgsExpressionFunction *function, bool transferOwnership=false)
Registers a function to the expression engine.
QStringList readListEntry(const QString &scope, const QString &key, const QStringList &def=QStringList(), bool *ok=nullptr) const
Key value accessors.
Reads and writes project states.
static void setCustomVariable(const QString &name, const QVariant &value)
Set a single custom expression variable.
QColor color() const
Returns the symbol's color.
QString currentFilename() const
Returns the current feature filename.
int page() const
Returns the page the item is currently on, with the first page returning 0.
QString id() const
Returns the item's ID name.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
int count() override
Returns the number of features to iterate over.
Single scope for storing variables and functions for use within a QgsExpressionContext.
An expression node for expression functions.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the scope.
void setCustomVariables(const QVariantMap &customVariables)
A map of custom project variables.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
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
QgsProjectStorage * projectStorage() const
Returns pointer to project storage implementation that handles read/write of the project file...
static void setLayerVariable(QgsMapLayer *layer, const QString &name, const QVariant &value)
Sets a layer context variable.
static QString userLoginName()
Returns the user's operating system login account name.
static QString osName()
Returns a string name of the operating system QGIS is running on.
int pageCount() const
Returns the number of pages in the collection.
int currentFeatureNumber() const
Returns the current feature number, where a value of 0 corresponds to the first feature.
QString projectionAcronym() const
Returns the projection acronym for the projection used by the CRS.
QgsCoordinateTransformContext transformContext
QgsLayoutReportContext & reportContext()
Returns a reference to the layout's report context, which stores information relating to the current ...
static void setProjectVariable(QgsProject *project, const QString &name, const QVariant &value)
Sets a project context variable.
static QgsExpressionContextScope * processingModelAlgorithmScope(const QgsProcessingModelAlgorithm *model, const QVariantMap ¶meters, QgsProcessingContext &context)
Creates a new scope which contains variables and functions relating to a processing model algorithm...
static void registerContextFunctions()
Registers all known core functions provided by QgsExpressionContextScope objects. ...
static QgsExpressionContextScope * atlasScope(QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object...
static const QString QGIS_RELEASE_NAME
Release name.
static QgsExpressionContextScope * layoutScope(const QgsLayout *layout)
Creates a new scope which contains variables and functions relating to a QgsLayout layout...
static QgsProject * instance()
Returns the QgsProject singleton instance.
virtual QString uuid() const
Returns the item identification string.
This class represents a coordinate reference system (CRS).
QString toWkt() const
Returns a WKT representation of this CRS.
QString nameForPage(int page) const
Returns the calculated name for a specified atlas page number.
static QString platform()
Returns the QGIS platform name, e.g., "desktop" or "server".
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the layout.
bool enabled() const
Returns whether the atlas generation is enabled.
QgsVectorLayer * coverageLayer() const
Returns the coverage layer used for the atlas features.
QgsFeature feature() const
Returns the current feature for evaluating the layout.
Represents a single parameter passed to a function.
QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
QString title() const
Returns the project's title.
QgsPointXY center() const
Returns the center point of the rectangle.
QgsUnitTypes::DistanceUnit distanceUnits() const
Convenience function to query default distance measurement units for project.
QVariantMap customVariables() const
A map of custom project variables.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
QString baseName() const
Returns the base name of the project file without the path and without extension - derived from fileN...
Interface for master layout type objects, such as print layouts and reports.
QString absoluteFilePath() const
Returns full absolute path to the project file if the project is stored in a file system - derived fr...
Represents a vector layer which manages a vector based data sets.
Contains information about the context in which a processing algorithm is executed.
static void setGlobalVariables(const QVariantMap &variables)
Sets all global context variables.
Expression function for use within a QgsExpressionContextScope.
double angle() const
Returns the marker angle for the whole symbol.
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
QString authid() const
Returns the authority identifier for the CRS.
QgsCoordinateReferenceSystem crs
QSet< QString > referencedColumns(const QgsExpressionNodeFunction *node) const override
Returns a set of field names which are required for this function.
static QColor decodeColor(const QString &str)
double height() const
Returns the height of the rectangle.
Item representing the paper in a layout.