@@ -338,6 +338,15 @@ static QgsFeature getFeature( const QVariant& value, QgsExpression* parent )
338
338
return 0 ;
339
339
}
340
340
341
+ static QgsExpression::Node* getNode ( const QVariant& value, QgsExpression* parent )
342
+ {
343
+ if ( value.canConvert <QgsExpression::Node*>() )
344
+ return value.value <QgsExpression::Node*>();
345
+
346
+ parent->setEvalErrorString ( " Cannot convert to Node" );
347
+ return 0 ;
348
+ }
349
+
341
350
// this handles also NULL values
342
351
static TVL getTVLValue ( const QVariant& value, QgsExpression* parent )
343
352
{
@@ -1355,6 +1364,27 @@ static QVariant fcnColorRgb( const QVariantList &values, const QgsFeature *, Qgs
1355
1364
return QString ( " %1,%2,%3" ).arg ( color.red () ).arg ( color.green () ).arg ( color.blue () );
1356
1365
}
1357
1366
1367
+ static QVariant fcnIf ( const QVariantList &values, const QgsFeature *f, QgsExpression *parent )
1368
+ {
1369
+ QgsExpression::Node* node = getNode ( values.at ( 0 ), parent );
1370
+ ENSURE_NO_EVAL_ERROR;
1371
+ QVariant value = node->eval ( parent, f );
1372
+ ENSURE_NO_EVAL_ERROR;
1373
+ if ( value.toBool () ) {
1374
+ node = getNode ( values.at ( 1 ), parent );
1375
+ ENSURE_NO_EVAL_ERROR;
1376
+ value = node->eval ( parent, f );
1377
+ ENSURE_NO_EVAL_ERROR;
1378
+ }
1379
+ else {
1380
+ node = getNode ( values.at ( 2 ), parent );
1381
+ ENSURE_NO_EVAL_ERROR;
1382
+ value = node->eval ( parent, f );
1383
+ ENSURE_NO_EVAL_ERROR;
1384
+ }
1385
+ return value;
1386
+ }
1387
+
1358
1388
static QVariant fncColorRgba ( const QVariantList &values, const QgsFeature *, QgsExpression *parent )
1359
1389
{
1360
1390
int red = getIntValue ( values.at ( 0 ), parent );
@@ -1660,6 +1690,7 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
1660
1690
<< new StaticFunction ( " totime" , 1 , fcnToTime, " Conversions" )
1661
1691
<< new StaticFunction ( " tointerval" , 1 , fcnToInterval, " Conversions" )
1662
1692
<< new StaticFunction ( " coalesce" , -1 , fcnCoalesce, " Conditionals" )
1693
+ << new StaticFunction ( " if" , 3 , fcnIf, " Conditionals" , " " , False, QStringList (), true )
1663
1694
<< new StaticFunction ( " regexp_match" , 2 , fcnRegexpMatch, " Conditionals" )
1664
1695
<< new StaticFunction ( " $now" , 0 , fcnNow, " Date and Time" )
1665
1696
<< new StaticFunction ( " age" , 2 , fcnAge, " Date and Time" )
@@ -2533,10 +2564,17 @@ QVariant QgsExpression::NodeFunction::eval( QgsExpression* parent, const QgsFeat
2533
2564
{
2534
2565
foreach ( Node* n, mArgs ->list () )
2535
2566
{
2536
- QVariant v = n->eval ( parent, f );
2537
- ENSURE_NO_EVAL_ERROR;
2538
- if ( isNull ( v ) && fd->name () != " coalesce" )
2539
- return QVariant (); // all "normal" functions return NULL, when any parameter is NULL (so coalesce is abnormal)
2567
+ QVariant v;
2568
+ if ( fd->lazyEval () ) {
2569
+ // Pass in the node for the function to eval as it needs.
2570
+ v = QVariant::fromValue ( n );
2571
+ }
2572
+ else {
2573
+ v = n->eval ( parent, f );
2574
+ ENSURE_NO_EVAL_ERROR;
2575
+ if ( isNull ( v ) && fd->name () != " coalesce" )
2576
+ return QVariant (); // all "normal" functions return NULL, when any parameter is NULL (so coalesce is abnormal)
2577
+ }
2540
2578
argValues.append ( v );
2541
2579
}
2542
2580
}
0 commit comments