|
| 1 | +/** \ingroup core |
| 2 | + * \class QgsScopedExpressionFunction |
| 3 | + * \brief Expression function for use within a QgsExpressionContextScope. This differs from a |
| 4 | + * standard QgsExpression::Function in that it requires an implemented |
| 5 | + * clone() method. |
| 6 | + * \note added in QGIS 2.12 |
| 7 | + */ |
| 8 | + |
| 9 | +class QgsScopedExpressionFunction : QgsExpression::Function |
| 10 | +{ |
| 11 | +%TypeHeaderCode |
| 12 | +#include <qgsexpressioncontext.h> |
| 13 | +%End |
| 14 | + public: |
| 15 | + QgsScopedExpressionFunction( QString fnname, |
| 16 | + int params, |
| 17 | + QString group, |
| 18 | + QString helpText = QString(), |
| 19 | + bool usesGeometry = false, |
| 20 | + QStringList referencedColumns = QStringList(), |
| 21 | + bool lazyEval = false, |
| 22 | + bool handlesNull = false ); |
| 23 | + |
| 24 | + virtual ~QgsScopedExpressionFunction(); |
| 25 | + |
| 26 | + virtual QVariant func( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent ) = 0; |
| 27 | + |
| 28 | + /** Returns a clone of the function. |
| 29 | + */ |
| 30 | + virtual QgsScopedExpressionFunction* clone() const = 0 /Factory/; |
| 31 | + |
| 32 | +}; |
| 33 | + |
| 34 | +/** \ingroup core |
| 35 | + * \class QgsExpressionContextScope |
| 36 | + * \brief Single scope for storing variables and functions for use within a QgsExpressionContext. |
| 37 | + * Examples include a project's scope, which could contain information about the current project such as |
| 38 | + * the project file's location. QgsExpressionContextScope can encapsulate both variables (static values) |
| 39 | + * and functions(which are calculated only when an expression is evaluated). |
| 40 | + * \note added in QGIS 2.12 |
| 41 | + */ |
| 42 | + |
| 43 | +class QgsExpressionContextScope |
| 44 | +{ |
| 45 | +%TypeHeaderCode |
| 46 | +#include <qgsexpressioncontext.h> |
| 47 | +%End |
| 48 | + public: |
| 49 | + |
| 50 | + /** Single variable definition for use within a QgsExpressionContextScope. |
| 51 | + */ |
| 52 | + struct StaticVariable |
| 53 | + { |
| 54 | + /** Constructor for StaticVariable. |
| 55 | + * @param name variable name (should be unique within the QgsExpressionContextScope) |
| 56 | + * @param value intial variable value |
| 57 | + * @param readOnly true if variable should not be editable by users |
| 58 | + */ |
| 59 | + StaticVariable( const QString& name = QString(), const QVariant& value = QVariant(), bool readOnly = false ); |
| 60 | + |
| 61 | + /** Variable name */ |
| 62 | + QString name; |
| 63 | + |
| 64 | + /** Variable value */ |
| 65 | + QVariant value; |
| 66 | + |
| 67 | + /** True if variable should not be editable by users */ |
| 68 | + bool readOnly; |
| 69 | + }; |
| 70 | + |
| 71 | + /** Constructor for QgsExpressionContextScope |
| 72 | + * @param name friendly display name for the context scope |
| 73 | + */ |
| 74 | + QgsExpressionContextScope( const QString& name = QString() ); |
| 75 | + |
| 76 | + QgsExpressionContextScope( const QgsExpressionContextScope& other ); |
| 77 | + |
| 78 | + ~QgsExpressionContextScope(); |
| 79 | + |
| 80 | + /** Returns the friendly display name of the context scope. |
| 81 | + */ |
| 82 | + QString name() const; |
| 83 | + |
| 84 | + /** Convenience method for setting a variable in the context scope by name and value. If a variable |
| 85 | + * with the same name is already set then its value is overwritten, otherwise a new variable is added to the scope. |
| 86 | + * @param name variable name |
| 87 | + * @param value variable value |
| 88 | + * @see addVariable() |
| 89 | + */ |
| 90 | + void setVariable( const QString& name, const QVariant& value ); |
| 91 | + |
| 92 | + /** Adds a variable into the context scope. If a variable with the same name is already set then its |
| 93 | + * value is overwritten, otherwise a new variable is added to the scope. |
| 94 | + * @param variable definition of variable to insert |
| 95 | + * @see setVariable() |
| 96 | + * @see addFunction() |
| 97 | + */ |
| 98 | + void addVariable( const QgsExpressionContextScope::StaticVariable& variable ); |
| 99 | + |
| 100 | + /** Removes a variable from the context scope, if found. |
| 101 | + * @param name name of variable to remove |
| 102 | + * @returns true if variable was removed from the scope, false if matching variable was not |
| 103 | + * found within the scope |
| 104 | + */ |
| 105 | + bool removeVariable( const QString& name ); |
| 106 | + |
| 107 | + /** Tests whether a variable with the specified name exists in the scope. |
| 108 | + * @param name variable name |
| 109 | + * @returns true if matching variable was found in the scope |
| 110 | + * @see variable() |
| 111 | + * @see hasFunction() |
| 112 | + */ |
| 113 | + bool hasVariable( const QString& name ) const; |
| 114 | + |
| 115 | + /** Retrieves a variable's value from the scope. |
| 116 | + * @param name variable name |
| 117 | + * @returns variable value, or invalid QVariant if matching variable could not be found |
| 118 | + * @see hasVariable() |
| 119 | + * @see function() |
| 120 | + */ |
| 121 | + QVariant variable( const QString& name ) const; |
| 122 | + |
| 123 | + /** Returns a list of variable names contained within the scope. |
| 124 | + */ |
| 125 | + QStringList variableNames() const; |
| 126 | + |
| 127 | + /** Tests whether the specified variable is read only and should not be editable |
| 128 | + * by users. |
| 129 | + * @param name variable name |
| 130 | + * @returns true if variable is read only |
| 131 | + */ |
| 132 | + bool isReadOnly( const QString& name ) const; |
| 133 | + |
| 134 | + /** Returns the count of variables contained within the scope. |
| 135 | + */ |
| 136 | + int variableCount() const; |
| 137 | + |
| 138 | + /** Tests whether a function with the specified name exists in the scope. |
| 139 | + * @param name function name |
| 140 | + * @returns true if matching function was found in the scope |
| 141 | + * @see function() |
| 142 | + * @see hasFunction() |
| 143 | + */ |
| 144 | + bool hasFunction( const QString &name ) const; |
| 145 | + |
| 146 | + /** Retrieves a function from the scope. |
| 147 | + * @param name function name |
| 148 | + * @returns function, or null if matching function could not be found |
| 149 | + * @see hasFunction() |
| 150 | + * @see variable() |
| 151 | + */ |
| 152 | + QgsExpression::Function* function( const QString &name ) const; |
| 153 | + |
| 154 | + /** Adds a function to the scope. |
| 155 | + * @param name function name |
| 156 | + * @param function function to insert. Ownership is transferred to the scope. |
| 157 | + * @see addVariable() |
| 158 | + */ |
| 159 | + void addFunction( const QString& name, QgsScopedExpressionFunction* function /Transfer/ ); |
| 160 | + |
| 161 | + /** Convenience function for setting a feature for the scope. Any existing |
| 162 | + * feature set by the scope will be overwritten. |
| 163 | + * @param feature feature for scope |
| 164 | + */ |
| 165 | + void setFeature( const QgsFeature& feature ); |
| 166 | + |
| 167 | + /** Convenience function for setting a fields for the scope. Any existing |
| 168 | + * fields set by the scope will be overwritten. |
| 169 | + * @param fields fields for scope |
| 170 | + */ |
| 171 | + void setFields( const QgsFields& fields ); |
| 172 | +}; |
| 173 | + |
| 174 | + |
| 175 | +/** \ingroup core |
| 176 | + * \class QgsExpressionContext |
| 177 | + * \brief Expression contexts are used to encapsulate the parameters around which a QgsExpression should |
| 178 | + * be evaluated. QgsExpressions can then utilise the information stored within a context to contextualise |
| 179 | + * their evaluated result. A QgsExpressionContext consists of a stack of QgsExpressionContextScope objects, |
| 180 | + * where scopes added later to the stack will override conflicting variables and functions from scopes |
| 181 | + * lower in the stack. |
| 182 | + * \note added in QGIS 2.12 |
| 183 | + */ |
| 184 | + |
| 185 | +class QgsExpressionContext |
| 186 | +{ |
| 187 | +%TypeHeaderCode |
| 188 | +#include <qgsexpressioncontext.h> |
| 189 | +%End |
| 190 | + public: |
| 191 | + |
| 192 | + QgsExpressionContext( ); |
| 193 | + QgsExpressionContext( const QgsExpressionContext& other ); |
| 194 | + |
| 195 | + ~QgsExpressionContext(); |
| 196 | + |
| 197 | + /** Check whether a variable is specified by any scope within the context. |
| 198 | + * @param name variable name |
| 199 | + * @returns true if variable is set |
| 200 | + * @see variable() |
| 201 | + * @see variableNames() |
| 202 | + */ |
| 203 | + bool hasVariable( const QString& name ) const; |
| 204 | + |
| 205 | + /** Fetches a matching variable from the context. The variable will be fetched |
| 206 | + * from the last scope contained within the context which has a matching |
| 207 | + * variable set. |
| 208 | + * @param name variable name |
| 209 | + * @returns variable value if matching variable exists in the context, otherwise an invalid QVariant |
| 210 | + * @see hasVariable() |
| 211 | + * @see variableNames() |
| 212 | + */ |
| 213 | + QVariant variable( const QString& name ) const; |
| 214 | + |
| 215 | + /** Returns the currently active scope from the context for a specified variable name. |
| 216 | + * As scopes later in the stack override earlier contexts, this will be the last matching |
| 217 | + * scope which contains a matching variable. |
| 218 | + * @param name variable name |
| 219 | + * @returns matching scope containing variable, or null if none found |
| 220 | + */ |
| 221 | + QgsExpressionContextScope* activeScopeForVariable( const QString& name ); |
| 222 | + |
| 223 | + /** Returns the currently active scope from the context for a specified variable name. |
| 224 | + * As scopes later in the stack override earlier contexts, this will be the last matching |
| 225 | + * scope which contains a matching variable. |
| 226 | + * @param name variable name |
| 227 | + * @returns matching scope containing variable, or null if none found |
| 228 | + */ |
| 229 | + //const QgsExpressionContextScope* activeScopeForVariable( const QString& name ) const; |
| 230 | + |
| 231 | + /** Returns the scope at the specified index within the context. |
| 232 | + * @param index index of scope |
| 233 | + * @returns matching scope, or null if none found |
| 234 | + * @see lastScope() |
| 235 | + */ |
| 236 | + QgsExpressionContextScope* scope( int index ); |
| 237 | + |
| 238 | + /** Returns the last scope added to the context. |
| 239 | + * @see scope() |
| 240 | + */ |
| 241 | + QgsExpressionContextScope* lastScope(); |
| 242 | + |
| 243 | + /** Returns a list of scopes contained within the stack. |
| 244 | + * @returns list of pointers to scopes |
| 245 | + */ |
| 246 | + QList< QgsExpressionContextScope* > scopes(); |
| 247 | + |
| 248 | + /** Returns the index of the specified scope if it exists within the context. |
| 249 | + * @param scope scope to find |
| 250 | + * @returns index of scope, or -1 if scope was not found within the context. |
| 251 | + */ |
| 252 | + int indexOfScope( QgsExpressionContextScope* scope ) const; |
| 253 | + |
| 254 | + /** Returns a list of variables names set by all scopes in the context. |
| 255 | + * @returns list of unique variable names |
| 256 | + * @see hasVariable |
| 257 | + * @see variable |
| 258 | + */ |
| 259 | + QStringList variableNames() const; |
| 260 | + |
| 261 | + /** Returns whether a variable is read only, and should not be modifiable by users. |
| 262 | + * @param name variable name |
| 263 | + * @returns true if variable is read only. Read only status will be taken from last |
| 264 | + * matching scope which contains a matching variable. |
| 265 | + */ |
| 266 | + bool isReadOnly( const QString& name ) const; |
| 267 | + |
| 268 | + /** Checks whether a specified function is contained in the context. |
| 269 | + * @param name function name |
| 270 | + * @returns true if context provides a matching function |
| 271 | + * @see function |
| 272 | + */ |
| 273 | + bool hasFunction( const QString& name ) const; |
| 274 | + |
| 275 | + /** Fetches a matching function from the context. The function will be fetched |
| 276 | + * from the last scope contained within the context which has a matching |
| 277 | + * function set. |
| 278 | + * @param name function name |
| 279 | + * @returns function if contained by the context, otherwise null. |
| 280 | + * @see hasFunction |
| 281 | + */ |
| 282 | + QgsExpression::Function* function( const QString& name ) const; |
| 283 | + |
| 284 | + /** Returns the number of scopes contained in the context. |
| 285 | + */ |
| 286 | + int scopeCount() const; |
| 287 | + |
| 288 | + /** Appends a scope to the end of the context. This scope will override |
| 289 | + * any matching variables or functions provided by existing scopes within the |
| 290 | + * context. Ownership of the scope is transferred to the stack. |
| 291 | + * @param scope expression context to append to context |
| 292 | + */ |
| 293 | + void appendScope( QgsExpressionContextScope* scope /Transfer/ ); |
| 294 | + |
| 295 | + /** Appends a scope to the end of the context. This scope will override |
| 296 | + * any matching variables or functions provided by existing scopes within the |
| 297 | + * context. Ownership of the scope is transferred to the stack. |
| 298 | + */ |
| 299 | + QgsExpressionContext& operator<< ( QgsExpressionContextScope* scope /Transfer/ ); |
| 300 | + |
| 301 | + /** Convenience function for setting a feature for the context. The feature |
| 302 | + * will be set within the last scope of the context, so will override any |
| 303 | + * existing features within the context. |
| 304 | + * @param feature feature for context |
| 305 | + */ |
| 306 | + void setFeature( const QgsFeature& feature ); |
| 307 | + |
| 308 | + /** Convenience function for setting a fields for the context. The fields |
| 309 | + * will be set within the last scope of the context, so will override any |
| 310 | + * existing fields within the context. |
| 311 | + * @param fields fields for context |
| 312 | + */ |
| 313 | + void setFields( const QgsFields& fields ); |
| 314 | + |
| 315 | +}; |
| 316 | + |
| 317 | +/** \ingroup core |
| 318 | + * \class QgsExpressionContextUtils |
| 319 | + * \brief Contains utilities for working with QgsExpressionContext objects, including methods |
| 320 | + * for creating scopes for specific uses (eg project scopes, layer scopes). |
| 321 | + * \note added in QGIS 2.12 |
| 322 | + */ |
| 323 | + |
| 324 | +class QgsExpressionContextUtils |
| 325 | +{ |
| 326 | +%TypeHeaderCode |
| 327 | +#include <qgsexpressioncontext.h> |
| 328 | +%End |
| 329 | + public: |
| 330 | + |
| 331 | + /** Creates a new scope which contains variables and functions relating to the global QGIS context. |
| 332 | + * For instance, QGIS version numbers and variables specified through QGIS options. |
| 333 | + * @see setGlobalVariable() |
| 334 | + */ |
| 335 | + static QgsExpressionContextScope* globalScope() /Factory/; |
| 336 | + |
| 337 | + /** Sets a global context variable. This variable will be contained within scopes retrieved via |
| 338 | + * globalScope(). |
| 339 | + * @param name variable name |
| 340 | + * @param value variable value |
| 341 | + * @see setGlobalVariable() |
| 342 | + * @see globalScope() |
| 343 | + */ |
| 344 | + static void setGlobalVariable( const QString& name, const QVariant& value ); |
| 345 | + |
| 346 | + /** Sets all global context variables. Existing global variables will be removed and replaced |
| 347 | + * with the variables specified. |
| 348 | + * @param variables new set of global variables |
| 349 | + * @see setGlobalVariable() |
| 350 | + * @see globalScope() |
| 351 | + */ |
| 352 | + static void setGlobalVariables( const QgsStringMap& variables ); |
| 353 | + |
| 354 | + /** Creates a new scope which contains variables and functions relating to the current QGIS project. |
| 355 | + * For instance, project path and title, and variables specified through the project properties. |
| 356 | + * @see setProjectVariable() |
| 357 | + */ |
| 358 | + static QgsExpressionContextScope* projectScope() /Factory/; |
| 359 | + |
| 360 | + /** Sets a project context variable. This variable will be contained within scopes retrieved via |
| 361 | + * projectScope(). |
| 362 | + * @param name variable name |
| 363 | + * @param value variable value |
| 364 | + * @see setProjectVariables() |
| 365 | + * @see projectScope() |
| 366 | + */ |
| 367 | + static void setProjectVariable( const QString& name, const QVariant& value ); |
| 368 | + |
| 369 | + /** Sets all project context variables. Existing project variables will be removed and replaced |
| 370 | + * with the variables specified. |
| 371 | + * @param variables new set of project variables |
| 372 | + * @see setProjectVariable() |
| 373 | + * @see projectScope() |
| 374 | + */ |
| 375 | + static void setProjectVariables( const QgsStringMap& variables ); |
| 376 | + |
| 377 | + /** Creates a new scope which contains variables and functions relating to a QgsMapLayer. |
| 378 | + * For instance, layer name, id and fields. |
| 379 | + */ |
| 380 | + static QgsExpressionContextScope* layerScope( QgsMapLayer* layer ) /Factory/; |
| 381 | + |
| 382 | + /** Helper function for creating an expression context which contains just a feature and fields |
| 383 | + * collection. Generally this method should not be used as the created context does not include |
| 384 | + * standard scopes such as the global and project scopes. |
| 385 | + */ |
| 386 | + static QgsExpressionContext createFeatureBasedContext( const QgsFeature& feature, const QgsFields& fields ); |
| 387 | + |
| 388 | + /** Registers all known core functions provided by QgsExpressionContextScope objects. |
| 389 | + */ |
| 390 | + static void registerContextFunctions(); |
| 391 | +}; |
| 392 | + |
0 commit comments