QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
QgsFeatureRequest::NoGeometry
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Definition: qgsfeaturerequest.h:115
QgsVectorLayer::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Definition: qgsvectorlayer.cpp:1052
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
qgscolorrampimpl.h
QgsGradientColorRamp
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
Definition: qgscolorrampimpl.h:136
QgsGradientColorRamp::value
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
Definition: qgscolorrampimpl.cpp:300
QgsFeatureRequest::setExpressionContext
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
Definition: qgsfeaturerequest.cpp:187
QgsExpression::setEvalErrorString
void setEvalErrorString(const QString &str)
Sets evaluation error (used internally by evaluation functions)
Definition: qgsexpression.cpp:388
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:83
QgsFeatureRequest::NoFlags
@ NoFlags
Definition: qgsfeaturerequest.h:114
qgsproviderregistry.h
qgsexpressionutils.h
qgsexpressionnode.h
QgsProviderRegistry::decodeUri
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
Definition: qgsproviderregistry.cpp:555
QgsExpression::evaluate
QVariant evaluate()
Evaluate the feature and return the result.
Definition: qgsexpression.cpp:350
qgsvectorlayer.h
QgsExpressionUtils::determineResultType
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.
Definition: qgsexpressionutils.cpp:75
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:399
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsFeatureRequest::setLimit
QgsFeatureRequest & setLimit(long long limit)
Set the maximum number of features to request.
Definition: qgsfeaturerequest.cpp:216
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsExpression::needsGeometry
bool needsGeometry() const
Returns true if the expression uses feature geometry for some computation.
Definition: qgsexpression.cpp:270
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:289
QgsProviderRegistry::instance
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Definition: qgsproviderregistry.cpp:73
QgsFeatureRequest::setFlags
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
Definition: qgsfeaturerequest.cpp:222
QgsExpressionContext::setFeature
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Definition: qgsexpressioncontext.cpp:525