QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 #include "qgsproviderregistry.h"
21 
23 
24 QgsExpressionUtils::TVL QgsExpressionUtils::AND[3][3] =
25 {
26  // false true unknown
27  { False, False, False }, // false
28  { False, True, Unknown }, // true
29  { False, Unknown, Unknown } // unknown
30 };
31 QgsExpressionUtils::TVL QgsExpressionUtils::OR[3][3] =
32 {
33  { False, True, Unknown }, // false
34  { True, True, True }, // true
35  { Unknown, True, Unknown } // unknown
36 };
37 
38 QgsExpressionUtils::TVL QgsExpressionUtils::NOT[3] = { True, False, Unknown };
39 
40 
41 QgsGradientColorRamp QgsExpressionUtils::getRamp( const QVariant &value, QgsExpression *parent, bool report_error )
42 {
43  if ( value.canConvert<QgsGradientColorRamp>() )
44  return value.value<QgsGradientColorRamp>();
45 
46  // If we get here then we can't convert so we just error and return invalid.
47  if ( report_error )
48  parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to gradient ramp" ).arg( value.toString() ) );
49 
50  return QgsGradientColorRamp();
51 }
52 
53 QString QgsExpressionUtils::getFilePathValue( const QVariant &value, QgsExpression *parent )
54 {
55  // if it's a map layer, return the file path of that layer...
56  QString res;
57  if ( QgsMapLayer *layer = getMapLayer( value, parent ) )
58  {
59  const QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( layer->providerType(), layer->source() );
60  res = parts.value( QStringLiteral( "path" ) ).toString();
61  }
62 
63  if ( res.isEmpty() )
64  res = value.toString();
65 
66  if ( res.isEmpty() && !value.isNull() )
67  {
68  parent->setEvalErrorString( QObject::tr( "Cannot convert value to a file path" ) );
69  }
70  return res;
71 }
72 
74 
75 std::tuple<QVariant::Type, int> QgsExpressionUtils::determineResultType( const QString &expression, const QgsVectorLayer *layer, QgsFeatureRequest request, QgsExpressionContext context, bool *foundFeatures )
76 {
77  QgsExpression exp( expression );
78  request.setFlags( ( exp.needsGeometry() ) ?
81  request.setLimit( 10 );
82  request.setExpressionContext( context );
83 
84  QVariant value;
85  QgsFeature f;
86  QgsFeatureIterator it = layer->getFeatures( request );
87  bool hasFeature = it.nextFeature( f );
88  if ( foundFeatures )
89  *foundFeatures = hasFeature;
90  while ( hasFeature )
91  {
92  context.setFeature( f );
93  const QVariant value = exp.evaluate( &context );
94  if ( !value.isNull() )
95  {
96  return std::make_tuple( value.type(), value.userType() );
97  }
98  hasFeature = it.nextFeature( f );
99  }
100  value = QVariant();
101  return std::make_tuple( value.type(), value.userType() );
102 }
103 
104 
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.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
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.