@@ -231,14 +231,15 @@ QString QgsSearchTreeNode::makeSearchString()
231
231
// currently all functions take one parameter
232
232
str += QString ( " (%1)" ).arg ( mLeft ->makeSearchString () );
233
233
}
234
- else if ( mOp == opLENGTH || mOp == opAREA || mOp == opROWNUM )
234
+ else if ( mOp == opLENGTH || mOp == opAREA || mOp == opROWNUM || mOp == opID )
235
235
{
236
236
// special nullary opeators
237
237
switch ( mOp )
238
238
{
239
239
case opLENGTH: str += " $length" ; break ;
240
240
case opAREA: str += " $area" ; break ;
241
241
case opROWNUM: str += " $rownum" ; break ;
242
+ case opID: str += " $id" ; break ;
242
243
default : str += " ?" ;
243
244
}
244
245
}
@@ -374,8 +375,16 @@ bool QgsSearchTreeNode::needsGeometry()
374
375
}
375
376
}
376
377
378
+ bool QgsSearchTreeNode::checkAgainst ( const QMap<int ,QgsField>& fields, const QMap<int , QVariant>& attributes, QgsGeometry* geom )
379
+ {
380
+ QgsFeature f;
381
+ f.setAttributeMap ( attributes );
382
+ if ( geom )
383
+ f.setGeometry ( *geom );
384
+ return checkAgainst ( fields, f );
385
+ }
377
386
378
- bool QgsSearchTreeNode::checkAgainst ( const QgsFieldMap& fields, const QgsAttributeMap& attributes, QgsGeometry* geom )
387
+ bool QgsSearchTreeNode::checkAgainst ( const QgsFieldMap& fields, QgsFeature &f )
379
388
{
380
389
QgsDebugMsgLevel ( " checkAgainst: " + makeSearchString (), 2 );
381
390
@@ -394,40 +403,32 @@ bool QgsSearchTreeNode::checkAgainst( const QgsFieldMap& fields, const QgsAttrib
394
403
switch ( mOp )
395
404
{
396
405
case opNOT:
397
- return !mLeft ->checkAgainst ( fields, attributes, geom );
406
+ return !mLeft ->checkAgainst ( fields, f );
398
407
399
408
case opAND:
400
- if ( !mLeft ->checkAgainst ( fields, attributes, geom ) )
409
+ if ( !mLeft ->checkAgainst ( fields, f ) )
401
410
return false ;
402
- return mRight ->checkAgainst ( fields, attributes, geom );
411
+ return mRight ->checkAgainst ( fields, f );
403
412
404
413
case opOR:
405
- if ( mLeft ->checkAgainst ( fields, attributes, geom ) )
414
+ if ( mLeft ->checkAgainst ( fields, f ) )
406
415
return true ;
407
- return mRight ->checkAgainst ( fields, attributes, geom );
416
+ return mRight ->checkAgainst ( fields, f );
408
417
409
418
case opISNULL:
410
419
case opISNOTNULL:
411
- if ( !getValue ( value1, mLeft , fields, attributes, geom ) )
420
+ if ( !getValue ( value1, mLeft , fields, f ) )
412
421
return false ;
413
422
414
- if ( mOp == opISNULL )
415
- {
416
- return value1.isNull ();
417
- }
418
- else if ( mOp == opISNOTNULL )
419
- {
420
- return !value1.isNull ();
421
- }
423
+ return ( mOp == opISNULL ) == value1.isNull ();
422
424
423
425
case opEQ:
424
426
case opNE:
425
427
case opGT:
426
428
case opLT:
427
429
case opGE:
428
430
case opLE:
429
-
430
- if ( !getValue ( value1, mLeft , fields, attributes, geom ) || !getValue ( value2, mRight , fields, attributes, geom ) )
431
+ if ( !getValue ( value1, mLeft , fields, f ) || !getValue ( value2, mRight , fields, f ) )
431
432
return false ;
432
433
433
434
if ( value1.isNull () || value2.isNull () )
@@ -440,12 +441,12 @@ bool QgsSearchTreeNode::checkAgainst( const QgsFieldMap& fields, const QgsAttrib
440
441
441
442
switch ( mOp )
442
443
{
443
- case opEQ: return ( res == 0 ) ;
444
- case opNE: return ( res != 0 ) ;
445
- case opGT: return ( res > 0 ) ;
446
- case opLT: return ( res < 0 ) ;
447
- case opGE: return ( res >= 0 ) ;
448
- case opLE: return ( res <= 0 ) ;
444
+ case opEQ: return res == 0 ;
445
+ case opNE: return res != 0 ;
446
+ case opGT: return res > 0 ;
447
+ case opLT: return res < 0 ;
448
+ case opGE: return res >= 0 ;
449
+ case opLE: return res <= 0 ;
449
450
default :
450
451
mError = QObject::tr ( " Unexpected state when evaluating operator!" );
451
452
return false ;
@@ -454,15 +455,15 @@ bool QgsSearchTreeNode::checkAgainst( const QgsFieldMap& fields, const QgsAttrib
454
455
case opIN:
455
456
case opNOTIN:
456
457
{
457
- if ( !getValue ( value1, mLeft , fields, attributes, geom ) ||
458
+ if ( !getValue ( value1, mLeft , fields, f ) ||
458
459
!mRight || mRight ->type () != tNodeList )
459
460
{
460
461
return false ;
461
462
}
462
463
463
464
foreach ( QgsSearchTreeNode *node, mRight ->mNodeList )
464
465
{
465
- if ( !getValue ( value2, node, fields, attributes, geom ) )
466
+ if ( !getValue ( value2, node, fields, f ) )
466
467
{
467
468
mError = QObject::tr ( " Could not retrieve value of list value" );
468
469
return false ;
@@ -485,8 +486,8 @@ bool QgsSearchTreeNode::checkAgainst( const QgsFieldMap& fields, const QgsAttrib
485
486
case opLike:
486
487
case opILike:
487
488
{
488
- if ( !getValue ( value1, mLeft , fields, attributes, geom ) ||
489
- !getValue ( value2, mRight , fields, attributes, geom ) )
489
+ if ( !getValue ( value1, mLeft , fields, f ) ||
490
+ !getValue ( value2, mRight , fields, f ) )
490
491
return false ;
491
492
492
493
// value1 is string to be matched
@@ -512,20 +513,29 @@ bool QgsSearchTreeNode::checkAgainst( const QgsFieldMap& fields, const QgsAttrib
512
513
}
513
514
else
514
515
{
515
- return ( QRegExp ( str ).indexIn ( value1.string () ) != -1 ) ;
516
+ return QRegExp ( str ).indexIn ( value1.string () ) != -1 ;
516
517
}
517
-
518
518
}
519
519
520
520
default :
521
521
mError = QObject::tr ( " Unknown operator: %1" ).arg ( mOp );
522
- return false ;
523
522
}
523
+
524
+ return false ;
524
525
}
525
526
526
- bool QgsSearchTreeNode::getValue ( QgsSearchTreeValue& value, QgsSearchTreeNode* node, const QgsFieldMap& fields, const QgsAttributeMap & attributes, QgsGeometry* geom )
527
+ bool QgsSearchTreeNode::getValue ( QgsSearchTreeValue& value, QgsSearchTreeNode* node, const QgsFieldMap& fields, const QMap< int , QVariant> & attributes, QgsGeometry* geom )
527
528
{
528
- value = node->valueAgainst ( fields, attributes, geom );
529
+ QgsFeature f;
530
+ f.setAttributeMap ( attributes );
531
+ if ( geom )
532
+ f.setGeometry ( *geom );
533
+ return getValue ( value, node, fields, f );
534
+ }
535
+
536
+ bool QgsSearchTreeNode::getValue ( QgsSearchTreeValue& value, QgsSearchTreeNode* node, const QgsFieldMap& fields, QgsFeature &f )
537
+ {
538
+ value = node->valueAgainst ( fields, f );
529
539
if ( value.isError () )
530
540
{
531
541
switch (( int )value.number () )
@@ -553,7 +563,16 @@ bool QgsSearchTreeNode::getValue( QgsSearchTreeValue& value, QgsSearchTreeNode*
553
563
return true ;
554
564
}
555
565
556
- QgsSearchTreeValue QgsSearchTreeNode::valueAgainst ( const QgsFieldMap& fields, const QgsAttributeMap& attributes, QgsGeometry* geom )
566
+ QgsSearchTreeValue QgsSearchTreeNode::valueAgainst ( const QgsFieldMap& fields, const QMap<int , QVariant>& attributes, QgsGeometry* geom )
567
+ {
568
+ QgsFeature f;
569
+ f.setAttributeMap ( attributes );
570
+ if ( geom )
571
+ f.setGeometry ( *geom );
572
+ return valueAgainst ( fields, f );
573
+ }
574
+
575
+ QgsSearchTreeValue QgsSearchTreeNode::valueAgainst ( const QgsFieldMap& fields, QgsFeature &f )
557
576
{
558
577
QgsDebugMsgLevel ( " valueAgainst: " + makeSearchString (), 2 );
559
578
@@ -587,7 +606,7 @@ QgsSearchTreeValue QgsSearchTreeNode::valueAgainst( const QgsFieldMap& fields, c
587
606
}
588
607
589
608
// get the value
590
- QVariant val = attributes [it.key ()];
609
+ QVariant val = f. attributeMap () [it.key ()];
591
610
if ( val.isNull () )
592
611
{
593
612
QgsDebugMsgLevel ( " NULL" , 2 );
@@ -612,30 +631,35 @@ QgsSearchTreeValue QgsSearchTreeNode::valueAgainst( const QgsFieldMap& fields, c
612
631
QgsSearchTreeValue value1, value2;
613
632
if ( mLeft )
614
633
{
615
- if ( !getValue ( value1, mLeft , fields, attributes, geom ) ) return value1;
634
+ if ( !getValue ( value1, mLeft , fields, f ) ) return value1;
616
635
}
617
636
if ( mRight )
618
637
{
619
- if ( !getValue ( value2, mRight , fields, attributes, geom ) ) return value2;
638
+ if ( !getValue ( value2, mRight , fields, f ) ) return value2;
620
639
}
621
640
622
641
if ( mOp == opLENGTH || mOp == opAREA )
623
642
{
624
- if ( !geom )
643
+ if ( !f. geometry () )
625
644
{
626
645
return QgsSearchTreeValue ( 2 , " Geometry is 0" );
627
646
}
628
647
629
648
// check that we don't use area for lines or length for polygons
630
- if ( mOp == opLENGTH && geom ->type () != QGis::Line )
649
+ if ( mOp == opLENGTH && f. geometry () ->type () != QGis::Line )
631
650
{
632
651
return QgsSearchTreeValue ( 0 );
633
652
}
634
- if ( mOp == opAREA && geom ->type () != QGis::Polygon )
653
+ if ( mOp == opAREA && f. geometry () ->type () != QGis::Polygon )
635
654
{
636
655
return QgsSearchTreeValue ( 0 );
637
656
}
638
- return QgsSearchTreeValue ( mCalc ->measure ( geom ) );
657
+ return QgsSearchTreeValue ( mCalc ->measure ( f.geometry () ) );
658
+ }
659
+
660
+ if ( mOp == opID )
661
+ {
662
+ return QgsSearchTreeValue ( f.id () );
639
663
}
640
664
641
665
if ( mOp == opROWNUM )
0 commit comments