QGIS API Documentation  2.12.0-Lyon
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 
41 {
42  public:
44  int params,
45  const QString& group,
46  const QString& helpText = QString(),
47  bool usesGeometry = false,
48  const QStringList& referencedColumns = QStringList(),
49  bool lazyEval = false,
50  bool handlesNull = false,
51  bool isContextual = true )
52  : QgsExpression::Function( fnname, params, group, helpText, usesGeometry, referencedColumns, lazyEval, handlesNull, isContextual )
53  {}
54 
56 
57  virtual QVariant func( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent ) override = 0;
58 
61  virtual QgsScopedExpressionFunction* clone() const = 0;
62 
63 };
64 
65 
75 class CORE_EXPORT QgsExpressionContextScope
76 {
77  public:
78 
82  {
88  StaticVariable( const QString& name = QString(), const QVariant& value = QVariant(), bool readOnly = false ) : name( name ), value( value ), readOnly( readOnly ) {}
89 
92 
95 
97  bool readOnly;
98  };
99 
103  QgsExpressionContextScope( const QString& name = QString() );
104 
108 
109  QgsExpressionContextScope& operator=( const QgsExpressionContextScope& other );
110 
112 
115  QString name() const { return mName; }
116 
123  void setVariable( const QString& name, const QVariant& value );
124 
131  void addVariable( const QgsExpressionContextScope::StaticVariable& variable );
132 
138  bool removeVariable( const QString& name );
139 
146  bool hasVariable( const QString& name ) const;
147 
154  QVariant variable( const QString& name ) const;
155 
160  QStringList variableNames() const;
161 
167  QStringList filteredVariableNames() const;
168 
174  bool isReadOnly( const QString& name ) const;
175 
178  int variableCount() const { return mVariables.count(); }
179 
186  bool hasFunction( const QString &name ) const;
187 
195  QgsExpression::Function* function( const QString &name ) const;
196 
201  QStringList functionNames() const;
202 
208  void addFunction( const QString& name, QgsScopedExpressionFunction* function );
209 
214  void setFeature( const QgsFeature& feature );
215 
220  void setFields( const QgsFields& fields );
221 
222  private:
223  QString mName;
226 
227  bool variableNameSort( const QString &a, const QString &b );
228 };
229 
239 class CORE_EXPORT QgsExpressionContext
240 {
241  public:
242 
244 
248 
249  QgsExpressionContext& operator=( const QgsExpressionContext& other );
250 
252 
259  bool hasVariable( const QString& name ) const;
260 
269  QVariant variable( const QString& name ) const;
270 
277  bool isHighlightedVariable( const QString& name ) const;
278 
284  void setHighlightedVariables( const QStringList& variableNames );
285 
292  QgsExpressionContextScope* activeScopeForVariable( const QString& name );
293 
300  const QgsExpressionContextScope* activeScopeForVariable( const QString& name ) const;
301 
307  QgsExpressionContextScope* scope( int index );
308 
312  QgsExpressionContextScope* lastScope();
313 
318 
323  int indexOfScope( QgsExpressionContextScope* scope ) const;
324 
332  QStringList variableNames() const;
333 
339  QStringList filteredVariableNames() const;
340 
346  bool isReadOnly( const QString& name ) const;
347 
353  bool hasFunction( const QString& name ) const;
354 
359  QStringList functionNames() const;
360 
368  QgsExpression::Function* function( const QString& name ) const;
369 
372  int scopeCount() const;
373 
379  void appendScope( QgsExpressionContextScope* scope );
380 
386 
393  void setFeature( const QgsFeature& feature );
394 
398  QgsFeature feature() const;
399 
406  void setFields( const QgsFields& fields );
407 
411  QgsFields fields() const;
412 
418  void setOriginalValueVariable( const QVariant& value );
419 
420  static const QString EXPR_FIELDS;
421  static const QString EXPR_FEATURE;
423 
424  private:
425 
427  QStringList mHighlightedVariables;
428 
429 };
430 
438 class CORE_EXPORT QgsExpressionContextUtils
439 {
440  public:
441 
446  static QgsExpressionContextScope* globalScope();
447 
455  static void setGlobalVariable( const QString& name, const QVariant& value );
456 
463  static void setGlobalVariables( const QgsStringMap& variables );
464 
469  static QgsExpressionContextScope* projectScope();
470 
478  static void setProjectVariable( const QString& name, const QVariant& value );
479 
486  static void setProjectVariables( const QgsStringMap& variables );
487 
491  static QgsExpressionContextScope* layerScope( const QgsMapLayer *layer );
492 
501  static void setLayerVariable( QgsMapLayer* layer, const QString& name, const QVariant& value );
502 
510  static void setLayerVariables( QgsMapLayer* layer, const QgsStringMap& variables );
511 
515  static QgsExpressionContextScope* mapSettingsScope( const QgsMapSettings &mapSettings );
516 
521  static QgsExpressionContextScope* compositionScope( const QgsComposition *composition );
522 
531  static void setCompositionVariable( QgsComposition* composition, const QString& name, const QVariant& value );
532 
540  static void setCompositionVariables( QgsComposition* composition, const QgsStringMap& variables );
541 
546  static QgsExpressionContextScope* atlasScope( const QgsAtlasComposition* atlas );
547 
552  static QgsExpressionContextScope* composerItemScope( const QgsComposerItem *composerItem );
553 
562  static void setComposerItemVariable( QgsComposerItem* composerItem, const QString& name, const QVariant& value );
563 
571  static void setComposerItemVariables( QgsComposerItem* composerItem, const QgsStringMap& variables );
572 
577  static QgsExpressionContext createFeatureBasedContext( const QgsFeature& feature, const QgsFields& fields );
578 
581  static void registerContextFunctions();
582 
583 };
584 
585 #endif // QGSEXPRESSIONCONTEXT_H
static const QString EXPR_ORIGINAL_VALUE
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:92
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:177
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:176
The QgsMapSettings class contains configuration for rendering of the map.
QString name() const
Returns the friendly display name of the context scope.
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.
std::ostream & operator<<(std::ostream &os, const QgsCoordinateReferenceSystem &r)
Output stream operator.
static const QString EXPR_FIELDS
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
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.
Expression function for use within a QgsExpressionContextScope.