17 #ifndef QGSEXPRESSIONUTILS_H
18 #define QGSEXPRESSIONUTILS_H
31 #define ENSURE_NO_EVAL_ERROR { if ( parent->hasEvalError() ) return QVariant(); }
32 #define SET_EVAL_ERROR(x) { parent->setEvalErrorString( x ); return QVariant(); }
34 #define FEAT_FROM_CONTEXT(c, f) if ( !(c) || !( c )->hasFeature() ) return QVariant(); \
35 QgsFeature f = ( c )->feature();
41 class QgsExpressionUtils
58 #define TVL_True QVariant( 1 )
59 #define TVL_False QVariant( 0 )
60 #define TVL_Unknown QVariant()
62 static QVariant tvl2variant( TVL v )
77 static TVL getTVLValue(
const QVariant &value,
QgsExpression *parent )
88 return geom.
isNull() ? False : True;
94 return feat.
isValid() ? True : False;
97 if ( value.type() == QVariant::Int )
98 return value.toInt() != 0 ? True : False;
101 double x = value.toDouble( &ok );
104 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to boolean" ).arg( value.toString() ) );
111 static inline bool isIntSafe(
const QVariant &v )
113 if ( v.type() == QVariant::Int )
115 if ( v.type() == QVariant::UInt )
117 if ( v.type() == QVariant::LongLong )
119 if ( v.type() == QVariant::ULongLong )
121 if ( v.type() == QVariant::Double )
123 if ( v.type() == QVariant::String )
126 v.toString().toInt( &ok );
132 static inline bool isDoubleSafe(
const QVariant &v )
134 if ( v.type() == QVariant::Double )
136 if ( v.type() == QVariant::Int )
138 if ( v.type() == QVariant::UInt )
140 if ( v.type() == QVariant::LongLong )
142 if ( v.type() == QVariant::ULongLong )
144 if ( v.type() == QVariant::String )
147 double val = v.toString().toDouble( &ok );
148 ok = ok && std::isfinite( val ) && !std::isnan( val );
154 static inline bool isDateTimeSafe(
const QVariant &v )
156 return v.type() == QVariant::DateTime
157 || v.type() == QVariant::Date
158 || v.type() == QVariant::Time;
161 static inline bool isIntervalSafe(
const QVariant &v )
168 if ( v.type() == QVariant::String )
175 static inline bool isNull(
const QVariant &v )
180 static inline bool isList(
const QVariant &v )
182 return v.type() == QVariant::List;
186 static QString getStringValue(
const QVariant &value,
QgsExpression * )
188 return value.toString();
198 static QByteArray getBinaryValue(
const QVariant &value,
QgsExpression *parent )
200 if ( value.type() != QVariant::ByteArray )
205 return value.toByteArray();
208 static double getDoubleValue(
const QVariant &value,
QgsExpression *parent )
211 double x = value.toDouble( &ok );
212 if ( !ok || std::isnan( x ) || !std::isfinite( x ) )
214 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to double" ).arg( value.toString() ) );
220 static qlonglong getIntValue(
const QVariant &value,
QgsExpression *parent )
223 qlonglong x = value.toLongLong( &ok );
230 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to int" ).arg( value.toString() ) );
235 static int getNativeIntValue(
const QVariant &value,
QgsExpression *parent )
238 qlonglong x = value.toLongLong( &ok );
239 if ( ok && x >= std::numeric_limits<int>::min() && x <= std::numeric_limits<int>::max() )
241 return static_cast<int>( x );
245 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to native int" ).arg( value.toString() ) );
250 static QDateTime getDateTimeValue(
const QVariant &value,
QgsExpression *parent )
252 QDateTime d = value.toDateTime();
259 QTime t = value.toTime();
262 return QDateTime( QDate( 1, 1, 1 ), t );
265 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to DateTime" ).arg( value.toString() ) );
270 static QDate getDateValue(
const QVariant &value,
QgsExpression *parent )
272 QDate d = value.toDate();
279 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to Date" ).arg( value.toString() ) );
284 static QTime getTimeValue(
const QVariant &value,
QgsExpression *parent )
286 QTime t = value.toTime();
293 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to Time" ).arg( value.toString() ) );
304 if ( inter.isValid() )
310 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to interval" ).arg( value.toString() ) );
322 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to gradient ramp" ).arg( value.toString() ) );
362 ml = project->
mapLayer( value.toString() );
371 static std::unique_ptr<QgsVectorLayerFeatureSource> getFeatureSource(
const QVariant &value,
QgsExpression *e )
373 std::unique_ptr<QgsVectorLayerFeatureSource> featureSource;
375 auto getFeatureSource = [ &value, e, &featureSource ]
385 #if QT_VERSION >= QT_VERSION_CHECK( 5, 10, 0 )
388 if ( QThread::currentThread() == qApp->thread() )
391 QMetaObject::invokeMethod( qApp, getFeatureSource, Qt::BlockingQueuedConnection );
396 return featureSource;
401 return qobject_cast<QgsVectorLayer *>( getMapLayer( value, e ) );
406 return qobject_cast<QgsRasterLayer *>( getMapLayer( value, e ) );
409 static QVariantList getListValue(
const QVariant &value,
QgsExpression *parent )
411 if ( value.type() == QVariant::List || value.type() == QVariant::StringList )
413 return value.toList();
417 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to array" ).arg( value.toString() ) );
418 return QVariantList();
422 static QVariantMap getMapValue(
const QVariant &value,
QgsExpression *parent )
424 if ( value.type() == QVariant::Map )
426 return value.toMap();
430 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to map" ).arg( value.toString() ) );
431 return QVariantMap();
438 #endif // QGSEXPRESSIONUTILS_H