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();
191 static double getDoubleValue(
const QVariant &value,
QgsExpression *parent )
194 double x = value.toDouble( &ok );
195 if ( !ok || std::isnan( x ) || !std::isfinite( x ) )
197 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to double" ).arg( value.toString() ) );
203 static qlonglong getIntValue(
const QVariant &value,
QgsExpression *parent )
206 qlonglong x = value.toLongLong( &ok );
213 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to int" ).arg( value.toString() ) );
218 static int getNativeIntValue(
const QVariant &value,
QgsExpression *parent )
221 qlonglong x = value.toLongLong( &ok );
222 if ( ok && x >= std::numeric_limits<int>::min() && x <= std::numeric_limits<int>::max() )
228 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to native int" ).arg( value.toString() ) );
233 static QDateTime getDateTimeValue(
const QVariant &value,
QgsExpression *parent )
235 QDateTime d = value.toDateTime();
242 QTime t = value.toTime();
245 return QDateTime( QDate( 1, 1, 1 ), t );
248 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to DateTime" ).arg( value.toString() ) );
253 static QDate getDateValue(
const QVariant &value,
QgsExpression *parent )
255 QDate d = value.toDate();
262 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to Date" ).arg( value.toString() ) );
267 static QTime getTimeValue(
const QVariant &value,
QgsExpression *parent )
269 QTime t = value.toTime();
276 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to Time" ).arg( value.toString() ) );
287 if ( inter.isValid() )
293 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to interval" ).arg( value.toString() ) );
305 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to gradient ramp" ).arg( value.toString() ) );
345 ml = project->
mapLayer( value.toString() );
354 static std::unique_ptr<QgsVectorLayerFeatureSource> getFeatureSource(
const QVariant &value,
QgsExpression *e )
356 std::unique_ptr<QgsVectorLayerFeatureSource> featureSource;
358 auto getFeatureSource = [ &value, e, &featureSource ]
368 #if QT_VERSION >= QT_VERSION_CHECK( 5, 10, 0 ) 371 if ( QThread::currentThread() == qApp->thread() )
374 QMetaObject::invokeMethod( qApp, getFeatureSource, Qt::BlockingQueuedConnection );
379 return featureSource;
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();
421 #endif // QGSEXPRESSIONUTILS_H bool isValid() const
Returns the validity of this feature.
Class for parsing and evaluation of expressions (formerly called "search strings").
Base class for all map layer types.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
static QgsInterval fromString(const QString &string)
Converts a string to an interval.
A geometry is the spatial representation of a feature.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
void setEvalErrorString(const QString &str)
Sets evaluation error (used internally by evaluation functions)
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
Reads and writes project states.
Abstract base class for all nodes that can appear in an expression.
QList< QgsMapLayer * > mapLayersByName(const QString &layerName) const
Retrieve a list of matching registered layers by layer name.
Partial snapshot of vector layer's state (only the members necessary for access to features) ...
A representation of the interval between two datetime values.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
Represents a vector layer which manages a vector based data sets.