QGIS API Documentation  2.12.0-Lyon
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 <QSettings>
29 #include <QDir>
30 
31 
32 const QString QgsExpressionContext::EXPR_FIELDS( "_fields_" );
33 const QString QgsExpressionContext::EXPR_FEATURE( "_feature_" );
35 
36 //
37 // QgsExpressionContextScope
38 //
39 
41  : mName( name )
42 {
43 
44 }
45 
47  : mName( other.mName )
48  , mVariables( other.mVariables )
49 {
50  Q_FOREACH ( const QString& key, other.mFunctions.keys() )
51  {
52  mFunctions.insert( key, other.mFunctions.value( key )->clone() );
53  }
54 }
55 
57 {
58  mName = other.mName;
59  mVariables = other.mVariables;
60 
61  qDeleteAll( mFunctions );
62  mFunctions.clear();
63  Q_FOREACH ( const QString& key, other.mFunctions.keys() )
64  {
65  mFunctions.insert( key, other.mFunctions.value( key )->clone() );
66  }
67 
68  return *this;
69 }
70 
72 {
73  qDeleteAll( mFunctions );
74 }
75 
76 void QgsExpressionContextScope::setVariable( const QString &name, const QVariant &value )
77 {
78  if ( mVariables.contains( name ) )
79  {
80  StaticVariable existing = mVariables.value( name );
81  existing.value = value;
82  addVariable( existing );
83  }
84  else
85  {
87  }
88 }
89 
91 {
92  mVariables.insert( variable.name, variable );
93 }
94 
96 {
97  return mVariables.remove( name ) > 0;
98 }
99 
101 {
102  return mVariables.contains( name );
103 }
104 
106 {
107  return hasVariable( name ) ? mVariables.value( name ).value : QVariant();
108 }
109 
111 {
112  QStringList names = mVariables.keys();
113  return names;
114 }
115 
116 bool QgsExpressionContextScope::variableNameSort( const QString& a, const QString& b )
117 {
118  return QString::localeAwareCompare( a, b ) < 0;
119 }
120 
121 // not public API
123 class QgsExpressionContextVariableCompare
124 {
125  public:
126  explicit QgsExpressionContextVariableCompare( const QgsExpressionContextScope& scope )
127  : mScope( scope )
128  { }
129 
130  bool operator()( const QString& a, const QString& b ) const
131  {
132  bool aReadOnly = mScope.isReadOnly( a );
133  bool bReadOnly = mScope.isReadOnly( b );
134  if ( aReadOnly != bReadOnly )
135  return aReadOnly;
136  return QString::localeAwareCompare( a, b ) < 0;
137  }
138 
139  private:
140  const QgsExpressionContextScope& mScope;
141 };
143 
145 {
146  QStringList allVariables = mVariables.keys();
147  QStringList filtered;
148  Q_FOREACH ( const QString& variable, allVariables )
149  {
150  if ( variable.startsWith( "_" ) )
151  continue;
152 
153  filtered << variable;
154  }
155  QgsExpressionContextVariableCompare cmp( *this );
156  qSort( filtered.begin(), filtered.end(), cmp );
157 
158  return filtered;
159 }
160 
162 {
163  return hasVariable( name ) ? mVariables.value( name ).readOnly : false;
164 }
165 
167 {
168  return mFunctions.contains( name );
169 }
170 
172 {
173  return mFunctions.contains( name ) ? mFunctions.value( name ) : 0;
174 }
175 
177 {
178  return mFunctions.keys();
179 }
180 
182 {
183  mFunctions.insert( name, function );
184 }
185 
187 {
189 }
190 
192 {
194 }
195 
196 
197 //
198 // QgsExpressionContext
199 //
200 
202 {
203  Q_FOREACH ( const QgsExpressionContextScope* scope, other.mStack )
204  {
205  mStack << new QgsExpressionContextScope( *scope );
206  }
207  mHighlightedVariables = other.mHighlightedVariables;
208 }
209 
211 {
212  qDeleteAll( mStack );
213  mStack.clear();
214  Q_FOREACH ( const QgsExpressionContextScope* scope, other.mStack )
215  {
216  mStack << new QgsExpressionContextScope( *scope );
217  }
218  mHighlightedVariables = other.mHighlightedVariables;
219  return *this;
220 }
221 
223 {
224  qDeleteAll( mStack );
225  mStack.clear();
226 }
227 
229 {
230  Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
231  {
232  if ( scope->hasVariable( name ) )
233  return true;
234  }
235  return false;
236 }
237 
239 {
241  return scope ? scope->variable( name ) : QVariant();
242 }
243 
245 {
246  return mHighlightedVariables.contains( name );
247 }
248 
250 {
251  mHighlightedVariables = variableNames;
252 }
253 
255 {
256  //iterate through stack backwards, so that higher priority variables take precedence
258  while ( it != mStack.constBegin() )
259  {
260  --it;
261  if (( *it )->hasVariable( name ) )
262  return ( *it );
263  }
264  return 0;
265 }
266 
268 {
269  //iterate through stack backwards, so that higher priority variables take precedence
271  while ( it != mStack.begin() )
272  {
273  --it;
274  if (( *it )->hasVariable( name ) )
275  return ( *it );
276  }
277  return 0;
278 }
279 
281 {
282  if ( index < 0 || index >= mStack.count() )
283  return 0;
284 
285  return mStack[index];
286 }
287 
289 {
290  if ( mStack.count() < 1 )
291  return 0;
292 
293  return mStack.last();
294 }
295 
297 {
298  if ( !scope )
299  return -1;
300 
301  return mStack.indexOf( scope );
302 }
303 
305 {
306  QStringList names;
307  Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
308  {
309  names << scope->variableNames();
310  }
311  return names.toSet().toList();
312 }
313 
315 {
316  QStringList allVariables = variableNames();
317  QStringList filtered;
318  Q_FOREACH ( const QString& variable, allVariables )
319  {
320  if ( variable.startsWith( "_" ) )
321  continue;
322 
323  filtered << variable;
324  }
325 
326  filtered.sort();
327  return filtered;
328 }
329 
330 bool QgsExpressionContext::isReadOnly( const QString& name ) const
331 {
332  Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
333  {
334  if ( scope->isReadOnly( name ) )
335  return true;
336  }
337  return false;
338 }
339 
341 {
342  Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
343  {
344  if ( scope->hasFunction( name ) )
345  return true;
346  }
347  return false;
348 }
349 
351 {
352  QStringList result;
353  Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
354  {
355  result << scope->functionNames();
356  }
357  result = result.toSet().toList();
358  result.sort();
359  return result;
360 }
361 
363 {
364  //iterate through stack backwards, so that higher priority variables take precedence
366  while ( it != mStack.constBegin() )
367  {
368  --it;
369  if (( *it )->hasFunction( name ) )
370  return ( *it )->function( name );
371  }
372  return 0;
373 }
374 
376 {
377  return mStack.count();
378 }
379 
381 {
382  mStack.append( scope );
383 }
384 
386 {
387  mStack.append( scope );
388  return *this;
389 }
390 
392 {
393  if ( mStack.isEmpty() )
394  mStack.append( new QgsExpressionContextScope() );
395 
396  mStack.last()->setFeature( feature );
397 }
398 
400 {
401  return qvariant_cast<QgsFeature>( variable( QgsExpressionContext::EXPR_FEATURE ) );
402 }
403 
405 {
406  if ( mStack.isEmpty() )
407  mStack.append( new QgsExpressionContextScope() );
408 
409  mStack.last()->setFields( fields );
410 }
411 
413 {
414  return qvariant_cast<QgsFields>( variable( QgsExpressionContext::EXPR_FIELDS ) );
415 }
416 
418 {
419  if ( mStack.isEmpty() )
420  mStack.append( new QgsExpressionContextScope() );
421 
422  mStack.last()->setVariable( QgsExpressionContext::EXPR_ORIGINAL_VALUE, value );
423 }
424 
425 
426 //
427 // QgsExpressionContextUtils
428 //
429 
431 {
433 
434  //read values from QSettings
435  QSettings settings;
436 
437  //check if settings contains any variables
438  if ( settings.contains( QString( "/variables/values" ) ) )
439  {
440  QList< QVariant > customVariableVariants = settings.value( QString( "/variables/values" ) ).toList();
441  QList< QVariant > customVariableNames = settings.value( QString( "/variables/names" ) ).toList();
442  int variableIndex = 0;
443  for ( QList< QVariant >::const_iterator it = customVariableVariants.constBegin();
444  it != customVariableVariants.constEnd(); ++it )
445  {
446  if ( variableIndex >= customVariableNames.length() )
447  {
448  break;
449  }
450 
451  QVariant value = ( *it );
452  QString name = customVariableNames.at( variableIndex ).toString();
453 
454  scope->setVariable( name, value );
455  variableIndex++;
456  }
457  }
458 
459  //add some extra global variables
462  scope->addVariable( QgsExpressionContextScope::StaticVariable( "qgis_release_name", QGis::QGIS_RELEASE_NAME, true ) );
463 
464  return scope;
465 }
466 
468 {
469  // save variable to settings
470  QSettings settings;
471 
472  QList< QVariant > customVariableVariants = settings.value( QString( "/variables/values" ) ).toList();
473  QList< QVariant > customVariableNames = settings.value( QString( "/variables/names" ) ).toList();
474 
475  customVariableVariants << value;
476  customVariableNames << name;
477 
478  settings.setValue( QString( "/variables/names" ), customVariableNames );
479  settings.setValue( QString( "/variables/values" ), customVariableVariants );
480 }
481 
483 {
484  QSettings settings;
485 
486  QList< QVariant > customVariableVariants;
487  QList< QVariant > customVariableNames;
488 
489  Q_FOREACH ( const QString& variable, variables.keys() )
490  {
491  customVariableNames << variable;
492  customVariableVariants << variables.value( variable );
493  }
494 
495  settings.setValue( QString( "/variables/names" ), customVariableNames );
496  settings.setValue( QString( "/variables/values" ), customVariableVariants );
497 }
498 
500 {
501  public:
503  : QgsScopedExpressionFunction( "project_color", 1, "Color" )
504  {
505  //build up color list from project. Do this in advance for speed
506  QStringList colorStrings = QgsProject::instance()->readListEntry( "Palette", "/Colors" );
507  QStringList colorLabels = QgsProject::instance()->readListEntry( "Palette", "/Labels" );
508 
509  //generate list from custom colors
510  int colorIndex = 0;
511  for ( QStringList::iterator it = colorStrings.begin();
512  it != colorStrings.end(); ++it )
513  {
515  QString label;
516  if ( colorLabels.length() > colorIndex )
517  {
518  label = colorLabels.at( colorIndex );
519  }
520 
521  mColors.insert( label.toLower(), color );
522  colorIndex++;
523  }
524  }
525 
526  virtual QVariant func( const QVariantList& values, const QgsExpressionContext*, QgsExpression* ) override
527  {
528  QString colorName = values.at( 0 ).toString().toLower();
529  if ( mColors.contains( colorName ) )
530  {
531  return QString( "%1,%2,%3" ).arg( mColors.value( colorName ).red() ).arg( mColors.value( colorName ).green() ).arg( mColors.value( colorName ).blue() );
532  }
533  else
534  return QVariant();
535  }
536 
538  {
539  return new GetNamedProjectColor();
540  }
541 
542  private:
543 
544  QHash< QString, QColor > mColors;
545 
546 };
547 
549 {
550  QgsProject* project = QgsProject::instance();
551 
553 
554  //add variables defined in project file
555  QStringList variableNames = project->readListEntry( "Variables", "/variableNames" );
556  QStringList variableValues = project->readListEntry( "Variables", "/variableValues" );
557 
558  int varIndex = 0;
559  Q_FOREACH ( const QString& variableName, variableNames )
560  {
561  if ( varIndex >= variableValues.length() )
562  {
563  break;
564  }
565 
566  QString varValueString = variableValues.at( varIndex );
567  varIndex++;
568  scope->setVariable( variableName, varValueString );
569  }
570 
571  //add other known project variables
572  scope->addVariable( QgsExpressionContextScope::StaticVariable( "project_title", project->title(), true ) );
573  scope->addVariable( QgsExpressionContextScope::StaticVariable( "project_path", project->fileInfo().filePath(), true ) );
574  scope->addVariable( QgsExpressionContextScope::StaticVariable( "project_folder", project->fileInfo().dir().path(), true ) );
575  scope->addVariable( QgsExpressionContextScope::StaticVariable( "project_filename", project->fileInfo().fileName(), true ) );
576 
577  scope->addFunction( "project_color", new GetNamedProjectColor() );
578  return scope;
579 }
580 
582 {
583  QgsProject* project = QgsProject::instance();
584 
585  //write variable to project
586  QStringList variableNames = project->readListEntry( "Variables", "/variableNames" );
587  QStringList variableValues = project->readListEntry( "Variables", "/variableValues" );
588 
589  variableNames << name;
590  variableValues << value.toString();
591 
592  project->writeEntry( "Variables", "/variableNames", variableNames );
593  project->writeEntry( "Variables", "/variableValues", variableValues );
594 }
595 
597 {
598  QgsProject* project = QgsProject::instance();
599 
600  //write variable to project
601  QStringList variableNames;
602  QStringList variableValues;
603 
604  Q_FOREACH ( const QString& variable, variables.keys() )
605  {
606  variableNames << variable;
607  variableValues << variables.value( variable );
608  }
609 
610  project->writeEntry( "Variables", "/variableNames", variableNames );
611  project->writeEntry( "Variables", "/variableValues", variableValues );
612 }
613 
615 {
617 
618  if ( !layer )
619  return scope;
620 
621  //add variables defined in layer properties
622  QStringList variableNames = layer->customProperty( "variableNames" ).toStringList();
623  QStringList variableValues = layer->customProperty( "variableValues" ).toStringList();
624 
625  int varIndex = 0;
626  Q_FOREACH ( const QString& variableName, variableNames )
627  {
628  if ( varIndex >= variableValues.length() )
629  {
630  break;
631  }
632 
633  QVariant varValue = variableValues.at( varIndex );
634  varIndex++;
635  scope->setVariable( variableName, varValue );
636  }
637 
638  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layer_name", layer->name(), true ) );
639  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layer_id", layer->id(), true ) );
640 
641  const QgsVectorLayer* vLayer = dynamic_cast< const QgsVectorLayer* >( layer );
642  if ( vLayer )
643  {
644  scope->setFields( vLayer->fields() );
645  }
646 
647  //TODO - add functions. Possibilities include:
648  //is_selected
649  //field summary stats
650 
651  return scope;
652 }
653 
655 {
656  if ( !layer )
657  return;
658 
659  //write variable to layer
660  QStringList variableNames = layer->customProperty( "variableNames" ).toStringList();
661  QStringList variableValues = layer->customProperty( "variableValues" ).toStringList();
662 
663  variableNames << name;
664  variableValues << value.toString();
665 
666  layer->setCustomProperty( "variableNames", variableNames );
667  layer->setCustomProperty( "variableValues", variableValues );
668 }
669 
671 {
672  if ( !layer )
673  return;
674 
675  QStringList variableNames;
676  QStringList variableValues;
677 
678  Q_FOREACH ( const QString& variable, variables.keys() )
679  {
680  variableNames << variable;
681  variableValues << variables.value( variable );
682  }
683 
684  layer->setCustomProperty( "variableNames", variableNames );
685  layer->setCustomProperty( "variableValues", variableValues );
686 }
687 
689 {
690  QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Map Settings" ) );
691 
692  //add known map settings context variables
693  scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_id", "canvas", true ) );
694  scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_rotation", mapSettings.rotation(), true ) );
695  scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_scale", mapSettings.scale(), true ) );
696 
697  return scope;
698 }
699 
701 {
702  QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Composition" ) );
703  if ( !composition )
704  return scope;
705 
706  //add variables defined in composition properties
707  QStringList variableNames = composition->customProperty( "variableNames" ).toStringList();
708  QStringList variableValues = composition->customProperty( "variableValues" ).toStringList();
709 
710  int varIndex = 0;
711  Q_FOREACH ( const QString& variableName, variableNames )
712  {
713  if ( varIndex >= variableValues.length() )
714  {
715  break;
716  }
717 
718  QVariant varValue = variableValues.at( varIndex );
719  varIndex++;
720  scope->setVariable( variableName, varValue );
721  }
722 
723  //add known composition context variables
724  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layout_numpages", composition->numPages(), true ) );
725  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layout_pageheight", composition->paperHeight(), true ) );
726  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layout_pagewidth", composition->paperWidth(), true ) );
727  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layout_dpi", composition->printResolution(), true ) );
728 
729  return scope;
730 }
731 
733 {
734  if ( !composition )
735  return;
736 
737  //write variable to composition
738  QStringList variableNames = composition->customProperty( "variableNames" ).toStringList();
739  QStringList variableValues = composition->customProperty( "variableValues" ).toStringList();
740 
741  variableNames << name;
742  variableValues << value.toString();
743 
744  composition->setCustomProperty( "variableNames", variableNames );
745  composition->setCustomProperty( "variableValues", variableValues );
746 }
747 
749 {
750  if ( !composition )
751  return;
752 
753  QStringList variableNames;
754  QStringList variableValues;
755 
756  Q_FOREACH ( const QString& variable, variables.keys() )
757  {
758  variableNames << variable;
759  variableValues << variables.value( variable );
760  }
761 
762  composition->setCustomProperty( "variableNames", variableNames );
763  composition->setCustomProperty( "variableValues", variableValues );
764 }
765 
767 {
769  if ( !atlas )
770  {
771  //add some dummy atlas variables. This is done so that as in certain contexts we want to show
772  //users that these variables are available even if they have no current value
773  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_pagename", QString(), true ) );
775  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_featureid", 0, true ) );
777  return scope;
778  }
779 
780  //add known atlas variables
781  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_totalfeatures", atlas->numFeatures(), true ) );
782  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_featurenumber", atlas->currentFeatureNumber() + 1, true ) );
783  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_filename", atlas->currentFilename(), true ) );
784  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_pagename", atlas->currentPageName(), true ) );
785 
786  if ( atlas->enabled() && atlas->coverageLayer() )
787  {
788  scope->setFields( atlas->coverageLayer()->fields() );
789  }
790 
791  if ( atlas->enabled() )
792  {
793  QgsFeature atlasFeature = atlas->feature();
794  scope->setFeature( atlasFeature );
795  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_feature", QVariant::fromValue( atlasFeature ), true ) );
796  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_featureid", atlasFeature.id(), true ) );
797  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_geometry", QVariant::fromValue( *atlasFeature.constGeometry() ), true ) );
798  }
799 
800  return scope;
801 }
802 
804 {
805  QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Composer Item" ) );
806  if ( !composerItem )
807  return scope;
808 
809  //add variables defined in composer item properties
810  QStringList variableNames = composerItem->customProperty( "variableNames" ).toStringList();
811  QStringList variableValues = composerItem->customProperty( "variableValues" ).toStringList();
812 
813  int varIndex = 0;
814  Q_FOREACH ( const QString& variableName, variableNames )
815  {
816  if ( varIndex >= variableValues.length() )
817  {
818  break;
819  }
820 
821  QVariant varValue = variableValues.at( varIndex );
822  varIndex++;
823  scope->setVariable( variableName, varValue );
824  }
825 
826  //add known composer item context variables
827  scope->addVariable( QgsExpressionContextScope::StaticVariable( "item_id", composerItem->id(), true ) );
828  scope->addVariable( QgsExpressionContextScope::StaticVariable( "item_uuid", composerItem->uuid(), true ) );
829  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layout_page", composerItem->page(), true ) );
830 
831  return scope;
832 }
833 
835 {
836  if ( !composerItem )
837  return;
838 
839  //write variable to composer item
840  QStringList variableNames = composerItem->customProperty( "variableNames" ).toStringList();
841  QStringList variableValues = composerItem->customProperty( "variableValues" ).toStringList();
842 
843  variableNames << name;
844  variableValues << value.toString();
845 
846  composerItem->setCustomProperty( "variableNames", variableNames );
847  composerItem->setCustomProperty( "variableValues", variableValues );
848 }
849 
851 {
852  if ( !composerItem )
853  return;
854 
855  QStringList variableNames;
856  QStringList variableValues;
857 
858  Q_FOREACH ( const QString& variable, variables.keys() )
859  {
860  variableNames << variable;
861  variableValues << variables.value( variable );
862  }
863 
864  composerItem->setCustomProperty( "variableNames", variableNames );
865  composerItem->setCustomProperty( "variableValues", variableValues );
866 }
867 
869 {
871  scope->setFeature( feature );
872  scope->setFields( fields );
873  return QgsExpressionContext() << scope;
874 }
875 
877 {
879 }
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:53
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
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:92
void clear()
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.
int localeAwareCompare(const QString &other) const
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 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.
bool contains(const QString &str, Qt::CaseSensitivity cs) const
bool enabled() const
Returns whether the atlas generation is enabled.
Container of fields for a vector layer.
Definition: qgsfield.h:177
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
virtual QVariant func(const QVariantList &values, const QgsExpressionContext *, QgsExpression *) override
Returns result of evaluating the function.
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:176
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.
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.
int indexOf(const T &value, int from) const
QString filePath() const
const QString & name() const
Get the display name of the layer.
QList< Key > keys() const
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.
int count(const T &value) const
void append(const T &value)
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.
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:89
bool isEmpty() const
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.
QgsFields fields() const
Convenience function for retrieving the fields for the context, if set.
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
static void setComposerItemVariable(QgsComposerItem *composerItem, const QString &name, const QVariant &value)
Sets a composer item context 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's unique ID, this ID is used to access this layer from map layer registry.
Reads and writes project states.
Definition: qgsproject.h:69
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
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'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.
QList< Key > keys() const
static void setLayerVariable(QgsMapLayer *layer, const QString &name, const QVariant &value)
Sets a layer context variable.
void clear()
iterator end()
QString toLower() const
const T value(const Key &key) const
static const char * QGIS_RELEASE_NAME
Definition: qgis.h:46
QVariant fromValue(const T &value)
QChar toLower() 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
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
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:353
T & last()
const QgsGeometry * constGeometry() const
Gets a const pointer to the geometry object associated with this feature.
Definition: qgsfeature.cpp:70
double paperHeight() const
Height of paper item.
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...
const QString & currentFilename() const
Returns the current filename.
static QColor decodeColor(const QString &str)
bool contains(const Key &key) const
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.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
const_iterator constEnd() const
QgsScopedExpressionFunction * clone() const override
Returns a clone of the function.
const_iterator constBegin() const
QFileInfo fileInfo() const
Returns QFileInfo object for the project's associated file.
Definition: qgsproject.cpp:408
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.
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
QString toString() const
static const int QGIS_VERSION_INT
Definition: qgis.h:44
iterator begin()
Expression function for use within a QgsExpressionContextScope.
QStringList readListEntry(const QString &scope, const QString &key, const QStringList &def=QStringList(), bool *ok=0) const
Key value accessors.
QStringList variableNames() const
Returns a list of variable names contained within the scope.
const T value(const Key &key) 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's id (which is not necessarly unique)