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