17 #ifndef QGSEXPRESSIONUTILS_H 18 #define QGSEXPRESSIONUTILS_H 30 #define ENSURE_NO_EVAL_ERROR { if ( parent->hasEvalError() ) return QVariant(); } 31 #define SET_EVAL_ERROR(x) { parent->setEvalErrorString( x ); return QVariant(); } 33 #define FEAT_FROM_CONTEXT(c, f) if ( !(c) || !( c )->hasFeature() ) return QVariant(); \ 34 QgsFeature f = ( c )->feature(); 40 class QgsExpressionUtils
57 #define TVL_True QVariant( 1 ) 58 #define TVL_False QVariant( 0 ) 59 #define TVL_Unknown QVariant() 61 static QVariant tvl2variant( TVL v )
76 static TVL getTVLValue(
const QVariant &value,
QgsExpression *parent )
87 return geom.
isNull() ? False : True;
93 return feat.
isValid() ? True : False;
96 if ( value.type() == QVariant::Int )
97 return value.toInt() != 0 ? True : False;
100 double x = value.toDouble( &ok );
103 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to boolean" ).arg( value.toString() ) );
110 static inline bool isIntSafe(
const QVariant &v )
112 if ( v.type() == QVariant::Int )
114 if ( v.type() == QVariant::UInt )
116 if ( v.type() == QVariant::LongLong )
118 if ( v.type() == QVariant::ULongLong )
120 if ( v.type() == QVariant::Double )
122 if ( v.type() == QVariant::String )
125 v.toString().toInt( &ok );
131 static inline bool isDoubleSafe(
const QVariant &v )
133 if ( v.type() == QVariant::Double )
135 if ( v.type() == QVariant::Int )
137 if ( v.type() == QVariant::UInt )
139 if ( v.type() == QVariant::LongLong )
141 if ( v.type() == QVariant::ULongLong )
143 if ( v.type() == QVariant::String )
146 double val = v.toString().toDouble( &ok );
147 ok = ok && std::isfinite( val ) && !std::isnan( val );
153 static inline bool isDateTimeSafe(
const QVariant &v )
155 return v.type() == QVariant::DateTime
156 || v.type() == QVariant::Date
157 || v.type() == QVariant::Time;
160 static inline bool isIntervalSafe(
const QVariant &v )
167 if ( v.type() == QVariant::String )
174 static inline bool isNull(
const QVariant &v )
179 static inline bool isList(
const QVariant &v )
181 return v.type() == QVariant::List;
185 static QString getStringValue(
const QVariant &value,
QgsExpression * )
187 return value.toString();
190 static double getDoubleValue(
const QVariant &value,
QgsExpression *parent )
193 double x = value.toDouble( &ok );
194 if ( !ok || std::isnan( x ) || !std::isfinite( x ) )
196 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to double" ).arg( value.toString() ) );
202 static qlonglong getIntValue(
const QVariant &value,
QgsExpression *parent )
205 qlonglong x = value.toLongLong( &ok );
212 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to int" ).arg( value.toString() ) );
217 static int getNativeIntValue(
const QVariant &value,
QgsExpression *parent )
220 qlonglong x = value.toLongLong( &ok );
221 if ( ok && x >= std::numeric_limits<int>::min() && x <= std::numeric_limits<int>::max() )
227 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to native int" ).arg( value.toString() ) );
232 static QDateTime getDateTimeValue(
const QVariant &value,
QgsExpression *parent )
234 QDateTime d = value.toDateTime();
241 QTime t = value.toTime();
244 return QDateTime( QDate( 1, 1, 1 ), t );
247 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to DateTime" ).arg( value.toString() ) );
252 static QDate getDateValue(
const QVariant &value,
QgsExpression *parent )
254 QDate d = value.toDate();
261 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to Date" ).arg( value.toString() ) );
266 static QTime getTimeValue(
const QVariant &value,
QgsExpression *parent )
268 QTime t = value.toTime();
275 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to Time" ).arg( value.toString() ) );
286 if ( inter.isValid() )
292 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to interval" ).arg( value.toString() ) );
304 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to gradient ramp" ).arg( value.toString() ) );
344 ml = project->
mapLayer( value.toString() );
359 static QVariantList getListValue(
const QVariant &value,
QgsExpression *parent )
361 if ( value.type() == QVariant::List || value.type() == QVariant::StringList )
363 return value.toList();
367 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to array" ).arg( value.toString() ) );
368 return QVariantList();
372 static QVariantMap getMapValue(
const QVariant &value,
QgsExpression *parent )
374 if ( value.type() == QVariant::Map )
376 return value.toMap();
380 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to map" ).arg( value.toString() ) );
381 return QVariantMap();
388 #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.
bool isNull() const
Returns true if the geometry is null (ie, contains no underlying geometry accessible via geometry() )...
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.
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.