78#define TVL_True QVariant( 1 )
79#define TVL_False QVariant( 0 )
80#define TVL_Unknown QVariant()
82 static QVariant tvl2variant( TVL v )
97 static TVL getTVLValue(
const QVariant &value,
QgsExpression *parent )
104 int userType = value.userType();
105 if ( value.type() == QVariant::UserType )
107 if ( userType == qMetaTypeId< QgsGeometry>() || userType == qMetaTypeId<QgsReferencedGeometry>() )
110 const QgsGeometry geom = getGeometry( value,
nullptr );
111 return geom.
isNull() ? False : True;
113 else if ( userType == qMetaTypeId<QgsFeature>() )
117 return feat.
isValid() ? True : False;
121 if ( userType == QMetaType::Type::Int )
122 return value.toInt() != 0 ? True : False;
125 const double x = value.toDouble( &ok );
129 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to boolean" ).arg( value.toString() ) );
136 static inline bool isIntSafe(
const QVariant &v )
138 if ( v.userType() == QMetaType::Type::Int )
140 if ( v.userType() == QMetaType::Type::UInt )
142 if ( v.userType() == QMetaType::Type::LongLong )
144 if ( v.userType() == QMetaType::Type::ULongLong )
146 if ( v.userType() == QMetaType::Type::Double )
148 if ( v.userType() == QMetaType::Type::QString )
151 v.toString().toInt( &ok );
157 static inline bool isDoubleSafe(
const QVariant &v )
159 if ( v.userType() == QMetaType::Type::Double )
161 if ( v.userType() == QMetaType::Type::Int )
163 if ( v.userType() == QMetaType::Type::UInt )
165 if ( v.userType() == QMetaType::Type::LongLong )
167 if ( v.userType() == QMetaType::Type::ULongLong )
169 if ( v.userType() == QMetaType::Type::QString )
172 const double val = v.toString().toDouble( &ok );
173 ok = ok && std::isfinite( val ) && !std::isnan( val );
179 static inline bool isDateTimeSafe(
const QVariant &v )
181 return v.userType() == QMetaType::Type::QDateTime
182 || v.userType() == QMetaType::Type::QDate
183 || v.userType() == QMetaType::Type::QTime;
186 static inline bool isIntervalSafe(
const QVariant &v )
188 if ( v.userType() == qMetaTypeId<QgsInterval>() )
193 if ( v.userType() == QMetaType::Type::QString )
200 static inline bool isNull(
const QVariant &v )
205 static inline bool isList(
const QVariant &v )
207 return v.userType() == QMetaType::Type::QVariantList || v.userType() == QMetaType::Type::QStringList;
211 static QString getStringValue(
const QVariant &value,
QgsExpression * )
213 return value.toString();
223 static QByteArray getBinaryValue(
const QVariant &value,
QgsExpression *parent )
225 if ( value.userType() != QMetaType::Type::QByteArray )
231 return value.toByteArray();
234 static double getDoubleValue(
const QVariant &value,
QgsExpression *parent )
237 const double x = value.toDouble( &ok );
238 if ( !ok || std::isnan( x ) || !std::isfinite( x ) )
241 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to double" ).arg( value.toString() ) );
247 static qlonglong getIntValue(
const QVariant &value,
QgsExpression *parent )
250 const qlonglong x = value.toLongLong( &ok );
258 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to int" ).arg( value.toString() ) );
263 static int getNativeIntValue(
const QVariant &value,
QgsExpression *parent )
266 const qlonglong x = value.toLongLong( &ok );
267 if ( ok && x >= std::numeric_limits<int>::min() && x <= std::numeric_limits<int>::max() )
269 return static_cast<int>( x );
274 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to native int" ).arg( value.toString() ) );
279 static QDateTime getDateTimeValue(
const QVariant &value,
QgsExpression *parent )
281 QDateTime d = value.toDateTime();
288 const QTime t = value.toTime();
291 return QDateTime( QDate( 1, 1, 1 ), t );
295 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to DateTime" ).arg( value.toString() ) );
300 static QDate getDateValue(
const QVariant &value,
QgsExpression *parent )
302 QDate d = value.toDate();
310 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to Date" ).arg( value.toString() ) );
315 static QTime getTimeValue(
const QVariant &value,
QgsExpression *parent )
317 QTime t = value.toTime();
325 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to Time" ).arg( value.toString() ) );
330 static QColor getColorValue(
const QVariant &value,
QgsExpression *parent,
bool &isQColor );
334 if ( value.userType() == qMetaTypeId<QgsInterval>() )
338 if ( inter.isValid() )
343 if ( report_error && parent )
344 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to interval" ).arg( value.toString() ) );
353 if ( value.userType() == qMetaTypeId< QgsReferencedGeometry>() )
356 if ( value.userType() == qMetaTypeId< QgsGeometry>() )
359 if ( !tolerant && parent )
366 if ( value.userType() == qMetaTypeId<QgsFeature>() )
386 static QTimeZone getTimeZoneValue(
const QVariant &value,
QgsExpression *parent );
420 static std::unique_ptr<QgsVectorLayerFeatureSource> getFeatureSource(
const QVariant &value,
const QgsExpressionContext *context,
QgsExpression *e,
bool &foundLayer );
434 static QVariantList getListValue(
const QVariant &value,
QgsExpression *parent )
436 if ( value.userType() == QMetaType::Type::QVariantList || value.userType() == QMetaType::Type::QStringList )
438 return value.toList();
443 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to array" ).arg( value.toString() ) );
444 return QVariantList();
448 static QVariantMap getMapValue(
const QVariant &value,
QgsExpression *parent )
450 if ( value.userType() == QMetaType::Type::QVariantMap )
452 return value.toMap();
457 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to map" ).arg( value.toString() ) );
458 return QVariantMap();
468 static QString toLocalizedString(
const QVariant &value )
470 if ( value.userType() == QMetaType::Type::Int || value.userType() == QMetaType::Type::UInt || value.userType() == QMetaType::Type::LongLong || value.userType() == QMetaType::Type::ULongLong )
475 if ( value.userType() == QMetaType::Type::ULongLong )
477 res = QLocale().toString( value.toULongLong( &ok ) );
481 res = QLocale().toString( value.toLongLong( &ok ) );
490 return value.toString();
494 else if ( value.userType() == QMetaType::Type::Double || value.userType() ==
static_cast<QMetaType::Type
>( QMetaType::Float ) )
497 const QString strVal = value.toString();
498 const int dotPosition = strVal.indexOf(
'.' );
499 const int precision = dotPosition > 0 ? strVal.length() - dotPosition - 1 : 0;
500 const QString res = QLocale().toString( value.toDouble( &ok ),
'f', precision );
508 return value.toString();
513 return value.toString();
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...