QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
33 {
34  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Global" ) );
35 
36  QVariantMap customVariables = QgsApplication::customVariables();
37 
38  for ( QVariantMap::const_iterator it = customVariables.constBegin(); it != customVariables.constEnd(); ++it )
39  {
40  scope->setVariable( it.key(), it.value(), true );
41  }
42 
43  //add some extra global variables
44  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_version" ), Qgis::QGIS_VERSION, true, true ) );
45  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_version_no" ), Qgis::QGIS_VERSION_INT, true, true ) );
46  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_short_version" ), QStringLiteral( "%1.%2" ).arg( Qgis::QGIS_VERSION_INT / 10000 ).arg( Qgis::QGIS_VERSION_INT / 100 % 100 ), true, true ) );
47  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_release_name" ), Qgis::QGIS_RELEASE_NAME, true, true ) );
48  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_platform" ), QgsApplication::platform(), true, true ) );
49  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_os_name" ), QgsApplication::osName(), true, true ) );
50  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_locale" ), QgsApplication::locale(), true, true ) );
51  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "user_account_name" ), QgsApplication::userLoginName(), true, true ) );
52  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "user_full_name" ), QgsApplication::userFullName(), true, true ) );
53 
54  return scope;
55 }
56 
57 void QgsExpressionContextUtils::setGlobalVariable( const QString &name, const QVariant &value )
58 {
59  QgsApplication::setCustomVariable( name, value );
60 }
61 
62 void QgsExpressionContextUtils::setGlobalVariables( const QVariantMap &variables )
63 {
65 }
66 
68 {
69  QVariantMap vars = QgsApplication::customVariables();
70  if ( vars.remove( name ) )
72 }
73 
74 
76 
77 class GetNamedProjectColor : public QgsScopedExpressionFunction
78 {
79  public:
80  GetNamedProjectColor( const QgsProject *project )
81  : QgsScopedExpressionFunction( QStringLiteral( "project_color" ), 1, QStringLiteral( "Color" ) )
82  , mProject( project )
83  {
84  if ( !project )
85  return;
86 
87  //build up color list from project. Do this in advance for speed
88  QStringList colorStrings = project->readListEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Colors" ) );
89  QStringList colorLabels = project->readListEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Labels" ) );
90 
91  //generate list from custom colors
92  int colorIndex = 0;
93  for ( QStringList::iterator it = colorStrings.begin();
94  it != colorStrings.end(); ++it )
95  {
96  QColor color = QgsSymbolLayerUtils::decodeColor( *it );
97  QString label;
98  if ( colorLabels.length() > colorIndex )
99  {
100  label = colorLabels.at( colorIndex );
101  }
102 
103  mColors.insert( label.toLower(), color );
104  colorIndex++;
105  }
106  }
107 
108  QVariant func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * ) override
109  {
110  QString colorName = values.at( 0 ).toString().toLower();
111  if ( mColors.contains( colorName ) )
112  {
113  return QStringLiteral( "%1,%2,%3" ).arg( mColors.value( colorName ).red() ).arg( mColors.value( colorName ).green() ).arg( mColors.value( colorName ).blue() );
114  }
115  else
116  return QVariant();
117  }
118 
119  QgsScopedExpressionFunction *clone() const override
120  {
121  return new GetNamedProjectColor( mProject );
122  }
123 
124  private:
125 
126  const QgsProject *mProject = nullptr;
127  QHash< QString, QColor > mColors;
128 
129 };
130 
131 class GetLayoutItemVariables : public QgsScopedExpressionFunction
132 {
133  public:
134  GetLayoutItemVariables( const QgsLayout *c )
135  : QgsScopedExpressionFunction( QStringLiteral( "item_variables" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "id" ) ), QStringLiteral( "Layout" ) )
136  , mLayout( c )
137  {}
138 
139  QVariant func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * ) override
140  {
141  if ( !mLayout )
142  return QVariant();
143 
144  QString id = values.at( 0 ).toString();
145 
146  const QgsLayoutItem *item = mLayout->itemById( id );
147  if ( !item )
148  return QVariant();
149 
151 
152  return c.variablesToMap();
153  }
154 
155  QgsScopedExpressionFunction *clone() const override
156  {
157  return new GetLayoutItemVariables( mLayout );
158  }
159 
160  private:
161 
162  const QgsLayout *mLayout = nullptr;
163 
164 };
165 
166 class GetCurrentFormFieldValue : public QgsScopedExpressionFunction
167 {
168  public:
169  GetCurrentFormFieldValue( )
170  : QgsScopedExpressionFunction( QStringLiteral( "current_value" ), QgsExpressionFunction::ParameterList() << QStringLiteral( "field_name" ), QStringLiteral( "Form" ) )
171  {}
172 
173  QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *, const QgsExpressionNodeFunction * ) override
174  {
175  QString fieldName( values.at( 0 ).toString() );
176  const QgsFeature feat( context->variable( QStringLiteral( "current_feature" ) ).value<QgsFeature>() );
177  if ( fieldName.isEmpty() || ! feat.isValid( ) )
178  {
179  return QVariant();
180  }
181  return feat.attribute( fieldName ) ;
182  }
183 
184  QgsScopedExpressionFunction *clone() const override
185  {
186  return new GetCurrentFormFieldValue( );
187  }
188 
189 };
190 
191 
192 class GetProcessingParameterValue : public QgsScopedExpressionFunction
193 {
194  public:
195  GetProcessingParameterValue( const QVariantMap &params )
196  : QgsScopedExpressionFunction( QStringLiteral( "parameter" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "name" ) ), QStringLiteral( "Processing" ) )
197  , mParams( params )
198  {}
199 
200  QVariant func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * ) override
201  {
202  return mParams.value( values.at( 0 ).toString() );
203  }
204 
205  QgsScopedExpressionFunction *clone() const override
206  {
207  return new GetProcessingParameterValue( mParams );
208  }
209 
210  private:
211 
212  const QVariantMap mParams;
213 
214 };
215 
217 
218 
219 QgsExpressionContextScope *QgsExpressionContextUtils::formScope( const QgsFeature &formFeature, const QString &formMode )
220 {
221  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Form" ) );
222  scope->addFunction( QStringLiteral( "current_value" ), new GetCurrentFormFieldValue( ) );
223  scope->setVariable( QStringLiteral( "current_geometry" ), formFeature.geometry( ), true );
224  scope->setVariable( QStringLiteral( "current_feature" ), formFeature, true );
225  scope->setVariable( QStringLiteral( "form_mode" ), formMode, true );
226  return scope;
227 }
228 
230 {
231  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Project" ) );
232 
233  if ( !project )
234  return scope;
235 
236  const QVariantMap vars = project->customVariables();
237 
238  QVariantMap::const_iterator it = vars.constBegin();
239 
240  for ( ; it != vars.constEnd(); ++it )
241  {
242  scope->setVariable( it.key(), it.value(), true );
243  }
244 
245  QString projectPath = project->projectStorage() ? project->fileName() : project->absoluteFilePath();
246  QString projectFolder = QFileInfo( projectPath ).path();
247  QString projectFilename = QFileInfo( projectPath ).fileName();
248  QString projectBasename = project->baseName();
249 
250  //add other known project variables
251  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_title" ), project->title(), true, true ) );
252  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_path" ), QDir::toNativeSeparators( projectPath ), true, true ) );
253  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_folder" ), QDir::toNativeSeparators( projectFolder ), true, true ) );
254  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_filename" ), projectFilename, true, true ) );
255  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_basename" ), projectBasename, true, true ) );
256  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_home" ), QDir::toNativeSeparators( project->homePath() ), true, true ) );
257  QgsCoordinateReferenceSystem projectCrs = project->crs();
258  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs" ), projectCrs.authid(), true, true ) );
259  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs_definition" ), projectCrs.toProj4(), true, true ) );
260  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_ellipsoid" ), project->ellipsoid(), true, true ) );
261  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_distance_units" ), QgsUnitTypes::toString( project->distanceUnits() ), true, true ) );
262  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_area_units" ), QgsUnitTypes::toString( project->areaUnits() ), true, true ) );
263  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "_project_transform_context" ), QVariant::fromValue<QgsCoordinateTransformContext>( project->transformContext() ), true, true ) );
264 
265  // metadata
266  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_author" ), project->metadata().author(), true, true ) );
267  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_abstract" ), project->metadata().abstract(), true, true ) );
268  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_creation_date" ), project->metadata().creationDateTime(), true, true ) );
269  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_identifier" ), project->metadata().identifier(), true, true ) );
270 
271  // keywords
272  QVariantMap keywords;
273  QgsAbstractMetadataBase::KeywordMap metadataKeywords = project->metadata().keywords();
274  for ( auto it = metadataKeywords.constBegin(); it != metadataKeywords.constEnd(); ++it )
275  {
276  keywords.insert( it.key(), it.value() );
277  }
278  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_keywords" ), keywords, true, true ) );
279 
280  scope->addFunction( QStringLiteral( "project_color" ), new GetNamedProjectColor( project ) );
281  return scope;
282 }
283 
284 void QgsExpressionContextUtils::setProjectVariable( QgsProject *project, const QString &name, const QVariant &value )
285 {
286  if ( !project )
287  return;
288 
289  QVariantMap vars = project->customVariables();
290 
291  vars.insert( name, value );
292 
293  project->setCustomVariables( vars );
294 }
295 
296 void QgsExpressionContextUtils::setProjectVariables( QgsProject *project, const QVariantMap &variables )
297 {
298  if ( !project )
299  return;
300 
301  project->setCustomVariables( variables );
302 }
303 
305 {
306  if ( !project )
307  {
308  return;
309  }
310 
311  QVariantMap vars = project->customVariables();
312  if ( vars.remove( name ) )
313  project->setCustomVariables( vars );
314 }
315 
317 {
318  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Layer" ) );
319 
320  if ( !layer )
321  return scope;
322 
323  //add variables defined in layer properties
324  const QStringList variableNames = layer->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
325  const QStringList variableValues = layer->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
326 
327  int varIndex = 0;
328  for ( const QString &variableName : variableNames )
329  {
330  if ( varIndex >= variableValues.length() )
331  {
332  break;
333  }
334 
335  QVariant varValue = variableValues.at( varIndex );
336  varIndex++;
337  scope->setVariable( variableName, varValue, true );
338  }
339 
340  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_name" ), layer->name(), true, true ) );
341  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_id" ), layer->id(), true, true ) );
342  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "_layer_crs" ), QVariant::fromValue<QgsCoordinateReferenceSystem>( layer->crs() ), true, true ) );
343  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer" ), QVariant::fromValue<QgsWeakMapLayerPointer >( QgsWeakMapLayerPointer( const_cast<QgsMapLayer *>( layer ) ) ), true, true ) );
344 
345  const QgsVectorLayer *vLayer = qobject_cast< const QgsVectorLayer * >( layer );
346  if ( vLayer )
347  {
348  scope->setFields( vLayer->fields() );
349  }
350 
351  //TODO - add functions. Possibilities include:
352  //is_selected
353  //field summary stats
354 
355  return scope;
356 }
357 
358 QList<QgsExpressionContextScope *> QgsExpressionContextUtils::globalProjectLayerScopes( const QgsMapLayer *layer )
359 {
360  QList<QgsExpressionContextScope *> scopes;
361  scopes << globalScope();
362 
363  QgsProject *project = QgsProject::instance(); // TODO: use project associated with layer
364  if ( project )
365  scopes << projectScope( project );
366 
367  if ( layer )
368  scopes << layerScope( layer );
369  return scopes;
370 }
371 
372 
373 void QgsExpressionContextUtils::setLayerVariable( QgsMapLayer *layer, const QString &name, const QVariant &value )
374 {
375  if ( !layer )
376  return;
377 
378  //write variable to layer
379  QStringList variableNames = layer->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
380  QStringList variableValues = layer->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
381 
382  variableNames << name;
383  variableValues << value.toString();
384 
385  layer->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
386  layer->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
387 }
388 
389 void QgsExpressionContextUtils::setLayerVariables( QgsMapLayer *layer, const QVariantMap &variables )
390 {
391  if ( !layer )
392  return;
393 
394  QStringList variableNames;
395  QStringList variableValues;
396 
397  QVariantMap::const_iterator it = variables.constBegin();
398  for ( ; it != variables.constEnd(); ++it )
399  {
400  variableNames << it.key();
401  variableValues << it.value().toString();
402  }
403 
404  layer->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
405  layer->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
406 }
407 
409 {
410  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
411  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
412 
413  // and because people don't read that ^^, I'm going to blast it all over this function
414 
415  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Map Settings" ) );
416 
417  //add known map settings context variables
418  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_id" ), "canvas", true ) );
419  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_rotation" ), mapSettings.rotation(), true ) );
420  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_scale" ), mapSettings.scale(), true ) );
421 
422  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
423  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
424 
425  QgsGeometry extent = QgsGeometry::fromRect( mapSettings.visibleExtent() );
426  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent" ), QVariant::fromValue( extent ), true ) );
427  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_width" ), mapSettings.visibleExtent().width(), true ) );
428  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_height" ), mapSettings.visibleExtent().height(), true ) );
429 
430  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
431  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
432 
433  QgsGeometry centerPoint = QgsGeometry::fromPointXY( mapSettings.visibleExtent().center() );
434  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_center" ), QVariant::fromValue( centerPoint ), true ) );
435 
436  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
437  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
438 
439  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs" ), mapSettings.destinationCrs().authid(), true ) );
440  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_definition" ), mapSettings.destinationCrs().toProj4(), true ) );
441  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_units" ), QgsUnitTypes::toString( mapSettings.mapUnits() ), true ) );
442  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_description" ), mapSettings.destinationCrs().description(), true ) );
443  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_acronym" ), mapSettings.destinationCrs().projectionAcronym(), true ) );
444  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_ellipsoid" ), mapSettings.destinationCrs().ellipsoidAcronym(), true ) );
445  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_proj4" ), mapSettings.destinationCrs().toProj4(), true ) );
446  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_wkt" ), mapSettings.destinationCrs().toWkt(), true ) );
447 
448  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
449  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
450 
451  QVariantList layersIds;
452  QVariantList layers;
453  const QList<QgsMapLayer *> layersInMap = mapSettings.layers();
454  layersIds.reserve( layersInMap.count() );
455  layers.reserve( layersInMap.count() );
456  for ( QgsMapLayer *layer : layersInMap )
457  {
458  layersIds << layer->id();
459  layers << QVariant::fromValue<QgsWeakMapLayerPointer>( QgsWeakMapLayerPointer( layer ) );
460  }
461 
462  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
463  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
464 
465  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layer_ids" ), layersIds, true ) );
466  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layers" ), layers, true ) );
467 
468  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
469  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
470 
471  scope->addFunction( QStringLiteral( "is_layer_visible" ), new GetLayerVisibility( mapSettings.layers() ) );
472 
473  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
474  // (rationale is described in QgsLayoutItemMap::createExpressionContext() )
475 
476  return scope;
477 }
478 
479 QgsExpressionContextScope *QgsExpressionContextUtils::mapToolCaptureScope( const QList<QgsPointLocator::Match> &matches )
480 {
481  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Map Tool Capture" ) );
482 
483  QVariantList matchList;
484 
485  for ( const QgsPointLocator::Match &match : matches )
486  {
487  QVariantMap matchMap;
488 
489  matchMap.insert( QStringLiteral( "valid" ), match.isValid() );
490  matchMap.insert( QStringLiteral( "layer" ), QVariant::fromValue<QgsWeakMapLayerPointer>( QgsWeakMapLayerPointer( match.layer() ) ) );
491  matchMap.insert( QStringLiteral( "feature_id" ), match.featureId() );
492  matchMap.insert( QStringLiteral( "vertex_index" ), match.vertexIndex() );
493  matchMap.insert( QStringLiteral( "distance" ), match.distance() );
494 
495  matchList.append( matchMap );
496  }
497 
498  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "snapping_results" ), matchList ) );
499 
500  return scope;
501 }
502 
504 {
505  if ( !symbolScope )
506  return nullptr;
507 
508  symbolScope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_SYMBOL_COLOR, symbol ? symbol->color() : QColor(), true ) );
509 
510  double angle = 0.0;
511  const QgsMarkerSymbol *markerSymbol = dynamic_cast< const QgsMarkerSymbol * >( symbol );
512  if ( markerSymbol )
513  {
514  angle = markerSymbol->angle();
515  }
517 
518  return symbolScope;
519 }
520 
522 {
523  std::unique_ptr< QgsExpressionContextScope > scope( new QgsExpressionContextScope( QObject::tr( "Layout" ) ) );
524  if ( !layout )
525  return scope.release();
526 
527  //add variables defined in layout properties
528  const QStringList variableNames = layout->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
529  const QStringList variableValues = layout->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
530 
531  int varIndex = 0;
532 
533  for ( const QString &variableName : variableNames )
534  {
535  if ( varIndex >= variableValues.length() )
536  {
537  break;
538  }
539 
540  QVariant varValue = variableValues.at( varIndex );
541  varIndex++;
542  scope->setVariable( variableName, varValue );
543  }
544 
545  //add known layout context variables
546  if ( const QgsMasterLayoutInterface *l = dynamic_cast< const QgsMasterLayoutInterface * >( layout ) )
547  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_name" ), l->name(), true ) );
548 
549  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_numpages" ), layout->pageCollection()->pageCount(), true ) );
550  if ( layout->pageCollection()->pageCount() > 0 )
551  {
552  // just take first page size
553  QSizeF s = layout->pageCollection()->page( 0 )->sizeWithUnits().toQSizeF();
554  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageheight" ), s.height(), true ) );
555  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pagewidth" ), s.width(), true ) );
556  }
557  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_dpi" ), layout->renderContext().dpi(), true ) );
558 
559  scope->addFunction( QStringLiteral( "item_variables" ), new GetLayoutItemVariables( layout ) );
560 
561  if ( layout->reportContext().layer() )
562  {
563  scope->setFields( layout->reportContext().layer()->fields() );
564  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_layerid" ), layout->reportContext().layer()->id(), true ) );
565  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_layername" ), layout->reportContext().layer()->name(), true ) );
566  }
567 
568  if ( layout->reportContext().feature().isValid() )
569  {
570  QgsFeature atlasFeature = layout->reportContext().feature();
571  scope->setFeature( atlasFeature );
572  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_feature" ), QVariant::fromValue( atlasFeature ), true ) );
573  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featureid" ), atlasFeature.id(), true ) );
574  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_geometry" ), QVariant::fromValue( atlasFeature.geometry() ), true ) );
575  }
576 
577  return scope.release();
578 }
579 
580 void QgsExpressionContextUtils::setLayoutVariable( QgsLayout *layout, const QString &name, const QVariant &value )
581 {
582  if ( !layout )
583  return;
584 
585  //write variable to layout
586  QStringList variableNames = layout->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
587  QStringList variableValues = layout->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
588 
589  variableNames << name;
590  variableValues << value.toString();
591 
592  layout->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
593  layout->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
594 }
595 
596 void QgsExpressionContextUtils::setLayoutVariables( QgsLayout *layout, const QVariantMap &variables )
597 {
598  if ( !layout )
599  return;
600 
601  QStringList variableNames;
602  QStringList variableValues;
603 
604  QVariantMap::const_iterator it = variables.constBegin();
605  for ( ; it != variables.constEnd(); ++it )
606  {
607  variableNames << it.key();
608  variableValues << it.value().toString();
609  }
610 
611  layout->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
612  layout->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
613 }
614 
616 {
617  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Atlas" ) );
618  if ( !atlas )
619  {
620  //add some dummy atlas variables. This is done so that as in certain contexts we want to show
621  //users that these variables are available even if they have no current value
622  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_pagename" ), QString(), true ) );
623  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_feature" ), QVariant::fromValue( QgsFeature() ), true ) );
624  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featureid" ), 0, true ) );
625  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_geometry" ), QVariant::fromValue( QgsGeometry() ), true ) );
626  return scope;
627  }
628 
629  //add known atlas variables
630  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_totalfeatures" ), atlas->count(), true ) );
631  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featurenumber" ), atlas->currentFeatureNumber() + 1, true ) );
632  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_filename" ), atlas->currentFilename(), true ) );
633  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_pagename" ), atlas->nameForPage( atlas->currentFeatureNumber() ), true ) );
634 
635  if ( atlas->enabled() && atlas->coverageLayer() )
636  {
637  scope->setFields( atlas->coverageLayer()->fields() );
638  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_layerid" ), atlas->coverageLayer()->id(), true ) );
639  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_layername" ), atlas->coverageLayer()->name(), true ) );
640  }
641 
642  if ( atlas->enabled() )
643  {
644  QgsFeature atlasFeature = atlas->layout()->reportContext().feature();
645  scope->setFeature( atlasFeature );
646  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_feature" ), QVariant::fromValue( atlasFeature ), true ) );
647  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featureid" ), atlasFeature.id(), true ) );
648  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_geometry" ), QVariant::fromValue( atlasFeature.geometry() ), true ) );
649  }
650 
651  return scope;
652 }
653 
655 {
656  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Layout Item" ) );
657  if ( !item )
658  return scope;
659 
660  //add variables defined in layout item properties
661  const QStringList variableNames = item->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
662  const QStringList variableValues = item->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
663 
664  int varIndex = 0;
665  for ( const QString &variableName : variableNames )
666  {
667  if ( varIndex >= variableValues.length() )
668  {
669  break;
670  }
671 
672  QVariant varValue = variableValues.at( varIndex );
673  varIndex++;
674  scope->setVariable( variableName, varValue );
675  }
676 
677  //add known layout item context variables
678  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "item_id" ), item->id(), true ) );
679  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "item_uuid" ), item->uuid(), true ) );
680  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_page" ), item->page() + 1, true ) );
681 
682  if ( item->layout() )
683  {
684  const QgsLayoutItemPage *page = item->layout()->pageCollection()->page( item->page() );
685  if ( page )
686  {
687  const QSizeF s = page->sizeWithUnits().toQSizeF();
688  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageheight" ), s.height(), true ) );
689  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pagewidth" ), s.width(), true ) );
690  }
691  else
692  {
693  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageheight" ), QVariant(), true ) );
694  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pagewidth" ), QVariant(), true ) );
695  }
696  }
697 
698  return scope;
699 }
700 
701 void QgsExpressionContextUtils::setLayoutItemVariable( QgsLayoutItem *item, const QString &name, const QVariant &value )
702 {
703  if ( !item )
704  return;
705 
706  //write variable to layout item
707  QStringList variableNames = item->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
708  QStringList variableValues = item->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
709 
710  variableNames << name;
711  variableValues << value.toString();
712 
713  item->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
714  item->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
715 }
716 
717 void QgsExpressionContextUtils::setLayoutItemVariables( QgsLayoutItem *item, const QVariantMap &variables )
718 {
719  if ( !item )
720  return;
721 
722  QStringList variableNames;
723  QStringList variableValues;
724 
725  QVariantMap::const_iterator it = variables.constBegin();
726  for ( ; it != variables.constEnd(); ++it )
727  {
728  variableNames << it.key();
729  variableValues << it.value().toString();
730  }
731 
732  item->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
733  item->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
734 }
735 
737 {
739  scope->setFeature( feature );
740  scope->setFields( fields );
741  return QgsExpressionContext() << scope;
742 }
743 
745 {
746  // set aside for future use
747  Q_UNUSED( context )
748 
749  std::unique_ptr< QgsExpressionContextScope > scope( new QgsExpressionContextScope( QObject::tr( "Algorithm" ) ) );
750  scope->addFunction( QStringLiteral( "parameter" ), new GetProcessingParameterValue( parameters ) );
751 
752  if ( !algorithm )
753  return scope.release();
754 
755  //add standard algorithm variables
756  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "algorithm_id" ), algorithm->id(), true ) );
757 
758  return scope.release();
759 }
760 
761 QgsExpressionContextScope *QgsExpressionContextUtils::processingModelAlgorithmScope( const QgsProcessingModelAlgorithm *model, const QVariantMap &, QgsProcessingContext &context )
762 {
763  std::unique_ptr< QgsExpressionContextScope > modelScope( new QgsExpressionContextScope( QObject::tr( "Model" ) ) );
764  QString modelPath;
765  if ( !model->sourceFilePath().isEmpty() )
766  {
767  modelPath = model->sourceFilePath();
768  }
769  else if ( context.project() )
770  {
771  // fallback to project path -- the model may be embedded in a project, OR an unsaved model. In either case the
772  // project path is a logical value to fall back to
773  modelPath = context.project()->projectStorage() ? context.project()->fileName() : context.project()->absoluteFilePath();
774  }
775 
776  const QString modelFolder = !modelPath.isEmpty() ? QFileInfo( modelPath ).path() : QString();
777  modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_path" ), QDir::toNativeSeparators( modelPath ), true ) );
778  modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_folder" ), QDir::toNativeSeparators( modelFolder ), true, true ) );
779  modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_name" ), model->displayName(), true ) );
780  modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_group" ), model->group(), true ) );
781 
782  // custom variables
783  const QVariantMap customVariables = model->variables();
784  for ( auto it = customVariables.constBegin(); it != customVariables.constEnd(); ++it )
785  {
786  modelScope->addVariable( QgsExpressionContextScope::StaticVariable( it.key(), it.value(), true ) );
787  }
788 
789  return modelScope.release();
790 }
791 
793 {
794  std::unique_ptr< QgsExpressionContextScope > scope( new QgsExpressionContextScope() );
795  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "notification_message" ), message, true ) );
796  return scope.release();
797 }
798 
800 {
801  QgsExpression::registerFunction( new GetNamedProjectColor( nullptr ) );
802  QgsExpression::registerFunction( new GetLayoutItemVariables( nullptr ) );
803  QgsExpression::registerFunction( new GetLayerVisibility( QList<QgsMapLayer *>() ) );
804  QgsExpression::registerFunction( new GetProcessingParameterValue( QVariantMap() ) );
805  QgsExpression::registerFunction( new GetCurrentFormFieldValue( ) );
806 }
807 
809 {
810  Q_UNUSED( node )
811  return mUsesGeometry;
812 }
813 
815 {
816  Q_UNUSED( node )
817  return mReferencedColumns;
818 }
819 
821 {
822  return allParamsStatic( node, parent, context );
823 }
824 
825 //
826 // GetLayerVisibility
827 //
828 
829 QgsExpressionContextUtils::GetLayerVisibility::GetLayerVisibility( const QList<QgsMapLayer *> &layers )
830  : QgsScopedExpressionFunction( QStringLiteral( "is_layer_visible" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "id" ) ), QStringLiteral( "General" ) )
831  , mLayers( _qgis_listRawToQPointer( layers ) )
832 {}
833 
834 QVariant QgsExpressionContextUtils::GetLayerVisibility::func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
835 {
836  if ( mLayers.isEmpty() )
837  {
838  return false;
839  }
840 
841  QgsMapLayer *layer = QgsExpressionUtils::getMapLayer( values.at( 0 ), parent );
842  if ( layer )
843  {
844  return mLayers.contains( layer );
845  }
846  else
847  {
848  return false;
849  }
850 }
851 
852 QgsScopedExpressionFunction *QgsExpressionContextUtils::GetLayerVisibility::clone() const
853 {
854  return new GetLayerVisibility( _qgis_listQPointerToRaw( mLayers ) );
855 }
bool isValid() const
Returns the validity of this feature.
Definition: qgsfeature.cpp:183
Class for parsing and evaluation of expressions (formerly called "search strings").
static QString locale()
Returns the QGIS locale.
QgsFeatureId id
Definition: qgsfeature.h:64
static QgsExpressionContextScope * updateSymbolScope(const QgsSymbol *symbol, QgsExpressionContextScope *symbolScope=nullptr)
Updates a symbol scope related to a QgsSymbol to an expression context.
static QgsExpressionContextScope * processingAlgorithmScope(const QgsProcessingAlgorithm *algorithm, const QVariantMap &parameters, QgsProcessingContext &context)
Creates a new scope which contains variables and functions relating to a processing algorithm...
static void setLayoutItemVariable(QgsLayoutItem *item, const QString &name, const QVariant &value)
Sets a layout item context variable, with the given name and value.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Single variable definition for use within a QgsExpressionContextScope.
static void setGlobalVariable(const QString &name, const QVariant &value)
Sets a global context variable.
Base class for all map layer types.
Definition: qgsmaplayer.h:78
static void setLayoutItemVariables(QgsLayoutItem *item, const QVariantMap &variables)
Sets all layout item context variables for an item.
static const QString QGIS_VERSION
Version string.
Definition: qgis.h:51
static void setLayerVariables(QgsMapLayer *layer, const QVariantMap &variables)
Sets all layer context variables.
QString id() const
Returns the unique ID for the algorithm, which is a combination of the algorithm provider&#39;s ID and th...
QgsAbstractMetadataBase::KeywordMap keywords() const
Returns the keywords map, which is a set of descriptive keywords associated with the resource...
Base class for graphical items within a QgsLayout.
static Q_INVOKABLE QString toString(QgsUnitTypes::DistanceUnit unit)
Returns a translated string representing a distance unit.
static void setLayoutVariable(QgsLayout *layout, const QString &name, const QVariant &value)
Sets a layout context variable.
static void setCustomVariables(const QVariantMap &customVariables)
Custom expression variables for this application.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:61
double rotation() const
Returns the rotation of the resulting map image, in degrees clockwise.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from the layout.
Definition: qgslayout.cpp:413
void addFunction(const QString &name, QgsScopedExpressionFunction *function)
Adds a function to the scope.
static void removeProjectVariable(QgsProject *project, const QString &name)
Remove project context variable.
static QgsExpressionContext createFeatureBasedContext(const QgsFeature &feature, const QgsFields &fields)
Helper function for creating an expression context which contains just a feature and fields collectio...
QgsLayoutSize sizeWithUnits() const
Returns the item&#39;s current size, including units.
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for layer.
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the object.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from the object.
QString toProj4() const
Returns a Proj4 string representation of this CRS.
QString author() const
Returns the project author string.
Container of fields for a vector layer.
Definition: qgsfields.h:42
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
static QVariantMap customVariables()
Custom expression variables for this application.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the scope.
static QgsExpressionContextScope * mapToolCaptureScope(const QList< QgsPointLocator::Match > &matches)
Sets the expression context variables which are available for expressions triggered by a map tool cap...
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
QList< QgsMapLayer * > layers() const
Gets list of layers for map rendering The layers are stored in the reverse order of how they are rend...
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
QString ellipsoid
Definition: qgsproject.h:97
static QgsExpressionContextScope * formScope(const QgsFeature &formFeature=QgsFeature(), const QString &formMode=QString())
Creates a new scope which contains functions and variables from the current attribute form/table feat...
bool usesGeometry(const QgsExpressionNodeFunction *node) const override
Does this function use a geometry object.
static void setLayoutVariables(QgsLayout *layout, const QVariantMap &variables)
Sets all layout context variables.
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:1576
static const int QGIS_VERSION_INT
Version number used for comparing versions using the "Check QGIS Version" function.
Definition: qgis.h:53
Abstract base class for processing algorithms.
QList< QgsExpressionFunction::Parameter > ParameterList
List of parameters, used for function definition.
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout&#39;s render context, which stores information relating to the current ...
Definition: qgslayout.cpp:357
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes takes output image size into accou...
QVariant variable(const QString &name) const
Fetches a matching variable from the context.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
A marker symbol type, for rendering Point and MultiPoint geometries.
Definition: qgssymbol.h:766
static QString userFullName()
Returns the user&#39;s operating system login account full display name.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
QgsCoordinateReferenceSystem destinationCrs() const
returns CRS of destination coordinate reference system
QgsUnitTypes::DistanceUnit mapUnits() const
Gets units of map&#39;s geographical coordinates - used for scale calculation.
QgsLayout * layout() override
Returns the layout associated with the iterator.
The QgsMapSettings class contains configuration for rendering of the map.
void setVariable(const QString &name, const QVariant &value, bool isStatic=false)
Convenience method for setting a variable in the context scope by name name and value.
QString homePath
Definition: qgsproject.h:94
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QSizeF toQSizeF() const
Converts the layout size to a QSizeF.
static QgsExpressionContextScope * layoutItemScope(const QgsLayoutItem *item)
Creates a new scope which contains variables and functions relating to a QgsLayoutItem.
bool isStatic(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context) const override
Will be called during prepare to determine if the function is static.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
QgsVectorLayer * layer() const
Returns the vector layer associated with the layout&#39;s context.
static const QString EXPR_SYMBOL_ANGLE
Inbuilt variable name for symbol angle variable.
QString ellipsoidAcronym() const
Returns the ellipsoid acronym for the ellipsoid used by the CRS.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsProject.
const QgsLayout * layout() const
Returns the layout the object is attached to.
QgsLayoutItemPage * page(int pageNumber)
Returns a specific page (by pageNumber) from the collection.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QgsUnitTypes::AreaUnit areaUnits() const
Convenience function to query default area measurement units for project.
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout&#39;s page collection, which stores and manages page items in the layout...
Definition: qgslayout.cpp:457
static void setProjectVariables(QgsProject *project, const QVariantMap &variables)
Sets all project context variables.
QgsProjectMetadata metadata
Definition: qgsproject.h:102
double scale() const
Returns the calculated map scale.
double dpi() const
Returns the dpi for outputting the layout.
double width() const
Returns the width of the rectangle.
Definition: qgsrectangle.h:202
static QgsExpressionContextScope * notificationScope(const QString &message=QString())
Creates a new scope which contains variables and functions relating to provider notifications.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static void removeGlobalVariable(const QString &name)
Remove a global context variable.
QgsCoordinateReferenceSystem crs
Definition: qgsproject.h:95
QVariantMap variablesToMap() const
Returns a map of variable name to value representing all the expression variables contained by the co...
Class used to render QgsLayout as an atlas, by iterating over the features from an associated vector ...
static const QString EXPR_SYMBOL_COLOR
Inbuilt variable name for symbol color variable.
QDateTime creationDateTime() const
Returns the project&#39;s creation date/timestamp.
QString description() const
Returns the descriptive name of the CRS, e.g., "WGS 84" or "GDA 94 / Vicgrid94".
static bool registerFunction(QgsExpressionFunction *function, bool transferOwnership=false)
Registers a function to the expression engine.
QStringList readListEntry(const QString &scope, const QString &key, const QStringList &def=QStringList(), bool *ok=nullptr) const
Key value accessors.
Reads and writes project states.
Definition: qgsproject.h:89
static void setCustomVariable(const QString &name, const QVariant &value)
Set a single custom expression variable.
QColor color() const
Returns the symbol&#39;s color.
Definition: qgssymbol.cpp:480
QString currentFilename() const
Returns the current feature filename.
int page() const
Returns the page the item is currently on, with the first page returning 0.
QString id() const
Returns the item&#39;s ID name.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer&#39;s project and layer.
int count() override
Returns the number of features to iterate over.
Single scope for storing variables and functions for use within a QgsExpressionContext.
An expression node for expression functions.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the scope.
void setCustomVariables(const QVariantMap &customVariables)
A map of custom project variables.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
QgsProjectStorage * projectStorage() const
Returns pointer to project storage implementation that handles read/write of the project file...
Definition: qgsproject.cpp:589
QString abstract() const
Returns a free-form description of the resource.
static void setLayerVariable(QgsMapLayer *layer, const QString &name, const QVariant &value)
Sets a layer context variable.
static QString userLoginName()
Returns the user&#39;s operating system login account name.
static QString osName()
Returns a string name of the operating system QGIS is running on.
int pageCount() const
Returns the number of pages in the collection.
int currentFeatureNumber() const
Returns the current feature number, where a value of 0 corresponds to the first feature.
QString projectionAcronym() const
Returns the projection acronym for the projection used by the CRS.
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:96
QgsLayoutReportContext & reportContext()
Returns a reference to the layout&#39;s report context, which stores information relating to the current ...
Definition: qgslayout.cpp:367
static void setProjectVariable(QgsProject *project, const QString &name, const QVariant &value)
Sets a project context variable.
static QgsExpressionContextScope * processingModelAlgorithmScope(const QgsProcessingModelAlgorithm *model, const QVariantMap &parameters, QgsProcessingContext &context)
Creates a new scope which contains variables and functions relating to a processing model algorithm...
static void registerContextFunctions()
Registers all known core functions provided by QgsExpressionContextScope objects. ...
static QgsExpressionContextScope * atlasScope(QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object...
static const QString QGIS_RELEASE_NAME
Release name.
Definition: qgis.h:55
static QgsExpressionContextScope * layoutScope(const QgsLayout *layout)
Creates a new scope which contains variables and functions relating to a QgsLayout layout...
QMap< QString, QStringList > KeywordMap
Map of vocabulary string to keyword list.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:438
virtual QString uuid() const
Returns the item identification string.
This class represents a coordinate reference system (CRS).
QString toWkt() const
Returns a WKT representation of this CRS.
QString nameForPage(int page) const
Returns the calculated name for a specified atlas page number.
static QString platform()
Returns the QGIS platform name, e.g., "desktop" or "server".
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the layout.
Definition: qgslayout.cpp:405
bool enabled() const
Returns whether the atlas generation is enabled.
QgsVectorLayer * coverageLayer() const
Returns the coverage layer used for the atlas features.
QgsFeature feature() const
Returns the current feature for evaluating the layout.
QString name
Definition: qgsmaplayer.h:82
Represents a single parameter passed to a function.
QgsGeometry geometry
Definition: qgsfeature.h:67
QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
QString title() const
Returns the project&#39;s title.
Definition: qgsproject.cpp:458
QgsPointXY center() const
Returns the center point of the rectangle.
Definition: qgsrectangle.h:230
QgsUnitTypes::DistanceUnit distanceUnits() const
Convenience function to query default distance measurement units for project.
QVariantMap customVariables() const
A map of custom project variables.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
QString baseName() const
Returns the base name of the project file without the path and without extension - derived from fileN...
Definition: qgsproject.cpp:630
Interface for master layout type objects, such as print layouts and reports.
QString absoluteFilePath() const
Returns full absolute path to the project file if the project is stored in a file system - derived fr...
Definition: qgsproject.cpp:619
Represents a vector layer which manages a vector based data sets.
Contains information about the context in which a processing algorithm is executed.
QString fileName
Definition: qgsproject.h:93
static void setGlobalVariables(const QVariantMap &variables)
Sets all global context variables.
Expression function for use within a QgsExpressionContextScope.
double angle() const
Returns the marker angle for the whole symbol.
Definition: qgssymbol.cpp:1217
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
QString authid() const
Returns the authority identifier for the CRS.
QString identifier() const
A reference, URI, URL or some other mechanism to identify the resource.
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:85
QSet< QString > referencedColumns(const QgsExpressionNodeFunction *node) const override
Returns a set of field names which are required for this function.
static QColor decodeColor(const QString &str)
double height() const
Returns the height of the rectangle.
Definition: qgsrectangle.h:209
Item representing the paper in a layout.