/usr/include/qgis/qgsexpressioncontext.h is in libqgis-dev 2.18.17+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 | /***************************************************************************
qgsexpressioncontext.h
----------------------
Date : April 2015
Copyright : (C) 2015 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSEXPRESSIONCONTEXT_H
#define QGSEXPRESSIONCONTEXT_H
#include <QVariant>
#include <QHash>
#include <QString>
#include <QStringList>
#include <QSet>
#include "qgsexpression.h"
class QgsExpression;
class QgsMapLayer;
class QgsComposition;
class QgsComposerItem;
class QgsAtlasComposition;
class QgsMapSettings;
class QgsSymbolV2;
/** \ingroup core
* \class QgsScopedExpressionFunction
* \brief Expression function for use within a QgsExpressionContextScope. This differs from a
* standard QgsExpression::Function in that it requires an implemented
* clone() method.
* \note added in QGIS 2.12
*/
class CORE_EXPORT QgsScopedExpressionFunction : public QgsExpression::Function
{
public:
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 )
: QgsExpression::Function( fnname, params, group, helpText, usesGeometry, referencedColumns, lazyEval, handlesNull, isContextual )
{}
virtual ~QgsScopedExpressionFunction() {}
virtual QVariant func( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent ) override = 0;
/** Returns a clone of the function.
*/
virtual QgsScopedExpressionFunction* clone() const = 0;
};
/** \ingroup core
* \class QgsExpressionContextScope
* \brief Single scope for storing variables and functions for use within a QgsExpressionContext.
* Examples include a project's scope, which could contain information about the current project such as
* the project file's location. QgsExpressionContextScope can encapsulate both variables (static values)
* and functions(which are calculated only when an expression is evaluated).
*
* See QgsExpressionContextUtils for helper methods for working with QgsExpressionContextScope objects.
*
* \note added in QGIS 2.12
*/
class CORE_EXPORT QgsExpressionContextScope
{
public:
/** Single variable definition for use within a QgsExpressionContextScope.
*/
struct StaticVariable
{
/** Constructor for StaticVariable.
* @param name variable name (should be unique within the QgsExpressionContextScope)
* @param value intial variable value
* @param readOnly true if variable should not be editable by users
*/
StaticVariable( const QString& name = QString(), const QVariant& value = QVariant(), bool readOnly = false )
: name( name )
, value( value )
, readOnly( readOnly )
{}
/** Variable name */
QString name;
/** Variable value */
QVariant value;
/** True if variable should not be editable by users */
bool readOnly;
};
/** Constructor for QgsExpressionContextScope
* @param name friendly display name for the context scope
*/
QgsExpressionContextScope( const QString& name = QString() );
/** Copy constructor
*/
QgsExpressionContextScope( const QgsExpressionContextScope& other );
QgsExpressionContextScope& operator=( const QgsExpressionContextScope& other );
~QgsExpressionContextScope();
/** Returns the friendly display name of the context scope.
*/
QString name() const { return mName; }
/** Convenience method for setting a variable in the context scope by name and value. If a variable
* with the same name is already set then its value is overwritten, otherwise a new variable is added to the scope.
* @param name variable name
* @param value variable value
* @see addVariable()
*/
void setVariable( const QString& name, const QVariant& value );
/** Adds a variable into the context scope. If a variable with the same name is already set then its
* value is overwritten, otherwise a new variable is added to the scope.
* @param variable definition of variable to insert
* @see setVariable()
* @see addFunction()
*/
void addVariable( const QgsExpressionContextScope::StaticVariable& variable );
/** Removes a variable from the context scope, if found.
* @param name name of variable to remove
* @returns true if variable was removed from the scope, false if matching variable was not
* found within the scope
*/
bool removeVariable( const QString& name );
/** Tests whether a variable with the specified name exists in the scope.
* @param name variable name
* @returns true if matching variable was found in the scope
* @see variable()
* @see hasFunction()
*/
bool hasVariable( const QString& name ) const;
/** Retrieves a variable's value from the scope.
* @param name variable name
* @returns variable value, or invalid QVariant if matching variable could not be found
* @see hasVariable()
* @see function()
*/
QVariant variable( const QString& name ) const;
/** Returns a list of variable names contained within the scope.
* @see functionNames()
* @see filteredVariableNames()
*/
QStringList variableNames() const;
/** Returns a fitlered and sorted list of variable names contained within the scope.
* Hidden variable names will be excluded, and the list will be sorted so that
* read only variables are listed first.
* @see variableNames()
*/
QStringList filteredVariableNames() const;
/** Tests whether the specified variable is read only and should not be editable
* by users.
* @param name variable name
* @returns true if variable is read only
*/
bool isReadOnly( const QString& name ) const;
/** Returns the count of variables contained within the scope.
*/
int variableCount() const { return mVariables.count(); }
/** Tests whether a function with the specified name exists in the scope.
* @param name function name
* @returns true if matching function was found in the scope
* @see function()
* @see hasFunction()
*/
bool hasFunction( const QString &name ) const;
/** Retrieves a function from the scope.
* @param name function name
* @returns function, or null if matching function could not be found
* @see hasFunction()
* @see functionNames()
* @see variable()
*/
QgsExpression::Function* function( const QString &name ) const;
/** Retrieves a list of names of functions contained in the scope.
* @see function()
* @see variableNames()
*/
QStringList functionNames() const;
/** Adds a function to the scope.
* @param name function name
* @param function function to insert. Ownership is transferred to the scope.
* @see addVariable()
*/
void addFunction( const QString& name, QgsScopedExpressionFunction* function );
/** Convenience function for setting a feature for the scope. Any existing
* feature set by the scope will be overwritten.
* @param feature feature for scope
*/
void setFeature( const QgsFeature& feature );
/** Convenience function for setting a fields for the scope. Any existing
* fields set by the scope will be overwritten.
* @param fields fields for scope
*/
void setFields( const QgsFields& fields );
private:
QString mName;
QHash<QString, StaticVariable> mVariables;
QHash<QString, QgsScopedExpressionFunction* > mFunctions;
bool variableNameSort( const QString &a, const QString &b );
};
/** \ingroup core
* \class QgsExpressionContext
* \brief Expression contexts are used to encapsulate the parameters around which a QgsExpression should
* be evaluated. QgsExpressions can then utilise the information stored within a context to contextualise
* their evaluated result. A QgsExpressionContext consists of a stack of QgsExpressionContextScope objects,
* where scopes added later to the stack will override conflicting variables and functions from scopes
* lower in the stack.
*
* See QgsExpressionContextUtils for helper methods for working with QgsExpressionContext objects.
*
* \note added in QGIS 2.12
*/
class CORE_EXPORT QgsExpressionContext
{
public:
QgsExpressionContext() {}
/** Copy constructor
*/
QgsExpressionContext( const QgsExpressionContext& other );
QgsExpressionContext& operator=( const QgsExpressionContext& other );
~QgsExpressionContext();
/** Check whether a variable is specified by any scope within the context.
* @param name variable name
* @returns true if variable is set
* @see variable()
* @see variableNames()
*/
bool hasVariable( const QString& name ) const;
/** Fetches a matching variable from the context. The variable will be fetched
* from the last scope contained within the context which has a matching
* variable set.
* @param name variable name
* @returns variable value if matching variable exists in the context, otherwise an invalid QVariant
* @see hasVariable()
* @see variableNames()
*/
QVariant variable( const QString& name ) const;
/** Returns true if the specified variable name is intended to be highlighted to the
* user. This is used by the expression builder to more prominently display the
* variable.
* @param name variable name
* @see setHighlightedVariables()
*/
bool isHighlightedVariable( const QString& name ) const;
/** Sets the list of variable names within the context intended to be highlighted to the user. This
* is used by the expression builder to more prominently display these variables.
* @param variableNames variable names to highlight
* @see isHighlightedVariable()
*/
void setHighlightedVariables( const QStringList& variableNames );
/** Returns the currently active scope from the context for a specified variable name.
* As scopes later in the stack override earlier contexts, this will be the last matching
* scope which contains a matching variable.
* @param name variable name
* @returns matching scope containing variable, or null if none found
*/
QgsExpressionContextScope* activeScopeForVariable( const QString& name );
/** Returns the currently active scope from the context for a specified variable name.
* As scopes later in the stack override earlier contexts, this will be the last matching
* scope which contains a matching variable.
* @param name variable name
* @returns matching scope containing variable, or null if none found
* @note not available in python bindings
*/
const QgsExpressionContextScope* activeScopeForVariable( const QString& name ) const;
/** Returns the scope at the specified index within the context.
* @param index index of scope
* @returns matching scope, or null if none found
* @see lastScope()
*/
QgsExpressionContextScope* scope( int index );
/** Returns the last scope added to the context.
* @see scope()
*/
QgsExpressionContextScope* lastScope();
/** Returns a list of scopes contained within the stack.
* @returns list of pointers to scopes
*/
QList< QgsExpressionContextScope* > scopes() { return mStack; }
/** Returns the index of the specified scope if it exists within the context.
* @param scope scope to find
* @returns index of scope, or -1 if scope was not found within the context.
*/
int indexOfScope( QgsExpressionContextScope* scope ) const;
/** Returns the index of the first scope with a matching name within the context.
* @param scopeName name of scope to find
* @returns index of scope, or -1 if scope was not found within the context.
* @note added in QGIS 2.18
*/
int indexOfScope( const QString& scopeName ) const;
/** Returns a list of variables names set by all scopes in the context.
* @returns list of unique variable names
* @see filteredVariableNames
* @see functionNames
* @see hasVariable
* @see variable
*/
QStringList variableNames() const;
/** Returns a filtered list of variables names set by all scopes in the context. The included
* variables are those which should be seen by users.
* @returns filtered list of unique variable names
* @see variableNames
*/
QStringList filteredVariableNames() const;
/** Returns whether a variable is read only, and should not be modifiable by users.
* @param name variable name
* @returns true if variable is read only. Read only status will be taken from last
* matching scope which contains a matching variable.
*/
bool isReadOnly( const QString& name ) const;
/** Checks whether a specified function is contained in the context.
* @param name function name
* @returns true if context provides a matching function
* @see function
*/
bool hasFunction( const QString& name ) const;
/** Retrieves a list of function names contained in the context.
* @see function()
* @see variableNames()
*/
QStringList functionNames() const;
/** Fetches a matching function from the context. The function will be fetched
* from the last scope contained within the context which has a matching
* function set.
* @param name function name
* @returns function if contained by the context, otherwise null.
* @see hasFunction
*/
QgsExpression::Function* function( const QString& name ) const;
/** Returns the number of scopes contained in the context.
*/
int scopeCount() const;
/** Appends a scope to the end of the context. This scope will override
* any matching variables or functions provided by existing scopes within the
* context. Ownership of the scope is transferred to the stack.
* @param scope expression context to append to context
*/
void appendScope( QgsExpressionContextScope* scope );
/**
* Removes the last scope from the expression context and return it.
*/
QgsExpressionContextScope* popScope();
/** Appends a scope to the end of the context. This scope will override
* any matching variables or functions provided by existing scopes within the
* context. Ownership of the scope is transferred to the stack.
*/
QgsExpressionContext& operator<< ( QgsExpressionContextScope* scope );
/** Convenience function for setting a feature for the context. The feature
* will be set within the last scope of the context, so will override any
* existing features within the context.
* @param feature feature for context
* @see feature()
*/
void setFeature( const QgsFeature& feature );
/** Convenience function for retrieving the feature for the context, if set.
* @see setFeature
*/
QgsFeature feature() const;
/** Convenience function for setting a fields for the context. The fields
* will be set within the last scope of the context, so will override any
* existing fields within the context.
* @param fields fields for context
* @see fields()
*/
void setFields( const QgsFields& fields );
/** Convenience function for retrieving the fields for the context, if set.
* @see setFields
*/
QgsFields fields() const;
/** Sets the original value variable value for the context.
* @param value value for original value variable. This usually represents the an original widget
* value before any data defined overrides have been applied.
* @note added in QGIS 2.12
*/
void setOriginalValueVariable( const QVariant& value );
/** Sets a value to cache within the expression context. This can be used to cache the results
* of expensive expression sub-calculations, to speed up future evaluations using the same
* expression context.
* @param key unique key for retrieving cached value
* @param value value to cache
* @see hasCachedValue()
* @see cachedValue()
* @see clearCachedValues()
* @note added in QGIS 2.16
*/
void setCachedValue( const QString& key, const QVariant& value ) const;
/** Returns true if the expression context contains a cached value with a matching key.
* @param key unique key used to store cached value
* @see setCachedValue()
* @see cachedValue()
* @see clearCachedValues()
* @note added in QGIS 2.16
*/
bool hasCachedValue( const QString& key ) const;
/** Returns the matching cached value, if set. This can be used to retrieve the previously stored results
* of an expensive expression sub-calculation.
* @param key unique key used to store cached value
* @returns matching cached value, or invalid QVariant if not set
* @see setCachedValue()
* @see hasCachedValue()
* @see clearCachedValues()
* @note added in QGIS 2.16
*/
QVariant cachedValue( const QString& key ) const;
/** Clears all cached values from the context.
* @see setCachedValue()
* @see hasCachedValue()
* @see cachedValue()
* @note added in QGIS 2.16
*/
void clearCachedValues() const;
//! Inbuilt variable name for fields storage
static const QString EXPR_FIELDS;
//! Inbuilt variable name for feature storage
static const QString EXPR_FEATURE;
//! Inbuilt variable name for value original value variable
static const QString EXPR_ORIGINAL_VALUE;
//! Inbuilt variable name for symbol color variable
static const QString EXPR_SYMBOL_COLOR;
//! Inbuilt variable name for symbol angle variable
static const QString EXPR_SYMBOL_ANGLE;
//! Inbuilt variable name for geometry part count variable
static const QString EXPR_GEOMETRY_PART_COUNT;
//! Inbuilt variable name for geometry part number variable
static const QString EXPR_GEOMETRY_PART_NUM;
//! Inbuilt variable name for point count variable
static const QString EXPR_GEOMETRY_POINT_COUNT;
//! Inbuilt variable name for point number variable
static const QString EXPR_GEOMETRY_POINT_NUM;
private:
QList< QgsExpressionContextScope* > mStack;
QStringList mHighlightedVariables;
// Cache is mutable because we want to be able to add cached values to const contexts
mutable QMap< QString, QVariant > mCachedValues;
};
/** \ingroup core
* \class QgsExpressionContextUtils
* \brief Contains utilities for working with QgsExpressionContext objects, including methods
* for creating scopes for specific uses (eg project scopes, layer scopes).
* \note added in QGIS 2.12
*/
class CORE_EXPORT QgsExpressionContextUtils
{
public:
/** Creates a new scope which contains variables and functions relating to the global QGIS context.
* For instance, QGIS version numbers and variables specified through QGIS options.
* @see setGlobalVariable()
*/
static QgsExpressionContextScope* globalScope();
/** Sets a global context variable. This variable will be contained within scopes retrieved via
* globalScope().
* @param name variable name
* @param value variable value
* @see setGlobalVariable()
* @see globalScope()
*/
static void setGlobalVariable( const QString& name, const QVariant& value );
/** Sets all global context variables. Existing global variables will be removed and replaced
* with the variables specified.
* @param variables new set of global variables
* @see setGlobalVariable()
* @see globalScope()
*/
static void setGlobalVariables( const QgsStringMap& variables );
/** Creates a new scope which contains variables and functions relating to the current QGIS project.
* For instance, project path and title, and variables specified through the project properties.
* @see setProjectVariable()
*/
static QgsExpressionContextScope* projectScope();
/** Sets a project context variable. This variable will be contained within scopes retrieved via
* projectScope().
* @param name variable name
* @param value variable value
* @see setProjectVariables()
* @see projectScope()
*/
static void setProjectVariable( const QString& name, const QVariant& value );
/** Sets all project context variables. Existing project variables will be removed and replaced
* with the variables specified.
* @param variables new set of project variables
* @see setProjectVariable()
* @see projectScope()
*/
static void setProjectVariables( const QgsStringMap& variables );
/** Creates a new scope which contains variables and functions relating to a QgsMapLayer.
* For instance, layer name, id and fields.
*/
static QgsExpressionContextScope* layerScope( const QgsMapLayer *layer );
/** Sets a layer context variable. This variable will be contained within scopes retrieved via
* layerScope().
* @param layer map layer
* @param name variable name
* @param value variable value
* @see setLayerVariables()
* @see layerScope()
*/
static void setLayerVariable( QgsMapLayer* layer, const QString& name, const QVariant& value );
/** Sets all layer context variables. Existing layer variables will be removed and replaced
* with the variables specified.
* @param layer map layer
* @param variables new set of layer variables
* @see setLayerVariable()
* @see layerScope()
*/
static void setLayerVariables( QgsMapLayer* layer, const QgsStringMap& variables );
/** Creates a new scope which contains variables and functions relating to a QgsMapSettings object.
* For instance, map scale and rotation.
*/
static QgsExpressionContextScope* mapSettingsScope( const QgsMapSettings &mapSettings );
/**
* Updates a symbol scope related to a QgsSymbolV2 to an expression context.
* @param symbol symbol to extract properties from
* @param symbolScope pointer to an existing scope to update
* @note added in QGIS 2.14
*/
static QgsExpressionContextScope* updateSymbolScope( const QgsSymbolV2* symbol, QgsExpressionContextScope* symbolScope = nullptr );
/** Creates a new scope which contains variables and functions relating to a QgsComposition.
* For instance, number of pages and page sizes.
* @param composition source composition
*/
static QgsExpressionContextScope* compositionScope( const QgsComposition *composition );
/** Sets a composition context variable. This variable will be contained within scopes retrieved via
* compositionScope().
* @param composition target composition
* @param name variable name
* @param value variable value
* @see setCompositionVariables()
* @see compositionScope()
*/
static void setCompositionVariable( QgsComposition* composition, const QString& name, const QVariant& value );
/** Sets all composition context variables. Existing composition variables will be removed and replaced
* with the variables specified.
* @param composition target composition
* @param variables new set of layer variables
* @see setCompositionVariable()
* @see compositionScope()
*/
static void setCompositionVariables( QgsComposition* composition, const QgsStringMap& variables );
/** Creates a new scope which contains variables and functions relating to a QgsAtlasComposition.
* For instance, current page name and number.
* @param atlas source atlas. If null, a set of default atlas variables will be added to the scope.
*/
static QgsExpressionContextScope* atlasScope( const QgsAtlasComposition* atlas );
/** Creates a new scope which contains variables and functions relating to a QgsComposerItem.
* For instance, item size and position.
* @param composerItem source composer item
*/
static QgsExpressionContextScope* composerItemScope( const QgsComposerItem *composerItem );
/** Sets a composer item context variable. This variable will be contained within scopes retrieved via
* composerItemScope().
* @param composerItem target composer item
* @param name variable name
* @param value variable value
* @see setComposerItemVariables()
* @see composerItemScope()
*/
static void setComposerItemVariable( QgsComposerItem* composerItem, const QString& name, const QVariant& value );
/** Sets all composition context variables. Existing compositoin variables will be removed and replaced
* with the variables specified.
* @param composerItem target composer item
* @param variables new set of layer variables
* @see setComposerItemVariable()
* @see composerItemScope()
*/
static void setComposerItemVariables( QgsComposerItem* composerItem, const QgsStringMap& variables );
/** Helper function for creating an expression context which contains just a feature and fields
* collection. Generally this method should not be used as the created context does not include
* standard scopes such as the global and project scopes.
*/
static QgsExpressionContext createFeatureBasedContext( const QgsFeature& feature, const QgsFields& fields );
/** Registers all known core functions provided by QgsExpressionContextScope objects.
*/
static void registerContextFunctions();
};
#endif // QGSEXPRESSIONCONTEXT_H
|