@@ -217,6 +217,7 @@ class TestQgsProcessing: public QObject
217
217
void parameterOutputVectorLayer ();
218
218
void checkParamValues ();
219
219
void combineLayerExtent ();
220
+ void processingFeatureSource ();
220
221
void processingFeatureSink ();
221
222
222
223
private:
@@ -452,8 +453,6 @@ void TestQgsProcessing::context()
452
453
context.setDefaultEncoding ( " my_enc" );
453
454
QCOMPARE ( context.defaultEncoding (), QStringLiteral ( " my_enc" ) );
454
455
455
- context.setFlags ( QgsProcessingContext::UseSelectionIfPresent );
456
- QCOMPARE ( context.flags (), QgsProcessingContext::UseSelectionIfPresent );
457
456
context.setFlags ( QgsProcessingContext::Flags ( 0 ) );
458
457
QCOMPARE ( context.flags (), QgsProcessingContext::Flags ( 0 ) );
459
458
@@ -695,9 +694,9 @@ void TestQgsProcessing::features()
695
694
return ids;
696
695
};
697
696
698
- QgsProcessingParameterDefinition *def = new QgsProcessingParameterString ( QStringLiteral ( " string " ) );
697
+ QgsProcessingParameterDefinition *def = new QgsProcessingParameterString ( QStringLiteral ( " layer " ) );
699
698
QVariantMap params;
700
- params.insert ( QStringLiteral ( " string " ), layer->id () );
699
+ params.insert ( QStringLiteral ( " layer " ), layer->id () );
701
700
702
701
std::unique_ptr< QgsFeatureSource > source ( QgsProcessingParameters::parameterAsSource ( def, params, context ) );
703
702
@@ -707,32 +706,32 @@ void TestQgsProcessing::features()
707
706
QCOMPARE ( source->featureCount (), 5L );
708
707
709
708
// test with selected features
710
- context. setFlags ( QgsProcessingContext::UseSelectionIfPresent );
709
+ params. insert ( QStringLiteral ( " layer " ), QVariant::fromValue ( QgsProcessingFeatureSourceDefinition ( layer-> id (), true ) ) );
711
710
layer->selectByIds ( QgsFeatureIds () << 2 << 4 );
712
711
source.reset ( QgsProcessingParameters::parameterAsSource ( def, params, context ) );
713
712
ids = getIds ( source->getFeatures () );
714
713
QCOMPARE ( ids, QgsFeatureIds () << 2 << 4 );
715
714
QCOMPARE ( source->featureCount (), 2L );
716
715
717
716
// selection, but not using selected features
718
- context. setFlags ( QgsProcessingContext::Flags ( 0 ) );
717
+ params. insert ( QStringLiteral ( " layer " ), QVariant::fromValue ( QgsProcessingFeatureSourceDefinition ( layer-> id (), false ) ) );
719
718
layer->selectByIds ( QgsFeatureIds () << 2 << 4 );
720
719
source.reset ( QgsProcessingParameters::parameterAsSource ( def, params, context ) );
721
720
ids = getIds ( source->getFeatures () );
722
721
QCOMPARE ( ids, QgsFeatureIds () << 1 << 2 << 3 << 4 << 5 );
723
722
QCOMPARE ( source->featureCount (), 5L );
724
723
725
724
// using selected features, but no selection
726
- context. setFlags ( QgsProcessingContext::UseSelectionIfPresent );
725
+ params. insert ( QStringLiteral ( " layer " ), QVariant::fromValue ( QgsProcessingFeatureSourceDefinition ( layer-> id (), true ) ) );
727
726
layer->removeSelection ();
728
727
source.reset ( QgsProcessingParameters::parameterAsSource ( def, params, context ) );
729
728
ids = getIds ( source->getFeatures () );
730
- QCOMPARE ( ids, QgsFeatureIds () << 1 << 2 << 3 << 4 << 5 );
731
- QCOMPARE ( source->featureCount (), 5L );
729
+ QVERIFY ( ids. isEmpty () );
730
+ QCOMPARE ( source->featureCount (), 0L );
732
731
733
732
734
733
// test that feature request is honored
735
- context. setFlags ( QgsProcessingContext::Flags ( 0 ) );
734
+ params. insert ( QStringLiteral ( " layer " ), QVariant::fromValue ( QgsProcessingFeatureSourceDefinition ( layer-> id (), false ) ) );
736
735
source.reset ( QgsProcessingParameters::parameterAsSource ( def, params, context ) );
737
736
ids = getIds ( source->getFeatures ( QgsFeatureRequest ().setFilterFids ( QgsFeatureIds () << 1 << 3 << 5 ) ) );
738
737
QCOMPARE ( ids, QgsFeatureIds () << 1 << 3 << 5 );
@@ -741,7 +740,7 @@ void TestQgsProcessing::features()
741
740
QCOMPARE ( source->featureCount (), 5L );
742
741
743
742
// test that feature request is honored when using selections
744
- context. setFlags ( QgsProcessingContext::UseSelectionIfPresent );
743
+ params. insert ( QStringLiteral ( " layer " ), QVariant::fromValue ( QgsProcessingFeatureSourceDefinition ( layer-> id (), true ) ) );
745
744
layer->selectByIds ( QgsFeatureIds () << 2 << 4 );
746
745
source.reset ( QgsProcessingParameters::parameterAsSource ( def, params, context ) );
747
746
ids = getIds ( source->getFeatures ( QgsFeatureRequest ().setFlags ( QgsFeatureRequest::NoGeometry ) ) );
@@ -754,15 +753,14 @@ void TestQgsProcessing::features()
754
753
encountered = true ;
755
754
};
756
755
757
- context.setFlags ( QgsProcessingContext::Flags ( 0 ) );
758
756
context.setInvalidGeometryCheck ( QgsFeatureRequest::GeometryAbortOnInvalid );
759
757
context.setInvalidGeometryCallback ( callback );
760
758
QgsVectorLayer *polyLayer = new QgsVectorLayer ( " Polygon" , " v2" , " memory" );
761
759
QgsFeature f;
762
760
f.setGeometry ( QgsGeometry::fromWkt ( QStringLiteral ( " Polygon((0 0, 1 0, 0 1, 1 1, 0 0))" ) ) );
763
761
polyLayer->dataProvider ()->addFeatures ( QgsFeatureList () << f );
764
762
p.addMapLayer ( polyLayer );
765
- params.insert ( QStringLiteral ( " string " ), polyLayer->id () );
763
+ params.insert ( QStringLiteral ( " layer " ), polyLayer->id () );
766
764
767
765
source.reset ( QgsProcessingParameters::parameterAsSource ( def, params, context ) );
768
766
ids = getIds ( source->getFeatures () );
@@ -793,9 +791,9 @@ void TestQgsProcessing::uniqueValues()
793
791
p.addMapLayer ( layer );
794
792
context.setProject ( &p );
795
793
796
- QgsProcessingParameterDefinition *def = new QgsProcessingParameterString ( QStringLiteral ( " string " ) );
794
+ QgsProcessingParameterDefinition *def = new QgsProcessingParameterString ( QStringLiteral ( " layer " ) );
797
795
QVariantMap params;
798
- params.insert ( QStringLiteral ( " string " ), layer->id () );
796
+ params.insert ( QStringLiteral ( " layer " ), layer->id () );
799
797
800
798
std::unique_ptr< QgsFeatureSource > source ( QgsProcessingParameters::parameterAsSource ( def, params, context ) );
801
799
@@ -831,7 +829,7 @@ void TestQgsProcessing::uniqueValues()
831
829
QVERIFY ( vals.contains ( QString ( " C" ) ) );
832
830
833
831
// selection and using selection
834
- context. setFlags ( QgsProcessingContext::UseSelectionIfPresent );
832
+ params. insert ( QStringLiteral ( " layer " ), QVariant::fromValue ( QgsProcessingFeatureSourceDefinition ( layer-> id (), true ) ) );
835
833
source.reset ( QgsProcessingParameters::parameterAsSource ( def, params, context ) );
836
834
QVERIFY ( source->uniqueValues ( -1 ).isEmpty () );
837
835
QVERIFY ( source->uniqueValues ( 10001 ).isEmpty () );
@@ -860,23 +858,31 @@ void TestQgsProcessing::createIndex()
860
858
p.addMapLayer ( layer );
861
859
context.setProject ( &p );
862
860
863
- QgsProcessingParameterDefinition *def = new QgsProcessingParameterString ( QStringLiteral ( " string " ) );
861
+ QgsProcessingParameterDefinition *def = new QgsProcessingParameterString ( QStringLiteral ( " layer " ) );
864
862
QVariantMap params;
865
- params.insert ( QStringLiteral ( " string " ), layer->id () );
863
+ params.insert ( QStringLiteral ( " layer " ), layer->id () );
866
864
867
865
// disable selected features check
868
- context.setFlags ( QgsProcessingContext::Flags ( 0 ) );
869
866
std::unique_ptr< QgsFeatureSource > source ( QgsProcessingParameters::parameterAsSource ( def, params, context ) );
867
+ QVERIFY ( source.get () );
870
868
QgsSpatialIndex index ( *source.get () );
871
869
QList<QgsFeatureId> ids = index.nearestNeighbor ( QgsPointXY ( 2.1 , 2 ), 1 );
872
870
QCOMPARE ( ids, QList<QgsFeatureId>() << 2 );
873
871
874
872
// selected features check, but none selected
875
- context. setFlags ( QgsProcessingContext::UseSelectionIfPresent );
873
+ params. insert ( QStringLiteral ( " layer " ), QVariant::fromValue ( QgsProcessingFeatureSourceDefinition ( layer-> id (), true ) ) );
876
874
source.reset ( QgsProcessingParameters::parameterAsSource ( def, params, context ) );
877
- index = QgsSpatialIndex ( *source.get () );
878
- ids = index.nearestNeighbor ( QgsPointXY ( 2.1 , 2 ), 1 );
879
- QCOMPARE ( ids, QList<QgsFeatureId>() << 2 );
875
+ bool caught;
876
+ try
877
+ {
878
+ index = QgsSpatialIndex ( *source.get () );
879
+ ids = index.nearestNeighbor ( QgsPointXY ( 2.1 , 2 ), 1 );
880
+ }
881
+ catch ( ... )
882
+ {
883
+ caught = true ;
884
+ }
885
+ QVERIFY ( caught );
880
886
881
887
// create selection
882
888
layer->selectByIds ( QgsFeatureIds () << 4 << 5 );
@@ -886,7 +892,7 @@ void TestQgsProcessing::createIndex()
886
892
QCOMPARE ( ids, QList<QgsFeatureId>() << 4 );
887
893
888
894
// selection but not using selection mode
889
- context. setFlags ( QgsProcessingContext::Flags ( 0 ) );
895
+ params. insert ( QStringLiteral ( " layer " ), QVariant::fromValue ( QgsProcessingFeatureSourceDefinition ( layer-> id (), false ) ) );
890
896
source.reset ( QgsProcessingParameters::parameterAsSource ( def, params, context ) );
891
897
index = QgsSpatialIndex ( *source.get () );
892
898
ids = index.nearestNeighbor ( QgsPointXY ( 2.1 , 2 ), 1 );
@@ -2305,6 +2311,22 @@ void TestQgsProcessing::combineLayerExtent()
2305
2311
QGSCOMPARENEAR ( ext.yMaximum (), 3536664 , 10 );
2306
2312
}
2307
2313
2314
+ void TestQgsProcessing::processingFeatureSource ()
2315
+ {
2316
+ QVariant source ( QStringLiteral ( " test.shp" ) );
2317
+ QgsProcessingFeatureSourceDefinition fs ( source, true );
2318
+ QCOMPARE ( fs.source , source );
2319
+ QVERIFY ( fs.selectedFeaturesOnly );
2320
+
2321
+ // test storing QgsProcessingFeatureSource in variant and retrieving
2322
+ QVariant fsInVariant = QVariant::fromValue ( fs );
2323
+ QVERIFY ( fsInVariant.isValid () );
2324
+
2325
+ QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( fsInVariant );
2326
+ QCOMPARE ( fromVar.source , source );
2327
+ QVERIFY ( fromVar.selectedFeaturesOnly );
2328
+ }
2329
+
2308
2330
void TestQgsProcessing::processingFeatureSink ()
2309
2331
{
2310
2332
QVariant sink ( QStringLiteral ( " test.shp" ) );
0 commit comments