QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgsexpressioncontextutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsexpressioncontextutils.cpp
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
17#include "qgsapplication.h"
18#include "qgsvectorlayer.h"
19#include "qgsproject.h"
20#include "qgsexpression.h"
24#include "qgsmapsettings.h"
25#include "qgslayoutitem.h"
26#include "qgsexpressionutils.h"
28#include "qgslayoutatlas.h"
29#include "qgslayoutmultiframe.h"
30#include "qgsfeatureid.h"
31#include "qgslayoutitemmap.h"
33#include "qgsprojoperation.h"
34#include "qgsmarkersymbol.h"
35#include "qgstriangularmesh.h"
36#include "qgsvectortileutils.h"
37#include "qgsmeshlayer.h"
40#include "qgsproviderregistry.h"
41#include "qgsmaplayerfactory.h"
42#include "qgsunittypes.h"
44
46{
47 QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Global" ) );
48
49 const QVariantMap customVariables = QgsApplication::customVariables();
50
51 for ( QVariantMap::const_iterator it = customVariables.constBegin(); it != customVariables.constEnd(); ++it )
52 {
53 scope->setVariable( it.key(), it.value(), true );
54 }
55
56 //add some extra global variables
57 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_version" ), Qgis::version(), true, true ) );
58 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_version_no" ), Qgis::versionInt(), true, true ) );
59 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_short_version" ), QStringLiteral( "%1.%2" ).arg( Qgis::versionInt() / 10000 ).arg( Qgis::versionInt() / 100 % 100 ), true, true ) );
60 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_release_name" ), Qgis::releaseName(), true, true ) );
61 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_platform" ), QgsApplication::platform(), true, true ) );
62 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_os_name" ), QgsApplication::osName(), true, true ) );
63 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_locale" ), QgsApplication::locale(), true, true ) );
64 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "user_account_name" ), QgsApplication::userLoginName(), true, true ) );
65 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "user_full_name" ), QgsApplication::userFullName(), true, true ) );
66
67 return scope;
68}
69
70void QgsExpressionContextUtils::setGlobalVariable( const QString &name, const QVariant &value )
71{
73}
74
75void QgsExpressionContextUtils::setGlobalVariables( const QVariantMap &variables )
76{
78}
79
81{
82 QVariantMap vars = QgsApplication::customVariables();
83 if ( vars.remove( name ) )
85}
86
88
89class GetLayoutItemVariables : public QgsScopedExpressionFunction
90{
91 public:
92 GetLayoutItemVariables( const QgsLayout *c )
93 : QgsScopedExpressionFunction( QStringLiteral( "item_variables" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "id" ) ), QStringLiteral( "Layout" ) )
94 , mLayout( c )
95 {}
96
97 QVariant func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * ) override
98 {
99 if ( !mLayout )
100 return QVariant();
101
102 const QString id = values.at( 0 ).toString();
103
104 const QgsLayoutItem *item = mLayout->itemById( id );
105 if ( !item )
106 return QVariant();
107
109
110 return c.variablesToMap();
111 }
112
113 QgsScopedExpressionFunction *clone() const override
114 {
115 return new GetLayoutItemVariables( mLayout );
116 }
117
118 private:
119
120 const QgsLayout *mLayout = nullptr;
121
122};
123
124
125class GetLayoutMapLayerCredits : public QgsScopedExpressionFunction
126{
127 public:
128 GetLayoutMapLayerCredits( const QgsLayout *c )
129 : QgsScopedExpressionFunction( QStringLiteral( "map_credits" ),
130 QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "id" ), true )
131 << QgsExpressionFunction::Parameter( QStringLiteral( "include_layer_names" ), true, false )
132 << QgsExpressionFunction::Parameter( QStringLiteral( "layer_name_separator" ), true, QStringLiteral( ": " ) ), QStringLiteral( "Layout" ) )
133 , mLayout( c )
134 {}
135
136 QVariant func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * ) override
137 {
138 if ( !mLayout )
139 return QVariant();
140
141 const QString id = values.value( 0 ).toString();
142 const bool includeLayerNames = values.value( 1 ).toBool();
143 const QString layerNameSeparator = values.value( 2 ).toString();
144
145 QList< QgsLayoutItemMap * > maps;
146 mLayout->layoutItems( maps );
147
148 // collect all the layers in matching maps first
149 QList< const QgsMapLayer * > layers;
150 bool foundMap = false;
151 for ( QgsLayoutItemMap *map : std::as_const( maps ) )
152 {
153 if ( !id.isEmpty() && map->id() != id )
154 continue;
155
156 foundMap = true;
157
158 const QgsExpressionContext c = map->createExpressionContext();
159 const QVariantList mapLayers = c.variable( QStringLiteral( "map_layers" ) ).toList();
160
161
162 for ( const QVariant &value : mapLayers )
163 {
164 if ( const QgsMapLayer *layer = qobject_cast< const QgsMapLayer * >( value.value< QObject * >() ) )
165 {
166 if ( !layers.contains( layer ) )
167 layers << layer;
168 }
169 }
170 }
171 if ( !foundMap )
172 return QVariant();
173
174 QVariantList res;
175 res.reserve( layers.size() );
176 for ( const QgsMapLayer *layer : std::as_const( layers ) )
177 {
178 const QStringList credits = !layer->metadata().rights().isEmpty() ? layer->metadata().rights() : QStringList() << layer->attribution();
179 for ( const QString &credit : credits )
180 {
181 if ( credit.trimmed().isEmpty() )
182 continue;
183
184 const QString creditString = includeLayerNames ? layer->name() + layerNameSeparator + credit
185 : credit;
186
187 if ( !res.contains( creditString ) )
188 res << creditString;
189 }
190 }
191
192 return res;
193 }
194
195 QgsScopedExpressionFunction *clone() const override
196 {
197 return new GetLayoutMapLayerCredits( mLayout );
198 }
199
200 private:
201
202 const QgsLayout *mLayout = nullptr;
203
204};
205
206class GetCurrentFormFieldValue : public QgsScopedExpressionFunction
207{
208 public:
209 GetCurrentFormFieldValue( )
210 : QgsScopedExpressionFunction( QStringLiteral( "current_value" ), QgsExpressionFunction::ParameterList() << QStringLiteral( "field_name" ), QStringLiteral( "Form" ) )
211 {}
212
213 QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *, const QgsExpressionNodeFunction * ) override
214 {
215 const QString fieldName( values.at( 0 ).toString() );
216 const QgsFeature feat( context->variable( QStringLiteral( "current_feature" ) ).value<QgsFeature>() );
217 if ( fieldName.isEmpty() || ! feat.isValid( ) )
218 {
219 return QVariant();
220 }
221 return feat.attribute( fieldName ) ;
222 }
223
224 QgsScopedExpressionFunction *clone() const override
225 {
226 return new GetCurrentFormFieldValue( );
227 }
228
229 bool isStatic( const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext * ) const override
230 {
231 return false;
232 };
233
234};
235
236class GetCurrentParentFormFieldValue : public QgsScopedExpressionFunction
237{
238 public:
239 GetCurrentParentFormFieldValue( )
240 : QgsScopedExpressionFunction( QStringLiteral( "current_parent_value" ), QgsExpressionFunction::ParameterList() << QStringLiteral( "field_name" ), QStringLiteral( "Form" ) )
241 {}
242
243 QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *, const QgsExpressionNodeFunction * ) override
244 {
245 const QString fieldName( values.at( 0 ).toString() );
246 const QgsFeature feat( context->variable( QStringLiteral( "current_parent_feature" ) ).value<QgsFeature>() );
247 if ( fieldName.isEmpty() || ! feat.isValid( ) )
248 {
249 return QVariant();
250 }
251 return feat.attribute( fieldName ) ;
252 }
253
254 QgsScopedExpressionFunction *clone() const override
255 {
256 return new GetCurrentParentFormFieldValue( );
257 }
258
259 bool isStatic( const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext * ) const override
260 {
261 return false;
262 };
263
264};
265
266
267class GetProcessingParameterValue : public QgsScopedExpressionFunction
268{
269 public:
270 GetProcessingParameterValue( const QVariantMap &params )
271 : QgsScopedExpressionFunction( QStringLiteral( "parameter" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "name" ) ), QStringLiteral( "Processing" ) )
272 , mParams( params )
273 {}
274
275 QVariant func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * ) override
276 {
277 return mParams.value( values.at( 0 ).toString() );
278 }
279
280 QgsScopedExpressionFunction *clone() const override
281 {
282 return new GetProcessingParameterValue( mParams );
283 }
284
285 private:
286
287 const QVariantMap mParams;
288
289};
290
292
293
294QgsExpressionContextScope *QgsExpressionContextUtils::formScope( const QgsFeature &formFeature, const QString &formMode )
295{
296 QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Form" ) );
297 scope->addFunction( QStringLiteral( "current_value" ), new GetCurrentFormFieldValue( ) );
298 scope->setVariable( QStringLiteral( "current_geometry" ), formFeature.geometry( ), true );
299 scope->setVariable( QStringLiteral( "current_feature" ), formFeature, true );
300 scope->setVariable( QStringLiteral( "form_mode" ), formMode, true );
301 return scope;
302}
303
304
305QgsExpressionContextScope *QgsExpressionContextUtils::parentFormScope( const QgsFeature &parentFormFeature, const QString &parentFormMode )
306{
307 QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Parent Form" ) );
308 scope->addFunction( QStringLiteral( "current_parent_value" ), new GetCurrentParentFormFieldValue( ) );
309 scope->setVariable( QStringLiteral( "current_parent_geometry" ), parentFormFeature.geometry( ), true );
310 scope->setVariable( QStringLiteral( "current_parent_feature" ), parentFormFeature, true );
311 scope->setVariable( QStringLiteral( "parent_form_mode" ), parentFormMode, true );
312 return scope;
313}
314
316{
317 if ( !project )
318 {
319 QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Project" ) );
320 return scope;
321 }
322 else
323 return project->createExpressionContextScope();
324}
325
326void QgsExpressionContextUtils::setProjectVariable( QgsProject *project, const QString &name, const QVariant &value )
327{
328 if ( !project )
329 return;
330
331 QVariantMap vars = project->customVariables();
332
333 vars.insert( name, value );
334
335 project->setCustomVariables( vars );
336}
337
338void QgsExpressionContextUtils::setProjectVariables( QgsProject *project, const QVariantMap &variables )
339{
340 if ( !project )
341 return;
342
343 project->setCustomVariables( variables );
344}
345
347{
348 if ( !project )
349 {
350 return;
351 }
352
353 QVariantMap vars = project->customVariables();
354 if ( vars.remove( name ) )
355 project->setCustomVariables( vars );
356}
357
359{
360 QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Layer" ) );
361
362 if ( !layer )
363 return scope;
364
365 //add variables defined in layer properties
366 const QStringList variableNames = layer->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
367 const QStringList variableValues = layer->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
368
369 int varIndex = 0;
370 for ( const QString &variableName : variableNames )
371 {
372 if ( varIndex >= variableValues.length() )
373 {
374 break;
375 }
376
377 const QVariant varValue = variableValues.at( varIndex );
378 varIndex++;
379 scope->setVariable( variableName, varValue, true );
380 }
381
382 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_name" ), layer->name(), true, true ) );
383 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_id" ), layer->id(), true, true ) );
384 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "_layer_crs" ), QVariant::fromValue<QgsCoordinateReferenceSystem>( layer->crs() ), true, true ) );
385 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_crs" ), layer->crs().authid(), true, true ) );
386 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer" ), QVariant::fromValue<QgsWeakMapLayerPointer >( QgsWeakMapLayerPointer( const_cast<QgsMapLayer *>( layer ) ) ), true, true ) );
387 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_crs_ellipsoid" ), layer->crs().ellipsoidAcronym(), true, true ) );
388
389 const QgsVectorLayer *vLayer = qobject_cast< const QgsVectorLayer * >( layer );
390 if ( vLayer )
391 {
392 scope->setFields( vLayer->fields() );
393 }
394
395 //TODO - add functions. Possibilities include:
396 //is_selected
397 //field summary stats
398
399 return scope;
400}
401
402QList<QgsExpressionContextScope *> QgsExpressionContextUtils::globalProjectLayerScopes( const QgsMapLayer *layer )
403{
404 QList<QgsExpressionContextScope *> scopes;
405 scopes << globalScope();
406
407 QgsProject *project = QgsProject::instance(); // TODO: use project associated with layer
408 if ( project )
409 scopes << projectScope( project );
410
411 if ( layer )
412 scopes << layerScope( layer );
413 return scopes;
414}
415
416
417void QgsExpressionContextUtils::setLayerVariable( QgsMapLayer *layer, const QString &name, const QVariant &value )
418{
419 if ( !layer )
420 return;
421
422 //write variable to layer
423 QStringList variableNames = layer->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
424 QStringList variableValues = layer->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
425
426 variableNames << name;
427 variableValues << value.toString();
428
429 layer->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
430 layer->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
431}
432
433void QgsExpressionContextUtils::setLayerVariables( QgsMapLayer *layer, const QVariantMap &variables )
434{
435 if ( !layer )
436 return;
437
438 QStringList variableNames;
439 QStringList variableValues;
440
441 QVariantMap::const_iterator it = variables.constBegin();
442 for ( ; it != variables.constEnd(); ++it )
443 {
444 variableNames << it.key();
445 variableValues << it.value().toString();
446 }
447
448 layer->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
449 layer->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
450}
451
453{
454 // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
455 // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
456
457 // and because people don't read that ^^, I'm going to blast it all over this function
458
459 QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Map Settings" ) );
460
461 //add known map settings context variables
462 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_id" ), "canvas", true ) );
463 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_rotation" ), mapSettings.rotation(), true ) );
464 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_scale" ), mapSettings.scale(), true ) );
465
466 scope->setVariable( QStringLiteral( "zoom_level" ), QgsVectorTileUtils::scaleToZoomLevel( mapSettings.scale(), 0, 99999 ), true );
467 scope->setVariable( QStringLiteral( "vector_tile_zoom" ), QgsVectorTileUtils::scaleToZoom( mapSettings.scale() ), true );
468
469 // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
470 // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
471
472 const QgsGeometry extent = QgsGeometry::fromRect( mapSettings.visibleExtent() );
473 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent" ), QVariant::fromValue( extent ), true ) );
474 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_width" ), mapSettings.visibleExtent().width(), true ) );
475 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_height" ), mapSettings.visibleExtent().height(), true ) );
476
477 // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
478 // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
479
480 const QgsGeometry centerPoint = QgsGeometry::fromPointXY( mapSettings.visibleExtent().center() );
481 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_center" ), QVariant::fromValue( centerPoint ), true ) );
482
483 // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
484 // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
485
486 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs" ), mapSettings.destinationCrs().authid(), true ) );
487 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_definition" ), mapSettings.destinationCrs().toProj(), true ) );
488 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_units" ), QgsUnitTypes::toString( mapSettings.mapUnits() ), true ) );
489 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_description" ), mapSettings.destinationCrs().description(), true ) );
490 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_acronym" ), mapSettings.destinationCrs().projectionAcronym(), true ) );
491 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_projection" ), mapSettings.destinationCrs().operation().description(), true ) );
492 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_ellipsoid" ), mapSettings.destinationCrs().ellipsoidAcronym(), true ) );
493 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_proj4" ), mapSettings.destinationCrs().toProj(), true ) );
494 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_wkt" ), mapSettings.destinationCrs().toWkt( Qgis::CrsWktVariant::Preferred ), true ) );
495
496 // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
497 // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
498
499 QVariantList layersIds;
500 QVariantList layers;
501 const QList<QgsMapLayer *> layersInMap = mapSettings.layers( true );
502 layersIds.reserve( layersInMap.count() );
503 layers.reserve( layersInMap.count() );
504 for ( QgsMapLayer *layer : layersInMap )
505 {
506 layersIds << layer->id();
507 layers << QVariant::fromValue<QgsWeakMapLayerPointer>( QgsWeakMapLayerPointer( layer ) );
508 }
509
510 // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
511 // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
512
513 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layer_ids" ), layersIds, true ) );
514 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layers" ), layers, true ) );
515
516 // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
517 // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
518
519 scope->addFunction( QStringLiteral( "is_layer_visible" ), new GetLayerVisibility( mapSettings.layers( true ), mapSettings.scale() ) );
520
521 // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
522 // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
523
524 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_start_time" ), mapSettings.isTemporal() ? mapSettings.temporalRange().begin() : QVariant(), true ) );
525 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_end_time" ), mapSettings.isTemporal() ? mapSettings.temporalRange().end() : QVariant(), true ) );
526 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_interval" ), mapSettings.isTemporal() ? QgsInterval( mapSettings.temporalRange().end() - mapSettings.temporalRange().begin() ) : QVariant(), true ) );
527
528 // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
529 // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
530
531 if ( mapSettings.frameRate() >= 0 )
532 scope->setVariable( QStringLiteral( "frame_rate" ), mapSettings.frameRate(), true );
533 if ( mapSettings.currentFrame() >= 0 )
534 scope->setVariable( QStringLiteral( "frame_number" ), mapSettings.currentFrame(), true );
535
536 return scope;
537}
538
539QgsExpressionContextScope *QgsExpressionContextUtils::mapToolCaptureScope( const QList<QgsPointLocator::Match> &matches )
540{
541 QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Map Tool Capture" ) );
542
543 QVariantList matchList;
544
545 for ( const QgsPointLocator::Match &match : matches )
546 {
547 QVariantMap matchMap;
548
549 matchMap.insert( QStringLiteral( "valid" ), match.isValid() );
550 matchMap.insert( QStringLiteral( "layer" ), QVariant::fromValue<QgsWeakMapLayerPointer>( QgsWeakMapLayerPointer( match.layer() ) ) );
551 matchMap.insert( QStringLiteral( "feature_id" ), match.featureId() );
552 matchMap.insert( QStringLiteral( "vertex_index" ), match.vertexIndex() );
553 matchMap.insert( QStringLiteral( "distance" ), match.distance() );
554
555 matchList.append( matchMap );
556 }
557
558 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "snapping_results" ), matchList ) );
559
560 return scope;
561}
562
564{
565 QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Map Layer Position" ) );
566 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_cursor_point" ), QVariant::fromValue( QgsGeometry::fromPointXY( position ) ) ) );
567 return scope;
568}
569
571{
572 if ( !symbolScope )
573 return nullptr;
574
575 symbolScope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_SYMBOL_COLOR, symbol ? symbol->color() : QColor(), true ) );
576
577 double angle = 0.0;
578 const QgsMarkerSymbol *markerSymbol = dynamic_cast< const QgsMarkerSymbol * >( symbol );
579 if ( markerSymbol )
580 {
581 angle = markerSymbol->angle();
582 }
584
585 return symbolScope;
586}
587
589{
590 std::unique_ptr< QgsExpressionContextScope > scope( new QgsExpressionContextScope( QObject::tr( "Layout" ) ) );
591 if ( !layout )
592 return scope.release();
593
594 //add variables defined in layout properties
595 const QStringList variableNames = layout->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
596 const QStringList variableValues = layout->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
597
598 int varIndex = 0;
599
600 for ( const QString &variableName : variableNames )
601 {
602 if ( varIndex >= variableValues.length() )
603 {
604 break;
605 }
606
607 const QVariant varValue = variableValues.at( varIndex );
608 varIndex++;
609 scope->setVariable( variableName, varValue );
610 }
611
612 //add known layout context variables
613 if ( const QgsMasterLayoutInterface *l = dynamic_cast< const QgsMasterLayoutInterface * >( layout ) )
614 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_name" ), l->name(), true ) );
615
616 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_numpages" ), layout->pageCollection()->pageCount(), true ) );
617 if ( layout->pageCollection()->pageCount() > 0 )
618 {
619 // just take first page size
620 const QSizeF s = layout->pageCollection()->page( 0 )->sizeWithUnits().toQSizeF();
621 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageheight" ), s.height(), true ) );
622 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pagewidth" ), s.width(), true ) );
623 }
624
625 QVariantList offsets;
626 for ( int i = 0; i < layout->pageCollection()->pageCount(); i++ )
627 {
628 const QPointF p = layout->pageCollection()->pagePositionToLayoutPosition( i, QgsLayoutPoint( 0, 0 ) );
629 offsets << p.y();
630 }
631 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageoffsets" ), offsets, true ) );
632
633 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_dpi" ), layout->renderContext().dpi(), true ) );
634
635 scope->addFunction( QStringLiteral( "item_variables" ), new GetLayoutItemVariables( layout ) );
636 scope->addFunction( QStringLiteral( "map_credits" ), new GetLayoutMapLayerCredits( layout ) );
637
638 if ( layout->reportContext().layer() )
639 {
640 scope->setFields( layout->reportContext().layer()->fields() );
641 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_layerid" ), layout->reportContext().layer()->id(), true ) );
642 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_layername" ), layout->reportContext().layer()->name(), true ) );
643 }
644
645 if ( layout->reportContext().feature().isValid() )
646 {
647 const QgsFeature atlasFeature = layout->reportContext().feature();
648 scope->setFeature( atlasFeature );
649 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_feature" ), QVariant::fromValue( atlasFeature ), true ) );
650 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featureid" ), FID_IS_NULL( atlasFeature.id() ) ? QVariant() : atlasFeature.id(), true ) );
651 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_geometry" ), QVariant::fromValue( atlasFeature.geometry() ), true ) );
652 }
653
654 return scope.release();
655}
656
657void QgsExpressionContextUtils::setLayoutVariable( QgsLayout *layout, const QString &name, const QVariant &value )
658{
659 if ( !layout )
660 return;
661
662 //write variable to layout
663 QStringList variableNames = layout->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
664 QStringList variableValues = layout->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
665
666 variableNames << name;
667 variableValues << value.toString();
668
669 layout->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
670 layout->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
671}
672
673void QgsExpressionContextUtils::setLayoutVariables( QgsLayout *layout, const QVariantMap &variables )
674{
675 if ( !layout )
676 return;
677
678 QStringList variableNames;
679 QStringList variableValues;
680
681 QVariantMap::const_iterator it = variables.constBegin();
682 for ( ; it != variables.constEnd(); ++it )
683 {
684 variableNames << it.key();
685 variableValues << it.value().toString();
686 }
687
688 layout->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
689 layout->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
690}
691
693{
694 QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Atlas" ) );
695 if ( !atlas )
696 {
697 //add some dummy atlas variables. This is done so that as in certain contexts we want to show
698 //users that these variables are available even if they have no current value
699 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_pagename" ), QString(), true, true ) );
700 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_feature" ), QVariant::fromValue( QgsFeature() ), true, true ) );
701 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featureid" ), QVariant(), true, true ) );
702 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_geometry" ), QVariant::fromValue( QgsGeometry() ), true, true ) );
703 return scope;
704 }
705
706 //add known atlas variables
707 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_totalfeatures" ), atlas->count(), true, true ) );
708 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featurenumber" ), atlas->currentFeatureNumber() + 1, true, true ) );
709 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_filename" ), atlas->currentFilename(), true, true ) );
710 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_pagename" ), atlas->nameForPage( atlas->currentFeatureNumber() ), true, true ) );
711
712 if ( atlas->enabled() && atlas->coverageLayer() )
713 {
714 scope->setFields( atlas->coverageLayer()->fields() );
715 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_layerid" ), atlas->coverageLayer()->id(), true ) );
716 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_layername" ), atlas->coverageLayer()->name(), true ) );
717 }
718
719 if ( atlas->enabled() )
720 {
721 const QgsFeature atlasFeature = atlas->layout()->reportContext().feature();
722 scope->setFeature( atlasFeature );
723 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_feature" ), QVariant::fromValue( atlasFeature ), true ) );
724 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featureid" ), FID_IS_NULL( atlasFeature.id() ) ? QVariant() : atlasFeature.id(), true ) );
725 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_geometry" ), QVariant::fromValue( atlasFeature.geometry() ), true ) );
726 }
727
728 return scope;
729}
730
732{
733 QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Layout Item" ) );
734 if ( !item )
735 return scope;
736
737 //add variables defined in layout item properties
738 const QStringList variableNames = item->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
739 const QStringList variableValues = item->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
740
741 int varIndex = 0;
742 for ( const QString &variableName : variableNames )
743 {
744 if ( varIndex >= variableValues.length() )
745 {
746 break;
747 }
748
749 const QVariant varValue = variableValues.at( varIndex );
750 varIndex++;
751 scope->setVariable( variableName, varValue );
752 }
753
754 //add known layout item context variables
755 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "item_id" ), item->id(), true ) );
756 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "item_uuid" ), item->uuid(), true ) );
757 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_page" ), item->page() + 1, true ) );
758
759 if ( item->layout() )
760 {
761 const QgsLayoutItemPage *page = item->layout()->pageCollection()->page( item->page() );
762 if ( page )
763 {
764 const QSizeF s = page->sizeWithUnits().toQSizeF();
765 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageheight" ), s.height(), true ) );
766 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pagewidth" ), s.width(), true ) );
767 }
768 else
769 {
770 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageheight" ), QVariant(), true ) );
771 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pagewidth" ), QVariant(), true ) );
772 }
773 }
774
775 return scope;
776}
777
778void QgsExpressionContextUtils::setLayoutItemVariable( QgsLayoutItem *item, const QString &name, const QVariant &value )
779{
780 if ( !item )
781 return;
782
783 //write variable to layout item
784 QStringList variableNames = item->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
785 QStringList variableValues = item->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
786
787 variableNames << name;
788 variableValues << value.toString();
789
790 item->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
791 item->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
792}
793
794void QgsExpressionContextUtils::setLayoutItemVariables( QgsLayoutItem *item, const QVariantMap &variables )
795{
796 if ( !item )
797 return;
798
799 QStringList variableNames;
800 QStringList variableValues;
801
802 QVariantMap::const_iterator it = variables.constBegin();
803 for ( ; it != variables.constEnd(); ++it )
804 {
805 variableNames << it.key();
806 variableValues << it.value().toString();
807 }
808
809 item->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
810 item->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
811}
812
814{
815 QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Multiframe Item" ) );
816 if ( !frame )
817 return scope;
818
819 //add variables defined in layout item properties
820 const QStringList variableNames = frame->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
821 const QStringList variableValues = frame->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
822
823 int varIndex = 0;
824 for ( const QString &variableName : variableNames )
825 {
826 if ( varIndex >= variableValues.length() )
827 {
828 break;
829 }
830
831 const QVariant varValue = variableValues.at( varIndex );
832 varIndex++;
833 scope->setVariable( variableName, varValue );
834 }
835
836 return scope;
837}
838
839void QgsExpressionContextUtils::setLayoutMultiFrameVariable( QgsLayoutMultiFrame *frame, const QString &name, const QVariant &value )
840{
841 if ( !frame )
842 return;
843
844 //write variable to layout multiframe
845 QStringList variableNames = frame->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
846 QStringList variableValues = frame->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
847
848 variableNames << name;
849 variableValues << value.toString();
850
851 frame->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
852 frame->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
853}
854
856{
857 if ( !frame )
858 return;
859
860 QStringList variableNames;
861 QStringList variableValues;
862
863 QVariantMap::const_iterator it = variables.constBegin();
864 for ( ; it != variables.constEnd(); ++it )
865 {
866 variableNames << it.key();
867 variableValues << it.value().toString();
868 }
869
870 frame->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
871 frame->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
872}
873
875{
877 scope->setFeature( feature );
878 scope->setFields( fields );
879 return QgsExpressionContext() << scope;
880}
881
883{
884 // set aside for future use
885 Q_UNUSED( context )
886
887 std::unique_ptr< QgsExpressionContextScope > scope( new QgsExpressionContextScope( QObject::tr( "Algorithm" ) ) );
888 scope->addFunction( QStringLiteral( "parameter" ), new GetProcessingParameterValue( parameters ) );
889
890 if ( !algorithm )
891 return scope.release();
892
893 //add standard algorithm variables
894 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "algorithm_id" ), algorithm->id(), true ) );
895
896 return scope.release();
897}
898
899QgsExpressionContextScope *QgsExpressionContextUtils::processingModelAlgorithmScope( const QgsProcessingModelAlgorithm *model, const QVariantMap &, QgsProcessingContext &context )
900{
901 std::unique_ptr< QgsExpressionContextScope > modelScope( new QgsExpressionContextScope( QObject::tr( "Model" ) ) );
902 QString modelPath;
903 if ( !model->sourceFilePath().isEmpty() )
904 {
905 modelPath = model->sourceFilePath();
906 }
907 else if ( context.project() )
908 {
909 // fallback to project path -- the model may be embedded in a project, OR an unsaved model. In either case the
910 // project path is a logical value to fall back to
911 modelPath = context.project()->projectStorage() ? context.project()->fileName() : context.project()->absoluteFilePath();
912 }
913
914 const QString modelFolder = !modelPath.isEmpty() ? QFileInfo( modelPath ).path() : QString();
915 modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_path" ), QDir::toNativeSeparators( modelPath ), true ) );
916 modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_folder" ), QDir::toNativeSeparators( modelFolder ), true, true ) );
917 modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_name" ), model->displayName(), true ) );
918 modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_group" ), model->group(), true ) );
919
920 // custom variables
921 const QVariantMap customVariables = model->variables();
922 for ( auto it = customVariables.constBegin(); it != customVariables.constEnd(); ++it )
923 {
924 modelScope->addVariable( QgsExpressionContextScope::StaticVariable( it.key(), it.value(), true ) );
925 }
926
927 return modelScope.release();
928}
929
931{
932 std::unique_ptr< QgsExpressionContextScope > scope( new QgsExpressionContextScope() );
933 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "notification_message" ), message, true ) );
934 return scope.release();
935}
936
938{
939 QgsExpression::registerFunction( new GetNamedProjectColor( nullptr ) );
940 QgsExpression::registerFunction( new GetSensorData( ) );
941 QgsExpression::registerFunction( new GetLayoutItemVariables( nullptr ) );
942 QgsExpression::registerFunction( new GetLayoutMapLayerCredits( nullptr ) );
943 QgsExpression::registerFunction( new GetLayerVisibility( QList<QgsMapLayer *>(), 0.0 ) );
944 QgsExpression::registerFunction( new GetProcessingParameterValue( QVariantMap() ) );
945 QgsExpression::registerFunction( new GetCurrentFormFieldValue( ) );
946 QgsExpression::registerFunction( new GetCurrentParentFormFieldValue( ) );
947 QgsExpression::registerFunction( new LoadLayerFunction( ) );
948}
949
951{
952 Q_UNUSED( node )
953 return mUsesGeometry;
954}
955
957{
958 Q_UNUSED( node )
959 return mReferencedColumns;
960}
961
963{
964 return allParamsStatic( node, parent, context );
965}
966
967//
968// GetLayerVisibility
969//
970
971QgsExpressionContextUtils::GetLayerVisibility::GetLayerVisibility( const QList<QgsMapLayer *> &layers, double scale )
972 : QgsScopedExpressionFunction( QStringLiteral( "is_layer_visible" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "id" ) ), QStringLiteral( "General" ) )
973 , mLayers( _qgis_listRawToQPointer( layers ) )
974 , mScale( scale )
975{
976 for ( const auto &layer : mLayers )
977 {
978 if ( layer->hasScaleBasedVisibility() )
979 {
980 mScaleBasedVisibilityDetails[ layer ] = qMakePair( layer->minimumScale(), layer->maximumScale() );
981 }
982 }
983}
984
985QgsExpressionContextUtils::GetLayerVisibility::GetLayerVisibility()
986 : QgsScopedExpressionFunction( QStringLiteral( "is_layer_visible" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "id" ) ), QStringLiteral( "General" ) )
987{}
988
989QVariant QgsExpressionContextUtils::GetLayerVisibility::func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction * )
990{
991 if ( mLayers.isEmpty() )
992 {
993 return false;
994 }
995
996 bool isVisible = false;
998 QgsMapLayer *layer = QgsExpressionUtils::getMapLayer( values.at( 0 ), context, parent );
1000 if ( layer && mLayers.contains( layer ) )
1001 {
1002 isVisible = true;
1003 if ( mScaleBasedVisibilityDetails.contains( layer ) && !qgsDoubleNear( mScale, 0.0 ) )
1004 {
1005 if ( ( !qgsDoubleNear( mScaleBasedVisibilityDetails[ layer ].first, 0.0 ) && mScale > mScaleBasedVisibilityDetails[ layer ].first ) ||
1006 ( !qgsDoubleNear( mScaleBasedVisibilityDetails[ layer ].second, 0.0 ) && mScale < mScaleBasedVisibilityDetails[ layer ].second ) )
1007 {
1008 isVisible = false;
1009 }
1010 }
1011 }
1012
1013 return isVisible;
1014}
1015
1016QgsScopedExpressionFunction *QgsExpressionContextUtils::GetLayerVisibility::clone() const
1017{
1018 GetLayerVisibility *func = new GetLayerVisibility();
1019 func->mLayers = mLayers;
1020 func->mScale = mScale;
1021 func->mScaleBasedVisibilityDetails = mScaleBasedVisibilityDetails;
1022 return func;
1023}
1024
1025//
1026// mesh expression context
1027//
1028
1030class CurrentVertexZValueExpressionFunction: public QgsScopedExpressionFunction
1031{
1032 public:
1033 CurrentVertexZValueExpressionFunction():
1034 QgsScopedExpressionFunction( "$vertex_z",
1035 0,
1036 QStringLiteral( "Meshes" ) )
1037 {}
1038
1039 QgsScopedExpressionFunction *clone() const override {return new CurrentVertexZValueExpressionFunction();}
1040
1041 QVariant func( const QVariantList &, const QgsExpressionContext *context, QgsExpression *, const QgsExpressionNodeFunction * ) override
1042 {
1043 if ( context &&
1044 context->hasVariable( QStringLiteral( "_mesh_vertex_index" ) ) &&
1045 context->hasVariable( QStringLiteral( "_native_mesh" ) ) )
1046 {
1047 int vertexIndex = context->variable( QStringLiteral( "_mesh_vertex_index" ) ).toInt();
1048 const QgsMesh nativeMesh = qvariant_cast<QgsMesh>( context->variable( QStringLiteral( "_native_mesh" ) ) );
1049 const QgsMeshVertex &vertex = nativeMesh.vertex( vertexIndex );
1050 if ( !vertex.isEmpty() )
1051 return vertex.z();
1052 }
1053
1054 return QVariant();
1055 }
1056
1057 bool isStatic( const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext * ) const override
1058 {
1059 return false;
1060 }
1061};
1062
1063class CurrentVertexXValueExpressionFunction: public QgsScopedExpressionFunction
1064{
1065 public:
1066 CurrentVertexXValueExpressionFunction():
1067 QgsScopedExpressionFunction( "$vertex_x",
1068 0,
1069 QStringLiteral( "Meshes" ) )
1070 {}
1071
1072 QgsScopedExpressionFunction *clone() const override {return new CurrentVertexXValueExpressionFunction();}
1073
1074 QVariant func( const QVariantList &, const QgsExpressionContext *context, QgsExpression *, const QgsExpressionNodeFunction * ) override
1075 {
1076 if ( context &&
1077 context->hasVariable( QStringLiteral( "_mesh_vertex_index" ) ) &&
1078 context->hasVariable( QStringLiteral( "_native_mesh" ) ) )
1079 {
1080 int vertexIndex = context->variable( QStringLiteral( "_mesh_vertex_index" ) ).toInt();
1081 const QgsMesh nativeMesh = qvariant_cast<QgsMesh>( context->variable( QStringLiteral( "_native_mesh" ) ) );
1082 const QgsMeshVertex &vertex = nativeMesh.vertex( vertexIndex );
1083 if ( !vertex.isEmpty() )
1084 return vertex.x();
1085 }
1086
1087 return QVariant();
1088 }
1089
1090 bool isStatic( const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext * ) const override
1091 {
1092 return false;
1093 }
1094};
1095
1096class CurrentVertexYValueExpressionFunction: public QgsScopedExpressionFunction
1097{
1098 public:
1099 CurrentVertexYValueExpressionFunction():
1100 QgsScopedExpressionFunction( "$vertex_y",
1101 0,
1102 QStringLiteral( "Meshes" ) )
1103 {}
1104
1105 QgsScopedExpressionFunction *clone() const override {return new CurrentVertexYValueExpressionFunction();}
1106
1107 QVariant func( const QVariantList &, const QgsExpressionContext *context, QgsExpression *, const QgsExpressionNodeFunction * ) override
1108 {
1109 if ( context &&
1110 context->hasVariable( QStringLiteral( "_mesh_vertex_index" ) ) &&
1111 context->hasVariable( QStringLiteral( "_native_mesh" ) ) )
1112 {
1113 int vertexIndex = context->variable( QStringLiteral( "_mesh_vertex_index" ) ).toInt();
1114 const QgsMesh nativeMesh = qvariant_cast<QgsMesh>( context->variable( QStringLiteral( "_native_mesh" ) ) );
1115 const QgsMeshVertex &vertex = nativeMesh.vertex( vertexIndex );
1116 if ( !vertex.isEmpty() )
1117 return vertex.y();
1118 }
1119
1120 return QVariant();
1121 }
1122
1123 bool isStatic( const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext * ) const override
1124 {
1125 return false;
1126 }
1127};
1128
1129class CurrentVertexExpressionFunction: public QgsScopedExpressionFunction
1130{
1131 public:
1132 CurrentVertexExpressionFunction():
1133 QgsScopedExpressionFunction( "$vertex_as_point",
1134 0,
1135 QStringLiteral( "Meshes" ) )
1136 {}
1137
1138 QgsScopedExpressionFunction *clone() const override {return new CurrentVertexExpressionFunction();}
1139
1140 QVariant func( const QVariantList &, const QgsExpressionContext *context, QgsExpression *, const QgsExpressionNodeFunction * ) override
1141 {
1142 if ( context &&
1143 context->hasVariable( QStringLiteral( "_mesh_vertex_index" ) ) &&
1144 context->hasVariable( QStringLiteral( "_native_mesh" ) ) )
1145 {
1146 int vertexIndex = context->variable( QStringLiteral( "_mesh_vertex_index" ) ).toInt();
1147 const QgsMesh nativeMesh = qvariant_cast<QgsMesh>( context->variable( QStringLiteral( "_native_mesh" ) ) );
1148 const QgsMeshVertex &vertex = nativeMesh.vertex( vertexIndex );
1149 if ( !vertex.isEmpty() )
1150 return QVariant::fromValue( QgsGeometry( new QgsPoint( vertex ) ) );
1151 }
1152
1153 return QVariant();
1154 }
1155
1156 bool isStatic( const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext * ) const override
1157 {
1158 return false;
1159 }
1160};
1161
1162class CurrentVertexIndexExpressionFunction: public QgsScopedExpressionFunction
1163{
1164 public:
1165 CurrentVertexIndexExpressionFunction():
1166 QgsScopedExpressionFunction( "$vertex_index",
1167 0,
1168 QStringLiteral( "Meshes" ) )
1169 {}
1170
1171 QgsScopedExpressionFunction *clone() const override {return new CurrentVertexIndexExpressionFunction();}
1172
1173 QVariant func( const QVariantList &, const QgsExpressionContext *context, QgsExpression *, const QgsExpressionNodeFunction * ) override
1174 {
1175 if ( !context )
1176 return QVariant();
1177
1178 if ( !context->hasVariable( QStringLiteral( "_mesh_vertex_index" ) ) )
1179 return QVariant();
1180
1181 return context->variable( QStringLiteral( "_mesh_vertex_index" ) );
1182 }
1183
1184
1185 bool isStatic( const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext * ) const override
1186 {
1187 return false;
1188 }
1189};
1190
1191class CurrentFaceAreaExpressionFunction: public QgsScopedExpressionFunction
1192{
1193 public:
1194 CurrentFaceAreaExpressionFunction():
1195 QgsScopedExpressionFunction( "$face_area",
1196 0,
1197 QStringLiteral( "Meshes" ) )
1198 {}
1199
1200 QgsScopedExpressionFunction *clone() const override {return new CurrentFaceAreaExpressionFunction();}
1201
1202 QVariant func( const QVariantList &, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction * ) override
1203 {
1204 if ( context &&
1205 context->hasVariable( QStringLiteral( "_mesh_face_index" ) ) &&
1206 context->hasVariable( QStringLiteral( "_native_mesh" ) ) )
1207 {
1208 const int faceIndex = context->variable( QStringLiteral( "_mesh_face_index" ) ).toInt();
1209 const QgsMesh nativeMesh = qvariant_cast<QgsMesh>( context->variable( QStringLiteral( "_native_mesh" ) ) );
1210 const QgsMeshFace &face = nativeMesh.face( faceIndex );
1211 if ( !face.isEmpty() )
1212 {
1213 QgsDistanceArea *calc = parent->geomCalculator();
1214 QgsGeometry geom = QgsMeshUtils::toGeometry( nativeMesh.face( faceIndex ), nativeMesh.vertices );
1215 if ( calc )
1216 {
1217 double area = calc->measureArea( geom );
1218 area = calc->convertAreaMeasurement( area, parent->areaUnits() );
1219 return QVariant( area );
1220 }
1221 else
1222 {
1223 return QVariant( geom.area() );
1224 }
1225 }
1226 }
1227
1228 return QVariant();
1229 }
1230
1231 bool isStatic( const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext * ) const override
1232 {
1233 return false;
1234 }
1235};
1236
1237class CurrentFaceIndexExpressionFunction: public QgsScopedExpressionFunction
1238{
1239 public:
1240 CurrentFaceIndexExpressionFunction():
1241 QgsScopedExpressionFunction( "$face_index",
1242 0,
1243 QStringLiteral( "Meshes" ) )
1244 {}
1245
1246 QgsScopedExpressionFunction *clone() const override {return new CurrentFaceIndexExpressionFunction();}
1247
1248 QVariant func( const QVariantList &, const QgsExpressionContext *context, QgsExpression *, const QgsExpressionNodeFunction * ) override
1249 {
1250 if ( !context )
1251 return QVariant();
1252
1253 if ( !context->hasVariable( QStringLiteral( "_mesh_face_index" ) ) )
1254 return QVariant();
1255
1256 return context->variable( QStringLiteral( "_mesh_face_index" ) ).toInt();
1257
1258 }
1259
1260 bool isStatic( const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext * ) const override
1261 {
1262 return false;
1263 }
1264};
1265
1266
1267
1269{
1270 std::unique_ptr<QgsExpressionContextScope> scope = std::make_unique<QgsExpressionContextScope>();
1271
1272 switch ( elementType )
1273 {
1274 case QgsMesh::Vertex:
1275 {
1276 QgsExpression::registerFunction( new CurrentVertexExpressionFunction, true );
1277 QgsExpression::registerFunction( new CurrentVertexXValueExpressionFunction, true );
1278 QgsExpression::registerFunction( new CurrentVertexYValueExpressionFunction, true );
1279 QgsExpression::registerFunction( new CurrentVertexZValueExpressionFunction, true );
1280 QgsExpression::registerFunction( new CurrentVertexIndexExpressionFunction, true );
1281 scope->addFunction( "$vertex_as_point", new CurrentVertexExpressionFunction );
1282 scope->addFunction( "$vertex_x", new CurrentVertexXValueExpressionFunction );
1283 scope->addFunction( "$vertex_y", new CurrentVertexYValueExpressionFunction );
1284 scope->addFunction( "$vertex_z", new CurrentVertexZValueExpressionFunction );
1285 scope->addFunction( "$vertex_index", new CurrentVertexIndexExpressionFunction );
1286 }
1287 break;
1288 case QgsMesh::Face:
1289 {
1290 QgsExpression::registerFunction( new CurrentFaceAreaExpressionFunction, true );
1291 QgsExpression::registerFunction( new CurrentFaceIndexExpressionFunction, true );
1292 scope->addFunction( "$face_area", new CurrentFaceAreaExpressionFunction );
1293 scope->addFunction( "$face_index", new CurrentFaceIndexExpressionFunction );
1294 }
1295 break;
1296 case QgsMesh::Edge:
1297 break;
1298 }
1299
1300 return scope.release();
1301}
1302
1303
1304QVariant LoadLayerFunction::func( const QVariantList &, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
1305{
1306 parent->setEvalErrorString( QObject::tr( "Invalid arguments for load_layer function" ) );
1307 return QVariant();
1308}
1309
1310bool LoadLayerFunction::isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const
1311{
1312 if ( node->args()->count() > 1 )
1313 {
1314 if ( !context )
1315 return false;
1316
1317 QPointer< QgsMapLayerStore > store( context->loadedLayerStore() );
1318 if ( !store )
1319 {
1320 parent->setEvalErrorString( QObject::tr( "load_layer cannot be used in this context" ) );
1321 return false;
1322 }
1323
1324 QgsExpressionNode *uriNode = node->args()->at( 0 );
1325 QgsExpressionNode *providerNode = node->args()->at( 1 );
1326 if ( !uriNode->isStatic( parent, context ) )
1327 {
1328 parent->setEvalErrorString( QObject::tr( "load_layer requires a static value for the uri argument" ) );
1329 return false;
1330 }
1331 if ( !providerNode->isStatic( parent, context ) )
1332 {
1333 parent->setEvalErrorString( QObject::tr( "load_layer requires a static value for the provider argument" ) );
1334 return false;
1335 }
1336
1337 const QString uri = uriNode->eval( parent, context ).toString();
1338 if ( uri.isEmpty() )
1339 {
1340 parent->setEvalErrorString( QObject::tr( "Invalid uri argument for load_layer" ) );
1341 return false;
1342 }
1343
1344 const QString providerKey = providerNode->eval( parent, context ).toString();
1345 if ( providerKey.isEmpty() )
1346 {
1347 parent->setEvalErrorString( QObject::tr( "Invalid provider argument for load_layer" ) );
1348 return false;
1349 }
1350
1351 const QgsCoordinateTransformContext transformContext = context->variable( QStringLiteral( "_project_transform_context" ) ).value<QgsCoordinateTransformContext>();
1352
1353 bool res = false;
1354 auto loadLayer = [ uri, providerKey, store, node, parent, &res, &transformContext ]
1355 {
1357 if ( !metadata )
1358 {
1359 parent->setEvalErrorString( QObject::tr( "Invalid provider argument for load_layer" ) );
1360 return;
1361 }
1362
1363 if ( metadata->supportedLayerTypes().empty() )
1364 {
1365 parent->setEvalErrorString( QObject::tr( "Cannot use %1 provider for load_layer" ).arg( providerKey ) );
1366 return;
1367 }
1368
1369 QgsMapLayerFactory::LayerOptions layerOptions( transformContext );
1370 layerOptions.loadAllStoredStyles = false;
1371 layerOptions.loadDefaultStyle = false;
1372
1373 QgsMapLayer *layer = QgsMapLayerFactory::createLayer( uri, uri, metadata->supportedLayerTypes().value( 0 ), layerOptions, providerKey );
1374 if ( !layer )
1375 {
1376 parent->setEvalErrorString( QObject::tr( "Could not load_layer with uri: %1" ).arg( uri ) );
1377 return;
1378 }
1379 if ( !layer->isValid() )
1380 {
1381 delete layer;
1382 parent->setEvalErrorString( QObject::tr( "Could not load_layer with uri: %1" ).arg( uri ) );
1383 return;
1384 }
1385
1386 store->addMapLayer( layer );
1387
1388 node->setCachedStaticValue( QVariant::fromValue( QgsWeakMapLayerPointer( layer ) ) );
1389 res = true;
1390 };
1391
1392 // Make sure we load the layer on the thread where the store lives
1393 if ( QThread::currentThread() == store->thread() )
1394 loadLayer();
1395 else
1396 QMetaObject::invokeMethod( store, loadLayer, Qt::BlockingQueuedConnection );
1397
1398 return res;
1399 }
1400 return false;
1401}
1402
1403QgsScopedExpressionFunction *LoadLayerFunction::clone() const
1404{
1405 return new LoadLayerFunction();
1406}
1408
static QString version()
Version string.
Definition qgis.cpp:258
static QString releaseName()
Release name.
Definition qgis.cpp:270
static int versionInt()
Version number used for comparing versions using the "Check QGIS Version" function.
Definition qgis.cpp:263
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
static void setCustomVariables(const QVariantMap &customVariables)
Custom expression variables for this application.
static QString osName()
Returns a string name of the operating system QGIS is running on.
static QString platform()
Returns the QGIS platform name, e.g., "desktop", "server", "qgis_process" or "external" (for external...
static QVariantMap customVariables()
Custom expression variables for this application.
static QString locale()
Returns the QGIS locale.
static void setCustomVariable(const QString &name, const QVariant &value)
Set a single custom expression variable.
static QString userFullName()
Returns the user's operating system login account full display name.
static QString userLoginName()
Returns the user's operating system login account name.
QString toProj() const
Returns a Proj string representation of this CRS.
QString ellipsoidAcronym() const
Returns the ellipsoid acronym for the ellipsoid used by the CRS.
QString projectionAcronym() const
Returns the projection acronym for the projection used by the CRS.
QString toWkt(Qgis::CrsWktVariant variant=Qgis::CrsWktVariant::Wkt1Gdal, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
QgsProjOperation operation() const
Returns information about the PROJ operation associated with the coordinate reference system,...
Contains information about the context in which a coordinate transform is executed.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
double measureArea(const QgsGeometry &geometry) const
Measures the area of a geometry.
double convertAreaMeasurement(double area, Qgis::AreaUnit toUnits) const
Takes an area measurement calculated by this QgsDistanceArea object and converts it to a different ar...
Single scope for storing variables and functions for use within a QgsExpressionContext.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the scope.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the scope.
void addFunction(const QString &name, QgsScopedExpressionFunction *function)
Adds a function to the scope.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
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.
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...
QgsMapLayerStore * loadedLayerStore() const
Returns the destination layer store for any layers loaded during expression evaluation.
static const QString EXPR_SYMBOL_COLOR
Inbuilt variable name for symbol color variable.
bool hasVariable(const QString &name) const
Check whether a variable is specified by any scope within the context.
static const QString EXPR_SYMBOL_ANGLE
Inbuilt variable name for symbol angle variable.
QVariantMap variablesToMap() const
Returns a map of variable name to value representing all the expression variables contained by the co...
QVariant variable(const QString &name) const
Fetches a matching variable from the context.
A abstract base class for defining QgsExpression functions.
static bool allParamsStatic(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context)
This will return true if all the params for the provided function node are static within the constrai...
An expression node for expression functions.
QgsExpressionNode::NodeList * args() const
Returns a list of arguments specified for the function.
QgsExpressionNode * at(int i)
Gets the node at position i in the list.
int count() const
Returns the number of nodes in the list.
Abstract base class for all nodes that can appear in an expression.
QVariant eval(QgsExpression *parent, const QgsExpressionContext *context)
Evaluate this node with the given context and parent.
virtual bool isStatic(QgsExpression *parent, const QgsExpressionContext *context) const =0
Returns true if this node can be evaluated for a static value.
Class for parsing and evaluation of expressions (formerly called "search strings").
static bool registerFunction(QgsExpressionFunction *function, bool transferOwnership=false)
Registers a function to the expression engine.
Qgis::AreaUnit areaUnits() const
Returns the desired areal units for calculations involving geomCalculator(), e.g.,...
void setEvalErrorString(const QString &str)
Sets evaluation error (used internally by evaluation functions)
QgsDistanceArea * geomCalculator()
Returns calculator used for distance and area calculations (used by $length, $area and $perimeter fun...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:56
QgsFeatureId id
Definition qgsfeature.h:64
QgsGeometry geometry
Definition qgsfeature.h:67
bool isValid() const
Returns the validity of this feature.
Container of fields for a vector layer.
Definition qgsfields.h:45
A geometry is the spatial representation of a feature.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
double area() const
Returns the planar, 2-dimensional area of the geometry.
A representation of the interval between two datetime values.
Definition qgsinterval.h:46
Class used to render QgsLayout as an atlas, by iterating over the features from an associated vector ...
QString nameForPage(int page) const
Returns the calculated name for a specified atlas page number.
QgsLayout * layout() override
Returns the layout associated with the iterator.
bool enabled() const
Returns whether the atlas generation is enabled.
QString currentFilename() const
Returns the current feature filename.
int count() const override
Returns the number of features to iterate over.
QgsVectorLayer * coverageLayer() const
Returns the coverage layer used for the atlas features.
int currentFeatureNumber() const
Returns the current feature number, where a value of 0 corresponds to the first feature.
Layout graphical items for displaying a map.
Item representing the paper in a layout.
Base class for graphical items within a QgsLayout.
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
int page() const
Returns the page the item is currently on, with the first page returning 0.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
virtual QString uuid() const
Returns the item identification string.
QString id() const
Returns the item's ID name.
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from the object.
const QgsLayout * layout() const
Returns the layout the object is attached to.
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the object.
QPointF pagePositionToLayoutPosition(int page, const QgsLayoutPoint &position) const
Converts a position on a page to an absolute position in layout coordinates.
int pageCount() const
Returns the number of pages in the collection.
QgsLayoutItemPage * page(int pageNumber)
Returns a specific page (by pageNumber) from the collection.
This class provides a method of storing points, consisting of an x and y coordinate,...
double dpi() const
Returns the dpi for outputting the layout.
QgsFeature feature() const
Returns the current feature for evaluating the layout.
QgsVectorLayer * layer() const
Returns the vector layer associated with the layout's context.
QSizeF toQSizeF() const
Converts the layout size to a QSizeF.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:49
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the layout.
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout's render context, which stores information relating to the current ...
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout's page collection, which stores and manages page items in the layout.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from the layout.
QgsLayoutReportContext & reportContext()
Returns a reference to the layout's report context, which stores information relating to the current ...
static QgsMapLayer * createLayer(const QString &uri, const QString &name, Qgis::LayerType type, const LayerOptions &options, const QString &provider=QString())
Creates a map layer, given a uri, name, layer type and provider name.
Base class for all map layer types.
Definition qgsmaplayer.h:75
QString name
Definition qgsmaplayer.h:78
Q_INVOKABLE QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:81
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Q_INVOKABLE void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for layer.
The QgsMapSettings class contains configuration for rendering of the map.
Qgis::DistanceUnit mapUnits() const
Returns the units of the map's geographical coordinates - used for scale calculation.
QList< QgsMapLayer * > layers(bool expandGroupLayers=false) const
Returns the list of layers which will be rendered in the map.
double scale() const
Returns the calculated map scale.
long long currentFrame() const
Returns the current frame number of the map, for maps which are part of an animation.
double frameRate() const
Returns the frame rate of the map (in frames per second), for maps which are part of an animation.
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes output image size into account.
double rotation() const
Returns the rotation of the resulting map image, in degrees clockwise.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
A marker symbol type, for rendering Point and MultiPoint geometries.
double angle() const
Returns the marker angle for the whole symbol.
Interface for master layout type objects, such as print layouts and reports.
A class to represent a 2D point.
Definition qgspointxy.h:60
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
double z
Definition qgspoint.h:54
double x
Definition qgspoint.h:52
bool isEmpty() const override
Returns true if the geometry is empty.
Definition qgspoint.cpp:733
double y
Definition qgspoint.h:53
Abstract base class for processing algorithms.
QString id() const
Returns the unique ID for the algorithm, which is a combination of the algorithm provider's ID and th...
Contains information about the context in which a processing algorithm is executed.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
QString description() const
Description.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsExpressionContextScope * createExpressionContextScope() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QString fileName
Definition qgsproject.h:110
QString absoluteFilePath() const
Returns full absolute path to the project file if the project is stored in a file system - derived fr...
QVariantMap customVariables() const
A map of custom project variables.
void setCustomVariables(const QVariantMap &customVariables)
A map of custom project variables.
QgsProjectStorage * projectStorage() const
Returns pointer to project storage implementation that handles read/write of the project file.
Holds data provider key, description, and associated shared library file or function pointer informat...
virtual QList< Qgis::LayerType > supportedLayerTypes() const
Returns a list of the map layer types supported by the provider.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
double width() const
Returns the width of the rectangle.
QgsPointXY center() const
Returns the center point of the rectangle.
double height() const
Returns the height of the rectangle.
Expression function for use within a QgsExpressionContextScope.
QSet< QString > referencedColumns(const QgsExpressionNodeFunction *node) const override
Returns a set of field names which are required for this function.
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.
bool usesGeometry(const QgsExpressionNodeFunction *node) const override
Does this function use a geometry object.
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
QColor color() const
Returns the symbol's color.
const QgsDateTimeRange & temporalRange() const
Returns the datetime range for the object.
bool isTemporal() const
Returns true if the object's temporal range is enabled, and the object will be filtered when renderin...
T begin() const
Returns the beginning of the range.
Definition qgsrange.h:390
T end() const
Returns the upper bound of the range.
Definition qgsrange.h:397
static Q_INVOKABLE QString toString(Qgis::DistanceUnit unit)
Returns a translated string representing a distance unit.
Represents a vector layer which manages a vector based data sets.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
static double scaleToZoom(double mapScale, double z0Scale=559082264.0287178)
Finds zoom level given map scale denominator.
static int scaleToZoomLevel(double mapScale, int sourceMinZoom, int sourceMaxZoom, double z0Scale=559082264.0287178)
Finds the best fitting zoom level given a map scale denominator and allowed zoom level range.
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
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
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:5713
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:5712
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:5144
#define FID_IS_NULL(fid)
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
QVector< int > QgsMeshFace
List of vertex indexes.
Single variable definition for use within a QgsExpressionContextScope.
Setting options for loading layers.
Mesh - vertices, edges and faces.
QVector< QgsMeshVertex > vertices
QgsMeshFace face(int index) const
Returns a face at the index.
ElementType
Defines type of mesh elements.
QgsMeshVertex vertex(int index) const
Returns a vertex at the index.