QGIS API Documentation  3.23.0-Master (dd0cd13a00)
qgsexpressionutils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsexpressionutils.cpp
3  -------------------
4  begin : May 2017
5  copyright : (C) 2017 Matthias Kuhn
6  email : [email protected]
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 "qgsexpressionutils.h"
17 #include "qgsexpressionnode.h"
18 #include "qgsvectorlayer.h"
19 #include "qgscolorrampimpl.h"
20 
22 
23 QgsExpressionUtils::TVL QgsExpressionUtils::AND[3][3] =
24 {
25  // false true unknown
26  { False, False, False }, // false
27  { False, True, Unknown }, // true
28  { False, Unknown, Unknown } // unknown
29 };
30 QgsExpressionUtils::TVL QgsExpressionUtils::OR[3][3] =
31 {
32  { False, True, Unknown }, // false
33  { True, True, True }, // true
34  { Unknown, True, Unknown } // unknown
35 };
36 
37 QgsExpressionUtils::TVL QgsExpressionUtils::NOT[3] = { True, False, Unknown };
38 
39 
40 QgsGradientColorRamp QgsExpressionUtils::getRamp( const QVariant &value, QgsExpression *parent, bool report_error )
41 {
42  if ( value.canConvert<QgsGradientColorRamp>() )
43  return value.value<QgsGradientColorRamp>();
44 
45  // If we get here then we can't convert so we just error and return invalid.
46  if ( report_error )
47  parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to gradient ramp" ).arg( value.toString() ) );
48 
49  return QgsGradientColorRamp();
50 }
51 
53 
54 std::tuple<QVariant::Type, int> QgsExpressionUtils::determineResultType( const QString &expression, const QgsVectorLayer *layer, QgsFeatureRequest request, QgsExpressionContext context, bool *foundFeatures )
55 {
56  QgsExpression exp( expression );
57  request.setFlags( ( exp.needsGeometry() ) ?
60  request.setLimit( 10 );
61  request.setExpressionContext( context );
62 
63  QVariant value;
64  QgsFeature f;
65  QgsFeatureIterator it = layer->getFeatures( request );
66  bool hasFeature = it.nextFeature( f );
67  if ( foundFeatures )
68  *foundFeatures = hasFeature;
69  while ( hasFeature )
70  {
71  context.setFeature( f );
72  const QVariant value = exp.evaluate( &context );
73  if ( !value.isNull() )
74  {
75  return std::make_tuple( value.type(), value.userType() );
76  }
77  hasFeature = it.nextFeature( f );
78  }
79  value = QVariant();
80  return std::make_tuple( value.type(), value.userType() );
81 }
82 
83 
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
static std::tuple< QVariant::Type, int > determineResultType(const QString &expression, const QgsVectorLayer *layer, QgsFeatureRequest request=QgsFeatureRequest(), QgsExpressionContext context=QgsExpressionContext(), bool *foundFeatures=nullptr)
Returns a value type and user type for a given expression.
Class for parsing and evaluation of expressions (formerly called "search strings").
void setEvalErrorString(const QString &str)
Sets evaluation error (used internally by evaluation functions)
bool needsGeometry() const
Returns true if the expression uses feature geometry for some computation.
QVariant evaluate()
Evaluate the feature and return the result.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setLimit(long long limit)
Set the maximum number of features to request.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
Represents a vector layer which manages a vector based data sets.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.