QGIS API Documentation  2.14.0-Essen
qgsexpressioncontext.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsexpressioncontext.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 
16 #include "qgsexpressioncontext.h"
17 
18 #include "qgslogger.h"
19 #include "qgsexpression.h"
20 #include "qgsfield.h"
21 #include "qgsvectorlayer.h"
22 #include "qgsproject.h"
23 #include "qgssymbollayerv2utils.h"
24 #include "qgsgeometry.h"
25 #include "qgscomposition.h"
26 #include "qgscomposeritem.h"
27 #include "qgsatlascomposition.h"
28 #include "qgsapplication.h"
29 #include <QSettings>
30 #include <QDir>
31 
32 
33 const QString QgsExpressionContext::EXPR_FIELDS( "_fields_" );
34 const QString QgsExpressionContext::EXPR_FEATURE( "_feature_" );
36 const QString QgsExpressionContext::EXPR_SYMBOL_COLOR( "symbol_color" );
37 const QString QgsExpressionContext::EXPR_SYMBOL_ANGLE( "symbol_angle" );
38 const QString QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT( "geometry_part_count" );
39 const QString QgsExpressionContext::EXPR_GEOMETRY_PART_NUM( "geometry_part_num" );
40 
41 //
42 // QgsExpressionContextScope
43 //
44 
46  : mName( name )
47 {
48 
49 }
50 
52  : mName( other.mName )
53  , mVariables( other.mVariables )
54 {
56  for ( ; it != other.mFunctions.constEnd(); ++it )
57  {
58  mFunctions.insert( it.key(), it.value()->clone() );
59  }
60 }
61 
63 {
64  mName = other.mName;
65  mVariables = other.mVariables;
66 
67  qDeleteAll( mFunctions );
68  mFunctions.clear();
70  for ( ; it != other.mFunctions.constEnd(); ++it )
71  {
72  mFunctions.insert( it.key(), it.value()->clone() );
73  }
74 
75  return *this;
76 }
77 
79 {
80  qDeleteAll( mFunctions );
81 }
82 
84 {
85  if ( mVariables.contains( name ) )
86  {
87  StaticVariable existing = mVariables.value( name );
88  existing.value = value;
89  addVariable( existing );
90  }
91  else
92  {
94  }
95 }
96 
98 {
99  mVariables.insert( variable.name, variable );
100 }
101 
103 {
104  return mVariables.remove( name ) > 0;
105 }
106 
108 {
109  return mVariables.contains( name );
110 }
111 
113 {
114  return hasVariable( name ) ? mVariables.value( name ).value : QVariant();
115 }
116 
118 {
119  QStringList names = mVariables.keys();
120  return names;
121 }
122 
123 bool QgsExpressionContextScope::variableNameSort( const QString& a, const QString& b )
124 {
125  return QString::localeAwareCompare( a, b ) < 0;
126 }
127 
129 class QgsExpressionContextVariableCompare
130 {
131  public:
132  explicit QgsExpressionContextVariableCompare( const QgsExpressionContextScope& scope )
133  : mScope( scope )
134  { }
135 
136  bool operator()( const QString& a, const QString& b ) const
137  {
138  bool aReadOnly = mScope.isReadOnly( a );
139  bool bReadOnly = mScope.isReadOnly( b );
140  if ( aReadOnly != bReadOnly )
141  return aReadOnly;
142  return QString::localeAwareCompare( a, b ) < 0;
143  }
144 
145  private:
146  const QgsExpressionContextScope& mScope;
147 };
149 
151 {
152  QStringList allVariables = mVariables.keys();
153  QStringList filtered;
154  Q_FOREACH ( const QString& variable, allVariables )
155  {
156  if ( variable.startsWith( '_' ) )
157  continue;
158 
159  filtered << variable;
160  }
161  QgsExpressionContextVariableCompare cmp( *this );
162  qSort( filtered.begin(), filtered.end(), cmp );
163 
164  return filtered;
165 }
166 
168 {
169  return hasVariable( name ) ? mVariables.value( name ).readOnly : false;
170 }
171 
173 {
174  return mFunctions.contains( name );
175 }
176 
178 {
179  return mFunctions.contains( name ) ? mFunctions.value( name ) : nullptr;
180 }
181 
183 {
184  return mFunctions.keys();
185 }
186 
188 {
189  mFunctions.insert( name, function );
190 }
191 
193 {
195 }
196 
198 {
200 }
201 
202 
203 //
204 // QgsExpressionContext
205 //
206 
208 {
209  Q_FOREACH ( const QgsExpressionContextScope* scope, other.mStack )
210  {
211  mStack << new QgsExpressionContextScope( *scope );
212  }
213  mHighlightedVariables = other.mHighlightedVariables;
214 }
215 
217 {
218  qDeleteAll( mStack );
219  mStack.clear();
220  Q_FOREACH ( const QgsExpressionContextScope* scope, other.mStack )
221  {
222  mStack << new QgsExpressionContextScope( *scope );
223  }
224  mHighlightedVariables = other.mHighlightedVariables;
225  return *this;
226 }
227 
229 {
230  qDeleteAll( mStack );
231  mStack.clear();
232 }
233 
235 {
236  Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
237  {
238  if ( scope->hasVariable( name ) )
239  return true;
240  }
241  return false;
242 }
243 
245 {
246  const QgsExpressionContextScope* scope = activeScopeForVariable( name );
247  return scope ? scope->variable( name ) : QVariant();
248 }
249 
251 {
252  return mHighlightedVariables.contains( name );
253 }
254 
256 {
257  mHighlightedVariables = variableNames;
258 }
259 
261 {
262  //iterate through stack backwards, so that higher priority variables take precedence
264  while ( it != mStack.constBegin() )
265  {
266  --it;
267  if (( *it )->hasVariable( name ) )
268  return ( *it );
269  }
270  return nullptr;
271 }
272 
274 {
275  //iterate through stack backwards, so that higher priority variables take precedence
277  while ( it != mStack.constBegin() )
278  {
279  --it;
280  if (( *it )->hasVariable( name ) )
281  return ( *it );
282  }
283  return nullptr;
284 }
285 
287 {
288  if ( index < 0 || index >= mStack.count() )
289  return nullptr;
290 
291  return mStack.at( index );
292 }
293 
295 {
296  if ( mStack.count() < 1 )
297  return nullptr;
298 
299  return mStack.last();
300 }
301 
303 {
304  if ( !scope )
305  return -1;
306 
307  return mStack.indexOf( scope );
308 }
309 
311 {
312  QStringList names;
313  Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
314  {
315  names << scope->variableNames();
316  }
317  return names.toSet().toList();
318 }
319 
321 {
322  QStringList allVariables = variableNames();
323  QStringList filtered;
324  Q_FOREACH ( const QString& variable, allVariables )
325  {
326  if ( variable.startsWith( '_' ) )
327  continue;
328 
329  filtered << variable;
330  }
331 
332  filtered.sort();
333  return filtered;
334 }
335 
337 {
338  Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
339  {
340  if ( scope->isReadOnly( name ) )
341  return true;
342  }
343  return false;
344 }
345 
347 {
348  Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
349  {
350  if ( scope->hasFunction( name ) )
351  return true;
352  }
353  return false;
354 }
355 
357 {
358  QStringList result;
359  Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
360  {
361  result << scope->functionNames();
362  }
363  result = result.toSet().toList();
364  result.sort();
365  return result;
366 }
367 
369 {
370  //iterate through stack backwards, so that higher priority variables take precedence
372  while ( it != mStack.constBegin() )
373  {
374  --it;
375  if (( *it )->hasFunction( name ) )
376  return ( *it )->function( name );
377  }
378  return nullptr;
379 }
380 
382 {
383  return mStack.count();
384 }
385 
387 {
388  mStack.append( scope );
389 }
390 
392 {
393  if ( !mStack.isEmpty() )
394  return mStack.takeLast();
395 
396  return nullptr;
397 }
398 
400 {
401  mStack.append( scope );
402  return *this;
403 }
404 
406 {
407  if ( mStack.isEmpty() )
408  mStack.append( new QgsExpressionContextScope() );
409 
410  mStack.last()->setFeature( feature );
411 }
412 
414 {
415  return qvariant_cast<QgsFeature>( variable( QgsExpressionContext::EXPR_FEATURE ) );
416 }
417 
419 {
420  if ( mStack.isEmpty() )
421  mStack.append( new QgsExpressionContextScope() );
422 
423  mStack.last()->setFields( fields );
424 }
425 
427 {
428  return qvariant_cast<QgsFields>( variable( QgsExpressionContext::EXPR_FIELDS ) );
429 }
430 
432 {
433  if ( mStack.isEmpty() )
434  mStack.append( new QgsExpressionContextScope() );
435 
437  value, true ) );
438 }
439 
440 
441 //
442 // QgsExpressionContextUtils
443 //
444 
446 {
448 
449  //read values from QSettings
450  QSettings settings;
451 
452  //check if settings contains any variables
453  if ( settings.contains( QString( "/variables/values" ) ) )
454  {
455  QList< QVariant > customVariableVariants = settings.value( QString( "/variables/values" ) ).toList();
456  QList< QVariant > customVariableNames = settings.value( QString( "/variables/names" ) ).toList();
457  int variableIndex = 0;
458  for ( QList< QVariant >::const_iterator it = customVariableVariants.constBegin();
459  it != customVariableVariants.constEnd(); ++it )
460  {
461  if ( variableIndex >= customVariableNames.length() )
462  {
463  break;
464  }
465 
466  QVariant value = ( *it );
467  QString name = customVariableNames.at( variableIndex ).toString();
468 
469  scope->setVariable( name, value );
470  variableIndex++;
471  }
472  }
473 
474  //add some extra global variables
477  scope->addVariable( QgsExpressionContextScope::StaticVariable( "qgis_release_name", QGis::QGIS_RELEASE_NAME, true ) );
482 
483  return scope;
484 }
485 
487 {
488  // save variable to settings
489  QSettings settings;
490 
491  QList< QVariant > customVariableVariants = settings.value( QString( "/variables/values" ) ).toList();
492  QList< QVariant > customVariableNames = settings.value( QString( "/variables/names" ) ).toList();
493 
494  customVariableVariants << value;
495  customVariableNames << name;
496 
497  settings.setValue( QString( "/variables/names" ), customVariableNames );
498  settings.setValue( QString( "/variables/values" ), customVariableVariants );
499 }
500 
502 {
503  QSettings settings;
504 
505  QList< QVariant > customVariableVariants;
506  QList< QVariant > customVariableNames;
507 
509  for ( ; it != variables.constEnd(); ++it )
510  {
511  customVariableNames << it.key();
512  customVariableVariants << it.value();
513  }
514 
515  settings.setValue( QString( "/variables/names" ), customVariableNames );
516  settings.setValue( QString( "/variables/values" ), customVariableVariants );
517 }
518 
520 
521 class GetNamedProjectColor : public QgsScopedExpressionFunction
522 {
523  public:
524  GetNamedProjectColor()
525  : QgsScopedExpressionFunction( "project_color", 1, "Color" )
526  {
527  //build up color list from project. Do this in advance for speed
528  QStringList colorStrings = QgsProject::instance()->readListEntry( "Palette", "/Colors" );
529  QStringList colorLabels = QgsProject::instance()->readListEntry( "Palette", "/Labels" );
530 
531  //generate list from custom colors
532  int colorIndex = 0;
533  for ( QStringList::iterator it = colorStrings.begin();
534  it != colorStrings.end(); ++it )
535  {
537  QString label;
538  if ( colorLabels.length() > colorIndex )
539  {
540  label = colorLabels.at( colorIndex );
541  }
542 
543  mColors.insert( label.toLower(), color );
544  colorIndex++;
545  }
546  }
547 
548  virtual QVariant func( const QVariantList& values, const QgsExpressionContext*, QgsExpression* ) override
549  {
550  QString colorName = values.at( 0 ).toString().toLower();
551  if ( mColors.contains( colorName ) )
552  {
553  return QString( "%1,%2,%3" ).arg( mColors.value( colorName ).red() ).arg( mColors.value( colorName ).green() ).arg( mColors.value( colorName ).blue() );
554  }
555  else
556  return QVariant();
557  }
558 
559  QgsScopedExpressionFunction* clone() const override
560  {
561  return new GetNamedProjectColor();
562  }
563 
564  private:
565 
566  QHash< QString, QColor > mColors;
567 
568 };
569 
571 
573 {
574  QgsProject* project = QgsProject::instance();
575 
577 
578  //add variables defined in project file
579  QStringList variableNames = project->readListEntry( "Variables", "/variableNames" );
580  QStringList variableValues = project->readListEntry( "Variables", "/variableValues" );
581 
582  int varIndex = 0;
583  Q_FOREACH ( const QString& variableName, variableNames )
584  {
585  if ( varIndex >= variableValues.length() )
586  {
587  break;
588  }
589 
590  QString varValueString = variableValues.at( varIndex );
591  varIndex++;
592  scope->setVariable( variableName, varValueString );
593  }
594 
595  //add other known project variables
596  scope->addVariable( QgsExpressionContextScope::StaticVariable( "project_title", project->title(), true ) );
597  scope->addVariable( QgsExpressionContextScope::StaticVariable( "project_path", project->fileInfo().filePath(), true ) );
598  scope->addVariable( QgsExpressionContextScope::StaticVariable( "project_folder", project->fileInfo().dir().path(), true ) );
599  scope->addVariable( QgsExpressionContextScope::StaticVariable( "project_filename", project->fileInfo().fileName(), true ) );
600 
601  scope->addFunction( "project_color", new GetNamedProjectColor() );
602  return scope;
603 }
604 
606 {
607  QgsProject* project = QgsProject::instance();
608 
609  //write variable to project
610  QStringList variableNames = project->readListEntry( "Variables", "/variableNames" );
611  QStringList variableValues = project->readListEntry( "Variables", "/variableValues" );
612 
613  variableNames << name;
614  variableValues << value.toString();
615 
616  project->writeEntry( "Variables", "/variableNames", variableNames );
617  project->writeEntry( "Variables", "/variableValues", variableValues );
618 }
619 
621 {
622  QgsProject* project = QgsProject::instance();
623 
624  //write variable to project
626  QStringList variableValues;
627 
629  for ( ; it != variables.constEnd(); ++it )
630  {
631  variableNames << it.key();
632  variableValues << it.value();
633  }
634 
635  project->writeEntry( "Variables", "/variableNames", variableNames );
636  project->writeEntry( "Variables", "/variableValues", variableValues );
637 }
638 
640 {
642 
643  if ( !layer )
644  return scope;
645 
646  //add variables defined in layer properties
647  QStringList variableNames = layer->customProperty( "variableNames" ).toStringList();
648  QStringList variableValues = layer->customProperty( "variableValues" ).toStringList();
649 
650  int varIndex = 0;
651  Q_FOREACH ( const QString& variableName, variableNames )
652  {
653  if ( varIndex >= variableValues.length() )
654  {
655  break;
656  }
657 
658  QVariant varValue = variableValues.at( varIndex );
659  varIndex++;
660  scope->setVariable( variableName, varValue );
661  }
662 
663  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layer_name", layer->name(), true ) );
664  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layer_id", layer->id(), true ) );
665 
666  const QgsVectorLayer* vLayer = dynamic_cast< const QgsVectorLayer* >( layer );
667  if ( vLayer )
668  {
669  scope->setFields( vLayer->fields() );
670  }
671 
672  //TODO - add functions. Possibilities include:
673  //is_selected
674  //field summary stats
675 
676  return scope;
677 }
678 
680 {
681  if ( !layer )
682  return;
683 
684  //write variable to layer
685  QStringList variableNames = layer->customProperty( "variableNames" ).toStringList();
686  QStringList variableValues = layer->customProperty( "variableValues" ).toStringList();
687 
688  variableNames << name;
689  variableValues << value.toString();
690 
691  layer->setCustomProperty( "variableNames", variableNames );
692  layer->setCustomProperty( "variableValues", variableValues );
693 }
694 
696 {
697  if ( !layer )
698  return;
699 
701  QStringList variableValues;
702 
704  for ( ; it != variables.constEnd(); ++it )
705  {
706  variableNames << it.key();
707  variableValues << it.value();
708  }
709 
710  layer->setCustomProperty( "variableNames", variableNames );
711  layer->setCustomProperty( "variableValues", variableValues );
712 }
713 
715 {
716  QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Map Settings" ) );
717 
718  //add known map settings context variables
719  scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_id", "canvas", true ) );
720  scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_rotation", mapSettings.rotation(), true ) );
721  scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_scale", mapSettings.scale(), true ) );
722  scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_extent_width", mapSettings.extent().width(), true ) );
723  scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_extent_height", mapSettings.extent().height(), true ) );
724  QgsGeometry* centerPoint = QgsGeometry::fromPoint( mapSettings.visibleExtent().center() );
725  scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_extent_center", QVariant::fromValue( *centerPoint ), true ) );
726  delete centerPoint;
727 
728  return scope;
729 }
730 
732 {
733  if ( !symbolScope )
734  return nullptr;
735 
737 
738  double angle = 0.0;
739  const QgsMarkerSymbolV2* markerSymbol = dynamic_cast< const QgsMarkerSymbolV2* >( symbol );
740  if ( markerSymbol )
741  {
742  angle = markerSymbol->angle();
743  }
745 
746  return symbolScope;
747 }
748 
750 {
751  QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Composition" ) );
752  if ( !composition )
753  return scope;
754 
755  //add variables defined in composition properties
756  QStringList variableNames = composition->customProperty( "variableNames" ).toStringList();
757  QStringList variableValues = composition->customProperty( "variableValues" ).toStringList();
758 
759  int varIndex = 0;
760  Q_FOREACH ( const QString& variableName, variableNames )
761  {
762  if ( varIndex >= variableValues.length() )
763  {
764  break;
765  }
766 
767  QVariant varValue = variableValues.at( varIndex );
768  varIndex++;
769  scope->setVariable( variableName, varValue );
770  }
771 
772  //add known composition context variables
773  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layout_numpages", composition->numPages(), true ) );
774  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layout_pageheight", composition->paperHeight(), true ) );
775  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layout_pagewidth", composition->paperWidth(), true ) );
776  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layout_dpi", composition->printResolution(), true ) );
777 
778  return scope;
779 }
780 
782 {
783  if ( !composition )
784  return;
785 
786  //write variable to composition
787  QStringList variableNames = composition->customProperty( "variableNames" ).toStringList();
788  QStringList variableValues = composition->customProperty( "variableValues" ).toStringList();
789 
790  variableNames << name;
791  variableValues << value.toString();
792 
793  composition->setCustomProperty( "variableNames", variableNames );
794  composition->setCustomProperty( "variableValues", variableValues );
795 }
796 
798 {
799  if ( !composition )
800  return;
801 
803  QStringList variableValues;
804 
806  for ( ; it != variables.constEnd(); ++it )
807  {
808  variableNames << it.key();
809  variableValues << it.value();
810  }
811 
812  composition->setCustomProperty( "variableNames", variableNames );
813  composition->setCustomProperty( "variableValues", variableValues );
814 }
815 
817 {
819  if ( !atlas )
820  {
821  //add some dummy atlas variables. This is done so that as in certain contexts we want to show
822  //users that these variables are available even if they have no current value
823  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_pagename", QString(), true ) );
825  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_featureid", 0, true ) );
827  return scope;
828  }
829 
830  //add known atlas variables
831  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_totalfeatures", atlas->numFeatures(), true ) );
832  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_featurenumber", atlas->currentFeatureNumber() + 1, true ) );
833  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_filename", atlas->currentFilename(), true ) );
834  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_pagename", atlas->currentPageName(), true ) );
835 
836  if ( atlas->enabled() && atlas->coverageLayer() )
837  {
838  scope->setFields( atlas->coverageLayer()->fields() );
839  }
840 
841  if ( atlas->enabled() )
842  {
843  QgsFeature atlasFeature = atlas->feature();
844  scope->setFeature( atlasFeature );
845  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_feature", QVariant::fromValue( atlasFeature ), true ) );
846  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_featureid", atlasFeature.id(), true ) );
847  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_geometry", QVariant::fromValue( *atlasFeature.constGeometry() ), true ) );
848  }
849 
850  return scope;
851 }
852 
854 {
855  QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Composer Item" ) );
856  if ( !composerItem )
857  return scope;
858 
859  //add variables defined in composer item properties
860  QStringList variableNames = composerItem->customProperty( "variableNames" ).toStringList();
861  QStringList variableValues = composerItem->customProperty( "variableValues" ).toStringList();
862 
863  int varIndex = 0;
864  Q_FOREACH ( const QString& variableName, variableNames )
865  {
866  if ( varIndex >= variableValues.length() )
867  {
868  break;
869  }
870 
871  QVariant varValue = variableValues.at( varIndex );
872  varIndex++;
873  scope->setVariable( variableName, varValue );
874  }
875 
876  //add known composer item context variables
877  scope->addVariable( QgsExpressionContextScope::StaticVariable( "item_id", composerItem->id(), true ) );
878  scope->addVariable( QgsExpressionContextScope::StaticVariable( "item_uuid", composerItem->uuid(), true ) );
879  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layout_page", composerItem->page(), true ) );
880 
881  return scope;
882 }
883 
885 {
886  if ( !composerItem )
887  return;
888 
889  //write variable to composer item
890  QStringList variableNames = composerItem->customProperty( "variableNames" ).toStringList();
891  QStringList variableValues = composerItem->customProperty( "variableValues" ).toStringList();
892 
893  variableNames << name;
894  variableValues << value.toString();
895 
896  composerItem->setCustomProperty( "variableNames", variableNames );
897  composerItem->setCustomProperty( "variableValues", variableValues );
898 }
899 
901 {
902  if ( !composerItem )
903  return;
904 
906  QStringList variableValues;
907 
909  for ( ; it != variables.constEnd(); ++it )
910  {
911  variableNames << it.key();
912  variableValues << it.value();
913  }
914 
915  composerItem->setCustomProperty( "variableNames", variableNames );
916  composerItem->setCustomProperty( "variableValues", variableValues );
917 }
918 
920 {
922  scope->setFeature( feature );
923  scope->setFields( fields );
924  return QgsExpressionContext() << scope;
925 }
926 
928 {
929  QgsExpression::registerFunction( new GetNamedProjectColor() );
930 }
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:65
static const char * QGIS_VERSION
Definition: qgis.h:42
QgsExpression::Function * function(const QString &name) const
Retrieves a function from the scope.
static const QString EXPR_ORIGINAL_VALUE
Inbuilt variable name for value original value variable.
Class for parsing and evaluation of expressions (formerly called "search strings").
bool isHighlightedVariable(const QString &name) const
Returns true if the specified variable name is intended to be highlighted to the user.
Single variable definition for use within a QgsExpressionContextScope.
static void setGlobalVariable(const QString &name, const QVariant &value)
Sets a global context variable.
static unsigned index
bool hasFunction(const QString &name) const
Checks whether a specified function is contained in the context.
Base class for all map layer types.
Definition: qgsmaplayer.h:49
double paperWidth() const
Width of paper item.
iterator insert(const Key &key, const T &value)
QgsExpressionContextScope * scope(int index)
Returns the scope at the specified index within the context.
static void setGlobalVariables(const QgsStringMap &variables)
Sets all global context variables.
double scale() const
Return the calculated scale of the map.
const Key key(const T &value) const
int localeAwareCompare(const QString &other) const
QString name() const
Get the display name of the layer.
bool hasVariable(const QString &name) const
Check whether a variable is specified by any scope within the context.
QgsExpressionContext & operator=(const QgsExpressionContext &other)
static QgsExpressionContextScope * atlasScope(const QgsAtlasComposition *atlas)
Creates a new scope which contains variables and functions relating to a QgsAtlasComposition.
void addFunction(const QString &name, QgsScopedExpressionFunction *function)
Adds a function to the scope.
int length() const
A abstract base class for defining QgsExpression functions.
QgsExpressionContextScope(const QString &name=QString())
Constructor for QgsExpressionContextScope.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
QgsFields fields() const
Returns the list of fields of this layer.
static void setLayerVariables(QgsMapLayer *layer, const QgsStringMap &variables)
Sets all layer context variables.
QgsExpressionContextScope * activeScopeForVariable(const QString &name)
Returns the currently active scope from the context for a specified variable name.
QList< QVariant > toList() const
static QgsExpressionContext createFeatureBasedContext(const QgsFeature &feature, const QgsFields &fields)
Helper function for creating an expression context which contains just a feature and fields collectio...
int scopeCount() const
Returns the number of scopes contained in the context.
const_iterator constBegin() const
const T & at(int i) const
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for layer.
A item that forms part of a map composition.
QgsRectangle visibleExtent() const
Return the actual extent derived from requested extent that takes takes output image size into accoun...
bool enabled() const
Returns whether the atlas generation is enabled.
Container of fields for a vector layer.
Definition: qgsfield.h:187
QStringList readListEntry(const QString &scope, const QString &key, const QStringList &def=QStringList(), bool *ok=nullptr) const
Key value accessors.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:76
bool hasVariable(const QString &name) const
Tests whether a variable with the specified name exists in the scope.
QgsFeature feature() const
Returns the current atlas feature.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the scope.
QSet< T > toSet() const
double rotation() const
Return the rotation of the resulting map image Units are clockwise degrees.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from the composition.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
QgsExpressionContextScope & operator=(const QgsExpressionContextScope &other)
QString currentPageName() const
Returns the name of the page for the current atlas feature.
int indexOfScope(QgsExpressionContextScope *scope) const
Returns the index of the specified scope if it exists within the context.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from the object.
QString tr(const char *sourceText, const char *disambiguation, int n)
int numPages() const
Returns the number of pages in the composition.
void setVariable(const QString &name, const QVariant &value)
Convenience method for setting a variable in the context scope by name and value. ...
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
QgsExpressionContext & operator<<(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
static QString userFullName()
Returns the user&#39;s operating system login account full display name.
QVariant variable(const QString &name) const
Fetches a matching variable from the context.
static void setCompositionVariables(QgsComposition *composition, const QgsStringMap &variables)
Sets all composition context variables.
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the composition.
The QgsMapSettings class contains configuration for rendering of the map.
QString filePath() const
QString name() const
Returns the friendly display name of the context scope.
void setValue(const QString &key, const QVariant &value)
QString uuid() const
Get item identification name.
bool hasFunction(const QString &name) const
Tests whether a function with the specified name exists in the scope.
bool writeEntry(const QString &scope, const QString &key, bool value)
QStringList variableNames() const
Returns a list of variables names set by all scopes in the context.
bool removeVariable(const QString &name)
Removes a variable from the context scope, if found.
QgsExpressionContextScope * lastScope()
Returns the last scope added to the context.
static const QString EXPR_SYMBOL_ANGLE
Inbuilt variable name for symbol angle variable.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
QStringList filteredVariableNames() const
Returns a fitlered and sorted list of variable names contained within the scope.
const_iterator constEnd() const
QString path() const
QString fileName() const
int printResolution() const
Q_DECL_DEPRECATED void title(const QString &title)
Every project has an associated title string.
Definition: qgsproject.h:90
bool contains(const QString &key) const
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static bool registerFunction(Function *function, bool transferOwnership=false)
Registers a function to the expression engine.
const_iterator constEnd() const
QgsFields fields() const
Convenience function for retrieving the fields for the context, if set.
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
double angle() const
static void setComposerItemVariable(QgsComposerItem *composerItem, const QString &name, const QVariant &value)
Sets a composer item context variable.
static const QString EXPR_SYMBOL_COLOR
Inbuilt variable name for symbol color variable.
bool isReadOnly(const QString &name) const
Returns whether a variable is read only, and should not be modifiable by users.
int numFeatures() const
Returns the number of features in the coverage layer.
QgsFeature feature() const
Convenience function for retrieving the feature for the context, if set.
QString id() const
Get this layer&#39;s unique ID, this ID is used to access this layer from map layer registry.
Reads and writes project states.
Definition: qgsproject.h:70
static void setCompositionVariable(QgsComposition *composition, const QString &name, const QVariant &value)
Sets a composition context variable.
QDir dir() const
QStringList functionNames() const
Retrieves a list of function names contained in the context.
static const QString EXPR_FIELDS
Inbuilt variable name for fields storage.
QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the scope.
QVariant variable(const QString &name) const
Retrieves a variable&#39;s value from the scope.
static QgsExpressionContextScope * composerItemScope(const QgsComposerItem *composerItem)
Creates a new scope which contains variables and functions relating to a QgsComposerItem.
Graphics scene for map printing.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
QString currentFilename() const
Returns the current filename.
static void setLayerVariable(QgsMapLayer *layer, const QString &name, const QVariant &value)
Sets a layer context variable.
static QgsGeometry * fromPoint(const QgsPoint &point)
Creates a new geometry from a QgsPoint object.
static QString userLoginName()
Returns the user&#39;s operating system login account name.
void clear()
double ANALYSIS_EXPORT angle(Point3D *p1, Point3D *p2, Point3D *p3, Point3D *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
iterator end()
QString toLower() const
const T value(const Key &key) const
static const char * QGIS_RELEASE_NAME
Definition: qgis.h:46
static QString osName()
Returns a string name of the operating system QGIS is running on.
QVariant fromValue(const T &value)
QChar toLower() const
const Key key(const T &value) const
static void setComposerItemVariables(QgsComposerItem *composerItem, const QgsStringMap &variables)
Sets all composition context variables.
static void registerContextFunctions()
Registers all known core functions provided by QgsExpressionContextScope objects. ...
QVariant value(const QString &key, const QVariant &defaultValue) const
const_iterator constBegin() const
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object...
QStringList toStringList() const
static const QString EXPR_FEATURE
Inbuilt variable name for feature storage.
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the object.
QStringList filteredVariableNames() const
Returns a filtered list of variables names set by all scopes in the context.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
const QChar at(int position) const
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:381
const QgsGeometry * constGeometry() const
Gets a const pointer to the geometry object associated with this feature.
Definition: qgsfeature.cpp:82
double paperHeight() const
Height of paper item.
QgsRectangle extent() const
Return geographical coordinates of the rectangle that should be rendered.
static QString platform()
Returns the QGIS platform name, eg "desktop" or "server".
int page() const
Gets the page the item is currently on.
QStringList functionNames() const
Retrieves a list of names of functions contained in the scope.
Class used to render an Atlas, iterating over geometry features.
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user...
static QColor decodeColor(const QString &str)
QgsVectorLayer * coverageLayer() const
Returns the coverage layer used for the atlas features.
static void setProjectVariable(const QString &name, const QVariant &value)
Sets a project context variable.
int currentFeatureNumber() const
Returns the current feature number, where a value of 0 corresponds to the first feature.
static QgsExpressionContextScope * projectScope()
Creates a new scope which contains variables and functions relating to the current QGIS project...
QgsExpression::Function * function(const QString &name) const
Fetches a matching function from the context.
QgsExpressionContextScope * popScope()
Removes the last scope from the expression context and return it.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
const_iterator constEnd() const
const_iterator constBegin() const
QFileInfo fileInfo() const
Returns QFileInfo object for the project&#39;s associated file.
Definition: qgsproject.cpp:436
double width() const
Width of the rectangle.
Definition: qgsrectangle.h:207
static const QString EXPR_GEOMETRY_PART_NUM
Inbuilt variable name for geometry part number variable.
Represents a vector layer which manages a vector based data sets.
bool isReadOnly(const QString &name) const
Tests whether the specified variable is read only and should not be editable by users.
static const QString EXPR_GEOMETRY_PART_COUNT
Inbuilt variable name for geometry part count variable.
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
static QgsExpressionContextScope * updateSymbolScope(const QgsSymbolV2 *symbol, QgsExpressionContextScope *symbolScope=nullptr)
Updates a symbol scope related to a QgsSymbolV2 to an expression context.
QString toString() const
QgsPoint center() const
Center point of the rectangle.
Definition: qgsrectangle.h:217
static const int QGIS_VERSION_INT
Definition: qgis.h:44
iterator begin()
Expression function for use within a QgsExpressionContextScope.
QStringList variableNames() const
Returns a list of variable names contained within the scope.
double height() const
Height of the rectangle.
Definition: qgsrectangle.h:212
const T value(const Key &key) const
QColor color() const
static void setProjectVariables(const QgsStringMap &variables)
Sets all project context variables.
static QgsExpressionContextScope * compositionScope(const QgsComposition *composition)
Creates a new scope which contains variables and functions relating to a QgsComposition.
QString id() const
Get item&#39;s id (which is not necessarly unique)