QGIS API Documentation  2.14.0-Essen
qgsexpressioncontext.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsexpressioncontext.h
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 #ifndef QGSEXPRESSIONCONTEXT_H
16 #define QGSEXPRESSIONCONTEXT_H
17 
18 #include <QVariant>
19 #include <QHash>
20 #include <QString>
21 #include <QStringList>
22 #include <QSet>
23 #include "qgsexpression.h"
24 
25 class QgsExpression;
26 class QgsMapLayer;
27 class QgsComposition;
28 class QgsComposerItem;
30 class QgsMapSettings;
31 class QgsSymbolV2;
32 
42 {
43  public:
45  int params,
46  const QString& group,
47  const QString& helpText = QString(),
48  bool usesGeometry = false,
49  const QStringList& referencedColumns = QStringList(),
50  bool lazyEval = false,
51  bool handlesNull = false,
52  bool isContextual = true )
53  : QgsExpression::Function( fnname, params, group, helpText, usesGeometry, referencedColumns, lazyEval, handlesNull, isContextual )
54  {}
55 
57 
58  virtual QVariant func( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent ) override = 0;
59 
62  virtual QgsScopedExpressionFunction* clone() const = 0;
63 
64 };
65 
66 
76 class CORE_EXPORT QgsExpressionContextScope
77 {
78  public:
79 
83  {
89  StaticVariable( const QString& name = QString(), const QVariant& value = QVariant(), bool readOnly = false ) : name( name ), value( value ), readOnly( readOnly ) {}
90 
93 
96 
98  bool readOnly;
99  };
100 
104  QgsExpressionContextScope( const QString& name = QString() );
105 
109 
110  QgsExpressionContextScope& operator=( const QgsExpressionContextScope& other );
111 
113 
116  QString name() const { return mName; }
117 
124  void setVariable( const QString& name, const QVariant& value );
125 
132  void addVariable( const QgsExpressionContextScope::StaticVariable& variable );
133 
139  bool removeVariable( const QString& name );
140 
147  bool hasVariable( const QString& name ) const;
148 
155  QVariant variable( const QString& name ) const;
156 
161  QStringList variableNames() const;
162 
168  QStringList filteredVariableNames() const;
169 
175  bool isReadOnly( const QString& name ) const;
176 
179  int variableCount() const { return mVariables.count(); }
180 
187  bool hasFunction( const QString &name ) const;
188 
196  QgsExpression::Function* function( const QString &name ) const;
197 
202  QStringList functionNames() const;
203 
209  void addFunction( const QString& name, QgsScopedExpressionFunction* function );
210 
215  void setFeature( const QgsFeature& feature );
216 
221  void setFields( const QgsFields& fields );
222 
223  private:
224  QString mName;
227 
228  bool variableNameSort( const QString &a, const QString &b );
229 };
230 
240 class CORE_EXPORT QgsExpressionContext
241 {
242  public:
243 
245 
249 
250  QgsExpressionContext& operator=( const QgsExpressionContext& other );
251 
253 
260  bool hasVariable( const QString& name ) const;
261 
270  QVariant variable( const QString& name ) const;
271 
278  bool isHighlightedVariable( const QString& name ) const;
279 
285  void setHighlightedVariables( const QStringList& variableNames );
286 
293  QgsExpressionContextScope* activeScopeForVariable( const QString& name );
294 
302  const QgsExpressionContextScope* activeScopeForVariable( const QString& name ) const;
303 
309  QgsExpressionContextScope* scope( int index );
310 
314  QgsExpressionContextScope* lastScope();
315 
320 
325  int indexOfScope( QgsExpressionContextScope* scope ) const;
326 
334  QStringList variableNames() const;
335 
341  QStringList filteredVariableNames() const;
342 
348  bool isReadOnly( const QString& name ) const;
349 
355  bool hasFunction( const QString& name ) const;
356 
361  QStringList functionNames() const;
362 
370  QgsExpression::Function* function( const QString& name ) const;
371 
374  int scopeCount() const;
375 
381  void appendScope( QgsExpressionContextScope* scope );
382 
386  QgsExpressionContextScope* popScope();
387 
393 
400  void setFeature( const QgsFeature& feature );
401 
405  QgsFeature feature() const;
406 
413  void setFields( const QgsFields& fields );
414 
418  QgsFields fields() const;
419 
425  void setOriginalValueVariable( const QVariant& value );
426 
428  static const QString EXPR_FIELDS;
430  static const QString EXPR_FEATURE;
441 
442  private:
443 
445  QStringList mHighlightedVariables;
446 
447 };
448 
456 class CORE_EXPORT QgsExpressionContextUtils
457 {
458  public:
459 
464  static QgsExpressionContextScope* globalScope();
465 
473  static void setGlobalVariable( const QString& name, const QVariant& value );
474 
481  static void setGlobalVariables( const QgsStringMap& variables );
482 
487  static QgsExpressionContextScope* projectScope();
488 
496  static void setProjectVariable( const QString& name, const QVariant& value );
497 
504  static void setProjectVariables( const QgsStringMap& variables );
505 
509  static QgsExpressionContextScope* layerScope( const QgsMapLayer *layer );
510 
519  static void setLayerVariable( QgsMapLayer* layer, const QString& name, const QVariant& value );
520 
528  static void setLayerVariables( QgsMapLayer* layer, const QgsStringMap& variables );
529 
533  static QgsExpressionContextScope* mapSettingsScope( const QgsMapSettings &mapSettings );
534 
541  static QgsExpressionContextScope* updateSymbolScope( const QgsSymbolV2* symbol, QgsExpressionContextScope* symbolScope = nullptr );
542 
547  static QgsExpressionContextScope* compositionScope( const QgsComposition *composition );
548 
557  static void setCompositionVariable( QgsComposition* composition, const QString& name, const QVariant& value );
558 
566  static void setCompositionVariables( QgsComposition* composition, const QgsStringMap& variables );
567 
572  static QgsExpressionContextScope* atlasScope( const QgsAtlasComposition* atlas );
573 
578  static QgsExpressionContextScope* composerItemScope( const QgsComposerItem *composerItem );
579 
588  static void setComposerItemVariable( QgsComposerItem* composerItem, const QString& name, const QVariant& value );
589 
597  static void setComposerItemVariables( QgsComposerItem* composerItem, const QgsStringMap& variables );
598 
603  static QgsExpressionContext createFeatureBasedContext( const QgsFeature& feature, const QgsFields& fields );
604 
607  static void registerContextFunctions();
608 
609 };
610 
611 #endif // QGSEXPRESSIONCONTEXT_H
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").
Single variable definition for use within a QgsExpressionContextScope.
static unsigned index
Base class for all map layer types.
Definition: qgsmaplayer.h:49
A abstract base class for defining QgsExpression functions.
QList< QgsExpressionContextScope * > scopes()
Returns a list of scopes contained within the stack.
A item that forms part of a map composition.
QgsScopedExpressionFunction(const QString &fnname, int params, const QString &group, const QString &helpText=QString(), bool usesGeometry=false, const QStringList &referencedColumns=QStringList(), bool lazyEval=false, bool handlesNull=false, bool isContextual=true)
Container of fields for a vector layer.
Definition: qgsfield.h:187
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
The QgsMapSettings class contains configuration for rendering of the map.
QString name() const
Returns the friendly display name of the context scope.
static const QString EXPR_SYMBOL_ANGLE
Inbuilt variable name for symbol angle variable.
virtual Q_DECL_DEPRECATED QVariant func(const QVariantList &, const QgsFeature *, QgsExpression *)
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
bool readOnly
True if variable should not be editable by users.
static const QString EXPR_SYMBOL_COLOR
Inbuilt variable name for symbol color variable.
std::ostream & operator<<(std::ostream &os, const QgsCoordinateReferenceSystem &r)
Output stream operator.
static const QString EXPR_FIELDS
Inbuilt variable name for fields storage.
Single scope for storing variables and functions for use within a QgsExpressionContext.
Graphics scene for map printing.
Contains utilities for working with QgsExpressionContext objects, including methods for creating scop...
static const QString EXPR_FEATURE
Inbuilt variable name for feature storage.
StaticVariable(const QString &name=QString(), const QVariant &value=QVariant(), bool readOnly=false)
Constructor for StaticVariable.
Class used to render an Atlas, iterating over geometry features.
int variableCount() const
Returns the count of variables contained within the scope.
static const QString EXPR_GEOMETRY_PART_NUM
Inbuilt variable name for geometry part number variable.
static const QString EXPR_GEOMETRY_PART_COUNT
Inbuilt variable name for geometry part count variable.
Expression function for use within a QgsExpressionContextScope.