QGIS API Documentation  3.6.0-Noosa (5873452)
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  QStringList variableNames = layer->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
325  QStringList variableValues = layer->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
326 
327  int varIndex = 0;
328  Q_FOREACH ( 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  QStringList variableNames = layout->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
529  QStringList variableValues = layout->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
530 
531  int varIndex = 0;
532  Q_FOREACH ( const QString &variableName, variableNames )
533  {
534  if ( varIndex >= variableValues.length() )
535  {
536  break;
537  }
538 
539  QVariant varValue = variableValues.at( varIndex );
540  varIndex++;
541  scope->setVariable( variableName, varValue );
542  }
543 
544  //add known layout context variables
545  if ( const QgsMasterLayoutInterface *l = dynamic_cast< const QgsMasterLayoutInterface * >( layout ) )
546  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_name" ), l->name(), true ) );
547 
548  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_numpages" ), layout->pageCollection()->pageCount(), true ) );
549  if ( layout->pageCollection()->pageCount() > 0 )
550  {
551  // just take first page size
552  QSizeF s = layout->pageCollection()->page( 0 )->sizeWithUnits().toQSizeF();
553  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageheight" ), s.height(), true ) );
554  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pagewidth" ), s.width(), true ) );
555  }
556  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_dpi" ), layout->renderContext().dpi(), true ) );
557 
558  scope->addFunction( QStringLiteral( "item_variables" ), new GetLayoutItemVariables( layout ) );
559 
560  if ( layout->reportContext().layer() )
561  {
562  scope->setFields( layout->reportContext().layer()->fields() );
563  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_layerid" ), layout->reportContext().layer()->id(), true ) );
564  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_layername" ), layout->reportContext().layer()->name(), true ) );
565  }
566 
567  if ( layout->reportContext().feature().isValid() )
568  {
569  QgsFeature atlasFeature = layout->reportContext().feature();
570  scope->setFeature( atlasFeature );
571  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_feature" ), QVariant::fromValue( atlasFeature ), true ) );
572  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featureid" ), atlasFeature.id(), true ) );
573  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_geometry" ), QVariant::fromValue( atlasFeature.geometry() ), true ) );
574  }
575 
576  return scope.release();
577 }
578 
579 void QgsExpressionContextUtils::setLayoutVariable( QgsLayout *layout, const QString &name, const QVariant &value )
580 {
581  if ( !layout )
582  return;
583 
584  //write variable to layout
585  QStringList variableNames = layout->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
586  QStringList variableValues = layout->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
587 
588  variableNames << name;
589  variableValues << value.toString();
590 
591  layout->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
592  layout->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
593 }
594 
595 void QgsExpressionContextUtils::setLayoutVariables( QgsLayout *layout, const QVariantMap &variables )
596 {
597  if ( !layout )
598  return;
599 
600  QStringList variableNames;
601  QStringList variableValues;
602 
603  QVariantMap::const_iterator it = variables.constBegin();
604  for ( ; it != variables.constEnd(); ++it )
605  {
606  variableNames << it.key();
607  variableValues << it.value().toString();
608  }
609 
610  layout->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
611  layout->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
612 }
613 
615 {
616  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Atlas" ) );
617  if ( !atlas )
618  {
619  //add some dummy atlas variables. This is done so that as in certain contexts we want to show
620  //users that these variables are available even if they have no current value
621  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_pagename" ), QString(), true ) );
622  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_feature" ), QVariant::fromValue( QgsFeature() ), true ) );
623  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featureid" ), 0, true ) );
624  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_geometry" ), QVariant::fromValue( QgsGeometry() ), true ) );
625  return scope;
626  }
627 
628  //add known atlas variables
629  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_totalfeatures" ), atlas->count(), true ) );
630  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featurenumber" ), atlas->currentFeatureNumber() + 1, true ) );
631  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_filename" ), atlas->currentFilename(), true ) );
632  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_pagename" ), atlas->nameForPage( atlas->currentFeatureNumber() ), true ) );
633 
634  if ( atlas->enabled() && atlas->coverageLayer() )
635  {
636  scope->setFields( atlas->coverageLayer()->fields() );
637  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_layerid" ), atlas->coverageLayer()->id(), true ) );
638  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_layername" ), atlas->coverageLayer()->name(), true ) );
639  }
640 
641  if ( atlas->enabled() )
642  {
643  QgsFeature atlasFeature = atlas->layout()->reportContext().feature();
644  scope->setFeature( atlasFeature );
645  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_feature" ), QVariant::fromValue( atlasFeature ), true ) );
646  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_featureid" ), atlasFeature.id(), true ) );
647  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_geometry" ), QVariant::fromValue( atlasFeature.geometry() ), true ) );
648  }
649 
650  return scope;
651 }
652 
654 {
655  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Layout Item" ) );
656  if ( !item )
657  return scope;
658 
659  //add variables defined in layout item properties
660  const QStringList variableNames = item->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
661  const QStringList variableValues = item->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
662 
663  int varIndex = 0;
664  for ( const QString &variableName : variableNames )
665  {
666  if ( varIndex >= variableValues.length() )
667  {
668  break;
669  }
670 
671  QVariant varValue = variableValues.at( varIndex );
672  varIndex++;
673  scope->setVariable( variableName, varValue );
674  }
675 
676  //add known layout item context variables
677  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "item_id" ), item->id(), true ) );
678  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "item_uuid" ), item->uuid(), true ) );
679  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_page" ), item->page() + 1, true ) );
680 
681  if ( item->layout() )
682  {
683  const QgsLayoutItemPage *page = item->layout()->pageCollection()->page( item->page() );
684  if ( page )
685  {
686  const QSizeF s = page->sizeWithUnits().toQSizeF();
687  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageheight" ), s.height(), true ) );
688  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pagewidth" ), s.width(), true ) );
689  }
690  else
691  {
692  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pageheight" ), QVariant(), true ) );
693  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layout_pagewidth" ), QVariant(), true ) );
694  }
695  }
696 
697  return scope;
698 }
699 
700 void QgsExpressionContextUtils::setLayoutItemVariable( QgsLayoutItem *item, const QString &name, const QVariant &value )
701 {
702  if ( !item )
703  return;
704 
705  //write variable to layout item
706  QStringList variableNames = item->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
707  QStringList variableValues = item->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
708 
709  variableNames << name;
710  variableValues << value.toString();
711 
712  item->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
713  item->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
714 }
715 
716 void QgsExpressionContextUtils::setLayoutItemVariables( QgsLayoutItem *item, const QVariantMap &variables )
717 {
718  if ( !item )
719  return;
720 
721  QStringList variableNames;
722  QStringList variableValues;
723 
724  QVariantMap::const_iterator it = variables.constBegin();
725  for ( ; it != variables.constEnd(); ++it )
726  {
727  variableNames << it.key();
728  variableValues << it.value().toString();
729  }
730 
731  item->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
732  item->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
733 }
734 
736 {
738  scope->setFeature( feature );
739  scope->setFields( fields );
740  return QgsExpressionContext() << scope;
741 }
742 
744 {
745  // set aside for future use
746  Q_UNUSED( context );
747 
748  std::unique_ptr< QgsExpressionContextScope > scope( new QgsExpressionContextScope( QObject::tr( "Algorithm" ) ) );
749  scope->addFunction( QStringLiteral( "parameter" ), new GetProcessingParameterValue( parameters ) );
750 
751  if ( !algorithm )
752  return scope.release();
753 
754  //add standard algorithm variables
755  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "algorithm_id" ), algorithm->id(), true ) );
756 
757  return scope.release();
758 }
759 
760 QgsExpressionContextScope *QgsExpressionContextUtils::processingModelAlgorithmScope( const QgsProcessingModelAlgorithm *model, const QVariantMap &, QgsProcessingContext &context )
761 {
762  std::unique_ptr< QgsExpressionContextScope > modelScope( new QgsExpressionContextScope( QObject::tr( "Model" ) ) );
763  QString modelPath;
764  if ( !model->sourceFilePath().isEmpty() )
765  {
766  modelPath = model->sourceFilePath();
767  }
768  else if ( context.project() )
769  {
770  // fallback to project path -- the model may be embedded in a project, OR an unsaved model. In either case the
771  // project path is a logical value to fall back to
772  modelPath = context.project()->projectStorage() ? context.project()->fileName() : context.project()->absoluteFilePath();
773  }
774 
775  const QString modelFolder = !modelPath.isEmpty() ? QFileInfo( modelPath ).path() : QString();
776  modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_path" ), QDir::toNativeSeparators( modelPath ), true ) );
777  modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_folder" ), QDir::toNativeSeparators( modelFolder ), true, true ) );
778  modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_name" ), model->displayName(), true ) );
779  modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_group" ), model->group(), true ) );
780  return modelScope.release();
781 }
782 
784 {
785  std::unique_ptr< QgsExpressionContextScope > scope( new QgsExpressionContextScope() );
786  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "notification_message" ), message, true ) );
787  return scope.release();
788 }
789 
791 {
792  QgsExpression::registerFunction( new GetNamedProjectColor( nullptr ) );
793  QgsExpression::registerFunction( new GetLayoutItemVariables( nullptr ) );
794  QgsExpression::registerFunction( new GetLayerVisibility( QList<QgsMapLayer *>() ) );
795  QgsExpression::registerFunction( new GetProcessingParameterValue( QVariantMap() ) );
796  QgsExpression::registerFunction( new GetCurrentFormFieldValue( ) );
797 }
798 
800 {
801  Q_UNUSED( node )
802  return mUsesGeometry;
803 }
804 
806 {
807  Q_UNUSED( node )
808  return mReferencedColumns;
809 }
810 
812 {
813  return allParamsStatic( node, parent, context );
814 }
815 
816 //
817 // GetLayerVisibility
818 //
819 
820 QgsExpressionContextUtils::GetLayerVisibility::GetLayerVisibility( const QList<QgsMapLayer *> &layers )
821  : QgsScopedExpressionFunction( QStringLiteral( "is_layer_visible" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "id" ) ), QStringLiteral( "General" ) )
822  , mLayers( _qgis_listRawToQPointer( layers ) )
823 {}
824 
825 QVariant QgsExpressionContextUtils::GetLayerVisibility::func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
826 {
827  if ( mLayers.isEmpty() )
828  {
829  return false;
830  }
831 
832  QgsMapLayer *layer = QgsExpressionUtils::getMapLayer( values.at( 0 ), parent );
833  if ( layer )
834  {
835  return mLayers.contains( layer );
836  }
837  else
838  {
839  return false;
840  }
841 }
842 
843 QgsScopedExpressionFunction *QgsExpressionContextUtils::GetLayerVisibility::clone() const
844 {
845  return new GetLayerVisibility( _qgis_listQPointerToRaw( mLayers ) );
846 }
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:64
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:106
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:1548
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:732
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:461
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:581
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:430
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:68
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:450
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:622
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:611
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:1180
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:71
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.