17 #ifndef QGSEXPRESSIONUTILS_H
18 #define QGSEXPRESSIONUTILS_H
35 #define ENSURE_NO_EVAL_ERROR { if ( parent->hasEvalError() ) return QVariant(); }
36 #define SET_EVAL_ERROR(x) { parent->setEvalErrorString( x ); return QVariant(); }
38 #define FEAT_FROM_CONTEXT(c, f) if ( !(c) || !( c )->hasFeature() ) return QVariant(); \
39 QgsFeature f = ( c )->feature();
68 #define TVL_True QVariant( 1 )
69 #define TVL_False QVariant( 0 )
70 #define TVL_Unknown QVariant()
72 static QVariant tvl2variant( TVL v )
87 static TVL getTVLValue(
const QVariant &value,
QgsExpression *parent )
98 return geom.
isNull() ? False : True;
104 return feat.
isValid() ? True : False;
107 if ( value.type() == QVariant::Int )
108 return value.toInt() != 0 ? True : False;
111 const double x = value.toDouble( &ok );
114 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to boolean" ).arg( value.toString() ) );
121 static inline bool isIntSafe(
const QVariant &v )
123 if ( v.type() == QVariant::Int )
125 if ( v.type() == QVariant::UInt )
127 if ( v.type() == QVariant::LongLong )
129 if ( v.type() == QVariant::ULongLong )
131 if ( v.type() == QVariant::Double )
133 if ( v.type() == QVariant::String )
136 v.toString().toInt( &ok );
142 static inline bool isDoubleSafe(
const QVariant &v )
144 if ( v.type() == QVariant::Double )
146 if ( v.type() == QVariant::Int )
148 if ( v.type() == QVariant::UInt )
150 if ( v.type() == QVariant::LongLong )
152 if ( v.type() == QVariant::ULongLong )
154 if ( v.type() == QVariant::String )
157 const double val = v.toString().toDouble( &ok );
158 ok = ok && std::isfinite( val ) && !std::isnan( val );
164 static inline bool isDateTimeSafe(
const QVariant &v )
166 return v.type() == QVariant::DateTime
167 || v.type() == QVariant::Date
168 || v.type() == QVariant::Time;
171 static inline bool isIntervalSafe(
const QVariant &v )
178 if ( v.type() == QVariant::String )
185 static inline bool isNull(
const QVariant &v )
190 static inline bool isList(
const QVariant &v )
192 return v.type() == QVariant::List;
196 static QString getStringValue(
const QVariant &value,
QgsExpression * )
198 return value.toString();
208 static QByteArray getBinaryValue(
const QVariant &value,
QgsExpression *parent )
210 if ( value.type() != QVariant::ByteArray )
215 return value.toByteArray();
218 static double getDoubleValue(
const QVariant &value,
QgsExpression *parent )
221 const double x = value.toDouble( &ok );
222 if ( !ok || std::isnan( x ) || !std::isfinite( x ) )
224 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to double" ).arg( value.toString() ) );
230 static qlonglong getIntValue(
const QVariant &value,
QgsExpression *parent )
233 const qlonglong x = value.toLongLong( &ok );
240 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to int" ).arg( value.toString() ) );
245 static int getNativeIntValue(
const QVariant &value,
QgsExpression *parent )
248 const qlonglong x = value.toLongLong( &ok );
249 if ( ok && x >= std::numeric_limits<int>::min() && x <= std::numeric_limits<int>::max() )
251 return static_cast<int>( x );
255 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to native int" ).arg( value.toString() ) );
260 static QDateTime getDateTimeValue(
const QVariant &value,
QgsExpression *parent )
262 QDateTime d = value.toDateTime();
269 const QTime t = value.toTime();
272 return QDateTime( QDate( 1, 1, 1 ), t );
275 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to DateTime" ).arg( value.toString() ) );
280 static QDate getDateValue(
const QVariant &value,
QgsExpression *parent )
282 QDate d = value.toDate();
289 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to Date" ).arg( value.toString() ) );
294 static QTime getTimeValue(
const QVariant &value,
QgsExpression *parent )
296 QTime t = value.toTime();
303 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to Time" ).arg( value.toString() ) );
314 if ( inter.isValid() )
320 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to interval" ).arg( value.toString() ) );
332 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to gradient ramp" ).arg( value.toString() ) );
372 ml = project->
mapLayer( value.toString() );
381 static std::unique_ptr<QgsVectorLayerFeatureSource> getFeatureSource(
const QVariant &value,
QgsExpression *e )
383 std::unique_ptr<QgsVectorLayerFeatureSource> featureSource;
385 auto getFeatureSource = [ &value, e, &featureSource ]
397 if ( QThread::currentThread() == qApp->thread() )
400 QMetaObject::invokeMethod( qApp, getFeatureSource, Qt::BlockingQueuedConnection );
402 return featureSource;
407 return qobject_cast<QgsVectorLayer *>( getMapLayer( value, e ) );
412 return qobject_cast<QgsRasterLayer *>( getMapLayer( value, e ) );
417 return qobject_cast<QgsMeshLayer *>( getMapLayer( value, e ) );
420 static QVariantList getListValue(
const QVariant &value,
QgsExpression *parent )
422 if ( value.type() == QVariant::List || value.type() == QVariant::StringList )
424 return value.toList();
428 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to array" ).arg( value.toString() ) );
429 return QVariantList();
433 static QVariantMap getMapValue(
const QVariant &value,
QgsExpression *parent )
435 if ( value.type() == QVariant::Map )
437 return value.toMap();
441 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to map" ).arg( value.toString() ) );
442 return QVariantMap();
452 static QString toLocalizedString(
const QVariant &value )
454 if ( value.type() == QVariant::Int || value.type() == QVariant::UInt || value.type() == QVariant::LongLong || value.type() == QVariant::ULongLong )
459 if ( value.type() == QVariant::ULongLong )
461 res = QLocale().toString( value.toULongLong( &ok ) );
465 res = QLocale().toString( value.toLongLong( &ok ) );
474 return value.toString();
478 else if ( value.type() == QVariant::Double || value.type() ==
static_cast<QVariant::Type
>( QMetaType::Float ) )
481 const QString strVal = value.toString();
482 const int dotPosition = strVal.indexOf(
'.' );
483 const int precision = dotPosition > 0 ? strVal.length() - dotPosition - 1 : 0;
484 const QString res = QLocale().toString( value.toDouble( &ok ),
'f',
precision );
492 return value.toString();
497 return value.toString();
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Abstract base class for all nodes that can appear in an expression.
A set of expression-related functions.
Class for parsing and evaluation of expressions (formerly called "search strings").
void setEvalErrorString(const QString &str)
Sets evaluation error (used internally by evaluation functions)
This class wraps a request for features to a vector layer (or directly its vector data provider).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
bool isValid() const
Returns the validity of this feature.
A geometry is the spatial representation of a feature.
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.
A representation of the interval between two datetime values.
bool isValid() const
Returns true if the interval is valid.
static QgsInterval fromString(const QString &string)
Converts a string to an interval.
Base class for all map layer types.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
QList< QgsMapLayer * > mapLayersByName(const QString &layerName) const
Retrieve a list of matching registered layers by layer name.
Represents a raster layer.
Partial snapshot of vector layer's state (only the members necessary for access to features)
Represents a vector layer which manages a vector based data sets.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.