diff --git a/python/core/auto_generated/processing/qgsprocessingutils.sip.in b/python/core/auto_generated/processing/qgsprocessingutils.sip.in index 8ecd73cd311..9603eeef3be 100644 --- a/python/core/auto_generated/processing/qgsprocessingutils.sip.in +++ b/python/core/auto_generated/processing/qgsprocessingutils.sip.in @@ -39,14 +39,15 @@ value. %End static QList< QgsVectorLayer * > compatibleVectorLayers( QgsProject *project, - const QList< QgsWkbTypes::GeometryType > &geometryTypes = QList< QgsWkbTypes::GeometryType >(), + const QList< int > &sourceTypes = QList< int >(), bool sort = true ); %Docstring Returns a list of vector layers from a ``project`` which are compatible with the processing framework. -If the ``geometryTypes`` list is non-empty then the layers will be sorted so that only -layers with geometry types included in the list will be returned. Leaving the ``geometryTypes`` +The ``sourceTypes`` list should be filled with a list of QgsProcessing.SourceType values. +If the ``sourceTypes`` list is non-empty then the layers will be sorted so that only +layers with the specified source type included in the list will be returned. Leaving the ``sourceTypes`` list empty will cause all vector layers, regardless of their geometry type, to be returned. If the ``sort`` argument is true then the layers will be sorted by their :py:func:`QgsMapLayer.name()` diff --git a/src/core/processing/qgsprocessing.h b/src/core/processing/qgsprocessing.h index a2f9bbbef0a..a45cb4fa5e6 100644 --- a/src/core/processing/qgsprocessing.h +++ b/src/core/processing/qgsprocessing.h @@ -20,7 +20,6 @@ #include "qgis_core.h" #include "qgis.h" -#include "qgsprocessingparameters.h" // // Output definitions diff --git a/src/core/processing/qgsprocessingalgorithm.h b/src/core/processing/qgsprocessingalgorithm.h index cef5148bc6b..4ec4ba41520 100644 --- a/src/core/processing/qgsprocessingalgorithm.h +++ b/src/core/processing/qgsprocessingalgorithm.h @@ -23,8 +23,8 @@ #include "qgsprocessingparameters.h" #include "qgsprocessingoutputs.h" #include "qgsprocessingcontext.h" -#include "qgsprocessingutils.h" #include "qgsfeaturesource.h" +#include "qgsprocessingutils.h" #include #include #include diff --git a/src/core/processing/qgsprocessingutils.cpp b/src/core/processing/qgsprocessingutils.cpp index 4e11d7a8c28..0e16a62d7db 100644 --- a/src/core/processing/qgsprocessingutils.cpp +++ b/src/core/processing/qgsprocessingutils.cpp @@ -51,7 +51,7 @@ QList QgsProcessingUtils::compatibleRasterLayers( QgsProject * return layers; } -QList QgsProcessingUtils::compatibleVectorLayers( QgsProject *project, const QList &geometryTypes, bool sort ) +QList QgsProcessingUtils::compatibleVectorLayers( QgsProject *project, const QList &geometryTypes, bool sort ) { if ( !project ) return QList(); @@ -81,7 +81,7 @@ QList QgsProcessingUtils::compatibleLayers( QgsProject *project, QList layers; Q_FOREACH ( QgsRasterLayer *rl, compatibleRasterLayers( project, false ) ) layers << rl; - Q_FOREACH ( QgsVectorLayer *vl, compatibleVectorLayers( project, QList< QgsWkbTypes::GeometryType >(), false ) ) + Q_FOREACH ( QgsVectorLayer *vl, compatibleVectorLayers( project, QList< int >(), false ) ) layers << vl; if ( sort ) @@ -288,10 +288,16 @@ bool QgsProcessingUtils::canUseLayer( const QgsRasterLayer *layer ) return layer && layer->providerType() == QStringLiteral( "gdal" ); } -bool QgsProcessingUtils::canUseLayer( const QgsVectorLayer *layer, const QList &geometryTypes ) +bool QgsProcessingUtils::canUseLayer( const QgsVectorLayer *layer, const QList &sourceTypes ) { return layer && - ( geometryTypes.isEmpty() || geometryTypes.contains( layer->geometryType() ) ); + ( sourceTypes.isEmpty() + || ( sourceTypes.contains( QgsProcessing::TypeVectorPoint ) && layer->geometryType() == QgsWkbTypes::PointGeometry ) + || ( sourceTypes.contains( QgsProcessing::TypeVectorLine ) && layer->geometryType() == QgsWkbTypes::LineGeometry ) + || ( sourceTypes.contains( QgsProcessing::TypeVectorPolygon ) && layer->geometryType() == QgsWkbTypes::PolygonGeometry ) + || ( sourceTypes.contains( QgsProcessing::TypeVectorAnyGeometry ) && layer->isSpatial() ) + || sourceTypes.contains( QgsProcessing::TypeVector ) + ); } QString QgsProcessingUtils::normalizeLayerSource( const QString &source ) @@ -330,7 +336,7 @@ void QgsProcessingUtils::parseDestinationString( QString &destination, QString & if ( !dsUri.table().isEmpty() ) { layerName = dsUri.table(); - options.insert( "layerName", layerName ); + options.insert( QStringLiteral( "layerName" ), layerName ); } uri = dsUri.database(); } @@ -654,6 +660,7 @@ QList QgsProcessingUtils::fieldNamesToIndices( const QStringList &fieldName QList indices; if ( !fieldNames.isEmpty() ) { + indices.reserve( fieldNames.count() ); for ( const QString &f : fieldNames ) { int idx = fields.lookupField( f ); @@ -663,6 +670,7 @@ QList QgsProcessingUtils::fieldNamesToIndices( const QStringList &fieldName } else { + indices.reserve( fields.count() ); for ( int i = 0; i < fields.count(); ++i ) indices.append( i ); } diff --git a/src/core/processing/qgsprocessingutils.h b/src/core/processing/qgsprocessingutils.h index 165cebfb0eb..193455ad9f7 100644 --- a/src/core/processing/qgsprocessingutils.h +++ b/src/core/processing/qgsprocessingutils.h @@ -24,6 +24,7 @@ #include "qgsvectorlayer.h" #include "qgsmessagelog.h" #include "qgsspatialindex.h" +#include "qgsprocessing.h" class QgsProject; class QgsProcessingContext; @@ -60,8 +61,9 @@ class CORE_EXPORT QgsProcessingUtils * Returns a list of vector layers from a \a project which are compatible with the processing * framework. * - * If the \a geometryTypes list is non-empty then the layers will be sorted so that only - * layers with geometry types included in the list will be returned. Leaving the \a geometryTypes + * The \a sourceTypes list should be filled with a list of QgsProcessing::SourceType values. + * If the \a sourceTypes list is non-empty then the layers will be sorted so that only + * layers with the specified source type included in the list will be returned. Leaving the \a sourceTypes * list empty will cause all vector layers, regardless of their geometry type, to be returned. * * If the \a sort argument is true then the layers will be sorted by their QgsMapLayer::name() @@ -70,7 +72,7 @@ class CORE_EXPORT QgsProcessingUtils * \see compatibleLayers() */ static QList< QgsVectorLayer * > compatibleVectorLayers( QgsProject *project, - const QList< QgsWkbTypes::GeometryType > &geometryTypes = QList< QgsWkbTypes::GeometryType >(), + const QList< int > &sourceTypes = QList< int >(), bool sort = true ); /** @@ -251,7 +253,7 @@ class CORE_EXPORT QgsProcessingUtils static bool canUseLayer( const QgsRasterLayer *layer ); static bool canUseLayer( const QgsVectorLayer *layer, - const QList< QgsWkbTypes::GeometryType > &geometryTypes = QList< QgsWkbTypes::GeometryType >() ); + const QList< int > &sourceTypes = QList< int >() ); /** * Interprets a \a string as a map layer from a store. diff --git a/tests/src/analysis/testqgsprocessing.cpp b/tests/src/analysis/testqgsprocessing.cpp index 379e94ebfe9..1a6707fcd7d 100644 --- a/tests/src/analysis/testqgsprocessing.cpp +++ b/tests/src/analysis/testqgsprocessing.cpp @@ -693,34 +693,45 @@ void TestQgsProcessing::compatibleLayers() // unsorted lIds.clear(); - Q_FOREACH ( QgsVectorLayer *vl, QgsProcessingUtils::compatibleVectorLayers( &p, QList(), false ) ) + Q_FOREACH ( QgsVectorLayer *vl, QgsProcessingUtils::compatibleVectorLayers( &p, QList(), false ) ) lIds << vl->name(); QCOMPARE( lIds, QStringList() << "V4" << "v1" << "v3" << "vvvv4" ); // point only lIds.clear(); - Q_FOREACH ( QgsVectorLayer *vl, QgsProcessingUtils::compatibleVectorLayers( &p, QList() << QgsWkbTypes::PointGeometry ) ) + Q_FOREACH ( QgsVectorLayer *vl, QgsProcessingUtils::compatibleVectorLayers( &p, QList() << QgsProcessing::TypeVectorPoint ) ) lIds << vl->name(); QCOMPARE( lIds, QStringList() << "v1" ); // polygon only lIds.clear(); - Q_FOREACH ( QgsVectorLayer *vl, QgsProcessingUtils::compatibleVectorLayers( &p, QList() << QgsWkbTypes::PolygonGeometry ) ) + Q_FOREACH ( QgsVectorLayer *vl, QgsProcessingUtils::compatibleVectorLayers( &p, QList() << QgsProcessing::TypeVectorPolygon ) ) lIds << vl->name(); QCOMPARE( lIds, QStringList() << "V4" ); // line only lIds.clear(); - Q_FOREACH ( QgsVectorLayer *vl, QgsProcessingUtils::compatibleVectorLayers( &p, QList() << QgsWkbTypes::LineGeometry ) ) + Q_FOREACH ( QgsVectorLayer *vl, QgsProcessingUtils::compatibleVectorLayers( &p, QList() << QgsProcessing::TypeVectorLine ) ) lIds << vl->name(); QCOMPARE( lIds, QStringList() << "v3" ); // point and line only lIds.clear(); - Q_FOREACH ( QgsVectorLayer *vl, QgsProcessingUtils::compatibleVectorLayers( &p, QList() << QgsWkbTypes::PointGeometry << QgsWkbTypes::LineGeometry ) ) + Q_FOREACH ( QgsVectorLayer *vl, QgsProcessingUtils::compatibleVectorLayers( &p, QList() << QgsProcessing::TypeVectorPoint << QgsProcessing::TypeVectorLine ) ) lIds << vl->name(); QCOMPARE( lIds, QStringList() << "v1" << "v3" ); + // any vector w geometry + lIds.clear(); + Q_FOREACH ( QgsVectorLayer *vl, QgsProcessingUtils::compatibleVectorLayers( &p, QList() << QgsProcessing::TypeVectorAnyGeometry ) ) + lIds << vl->name(); + QCOMPARE( lIds, QStringList() << "v1" << "v3" << "V4" ); + + // any vector + lIds.clear(); + Q_FOREACH ( QgsVectorLayer *vl, QgsProcessingUtils::compatibleVectorLayers( &p, QList() << QgsProcessing::TypeVector ) ) + lIds << vl->name(); + QCOMPARE( lIds, QStringList() << "v1" << "v3" << "V4" << "vvvv4" ); // all layers QVERIFY( QgsProcessingUtils::compatibleLayers( nullptr ).isEmpty() );