71#define TVL_True QVariant( 1 )
72#define TVL_False QVariant( 0 )
73#define TVL_Unknown QVariant()
75 static QVariant tvl2variant( TVL v )
90 static TVL getTVLValue(
const QVariant &value,
QgsExpression *parent )
97 if ( value.userType() == QMetaType::type(
"QgsGeometry" ) )
101 return geom.
isNull() ? False : True;
103 else if ( value.userType() == QMetaType::type(
"QgsFeature" ) )
107 return feat.
isValid() ? True : False;
110 if ( value.type() == QVariant::Int )
111 return value.toInt() != 0 ? True : False;
114 const double x = value.toDouble( &ok );
117 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to boolean" ).arg( value.toString() ) );
124 static inline bool isIntSafe(
const QVariant &v )
126 if ( v.type() == QVariant::Int )
128 if ( v.type() == QVariant::UInt )
130 if ( v.type() == QVariant::LongLong )
132 if ( v.type() == QVariant::ULongLong )
134 if ( v.type() == QVariant::Double )
136 if ( v.type() == QVariant::String )
139 v.toString().toInt( &ok );
145 static inline bool isDoubleSafe(
const QVariant &v )
147 if ( v.type() == QVariant::Double )
149 if ( v.type() == QVariant::Int )
151 if ( v.type() == QVariant::UInt )
153 if ( v.type() == QVariant::LongLong )
155 if ( v.type() == QVariant::ULongLong )
157 if ( v.type() == QVariant::String )
160 const double val = v.toString().toDouble( &ok );
161 ok = ok && std::isfinite( val ) && !std::isnan( val );
167 static inline bool isDateTimeSafe(
const QVariant &v )
169 return v.type() == QVariant::DateTime
170 || v.type() == QVariant::Date
171 || v.type() == QVariant::Time;
174 static inline bool isIntervalSafe(
const QVariant &v )
176 if ( v.userType() == QMetaType::type(
"QgsInterval" ) )
181 if ( v.type() == QVariant::String )
188 static inline bool isNull(
const QVariant &v )
193 static inline bool isList(
const QVariant &v )
195 return v.type() == QVariant::List || v.type() == QVariant::StringList;
199 static QString getStringValue(
const QVariant &value,
QgsExpression * )
201 return value.toString();
211 static QByteArray getBinaryValue(
const QVariant &value,
QgsExpression *parent )
213 if ( value.type() != QVariant::ByteArray )
218 return value.toByteArray();
221 static double getDoubleValue(
const QVariant &value,
QgsExpression *parent )
224 const double x = value.toDouble( &ok );
225 if ( !ok || std::isnan( x ) || !std::isfinite( x ) )
227 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to double" ).arg( value.toString() ) );
233 static qlonglong getIntValue(
const QVariant &value,
QgsExpression *parent )
236 const qlonglong x = value.toLongLong( &ok );
243 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to int" ).arg( value.toString() ) );
248 static int getNativeIntValue(
const QVariant &value,
QgsExpression *parent )
251 const qlonglong x = value.toLongLong( &ok );
252 if ( ok && x >= std::numeric_limits<int>::min() && x <= std::numeric_limits<int>::max() )
254 return static_cast<int>( x );
258 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to native int" ).arg( value.toString() ) );
263 static QDateTime getDateTimeValue(
const QVariant &value,
QgsExpression *parent )
265 QDateTime d = value.toDateTime();
272 const QTime t = value.toTime();
275 return QDateTime( QDate( 1, 1, 1 ), t );
278 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to DateTime" ).arg( value.toString() ) );
283 static QDate getDateValue(
const QVariant &value,
QgsExpression *parent )
285 QDate d = value.toDate();
292 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to Date" ).arg( value.toString() ) );
297 static QTime getTimeValue(
const QVariant &value,
QgsExpression *parent )
299 QTime t = value.toTime();
306 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to Time" ).arg( value.toString() ) );
313 if ( value.userType() == QMetaType::type(
"QgsInterval" ) )
317 if ( inter.isValid() )
323 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to interval" ).arg( value.toString() ) );
332 if ( value.userType() == QMetaType::type(
"QgsGeometry" ) )
341 if ( value.userType() == QMetaType::type(
"QgsFeature" ) )
379 static std::unique_ptr<QgsVectorLayerFeatureSource> getFeatureSource(
const QVariant &value,
const QgsExpressionContext *context,
QgsExpression *e,
bool &foundLayer );
393 static QVariantList getListValue(
const QVariant &value,
QgsExpression *parent )
395 if ( value.type() == QVariant::List || value.type() == QVariant::StringList )
397 return value.toList();
401 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to array" ).arg( value.toString() ) );
402 return QVariantList();
406 static QVariantMap getMapValue(
const QVariant &value,
QgsExpression *parent )
408 if ( value.type() == QVariant::Map )
410 return value.toMap();
414 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to map" ).arg( value.toString() ) );
415 return QVariantMap();
425 static QString toLocalizedString(
const QVariant &value )
427 if ( value.type() == QVariant::Int || value.type() == QVariant::UInt || value.type() == QVariant::LongLong || value.type() == QVariant::ULongLong )
432 if ( value.type() == QVariant::ULongLong )
434 res = QLocale().toString( value.toULongLong( &ok ) );
438 res = QLocale().toString( value.toLongLong( &ok ) );
447 return value.toString();
451 else if ( value.type() == QVariant::Double || value.type() ==
static_cast<QVariant::Type
>( QMetaType::Float ) )
454 const QString strVal = value.toString();
455 const int dotPosition = strVal.indexOf(
'.' );
456 const int precision = dotPosition > 0 ? strVal.length() - dotPosition - 1 : 0;
457 const QString res = QLocale().toString( value.toDouble( &ok ),
'f',
precision );
465 return value.toString();
470 return value.toString();
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...