QGIS API Documentation  2.14.11-Essen
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
83 void QgsExpressionContextScope::setVariable( const QString &name, const QVariant &value )
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 {
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 
310 int QgsExpressionContext::indexOfScope( const QString& scopeName ) const
311 {
312  int index = 0;
313  Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
314  {
315  if ( scope->name() == scopeName )
316  return index;
317 
318  index++;
319  }
320  return -1;
321 }
322 
324 {
325  QStringList names;
326  Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
327  {
328  names << scope->variableNames();
329  }
330  return names.toSet().toList();
331 }
332 
334 {
335  QStringList allVariables = variableNames();
336  QStringList filtered;
337  Q_FOREACH ( const QString& variable, allVariables )
338  {
339  if ( variable.startsWith( '_' ) )
340  continue;
341 
342  filtered << variable;
343  }
344 
345  filtered.sort();
346  return filtered;
347 }
348 
349 bool QgsExpressionContext::isReadOnly( const QString& name ) const
350 {
351  Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
352  {
353  if ( scope->isReadOnly( name ) )
354  return true;
355  }
356  return false;
357 }
358 
360 {
361  Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
362  {
363  if ( scope->hasFunction( name ) )
364  return true;
365  }
366  return false;
367 }
368 
370 {
371  QStringList result;
372  Q_FOREACH ( const QgsExpressionContextScope* scope, mStack )
373  {
374  result << scope->functionNames();
375  }
376  result = result.toSet().toList();
377  result.sort();
378  return result;
379 }
380 
382 {
383  //iterate through stack backwards, so that higher priority variables take precedence
385  while ( it != mStack.constBegin() )
386  {
387  --it;
388  if (( *it )->hasFunction( name ) )
389  return ( *it )->function( name );
390  }
391  return nullptr;
392 }
393 
395 {
396  return mStack.count();
397 }
398 
400 {
401  mStack.append( scope );
402 }
403 
405 {
406  if ( !mStack.isEmpty() )
407  return mStack.takeLast();
408 
409  return nullptr;
410 }
411 
413 {
414  mStack.append( scope );
415  return *this;
416 }
417 
419 {
420  if ( mStack.isEmpty() )
421  mStack.append( new QgsExpressionContextScope() );
422 
423  mStack.last()->setFeature( feature );
424 }
425 
427 {
428  return qvariant_cast<QgsFeature>( variable( QgsExpressionContext::EXPR_FEATURE ) );
429 }
430 
432 {
433  if ( mStack.isEmpty() )
434  mStack.append( new QgsExpressionContextScope() );
435 
436  mStack.last()->setFields( fields );
437 }
438 
440 {
441  return qvariant_cast<QgsFields>( variable( QgsExpressionContext::EXPR_FIELDS ) );
442 }
443 
445 {
446  if ( mStack.isEmpty() )
447  mStack.append( new QgsExpressionContextScope() );
448 
450  value, true ) );
451 }
452 
453 
454 //
455 // QgsExpressionContextUtils
456 //
457 
459 {
461 
462  //read values from QSettings
463  QSettings settings;
464 
465  //check if settings contains any variables
466  if ( settings.contains( QString( "/variables/values" ) ) )
467  {
468  QList< QVariant > customVariableVariants = settings.value( QString( "/variables/values" ) ).toList();
469  QList< QVariant > customVariableNames = settings.value( QString( "/variables/names" ) ).toList();
470  int variableIndex = 0;
471  for ( QList< QVariant >::const_iterator it = customVariableVariants.constBegin();
472  it != customVariableVariants.constEnd(); ++it )
473  {
474  if ( variableIndex >= customVariableNames.length() )
475  {
476  break;
477  }
478 
479  QVariant value = ( *it );
480  QString name = customVariableNames.at( variableIndex ).toString();
481 
482  scope->setVariable( name, value );
483  variableIndex++;
484  }
485  }
486 
487  //add some extra global variables
490  scope->addVariable( QgsExpressionContextScope::StaticVariable( "qgis_release_name", QGis::QGIS_RELEASE_NAME, true ) );
495 
496  return scope;
497 }
498 
500 {
501  // save variable to settings
502  QSettings settings;
503 
504  QList< QVariant > customVariableVariants = settings.value( QString( "/variables/values" ) ).toList();
505  QList< QVariant > customVariableNames = settings.value( QString( "/variables/names" ) ).toList();
506 
507  customVariableVariants << value;
508  customVariableNames << name;
509 
510  settings.setValue( QString( "/variables/names" ), customVariableNames );
511  settings.setValue( QString( "/variables/values" ), customVariableVariants );
512 }
513 
515 {
516  QSettings settings;
517 
518  QList< QVariant > customVariableVariants;
519  QList< QVariant > customVariableNames;
520 
522  for ( ; it != variables.constEnd(); ++it )
523  {
524  customVariableNames << it.key();
525  customVariableVariants << it.value();
526  }
527 
528  settings.setValue( QString( "/variables/names" ), customVariableNames );
529  settings.setValue( QString( "/variables/values" ), customVariableVariants );
530 }
531 
533 
534 class GetNamedProjectColor : public QgsScopedExpressionFunction
535 {
536  public:
537  GetNamedProjectColor()
538  : QgsScopedExpressionFunction( "project_color", 1, "Color" )
539  {
540  //build up color list from project. Do this in advance for speed
541  QStringList colorStrings = QgsProject::instance()->readListEntry( "Palette", "/Colors" );
542  QStringList colorLabels = QgsProject::instance()->readListEntry( "Palette", "/Labels" );
543 
544  //generate list from custom colors
545  int colorIndex = 0;
546  for ( QStringList::iterator it = colorStrings.begin();
547  it != colorStrings.end(); ++it )
548  {
550  QString label;
551  if ( colorLabels.length() > colorIndex )
552  {
553  label = colorLabels.at( colorIndex );
554  }
555 
556  mColors.insert( label.toLower(), color );
557  colorIndex++;
558  }
559  }
560 
561  virtual QVariant func( const QVariantList& values, const QgsExpressionContext*, QgsExpression* ) override
562  {
563  QString colorName = values.at( 0 ).toString().toLower();
564  if ( mColors.contains( colorName ) )
565  {
566  return QString( "%1,%2,%3" ).arg( mColors.value( colorName ).red() ).arg( mColors.value( colorName ).green() ).arg( mColors.value( colorName ).blue() );
567  }
568  else
569  return QVariant();
570  }
571 
572  QgsScopedExpressionFunction* clone() const override
573  {
574  return new GetNamedProjectColor();
575  }
576 
577  private:
578 
579  QHash< QString, QColor > mColors;
580 
581 };
582 
584 
586 {
587  QgsProject* project = QgsProject::instance();
588 
590 
591  //add variables defined in project file
592  QStringList variableNames = project->readListEntry( "Variables", "/variableNames" );
593  QStringList variableValues = project->readListEntry( "Variables", "/variableValues" );
594 
595  int varIndex = 0;
596  Q_FOREACH ( const QString& variableName, variableNames )
597  {
598  if ( varIndex >= variableValues.length() )
599  {
600  break;
601  }
602 
603  QString varValueString = variableValues.at( varIndex );
604  varIndex++;
605  scope->setVariable( variableName, varValueString );
606  }
607 
608  //add other known project variables
609  scope->addVariable( QgsExpressionContextScope::StaticVariable( "project_title", project->title(), true ) );
610  scope->addVariable( QgsExpressionContextScope::StaticVariable( "project_path", project->fileInfo().filePath(), true ) );
611  scope->addVariable( QgsExpressionContextScope::StaticVariable( "project_folder", project->fileInfo().dir().path(), true ) );
612  scope->addVariable( QgsExpressionContextScope::StaticVariable( "project_filename", project->fileInfo().fileName(), true ) );
613 
614  scope->addFunction( "project_color", new GetNamedProjectColor() );
615  return scope;
616 }
617 
619 {
620  QgsProject* project = QgsProject::instance();
621 
622  //write variable to project
623  QStringList variableNames = project->readListEntry( "Variables", "/variableNames" );
624  QStringList variableValues = project->readListEntry( "Variables", "/variableValues" );
625 
626  variableNames << name;
627  variableValues << value.toString();
628 
629  project->writeEntry( "Variables", "/variableNames", variableNames );
630  project->writeEntry( "Variables", "/variableValues", variableValues );
631 }
632 
634 {
635  QgsProject* project = QgsProject::instance();
636 
637  //write variable to project
638  QStringList variableNames;
639  QStringList variableValues;
640 
642  for ( ; it != variables.constEnd(); ++it )
643  {
644  variableNames << it.key();
645  variableValues << it.value();
646  }
647 
648  project->writeEntry( "Variables", "/variableNames", variableNames );
649  project->writeEntry( "Variables", "/variableValues", variableValues );
650 }
651 
653 {
655 
656  if ( !layer )
657  return scope;
658 
659  //add variables defined in layer properties
660  QStringList variableNames = layer->customProperty( "variableNames" ).toStringList();
661  QStringList variableValues = layer->customProperty( "variableValues" ).toStringList();
662 
663  int varIndex = 0;
664  Q_FOREACH ( 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  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layer_name", layer->name(), true ) );
677  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layer_id", layer->id(), true ) );
678 
679  const QgsVectorLayer* vLayer = dynamic_cast< const QgsVectorLayer* >( layer );
680  if ( vLayer )
681  {
682  scope->setFields( vLayer->fields() );
683  }
684 
685  //TODO - add functions. Possibilities include:
686  //is_selected
687  //field summary stats
688 
689  return scope;
690 }
691 
693 {
694  if ( !layer )
695  return;
696 
697  //write variable to layer
698  QStringList variableNames = layer->customProperty( "variableNames" ).toStringList();
699  QStringList variableValues = layer->customProperty( "variableValues" ).toStringList();
700 
701  variableNames << name;
702  variableValues << value.toString();
703 
704  layer->setCustomProperty( "variableNames", variableNames );
705  layer->setCustomProperty( "variableValues", variableValues );
706 }
707 
709 {
710  if ( !layer )
711  return;
712 
713  QStringList variableNames;
714  QStringList variableValues;
715 
717  for ( ; it != variables.constEnd(); ++it )
718  {
719  variableNames << it.key();
720  variableValues << it.value();
721  }
722 
723  layer->setCustomProperty( "variableNames", variableNames );
724  layer->setCustomProperty( "variableValues", variableValues );
725 }
726 
728 {
729  // IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsComposerMap::createExpressionContext()
730  // (rationale is described in QgsComposerMap::createExpressionContext() )
731 
732  QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Map Settings" ) );
733 
734  //add known map settings context variables
735  scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_id", "canvas", true ) );
736  scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_rotation", mapSettings.rotation(), true ) );
737  scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_scale", mapSettings.scale(), true ) );
738  scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_extent_width", mapSettings.visibleExtent().width(), true ) );
739  scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_extent_height", mapSettings.visibleExtent().height(), true ) );
740  QgsGeometry* centerPoint = QgsGeometry::fromPoint( mapSettings.visibleExtent().center() );
741  scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_extent_center", QVariant::fromValue( *centerPoint ), true ) );
742  delete centerPoint;
743 
744  return scope;
745 }
746 
748 {
749  if ( !symbolScope )
750  return nullptr;
751 
753 
754  double angle = 0.0;
755  const QgsMarkerSymbolV2* markerSymbol = dynamic_cast< const QgsMarkerSymbolV2* >( symbol );
756  if ( markerSymbol )
757  {
758  angle = markerSymbol->angle();
759  }
761 
762  return symbolScope;
763 }
764 
766 {
767  QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Composition" ) );
768  if ( !composition )
769  return scope;
770 
771  //add variables defined in composition properties
772  QStringList variableNames = composition->customProperty( "variableNames" ).toStringList();
773  QStringList variableValues = composition->customProperty( "variableValues" ).toStringList();
774 
775  int varIndex = 0;
776  Q_FOREACH ( const QString& variableName, variableNames )
777  {
778  if ( varIndex >= variableValues.length() )
779  {
780  break;
781  }
782 
783  QVariant varValue = variableValues.at( varIndex );
784  varIndex++;
785  scope->setVariable( variableName, varValue );
786  }
787 
788  //add known composition context variables
789  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layout_numpages", composition->numPages(), true ) );
790  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layout_pageheight", composition->paperHeight(), true ) );
791  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layout_pagewidth", composition->paperWidth(), true ) );
792  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layout_dpi", composition->printResolution(), true ) );
793 
794  return scope;
795 }
796 
798 {
799  if ( !composition )
800  return;
801 
802  //write variable to composition
803  QStringList variableNames = composition->customProperty( "variableNames" ).toStringList();
804  QStringList variableValues = composition->customProperty( "variableValues" ).toStringList();
805 
806  variableNames << name;
807  variableValues << value.toString();
808 
809  composition->setCustomProperty( "variableNames", variableNames );
810  composition->setCustomProperty( "variableValues", variableValues );
811 }
812 
814 {
815  if ( !composition )
816  return;
817 
818  QStringList variableNames;
819  QStringList variableValues;
820 
822  for ( ; it != variables.constEnd(); ++it )
823  {
824  variableNames << it.key();
825  variableValues << it.value();
826  }
827 
828  composition->setCustomProperty( "variableNames", variableNames );
829  composition->setCustomProperty( "variableValues", variableValues );
830 }
831 
833 {
835  if ( !atlas )
836  {
837  //add some dummy atlas variables. This is done so that as in certain contexts we want to show
838  //users that these variables are available even if they have no current value
839  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_pagename", QString(), true ) );
841  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_featureid", 0, true ) );
843  return scope;
844  }
845 
846  //add known atlas variables
847  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_totalfeatures", atlas->numFeatures(), true ) );
848  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_featurenumber", atlas->currentFeatureNumber() + 1, true ) );
849  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_filename", atlas->currentFilename(), true ) );
850  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_pagename", atlas->currentPageName(), true ) );
851 
852  if ( atlas->enabled() && atlas->coverageLayer() )
853  {
854  scope->setFields( atlas->coverageLayer()->fields() );
855  }
856 
857  if ( atlas->enabled() )
858  {
859  QgsFeature atlasFeature = atlas->feature();
860  scope->setFeature( atlasFeature );
861  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_feature", QVariant::fromValue( atlasFeature ), true ) );
862  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_featureid", atlasFeature.id(), true ) );
863  scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_geometry", QVariant::fromValue( *atlasFeature.constGeometry() ), true ) );
864  }
865 
866  return scope;
867 }
868 
870 {
871  QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Composer Item" ) );
872  if ( !composerItem )
873  return scope;
874 
875  //add variables defined in composer item properties
876  QStringList variableNames = composerItem->customProperty( "variableNames" ).toStringList();
877  QStringList variableValues = composerItem->customProperty( "variableValues" ).toStringList();
878 
879  int varIndex = 0;
880  Q_FOREACH ( const QString& variableName, variableNames )
881  {
882  if ( varIndex >= variableValues.length() )
883  {
884  break;
885  }
886 
887  QVariant varValue = variableValues.at( varIndex );
888  varIndex++;
889  scope->setVariable( variableName, varValue );
890  }
891 
892  //add known composer item context variables
893  scope->addVariable( QgsExpressionContextScope::StaticVariable( "item_id", composerItem->id(), true ) );
894  scope->addVariable( QgsExpressionContextScope::StaticVariable( "item_uuid", composerItem->uuid(), true ) );
895  scope->addVariable( QgsExpressionContextScope::StaticVariable( "layout_page", composerItem->page(), true ) );
896 
897  return scope;
898 }
899 
901 {
902  if ( !composerItem )
903  return;
904 
905  //write variable to composer item
906  QStringList variableNames = composerItem->customProperty( "variableNames" ).toStringList();
907  QStringList variableValues = composerItem->customProperty( "variableValues" ).toStringList();
908 
909  variableNames << name;
910  variableValues << value.toString();
911 
912  composerItem->setCustomProperty( "variableNames", variableNames );
913  composerItem->setCustomProperty( "variableValues", variableValues );
914 }
915 
917 {
918  if ( !composerItem )
919  return;
920 
921  QStringList variableNames;
922  QStringList variableValues;
923 
925  for ( ; it != variables.constEnd(); ++it )
926  {
927  variableNames << it.key();
928  variableValues << it.value();
929  }
930 
931  composerItem->setCustomProperty( "variableNames", variableNames );
932  composerItem->setCustomProperty( "variableValues", variableValues );
933 }
934 
936 {
938  scope->setFeature( feature );
939  scope->setFields( fields );
940  return QgsExpressionContext() << scope;
941 }
942 
944 {
945  QgsExpression::registerFunction( new GetNamedProjectColor() );
946 }
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").
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.
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.
bool contains(const QString &str, Qt::CaseSensitivity cs) const
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'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.
int indexOf(const T &value, int from) const
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.
int count(const T &value) const
static const QString EXPR_SYMBOL_ANGLE
Inbuilt variable name for symbol angle variable.
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.
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 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.
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
Returns the marker angle for the whole symbol.
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'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'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
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'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
virtual QVariant func(const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent) override=0
Returns result of evaluating the function.
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
T takeLast()
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object...
virtual QgsScopedExpressionFunction * clone() const =0
Returns a clone of the function.
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
T & last()
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.
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)
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.
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's associated file.
Definition: qgsproject.cpp:440
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's id (which is not necessarly unique)