QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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"
21 #include "qgsprocessingcontext.h"
22 #include "qgsprocessingmodelalgorithm.h"
23 #include "qgsprocessingalgorithm.h"
24 #include "qgsmapsettings.h"
25 #include "qgssymbollayerutils.h"
26 #include "qgslayout.h"
27 #include "qgslayoutitem.h"
28 #include "qgsexpressionutils.h"
30 #include "qgslayoutatlas.h"
31 #include "qgslayoutmultiframe.h"
32 #include "qgsfeatureid.h"
33 
35 {
36  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Global" ) );
37 
38  QVariantMap customVariables = QgsApplication::customVariables();
39 
40  for ( QVariantMap::const_iterator it = customVariables.constBegin(); it != customVariables.constEnd(); ++it )
41  {
42  scope->setVariable( it.key(), it.value(), true );
43  }
44 
45  //add some extra global variables
46  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_version" ), Qgis::version(), true, true ) );
47  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_version_no" ), Qgis::versionInt(), true, true ) );
48  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_short_version" ), QStringLiteral( "%1.%2" ).arg( Qgis::versionInt() / 10000 ).arg( Qgis::versionInt() / 100 % 100 ), true, true ) );
49  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_release_name" ), Qgis::releaseName(), true, true ) );
50  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_platform" ), QgsApplication::platform(), true, true ) );
51  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_os_name" ), QgsApplication::osName(), true, true ) );
52  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_locale" ), QgsApplication::locale(), true, true ) );
53  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "user_account_name" ), QgsApplication::userLoginName(), true, true ) );
54  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "user_full_name" ), QgsApplication::userFullName(), true, true ) );
55 
56  return scope;
57 }
58 
59 void QgsExpressionContextUtils::setGlobalVariable( const QString &name, const QVariant &value )
60 {
61  QgsApplication::setCustomVariable( name, value );
62 }
63 
64 void QgsExpressionContextUtils::setGlobalVariables( const QVariantMap &variables )
65 {
67 }
68 
70 {
71  QVariantMap vars = QgsApplication::customVariables();
72  if ( vars.remove( name ) )
74 }
75 
77 
78 class GetLayoutItemVariables : public QgsScopedExpressionFunction
79 {
80  public:
81  GetLayoutItemVariables( const QgsLayout *c )
82  : QgsScopedExpressionFunction( QStringLiteral( "item_variables" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "id" ) ), QStringLiteral( "Layout" ) )
83  , mLayout( c )
84  {}
85 
86  QVariant func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * ) override
87  {
88  if ( !mLayout )
89  return QVariant();
90 
91  QString id = values.at( 0 ).toString();
92 
93  const QgsLayoutItem *item = mLayout->itemById( id );
94  if ( !item )
95  return QVariant();
96 
98 
99  return c.variablesToMap();
100  }
101 
102  QgsScopedExpressionFunction *clone() const override
103  {
104  return new GetLayoutItemVariables( mLayout );
105  }
106 
107  private:
108 
109  const QgsLayout *mLayout = nullptr;
110 
111 };
112 
113 class GetCurrentFormFieldValue : public QgsScopedExpressionFunction
114 {
115  public:
116  GetCurrentFormFieldValue( )
117  : QgsScopedExpressionFunction( QStringLiteral( "current_value" ), QgsExpressionFunction::ParameterList() << QStringLiteral( "field_name" ), QStringLiteral( "Form" ) )
118  {}
119 
120  QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *, const QgsExpressionNodeFunction * ) override
121  {
122  QString fieldName( values.at( 0 ).toString() );
123  const QgsFeature feat( context->variable( QStringLiteral( "current_feature" ) ).value<QgsFeature>() );
124  if ( fieldName.isEmpty() || ! feat.isValid( ) )
125  {
126  return QVariant();
127  }
128  return feat.attribute( fieldName ) ;
129  }
130 
131  QgsScopedExpressionFunction *clone() const override
132  {
133  return new GetCurrentFormFieldValue( );
134  }
135 
136  bool isStatic( const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext * ) const override
137  {
138  return false;
139  };
140 
141 };
142 
143 class GetCurrentParentFormFieldValue : public QgsScopedExpressionFunction
144 {
145  public:
146  GetCurrentParentFormFieldValue( )
147  : QgsScopedExpressionFunction( QStringLiteral( "current_parent_value" ), QgsExpressionFunction::ParameterList() << QStringLiteral( "field_name" ), QStringLiteral( "Form" ) )
148  {}
149 
150  QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *, const QgsExpressionNodeFunction * ) override
151  {
152  QString fieldName( values.at( 0 ).toString() );
153  const QgsFeature feat( context->variable( QStringLiteral( "current_parent_feature" ) ).value<QgsFeature>() );
154  if ( fieldName.isEmpty() || ! feat.isValid( ) )
155  {
156  return QVariant();
157  }
158  return feat.attribute( fieldName ) ;
159  }
160 
161  QgsScopedExpressionFunction *clone() const override
162  {
163  return new GetCurrentParentFormFieldValue( );
164  }
165 
166  bool isStatic( const QgsExpressionNodeFunction *, QgsExpression *, const QgsExpressionContext * ) const override
167  {
168  return false;
169  };
170 
171 };
172 
173 
174 class GetProcessingParameterValue : public QgsScopedExpressionFunction
175 {
176  public:
177  GetProcessingParameterValue( const QVariantMap &params )
178  : QgsScopedExpressionFunction( QStringLiteral( "parameter" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "name" ) ), QStringLiteral( "Processing" ) )
179  , mParams( params )
180  {}
181 
182  QVariant func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * ) override
183  {
184  return mParams.value( values.at( 0 ).toString() );
185  }
186 
187  QgsScopedExpressionFunction *clone() const override
188  {
189  return new GetProcessingParameterValue( mParams );
190  }
191 
192  private:
193 
194  const QVariantMap mParams;
195 
196 };
197 
199 
200 
201 QgsExpressionContextScope *QgsExpressionContextUtils::formScope( const QgsFeature &formFeature, const QString &formMode )
202 {
203  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Form" ) );
204  scope->addFunction( QStringLiteral( "current_value" ), new GetCurrentFormFieldValue( ) );
205  scope->setVariable( QStringLiteral( "current_geometry" ), formFeature.geometry( ), true );
206  scope->setVariable( QStringLiteral( "current_feature" ), formFeature, true );
207  scope->setVariable( QStringLiteral( "form_mode" ), formMode, true );
208  return scope;
209 }
210 
211 
212 QgsExpressionContextScope *QgsExpressionContextUtils::parentFormScope( const QgsFeature &parentFormFeature, const QString &parentFormMode )
213 {
214  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Parent Form" ) );
215  scope->addFunction( QStringLiteral( "current_parent_value" ), new GetCurrentParentFormFieldValue( ) );
216  scope->setVariable( QStringLiteral( "current_parent_geometry" ), parentFormFeature.geometry( ), true );
217  scope->setVariable( QStringLiteral( "current_parent_feature" ), parentFormFeature, true );
218  scope->setVariable( QStringLiteral( "parent_form_mode" ), parentFormMode, true );
219  return scope;
220 }
221 
223 {
224  if ( !project )
225  {
226  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Project" ) );
227  return scope;
228  }
229  else
230  return project->createExpressionContextScope();
231 }
232 
233 void QgsExpressionContextUtils::setProjectVariable( QgsProject *project, const QString &name, const QVariant &value )
234 {
235  if ( !project )
236  return;
237 
238  QVariantMap vars = project->customVariables();
239 
240  vars.insert( name, value );
241 
242  project->setCustomVariables( vars );
243 }
244 
245 void QgsExpressionContextUtils::setProjectVariables( QgsProject *project, const QVariantMap &variables )
246 {
247  if ( !project )
248  return;
249 
250  project->setCustomVariables( variables );
251 }
252 
254 {
255  if ( !project )
256  {
257  return;
258  }
259 
260  QVariantMap vars = project->customVariables();
261  if ( vars.remove( name ) )
262  project->setCustomVariables( vars );
263 }
264 
266 {
267  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Layer" ) );
268 
269  if ( !layer )
270  return scope;
271 
272  //add variables defined in layer properties
273  const QStringList variableNames = layer->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
274  const QStringList variableValues = layer->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
275 
276  int varIndex = 0;
277  for ( const QString &variableName : variableNames )
278  {
279  if ( varIndex >= variableValues.length() )
280  {
281  break;
282  }
283 
284  QVariant varValue = variableValues.at( varIndex );
285  varIndex++;
286  scope->setVariable( variableName, varValue, true );
287  }
288 
289  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_name" ), layer->name(), true, true ) );
290  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_id" ), layer->id(), true, true ) );
291  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "_layer_crs" ), QVariant::fromValue<QgsCoordinateReferenceSystem>( layer->crs() ), true, true ) );
292  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer" ), QVariant::fromValue<QgsWeakMapLayerPointer >( QgsWeakMapLayerPointer( const_cast<QgsMapLayer *>( layer ) ) ), true, true ) );
293 
294  const QgsVectorLayer *vLayer = qobject_cast< const QgsVectorLayer * >( layer );
295  if ( vLayer )
296  {
297  scope->setFields( vLayer->fields() );
298  }
299 
300  //TODO - add functions. Possibilities include:
301  //is_selected
302  //field summary stats
303 
304  return scope;
305 }
306 
307 QList<QgsExpressionContextScope *> QgsExpressionContextUtils::globalProjectLayerScopes( const QgsMapLayer *layer )
308 {
309  QList<QgsExpressionContextScope *> scopes;
310  scopes << globalScope();
311 
312  QgsProject *project = QgsProject::instance(); // TODO: use project associated with layer
313  if ( project )
314  scopes << projectScope( project );
315 
316  if ( layer )
317  scopes << layerScope( layer );
318  return scopes;
319 }
320 
321 
322 void QgsExpressionContextUtils::setLayerVariable( QgsMapLayer *layer, const QString &name, const QVariant &value )
323 {
324  if ( !layer )
325  return;
326 
327  //write variable to layer
328  QStringList variableNames = layer->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
329  QStringList variableValues = layer->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
330 
331  variableNames << name;
332  variableValues << value.toString();
333 
334  layer->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
335  layer->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
336 }
337 
338 void QgsExpressionContextUtils::setLayerVariables( QgsMapLayer *layer, const QVariantMap &variables )
339 {
340  if ( !layer )
341  return;
342 
343  QStringList variableNames;
344  QStringList variableValues;
345 
346  QVariantMap::const_iterator it = variables.constBegin();
347  for ( ; it != variables.constEnd(); ++it )
348  {
349  variableNames << it.key();
350  variableValues << it.value().toString();
351  }
352 
353  layer->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
354  layer->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
355 }
356 
358 {
359  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
360  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
361 
362  // and because people don't read that ^^, I'm going to blast it all over this function
363 
364  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Map Settings" ) );
365 
366  //add known map settings context variables
367  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_id" ), "canvas", true ) );
368  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_rotation" ), mapSettings.rotation(), true ) );
369  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_scale" ), mapSettings.scale(), true ) );
370 
371  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
372  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
373 
374  QgsGeometry extent = QgsGeometry::fromRect( mapSettings.visibleExtent() );
375  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent" ), QVariant::fromValue( extent ), true ) );
376  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_width" ), mapSettings.visibleExtent().width(), true ) );
377  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_height" ), mapSettings.visibleExtent().height(), true ) );
378 
379  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
380  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
381 
382  QgsGeometry centerPoint = QgsGeometry::fromPointXY( mapSettings.visibleExtent().center() );
383  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_center" ), QVariant::fromValue( centerPoint ), true ) );
384 
385  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
386  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
387 
388  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs" ), mapSettings.destinationCrs().authid(), true ) );
389  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_definition" ), mapSettings.destinationCrs().toProj(), true ) );
390  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_units" ), QgsUnitTypes::toString( mapSettings.mapUnits() ), true ) );
391  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_description" ), mapSettings.destinationCrs().description(), true ) );
392  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_acronym" ), mapSettings.destinationCrs().projectionAcronym(), true ) );
393  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_ellipsoid" ), mapSettings.destinationCrs().ellipsoidAcronym(), true ) );
394  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_proj4" ), mapSettings.destinationCrs().toProj(), true ) );
395  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_wkt" ), mapSettings.destinationCrs().toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ), true ) );
396 
397  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
398  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
399 
400  QVariantList layersIds;
401  QVariantList layers;
402  const QList<QgsMapLayer *> layersInMap = mapSettings.layers();
403  layersIds.reserve( layersInMap.count() );
404  layers.reserve( layersInMap.count() );
405  for ( QgsMapLayer *layer : layersInMap )
406  {
407  layersIds << layer->id();
408  layers << QVariant::fromValue<QgsWeakMapLayerPointer>( QgsWeakMapLayerPointer( layer ) );
409  }
410 
411  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
412  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
413 
414  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layer_ids" ), layersIds, true ) );
415  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layers" ), layers, true ) );
416 
417  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
418  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
419 
420  scope->addFunction( QStringLiteral( "is_layer_visible" ), new GetLayerVisibility( mapSettings.layers(), mapSettings.scale() ) );
421 
422  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
423  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
424 
425  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_start_time" ), mapSettings.isTemporal() ? mapSettings.temporalRange().begin() : QVariant(), true ) );
426  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_end_time" ), mapSettings.isTemporal() ? mapSettings.temporalRange().end() : QVariant(), true ) );
427  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_interval" ), mapSettings.isTemporal() ? ( mapSettings.temporalRange().end() - mapSettings.temporalRange().begin() ) : QVariant(), true ) );
428 
429  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
430  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
431 
432  return scope;
433 }
434 
435 QgsExpressionContextScope *QgsExpressionContextUtils::mapToolCaptureScope( const QList<QgsPointLocator::Match> &matches )
436 {
437  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Map Tool Capture" ) );
438 
439  QVariantList matchList;
440 
441  for ( const QgsPointLocator::Match &match : matches )
442  {
443  QVariantMap matchMap;
444 
445  matchMap.insert( QStringLiteral( "valid" ), match.isValid() );
446  matchMap.insert( QStringLiteral( "layer" ), QVariant::fromValue<QgsWeakMapLayerPointer>( QgsWeakMapLayerPointer( match.layer() ) ) );
447  matchMap.insert( QStringLiteral( "feature_id" ), match.featureId() );
448  matchMap.insert( QStringLiteral( "vertex_index" ), match.vertexIndex() );
449  matchMap.insert( QStringLiteral( "distance" ), match.distance() );
450 
451  matchList.append( matchMap );
452  }
453 
454  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "snapping_results" ), matchList ) );
455 
456  return scope;
457 }
458 
460 {
461  if ( !symbolScope )
462  return nullptr;
463 
464  symbolScope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_SYMBOL_COLOR, symbol ? symbol->color() : QColor(), true ) );
465 
466  double angle = 0.0;
467  const QgsMarkerSymbol *markerSymbol = dynamic_cast< const QgsMarkerSymbol * >( symbol );
468  if ( markerSymbol )
469  {
470  angle = markerSymbol->angle();
471  }
473 
474  return symbolScope;
475 }
476 
478 {
479  std::unique_ptr< QgsExpressionContextScope > scope( new QgsExpressionContextScope( QObject::tr( "Layout" ) ) );
480  if ( !layout )
481  return scope.release();
482 
483  //add variables defined in layout properties
484  const QStringList variableNames = layout->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
485  const QStringList variableValues = layout->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
486 
487  int varIndex = 0;
488 
489  for ( const QString &variableName : variableNames )
490  {
491  if ( varIndex >= variableValues.length() )
492  {
493  break;
494  }
495 
496  QVariant varValue = variableValues.at( varIndex );
497  varIndex++;
498  scope->setVariable( variableName, varValue );
499  }
500 
501  //add known layout context variables
502  if ( const QgsMasterLayoutInterface *l = dynamic_cast< const QgsMasterLayoutInterface * >( layout ) )
503  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_name" ), l->name(), true ) );
504 
505  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_numpages" ), layout->pageCollection()->pageCount(), true ) );
506  if ( layout->pageCollection()->pageCount() > 0 )
507  {
508  // just take first page size
509  QSizeF s = layout->pageCollection()->page( 0 )->sizeWithUnits().toQSizeF();
510  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageheight" ), s.height(), true ) );
511  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pagewidth" ), s.width(), true ) );
512  }
513 
514  QVariantList offsets;
515  for ( int i = 0; i < layout->pageCollection()->pageCount(); i++ )
516  {
517  QPointF p = layout->pageCollection()->pagePositionToLayoutPosition( i, QgsLayoutPoint( 0, 0 ) );
518  offsets << p.y();
519  }
520  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageoffsets" ), offsets, true ) );
521 
522  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_dpi" ), layout->renderContext().dpi(), true ) );
523 
524  scope->addFunction( QStringLiteral( "item_variables" ), new GetLayoutItemVariables( layout ) );
525 
526  if ( layout->reportContext().layer() )
527  {
528  scope->setFields( layout->reportContext().layer()->fields() );
529  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_layerid" ), layout->reportContext().layer()->id(), true ) );
530  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_layername" ), layout->reportContext().layer()->name(), true ) );
531  }
532 
533  if ( layout->reportContext().feature().isValid() )
534  {
535  QgsFeature atlasFeature = layout->reportContext().feature();
536  scope->setFeature( atlasFeature );
537  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_feature" ), QVariant::fromValue( atlasFeature ), true ) );
538  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featureid" ), FID_IS_NULL( atlasFeature.id() ) ? QVariant() : atlasFeature.id(), true ) );
539  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_geometry" ), QVariant::fromValue( atlasFeature.geometry() ), true ) );
540  }
541 
542  return scope.release();
543 }
544 
545 void QgsExpressionContextUtils::setLayoutVariable( QgsLayout *layout, const QString &name, const QVariant &value )
546 {
547  if ( !layout )
548  return;
549 
550  //write variable to layout
551  QStringList variableNames = layout->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
552  QStringList variableValues = layout->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
553 
554  variableNames << name;
555  variableValues << value.toString();
556 
557  layout->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
558  layout->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
559 }
560 
561 void QgsExpressionContextUtils::setLayoutVariables( QgsLayout *layout, const QVariantMap &variables )
562 {
563  if ( !layout )
564  return;
565 
566  QStringList variableNames;
567  QStringList variableValues;
568 
569  QVariantMap::const_iterator it = variables.constBegin();
570  for ( ; it != variables.constEnd(); ++it )
571  {
572  variableNames << it.key();
573  variableValues << it.value().toString();
574  }
575 
576  layout->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
577  layout->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
578 }
579 
581 {
582  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Atlas" ) );
583  if ( !atlas )
584  {
585  //add some dummy atlas variables. This is done so that as in certain contexts we want to show
586  //users that these variables are available even if they have no current value
587  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_pagename" ), QString(), true ) );
588  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_feature" ), QVariant::fromValue( QgsFeature() ), true ) );
589  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featureid" ), QVariant(), true ) );
590  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_geometry" ), QVariant::fromValue( QgsGeometry() ), true ) );
591  return scope;
592  }
593 
594  //add known atlas variables
595  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_totalfeatures" ), atlas->count(), true ) );
596  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featurenumber" ), atlas->currentFeatureNumber() + 1, true ) );
597  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_filename" ), atlas->currentFilename(), true ) );
598  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_pagename" ), atlas->nameForPage( atlas->currentFeatureNumber() ), true ) );
599 
600  if ( atlas->enabled() && atlas->coverageLayer() )
601  {
602  scope->setFields( atlas->coverageLayer()->fields() );
603  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_layerid" ), atlas->coverageLayer()->id(), true ) );
604  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_layername" ), atlas->coverageLayer()->name(), true ) );
605  }
606 
607  if ( atlas->enabled() )
608  {
609  QgsFeature atlasFeature = atlas->layout()->reportContext().feature();
610  scope->setFeature( atlasFeature );
611  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_feature" ), QVariant::fromValue( atlasFeature ), true ) );
612  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featureid" ), FID_IS_NULL( atlasFeature.id() ) ? QVariant() : atlasFeature.id(), true ) );
613  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_geometry" ), QVariant::fromValue( atlasFeature.geometry() ), true ) );
614  }
615 
616  return scope;
617 }
618 
620 {
621  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Layout Item" ) );
622  if ( !item )
623  return scope;
624 
625  //add variables defined in layout item properties
626  const QStringList variableNames = item->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
627  const QStringList variableValues = item->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
628 
629  int varIndex = 0;
630  for ( const QString &variableName : variableNames )
631  {
632  if ( varIndex >= variableValues.length() )
633  {
634  break;
635  }
636 
637  QVariant varValue = variableValues.at( varIndex );
638  varIndex++;
639  scope->setVariable( variableName, varValue );
640  }
641 
642  //add known layout item context variables
643  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "item_id" ), item->id(), true ) );
644  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "item_uuid" ), item->uuid(), true ) );
645  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_page" ), item->page() + 1, true ) );
646 
647  if ( item->layout() )
648  {
649  const QgsLayoutItemPage *page = item->layout()->pageCollection()->page( item->page() );
650  if ( page )
651  {
652  const QSizeF s = page->sizeWithUnits().toQSizeF();
653  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageheight" ), s.height(), true ) );
654  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pagewidth" ), s.width(), true ) );
655  }
656  else
657  {
658  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageheight" ), QVariant(), true ) );
659  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pagewidth" ), QVariant(), true ) );
660  }
661  }
662 
663  return scope;
664 }
665 
666 void QgsExpressionContextUtils::setLayoutItemVariable( QgsLayoutItem *item, const QString &name, const QVariant &value )
667 {
668  if ( !item )
669  return;
670 
671  //write variable to layout item
672  QStringList variableNames = item->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
673  QStringList variableValues = item->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
674 
675  variableNames << name;
676  variableValues << value.toString();
677 
678  item->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
679  item->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
680 }
681 
682 void QgsExpressionContextUtils::setLayoutItemVariables( QgsLayoutItem *item, const QVariantMap &variables )
683 {
684  if ( !item )
685  return;
686 
687  QStringList variableNames;
688  QStringList variableValues;
689 
690  QVariantMap::const_iterator it = variables.constBegin();
691  for ( ; it != variables.constEnd(); ++it )
692  {
693  variableNames << it.key();
694  variableValues << it.value().toString();
695  }
696 
697  item->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
698  item->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
699 }
700 
702 {
703  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Multiframe Item" ) );
704  if ( !frame )
705  return scope;
706 
707  //add variables defined in layout item properties
708  const QStringList variableNames = frame->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
709  const QStringList variableValues = frame->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
710 
711  int varIndex = 0;
712  for ( const QString &variableName : variableNames )
713  {
714  if ( varIndex >= variableValues.length() )
715  {
716  break;
717  }
718 
719  QVariant varValue = variableValues.at( varIndex );
720  varIndex++;
721  scope->setVariable( variableName, varValue );
722  }
723 
724  return scope;
725 }
726 
727 void QgsExpressionContextUtils::setLayoutMultiFrameVariable( QgsLayoutMultiFrame *frame, const QString &name, const QVariant &value )
728 {
729  if ( !frame )
730  return;
731 
732  //write variable to layout multiframe
733  QStringList variableNames = frame->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
734  QStringList variableValues = frame->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
735 
736  variableNames << name;
737  variableValues << value.toString();
738 
739  frame->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
740  frame->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
741 }
742 
744 {
745  if ( !frame )
746  return;
747 
748  QStringList variableNames;
749  QStringList variableValues;
750 
751  QVariantMap::const_iterator it = variables.constBegin();
752  for ( ; it != variables.constEnd(); ++it )
753  {
754  variableNames << it.key();
755  variableValues << it.value().toString();
756  }
757 
758  frame->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
759  frame->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
760 }
761 
763 {
765  scope->setFeature( feature );
766  scope->setFields( fields );
767  return QgsExpressionContext() << scope;
768 }
769 
771 {
772  // set aside for future use
773  Q_UNUSED( context )
774 
775  std::unique_ptr< QgsExpressionContextScope > scope( new QgsExpressionContextScope( QObject::tr( "Algorithm" ) ) );
776  scope->addFunction( QStringLiteral( "parameter" ), new GetProcessingParameterValue( parameters ) );
777 
778  if ( !algorithm )
779  return scope.release();
780 
781  //add standard algorithm variables
782  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "algorithm_id" ), algorithm->id(), true ) );
783 
784  return scope.release();
785 }
786 
787 QgsExpressionContextScope *QgsExpressionContextUtils::processingModelAlgorithmScope( const QgsProcessingModelAlgorithm *model, const QVariantMap &, QgsProcessingContext &context )
788 {
789  std::unique_ptr< QgsExpressionContextScope > modelScope( new QgsExpressionContextScope( QObject::tr( "Model" ) ) );
790  QString modelPath;
791  if ( !model->sourceFilePath().isEmpty() )
792  {
793  modelPath = model->sourceFilePath();
794  }
795  else if ( context.project() )
796  {
797  // fallback to project path -- the model may be embedded in a project, OR an unsaved model. In either case the
798  // project path is a logical value to fall back to
799  modelPath = context.project()->projectStorage() ? context.project()->fileName() : context.project()->absoluteFilePath();
800  }
801 
802  const QString modelFolder = !modelPath.isEmpty() ? QFileInfo( modelPath ).path() : QString();
803  modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_path" ), QDir::toNativeSeparators( modelPath ), true ) );
804  modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_folder" ), QDir::toNativeSeparators( modelFolder ), true, true ) );
805  modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_name" ), model->displayName(), true ) );
806  modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_group" ), model->group(), true ) );
807 
808  // custom variables
809  const QVariantMap customVariables = model->variables();
810  for ( auto it = customVariables.constBegin(); it != customVariables.constEnd(); ++it )
811  {
812  modelScope->addVariable( QgsExpressionContextScope::StaticVariable( it.key(), it.value(), true ) );
813  }
814 
815  return modelScope.release();
816 }
817 
819 {
820  std::unique_ptr< QgsExpressionContextScope > scope( new QgsExpressionContextScope() );
821  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "notification_message" ), message, true ) );
822  return scope.release();
823 }
824 
826 {
827  QgsExpression::registerFunction( new GetNamedProjectColor( nullptr ) );
828  QgsExpression::registerFunction( new GetLayoutItemVariables( nullptr ) );
829  QgsExpression::registerFunction( new GetLayerVisibility( QList<QgsMapLayer *>(), 0.0 ) );
830  QgsExpression::registerFunction( new GetProcessingParameterValue( QVariantMap() ) );
831  QgsExpression::registerFunction( new GetCurrentFormFieldValue( ) );
832  QgsExpression::registerFunction( new GetCurrentParentFormFieldValue( ) );
833 }
834 
836 {
837  Q_UNUSED( node )
838  return mUsesGeometry;
839 }
840 
842 {
843  Q_UNUSED( node )
844  return mReferencedColumns;
845 }
846 
848 {
849  return allParamsStatic( node, parent, context );
850 }
851 
852 //
853 // GetLayerVisibility
854 //
855 
856 QgsExpressionContextUtils::GetLayerVisibility::GetLayerVisibility( const QList<QgsMapLayer *> &layers, double scale )
857  : QgsScopedExpressionFunction( QStringLiteral( "is_layer_visible" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "id" ) ), QStringLiteral( "General" ) )
858  , mLayers( _qgis_listRawToQPointer( layers ) )
859  , mScale( scale )
860 {
861  for ( const auto &layer : mLayers )
862  {
863  if ( layer->hasScaleBasedVisibility() )
864  {
865  mScaleBasedVisibilityDetails[ layer ] = qMakePair( layer->minimumScale(), layer->maximumScale() );
866  }
867  }
868 }
869 
870 QgsExpressionContextUtils::GetLayerVisibility::GetLayerVisibility()
871  : QgsScopedExpressionFunction( QStringLiteral( "is_layer_visible" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "id" ) ), QStringLiteral( "General" ) )
872 {}
873 
874 QVariant QgsExpressionContextUtils::GetLayerVisibility::func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
875 {
876  if ( mLayers.isEmpty() )
877  {
878  return false;
879  }
880 
881  bool isVisible = false;
882  QgsMapLayer *layer = QgsExpressionUtils::getMapLayer( values.at( 0 ), parent );
883  if ( layer && mLayers.contains( layer ) )
884  {
885  isVisible = true;
886  if ( mScaleBasedVisibilityDetails.contains( layer ) && !qgsDoubleNear( mScale, 0.0 ) )
887  {
888  if ( ( !qgsDoubleNear( mScaleBasedVisibilityDetails[ layer ].first, 0.0 ) && mScale > mScaleBasedVisibilityDetails[ layer ].first ) ||
889  ( !qgsDoubleNear( mScaleBasedVisibilityDetails[ layer ].second, 0.0 ) && mScale < mScaleBasedVisibilityDetails[ layer ].second ) )
890  {
891  isVisible = false;
892  }
893  }
894  }
895 
896  return isVisible;
897 }
898 
899 QgsScopedExpressionFunction *QgsExpressionContextUtils::GetLayerVisibility::clone() const
900 {
901  GetLayerVisibility *func = new GetLayerVisibility();
902  func->mLayers = mLayers;
903  func->mScale = mScale;
904  func->mScaleBasedVisibilityDetails = mScaleBasedVisibilityDetails;
905  return func;
906 }
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsLayoutObject::customProperty
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from the object.
Definition: qgslayoutobject.cpp:141
QgsMapLayer::crs
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:89
QgsApplication::osName
static QString osName()
Returns a string name of the operating system QGIS is running on.
Definition: qgsapplication.cpp:1166
QgsLayoutItem::id
QString id() const
Returns the item's ID name.
Definition: qgslayoutitem.h:357
QgsExpressionContextScope::addFunction
void addFunction(const QString &name, QgsScopedExpressionFunction *function)
Adds a function to the scope.
Definition: qgsexpressioncontext.cpp:189
QgsLayoutObject::layout
const QgsLayout * layout() const
Returns the layout the object is attached to.
Definition: qgslayoutobject.cpp:126
QgsExpressionContextScope::setFeature
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the scope.
Definition: qgsexpressioncontext.h:317
qgsexpressioncontextutils.h
QgsSymbol::color
QColor color() const
Returns the symbol's color.
Definition: qgssymbol.cpp:514
QgsRectangle::height
double height() const SIP_HOLDGIL
Returns the height of the rectangle.
Definition: qgsrectangle.h:209
QgsProject::customVariables
QVariantMap customVariables() const
A map of custom project variables.
Definition: qgsproject.cpp:1786
QgsFeature::id
Q_GADGET QgsFeatureId id
Definition: qgsfeature.h:64
QgsProject::projectStorage
QgsProjectStorage * projectStorage() const
Returns pointer to project storage implementation that handles read/write of the project file.
Definition: qgsproject.cpp:666
Qgis::version
static QString version()
Version string.
Definition: qgis.cpp:276
QgsCoordinateReferenceSystem::description
QString description() const
Returns the descriptive name of the CRS, e.g., "WGS 84" or "GDA 94 / Vicgrid94".
Definition: qgscoordinatereferencesystem.cpp:1326
QgsLayoutItemPage
Item representing the paper in a layout.
Definition: qgslayoutitempage.h:55
QgsExpressionContextScope::setVariable
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.
Definition: qgsexpressioncontext.cpp:78
QgsExpressionContextUtils::globalScope
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Definition: qgsexpressioncontextutils.cpp:34
QgsCoordinateReferenceSystem::projectionAcronym
QString projectionAcronym() const
Returns the projection acronym for the projection used by the CRS.
Definition: qgscoordinatereferencesystem.cpp:1361
QgsProcessingContext::project
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Definition: qgsprocessingcontext.h:105
QgsExpressionContextScope::addVariable
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
Definition: qgsexpressioncontext.cpp:93
QgsExpressionContextUtils::mapToolCaptureScope
static QgsExpressionContextScope * mapToolCaptureScope(const QList< QgsPointLocator::Match > &matches)
Sets the expression context variables which are available for expressions triggered by a map tool cap...
Definition: qgsexpressioncontextutils.cpp:435
qgsexpression.h
QgsMapLayer::setCustomProperty
Q_INVOKABLE void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for layer.
Definition: qgsmaplayer.cpp:1708
algorithm
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
QgsCoordinateReferenceSystem::WKT_PREFERRED
@ WKT_PREFERRED
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
Definition: qgscoordinatereferencesystem.h:679
QgsTemporalRangeObject::isTemporal
bool isTemporal() const
Returns true if the object's temporal range is enabled, and the object will be filtered when renderin...
Definition: qgstemporalrangeobject.cpp:30
QgsExpressionContextUtils::notificationScope
static QgsExpressionContextScope * notificationScope(const QString &message=QString())
Creates a new scope which contains variables and functions relating to provider notifications.
Definition: qgsexpressioncontextutils.cpp:818
QgsLayoutAtlas::count
int count() const override
Returns the number of features to iterate over.
Definition: qgslayoutatlas.cpp:391
QgsRectangle::center
QgsPointXY center() const SIP_HOLDGIL
Returns the center point of the rectangle.
Definition: qgsrectangle.h:230
QgsExpressionContextUtils::formScope
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...
Definition: qgsexpressioncontextutils.cpp:201
QgsApplication::setCustomVariables
static void setCustomVariables(const QVariantMap &customVariables)
Custom expression variables for this application.
Definition: qgsapplication.cpp:1801
QgsExpressionContextUtils::registerContextFunctions
static void registerContextFunctions()
Registers all known core functions provided by QgsExpressionContextScope objects.
Definition: qgsexpressioncontextutils.cpp:825
qgssymbollayerutils.h
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:45
QgsExpressionContextUtils::setProjectVariable
static void setProjectVariable(QgsProject *project, const QString &name, const QVariant &value)
Sets a project context variable.
Definition: qgsexpressioncontextutils.cpp:233
QgsGeometry::fromPointXY
static QgsGeometry fromPointXY(const QgsPointXY &point) SIP_HOLDGIL
Creates a new geometry from a QgsPointXY object.
Definition: qgsgeometry.cpp:164
QgsExpressionContextUtils::layerScope
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
Definition: qgsexpressioncontextutils.cpp:265
QgsExpressionContextUtils::setLayerVariables
static void setLayerVariables(QgsMapLayer *layer, const QVariantMap &variables)
Sets all layer context variables.
Definition: qgsexpressioncontextutils.cpp:338
qgslayoutmultiframe.h
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:67
QgsTemporalRangeObject::temporalRange
const QgsDateTimeRange & temporalRange() const
Returns the datetime range for the object.
Definition: qgstemporalrangeobject.cpp:43
QgsExpressionContextUtils::mapSettingsScope
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object.
Definition: qgsexpressioncontextutils.cpp:357
QgsProject::setCustomVariables
void setCustomVariables(const QVariantMap &customVariables)
A map of custom project variables.
Definition: qgsproject.cpp:1791
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:468
QgsExpressionContextUtils::globalProjectLayerScopes
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Definition: qgsexpressioncontextutils.cpp:307
QgsExpressionContextUtils::layoutScope
static QgsExpressionContextScope * layoutScope(const QgsLayout *layout)
Creates a new scope which contains variables and functions relating to a QgsLayout layout.
Definition: qgsexpressioncontextutils.cpp:477
QgsExpressionContextUtils::layoutItemScope
static QgsExpressionContextScope * layoutItemScope(const QgsLayoutItem *item)
Creates a new scope which contains variables and functions relating to a QgsLayoutItem.
Definition: qgsexpressioncontextutils.cpp:619
qgsfeatureid.h
QgsLayoutItem::page
int page() const
Returns the page the item is currently on, with the first page returning 0.
Definition: qgslayoutitem.cpp:541
QgsExpressionContextUtils::multiFrameScope
static QgsExpressionContextScope * multiFrameScope(const QgsLayoutMultiFrame *frame)
Creates a new scope which contains variables and functions relating to a QgsLayoutMultiFrame.
Definition: qgsexpressioncontextutils.cpp:701
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:64
QgsExpressionContextScope::setFields
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the scope.
Definition: qgsexpressioncontext.cpp:195
QgsLayoutMultiFrame
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
Definition: qgslayoutmultiframe.h:49
qgsmapsettings.h
QgsExpressionContextUtils::setLayerVariable
static void setLayerVariable(QgsMapLayer *layer, const QString &name, const QVariant &value)
Sets a layer context variable.
Definition: qgsexpressioncontextutils.cpp:322
QgsMarkerSymbol::angle
double angle() const
Returns the marker angle for the whole symbol.
Definition: qgssymbol.cpp:1514
QgsExpressionContextUtils::removeProjectVariable
static void removeProjectVariable(QgsProject *project, const QString &name)
Remove project context variable.
Definition: qgsexpressioncontextutils.cpp:253
QgsExpressionContext::EXPR_SYMBOL_COLOR
static const QString EXPR_SYMBOL_COLOR
Inbuilt variable name for symbol color variable.
Definition: qgsexpressioncontext.h:723
QgsExpressionContext::variable
QVariant variable(const QString &name) const
Fetches a matching variable from the context.
Definition: qgsexpressioncontext.cpp:296
QgsLayoutItem::sizeWithUnits
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
Definition: qgslayoutitem.h:671
FID_IS_NULL
#define FID_IS_NULL(fid)
Definition: qgsfeatureid.h:30
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:95
QgsApplication::platform
static QString platform()
Returns the QGIS platform name, e.g., "desktop" or "server".
Definition: qgsapplication.cpp:1189
QgsExpressionContext::EXPR_SYMBOL_ANGLE
static const QString EXPR_SYMBOL_ANGLE
Inbuilt variable name for symbol angle variable.
Definition: qgsexpressioncontext.h:725
QgsApplication::locale
static QString locale()
Returns the QGIS locale.
Definition: qgsapplication.cpp:1194
qgsapplication.h
QgsLayout::reportContext
QgsLayoutReportContext & reportContext()
Returns a reference to the layout's report context, which stores information relating to the current ...
Definition: qgslayout.cpp:369
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3283
QgsExpressionContextUtils::projectScope
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
Definition: qgsexpressioncontextutils.cpp:222
QgsExpressionContextUtils::setLayoutVariable
static void setLayoutVariable(QgsLayout *layout, const QString &name, const QVariant &value)
Sets a layout context variable.
Definition: qgsexpressioncontextutils.cpp:545
QgsLayout::renderContext
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout's render context, which stores information relating to the current ...
Definition: qgslayout.cpp:359
QgsMapSettings::rotation
double rotation() const
Returns the rotation of the resulting map image, in degrees clockwise.
Definition: qgsmapsettings.cpp:101
QgsProcessingAlgorithm::id
QString id() const
Returns the unique ID for the algorithm, which is a combination of the algorithm provider's ID and th...
Definition: qgsprocessingalgorithm.cpp:50
QgsLayoutSize::toQSizeF
QSizeF toQSizeF() const
Converts the layout size to a QSizeF.
Definition: qgslayoutsize.cpp:47
QgsLayout::customProperty
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from the layout.
Definition: qgslayout.cpp:415
QgsLayoutAtlas::enabled
bool enabled() const
Returns whether the atlas generation is enabled.
Definition: qgslayoutatlas.h:67
QgsExpressionContextUtils::setLayoutMultiFrameVariables
static void setLayoutMultiFrameVariables(QgsLayoutMultiFrame *frame, const QVariantMap &variables)
Sets all layout multiframe context variables for an frame.
Definition: qgsexpressioncontextutils.cpp:743
qgslayoutitem.h
QgsFeature::isValid
bool isValid() const
Returns the validity of this feature.
Definition: qgsfeature.cpp:185
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:44
QgsMarkerSymbol
A marker symbol type, for rendering Point and MultiPoint geometries.
Definition: qgssymbol.h:931
QgsUnitTypes::toString
static Q_INVOKABLE QString toString(QgsUnitTypes::DistanceUnit unit)
Returns a translated string representing a distance unit.
Definition: qgsunittypes.cpp:199
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
qgsprocessingalgorithm.h
QgsMapSettings::mapUnits
QgsUnitTypes::DistanceUnit mapUnits() const
Gets units of map's geographical coordinates - used for scale calculation.
Definition: qgsmapsettings.cpp:360
QgsLayoutReportContext::feature
QgsFeature feature() const
Returns the current feature for evaluating the layout.
Definition: qgslayoutreportcontext.h:62
QgsLayoutAtlas::coverageLayer
QgsVectorLayer * coverageLayer() const
Returns the coverage layer used for the atlas features.
Definition: qgslayoutatlas.h:116
QgsExpressionContextUtils::parentFormScope
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...
Definition: qgsexpressioncontextutils.cpp:212
QgsCoordinateReferenceSystem::authid
QString authid() const
Returns the authority identifier for the CRS.
Definition: qgscoordinatereferencesystem.cpp:1321
qgsexpressionutils.h
QgsExpressionContextUtils::processingAlgorithmScope
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,...
Definition: qgsexpressioncontextutils.cpp:770
QgsExpressionContextUtils::setGlobalVariable
static void setGlobalVariable(const QString &name, const QVariant &value)
Sets a global context variable.
Definition: qgsexpressioncontextutils.cpp:59
QgsCoordinateReferenceSystem::toWkt
QString toWkt(WktVariant variant=WKT1_GDAL, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
Definition: qgscoordinatereferencesystem.cpp:1954
QgsExpressionContextUtils::setLayoutItemVariable
static void setLayoutItemVariable(QgsLayoutItem *item, const QString &name, const QVariant &value)
Sets a layout item context variable, with the given name and value.
Definition: qgsexpressioncontextutils.cpp:666
QgsExpressionContextUtils::processingModelAlgorithmScope
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,...
Definition: qgsexpressioncontextutils.cpp:787
QgsExpressionContextUtils::setLayoutItemVariables
static void setLayoutItemVariables(QgsLayoutItem *item, const QVariantMap &variables)
Sets all layout item context variables for an item.
Definition: qgsexpressioncontextutils.cpp:682
QgsScopedExpressionFunction::referencedColumns
QSet< QString > referencedColumns(const QgsExpressionNodeFunction *node) const override
Returns a set of field names which are required for this function.
Definition: qgsexpressioncontextutils.cpp:841
QgsMapLayer::id
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Definition: qgsmaplayer.cpp:148
QgsLayoutPageCollection::page
QgsLayoutItemPage * page(int pageNumber)
Returns a specific page (by pageNumber) from the collection.
Definition: qgslayoutpagecollection.cpp:460
QgsApplication::setCustomVariable
static void setCustomVariable(const QString &name, const QVariant &value)
Set a single custom expression variable.
Definition: qgsapplication.cpp:1816
QgsWeakMapLayerPointer
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:1688
QgsMapSettings::scale
double scale() const
Returns the calculated map scale.
Definition: qgsmapsettings.cpp:396
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:113
QgsProject::fileName
QString fileName
Definition: qgsproject.h:98
QgsCoordinateReferenceSystem::toProj
QString toProj() const
Returns a Proj string representation of this CRS.
Definition: qgscoordinatereferencesystem.cpp:1420
QgsExpressionNodeFunction
An expression node for expression functions.
Definition: qgsexpressionnodeimpl.h:317
qgslayout.h
QgsLayoutObject::setCustomProperty
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the object.
Definition: qgslayoutobject.cpp:136
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext.
Definition: qgsexpressioncontext.h:112
QgsExpressionContextUtils::setGlobalVariables
static void setGlobalVariables(const QVariantMap &variables)
Sets all global context variables.
Definition: qgsexpressioncontextutils.cpp:64
QgsLayoutAtlas::currentFeatureNumber
int currentFeatureNumber() const
Returns the current feature number, where a value of 0 corresponds to the first feature.
Definition: qgslayoutatlas.h:254
QgsProject::absoluteFilePath
QString absoluteFilePath() const
Returns full absolute path to the project file if the project is stored in a file system - derived fr...
Definition: qgsproject.cpp:696
QgsPointLocator::Match
Definition: qgspointlocator.h:185
qgsvectorlayer.h
QgsMapSettings::destinationCrs
QgsCoordinateReferenceSystem destinationCrs() const
returns CRS of destination coordinate reference system
Definition: qgsmapsettings.cpp:318
QgsExpressionContextUtils::createFeatureBasedContext
static QgsExpressionContext createFeatureBasedContext(const QgsFeature &feature, const QgsFields &fields)
Helper function for creating an expression context which contains just a feature and fields collectio...
Definition: qgsexpressioncontextutils.cpp:762
QgsApplication::userLoginName
static QString userLoginName()
Returns the user's operating system login account name.
Definition: qgsapplication.cpp:1097
QgsLayoutItem::uuid
virtual QString uuid() const
Returns the item identification string.
Definition: qgslayoutitem.h:343
QgsMapLayer::customProperty
Q_INVOKABLE QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
Definition: qgsmaplayer.cpp:1723
QgsExpressionContextUtils::updateSymbolScope
static QgsExpressionContextScope * updateSymbolScope(const QgsSymbol *symbol, QgsExpressionContextScope *symbolScope=nullptr)
Updates a symbol scope related to a QgsSymbol to an expression context.
Definition: qgsexpressioncontextutils.cpp:459
QgsProcessingAlgorithm
Abstract base class for processing algorithms.
Definition: qgsprocessingalgorithm.h:52
QgsLayout::pageCollection
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout's page collection, which stores and manages page items in the layout.
Definition: qgslayout.cpp:459
c
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
Definition: porting_processing.dox:1
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsExpressionFunction
A abstract base class for defining QgsExpression functions.
Definition: qgsexpressionfunction.h:41
QgsLayoutAtlas::currentFilename
QString currentFilename() const
Returns the current feature filename.
Definition: qgslayoutatlas.cpp:493
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsRectangle::width
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
Definition: qgsrectangle.h:202
QgsScopedExpressionFunction::clone
virtual QgsScopedExpressionFunction * clone() const =0
Returns a clone of the function.
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QgsGeometry::fromRect
static QgsGeometry fromRect(const QgsRectangle &rect) SIP_HOLDGIL
Creates a new geometry from a QgsRectangle.
Definition: qgsgeometry.cpp:229
QgsMapLayer::name
QString name
Definition: qgsmaplayer.h:86
QgsExpressionContextUtils::removeGlobalVariable
static void removeGlobalVariable(const QString &name)
Remove a global context variable.
Definition: qgsexpressioncontextutils.cpp:69
QgsProject::createExpressionContextScope
QgsExpressionContextScope * createExpressionContextScope() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Definition: qgsproject.cpp:1871
QgsScopedExpressionFunction::isStatic
bool isStatic(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context) const override
Will be called during prepare to determine if the function is static.
Definition: qgsexpressioncontextutils.cpp:847
QgsLayoutAtlas::layout
QgsLayout * layout() override
Returns the layout associated with the iterator.
Definition: qgslayoutatlas.cpp:44
QgsExpressionContextUtils::atlasScope
static QgsExpressionContextScope * atlasScope(const QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
Definition: qgsexpressioncontextutils.cpp:580
qgsprocessingcontext.h
qgslayoutpagecollection.h
QgsExpressionFunction::allParamsStatic
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...
Definition: qgsexpressionfunction.cpp:234
Qgis::releaseName
static QString releaseName()
Release name.
Definition: qgis.cpp:288
QgsMapSettings::layers
QList< QgsMapLayer * > layers() const
Gets list of layers for map rendering The layers are stored in the reverse order of how they are rend...
Definition: qgsmapsettings.cpp:282
QgsApplication::customVariables
static QVariantMap customVariables()
Custom expression variables for this application.
Definition: qgsapplication.cpp:1782
QgsExpression::registerFunction
static bool registerFunction(QgsExpressionFunction *function, bool transferOwnership=false)
Registers a function to the expression engine.
Definition: qgsexpressionfunction.cpp:6978
QgsLayoutRenderContext::dpi
double dpi() const
Returns the dpi for outputting the layout.
Definition: qgslayoutrendercontext.cpp:86
QgsCoordinateReferenceSystem::ellipsoidAcronym
QString ellipsoidAcronym() const
Returns the ellipsoid acronym for the ellipsoid used by the CRS.
Definition: qgscoordinatereferencesystem.cpp:1373
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsMasterLayoutInterface
Interface for master layout type objects, such as print layouts and reports.
Definition: qgsmasterlayoutinterface.h:43
QgsLayout::setCustomProperty
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the layout.
Definition: qgslayout.cpp:407
QgsScopedExpressionFunction::usesGeometry
bool usesGeometry(const QgsExpressionNodeFunction *node) const override
Does this function use a geometry object.
Definition: qgsexpressioncontextutils.cpp:835
QgsExpressionContextScope::StaticVariable
Single variable definition for use within a QgsExpressionContextScope.
Definition: qgsexpressioncontext.h:119
QgsLayoutPageCollection::pageCount
int pageCount() const
Returns the number of pages in the collection.
Definition: qgslayoutpagecollection.cpp:455
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:105
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map.
Definition: qgsmapsettings.h:88
QgsMapSettings::visibleExtent
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes takes output image size into accou...
Definition: qgsmapsettings.cpp:371
QgsExpressionContextUtils::setLayoutMultiFrameVariable
static void setLayoutMultiFrameVariable(QgsLayoutMultiFrame *frame, const QString &name, const QVariant &value)
Sets a layout multi frame context variable, with the given name and value.
Definition: qgsexpressioncontextutils.cpp:727
QgsLayoutPoint
This class provides a method of storing points, consisting of an x and y coordinate,...
Definition: qgslayoutpoint.h:40
QgsScopedExpressionFunction
Expression function for use within a QgsExpressionContextScope.
Definition: qgsexpressioncontext.h:38
MathUtils::angle
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)
Definition: MathUtils.cpp:786
Qgis::versionInt
static int versionInt()
Version number used for comparing versions using the "Check QGIS Version" function.
Definition: qgis.cpp:281
QgsLayoutAtlas
Class used to render QgsLayout as an atlas, by iterating over the features from an associated vector ...
Definition: qgslayoutatlas.h:42
QgsLayoutItem::createExpressionContext
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Definition: qgslayoutitem.cpp:1158
QgsScopedExpressionFunction::func
QVariant func(const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node) override=0
Returns result of evaluating the function.
qgsproject.h
QgsLayoutReportContext::layer
QgsVectorLayer * layer() const
Returns the vector layer associated with the layout's context.
Definition: qgslayoutreportcontext.cpp:66
QgsExpressionContextUtils::setProjectVariables
static void setProjectVariables(QgsProject *project, const QVariantMap &variables)
Sets all project context variables.
Definition: qgsexpressioncontextutils.cpp:245
QgsExpressionContextUtils::setLayoutVariables
static void setLayoutVariables(QgsLayout *layout, const QVariantMap &variables)
Sets all layout context variables.
Definition: qgsexpressioncontextutils.cpp:561
QgsLayoutPageCollection::pagePositionToLayoutPosition
QPointF pagePositionToLayoutPosition(int page, const QgsLayoutPoint &position) const
Converts a position on a page to an absolute position in layout coordinates.
Definition: qgslayoutpagecollection.cpp:222
QgsApplication::userFullName
static QString userFullName()
Returns the user's operating system login account full display name.
Definition: qgsapplication.cpp:1132
qgslayoutatlas.h
QgsLayoutAtlas::nameForPage
QString nameForPage(int page) const
Returns the calculated name for a specified atlas page number.
Definition: qgslayoutatlas.cpp:174