17#ifndef QGSEXPRESSIONUTILS_H
18#define QGSEXPRESSIONUTILS_H
37#define ENSURE_NO_EVAL_ERROR { if ( parent->hasEvalError() ) return QVariant(); }
38#define SET_EVAL_ERROR(x) { parent->setEvalErrorString( x ); return QVariant(); }
40#define FEAT_FROM_CONTEXT(c, f) if ( !(c) || !( c )->hasFeature() ) return QVariant(); \
41 QgsFeature f = ( c )->feature();
70#define TVL_True QVariant( 1 )
71#define TVL_False QVariant( 0 )
72#define TVL_Unknown QVariant()
74 static QVariant tvl2variant( TVL v )
89 static TVL getTVLValue(
const QVariant &value,
QgsExpression *parent )
96 if ( value.userType() == QMetaType::type(
"QgsGeometry" ) )
100 return geom.
isNull() ? False : True;
102 else if ( value.userType() == QMetaType::type(
"QgsFeature" ) )
106 return feat.
isValid() ? True : False;
109 if ( value.type() == QVariant::Int )
110 return value.toInt() != 0 ? True : False;
113 const double x = value.toDouble( &ok );
116 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to boolean" ).arg( value.toString() ) );
123 static inline bool isIntSafe(
const QVariant &v )
125 if ( v.type() == QVariant::Int )
127 if ( v.type() == QVariant::UInt )
129 if ( v.type() == QVariant::LongLong )
131 if ( v.type() == QVariant::ULongLong )
133 if ( v.type() == QVariant::Double )
135 if ( v.type() == QVariant::String )
138 v.toString().toInt( &ok );
144 static inline bool isDoubleSafe(
const QVariant &v )
146 if ( v.type() == QVariant::Double )
148 if ( v.type() == QVariant::Int )
150 if ( v.type() == QVariant::UInt )
152 if ( v.type() == QVariant::LongLong )
154 if ( v.type() == QVariant::ULongLong )
156 if ( v.type() == QVariant::String )
159 const double val = v.toString().toDouble( &ok );
160 ok = ok && std::isfinite( val ) && !std::isnan( val );
166 static inline bool isDateTimeSafe(
const QVariant &v )
168 return v.type() == QVariant::DateTime
169 || v.type() == QVariant::Date
170 || v.type() == QVariant::Time;
173 static inline bool isIntervalSafe(
const QVariant &v )
175 if ( v.userType() == QMetaType::type(
"QgsInterval" ) )
180 if ( v.type() == QVariant::String )
187 static inline bool isNull(
const QVariant &v )
192 static inline bool isList(
const QVariant &v )
194 return v.type() == QVariant::List || v.type() == QVariant::StringList;
198 static QString getStringValue(
const QVariant &value,
QgsExpression * )
200 return value.toString();
210 static QByteArray getBinaryValue(
const QVariant &value,
QgsExpression *parent )
212 if ( value.type() != QVariant::ByteArray )
217 return value.toByteArray();
220 static double getDoubleValue(
const QVariant &value,
QgsExpression *parent )
223 const double x = value.toDouble( &ok );
224 if ( !ok || std::isnan( x ) || !std::isfinite( x ) )
226 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to double" ).arg( value.toString() ) );
232 static qlonglong getIntValue(
const QVariant &value,
QgsExpression *parent )
235 const qlonglong x = value.toLongLong( &ok );
242 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to int" ).arg( value.toString() ) );
247 static int getNativeIntValue(
const QVariant &value,
QgsExpression *parent )
250 const qlonglong x = value.toLongLong( &ok );
251 if ( ok && x >= std::numeric_limits<int>::min() && x <= std::numeric_limits<int>::max() )
253 return static_cast<int>( x );
257 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to native int" ).arg( value.toString() ) );
262 static QDateTime getDateTimeValue(
const QVariant &value,
QgsExpression *parent )
264 QDateTime d = value.toDateTime();
271 const QTime t = value.toTime();
274 return QDateTime( QDate( 1, 1, 1 ), t );
277 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to DateTime" ).arg( value.toString() ) );
282 static QDate getDateValue(
const QVariant &value,
QgsExpression *parent )
284 QDate d = value.toDate();
291 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to Date" ).arg( value.toString() ) );
296 static QTime getTimeValue(
const QVariant &value,
QgsExpression *parent )
298 QTime t = value.toTime();
305 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to Time" ).arg( value.toString() ) );
312 if ( value.userType() == QMetaType::type(
"QgsInterval" ) )
316 if ( inter.isValid() )
322 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to interval" ).arg( value.toString() ) );
331 if ( value.userType() == QMetaType::type(
"QgsGeometry" ) )
340 if ( value.userType() == QMetaType::type(
"QgsFeature" ) )
378 static std::unique_ptr<QgsVectorLayerFeatureSource> getFeatureSource(
const QVariant &value,
const QgsExpressionContext *context,
QgsExpression *e,
bool &foundLayer );
392 static QVariantList getListValue(
const QVariant &value,
QgsExpression *parent )
394 if ( value.type() == QVariant::List || value.type() == QVariant::StringList )
396 return value.toList();
400 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to array" ).arg( value.toString() ) );
401 return QVariantList();
405 static QVariantMap getMapValue(
const QVariant &value,
QgsExpression *parent )
407 if ( value.type() == QVariant::Map )
409 return value.toMap();
413 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to map" ).arg( value.toString() ) );
414 return QVariantMap();
424 static QString toLocalizedString(
const QVariant &value )
426 if ( value.type() == QVariant::Int || value.type() == QVariant::UInt || value.type() == QVariant::LongLong || value.type() == QVariant::ULongLong )
431 if ( value.type() == QVariant::ULongLong )
433 res = QLocale().toString( value.toULongLong( &ok ) );
437 res = QLocale().toString( value.toLongLong( &ok ) );
446 return value.toString();
450 else if ( value.type() == QVariant::Double || value.type() ==
static_cast<QVariant::Type
>( QMetaType::Float ) )
453 const QString strVal = value.toString();
454 const int dotPosition = strVal.indexOf(
'.' );
455 const int precision = dotPosition > 0 ? strVal.length() - dotPosition - 1 : 0;
456 const QString res = QLocale().toString( value.toDouble( &ok ),
'f',
precision );
464 return value.toString();
469 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 ...
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.
static bool isNull(const QVariant &variant)
Returns true if the specified variant should be considered a NULL value.
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)