Skip to content

Commit

Permalink
[processing][modeler] Fix some child algorithm vector results
Browse files Browse the repository at this point in the history
are not shown as input choices for other child algorithms
  • Loading branch information
nyalldawson committed May 7, 2018
1 parent 55a256f commit 3791bac
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,9 +688,9 @@ QgsProcessingModelChildParameterSources QgsProcessingModelAlgorithm::availableSo
{
const QgsProcessingOutputVectorLayer *vectorOut = static_cast< const QgsProcessingOutputVectorLayer *>( out );

if ( !( dataTypes.contains( vectorOut->dataType() ) || vectorOut->dataType() == QgsProcessing::TypeMapLayer || vectorOut->dataType() == QgsProcessing::TypeVector
|| vectorOut->dataType() == QgsProcessing::TypeVectorAnyGeometry ) )
if ( !vectorOutputIsCompatibleType( dataTypes, vectorOut->dataType() ) )
{
//unacceptable output
continue;
}
}
Expand Down Expand Up @@ -888,6 +888,23 @@ bool QgsProcessingModelAlgorithm::loadVariant( const QVariant &model )
return true;
}

bool QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( const QList<int> &acceptableDataTypes, QgsProcessing::SourceType outputType )
{
// This method is intended to be "permissive" rather than "restrictive".
// I.e. we only reject outputs which we know can NEVER be acceptable, but
// if there's doubt then we default to returning true.
return ( acceptableDataTypes.empty()
|| acceptableDataTypes.contains( outputType )
|| outputType == QgsProcessing::TypeMapLayer
|| outputType == QgsProcessing::TypeVector
|| outputType == QgsProcessing::TypeVectorAnyGeometry
|| acceptableDataTypes.contains( QgsProcessing::TypeVector )
|| acceptableDataTypes.contains( QgsProcessing::TypeMapLayer )
|| ( acceptableDataTypes.contains( QgsProcessing::TypeVectorAnyGeometry ) && ( outputType == QgsProcessing::TypeVectorPoint ||
outputType == QgsProcessing::TypeVectorLine ||
outputType == QgsProcessing::TypeVectorPolygon ) ) );
}

bool QgsProcessingModelAlgorithm::toFile( const QString &path ) const
{
QDomDocument doc = QDomDocument( QStringLiteral( "model" ) );
Expand Down
11 changes: 11 additions & 0 deletions src/core/processing/models/qgsprocessingmodelalgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,17 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
*/
bool loadVariant( const QVariant &model );

/**
* Checks whether the output vector type given by \a outputType is compatible
* with the list of acceptable data types specified by \a acceptableDataTypes.
* Returns true if vector output is compatible.
*
* \note This method is intended to be "permissive" rather than "restrictive".
* I.e. we only reject outputs which we know can NEVER be acceptable, but
* if there's doubt then we default to returning true.
*/
static bool vectorOutputIsCompatibleType( const QList<int> &acceptableDataTypes, QgsProcessing::SourceType outputType );

friend class TestQgsProcessing;
};

Expand Down
76 changes: 76 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ class TestQgsProcessing: public QObject
void asPythonCommand();
void modelerAlgorithm();
void modelExecution();
void modelVectorOutputIsCompatibleType();
void modelAcceptableValues();
void tempUtils();
void convertCompatible();
Expand Down Expand Up @@ -5908,6 +5909,81 @@ void TestQgsProcessing::modelExecution()
QCOMPARE( actualParts, expectedParts );
}

void TestQgsProcessing::modelVectorOutputIsCompatibleType()
{
// IMPORTANT: This method is intended to be "permissive" rather than "restrictive".
// I.e. we only reject outputs which we know can NEVER be acceptable, but
// if there's doubt then we default to returning true.

// empty acceptable type list = all are compatible
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( QList<int>(), QgsProcessing::TypeVector ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( QList<int>(), QgsProcessing::TypeVectorAnyGeometry ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( QList<int>(), QgsProcessing::TypeVectorPoint ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( QList<int>(), QgsProcessing::TypeVectorLine ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( QList<int>(), QgsProcessing::TypeVectorPolygon ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( QList<int>(), QgsProcessing::TypeMapLayer ) );

// accept any vector
QList< int > dataTypes;
dataTypes << QgsProcessing::TypeVector;
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVector ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorAnyGeometry ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorPoint ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorLine ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorPolygon ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeMapLayer ) );

// accept any vector with geometry
dataTypes.clear();
dataTypes << QgsProcessing::TypeVectorAnyGeometry;
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVector ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorAnyGeometry ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorPoint ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorLine ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorPolygon ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeMapLayer ) );

// accept any point vector
dataTypes.clear();
dataTypes << QgsProcessing::TypeVectorPoint;
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVector ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorAnyGeometry ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorPoint ) );
QVERIFY( !QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorLine ) );
QVERIFY( !QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorPolygon ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeMapLayer ) );

// accept any line vector
dataTypes.clear();
dataTypes << QgsProcessing::TypeVectorLine;
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVector ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorAnyGeometry ) );
QVERIFY( !QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorPoint ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorLine ) );
QVERIFY( !QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorPolygon ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeMapLayer ) );

// accept any polygon vector
dataTypes.clear();
dataTypes << QgsProcessing::TypeVectorPolygon;
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVector ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorAnyGeometry ) );
QVERIFY( !QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorPoint ) );
QVERIFY( !QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorLine ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorPolygon ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeMapLayer ) );

// accept any map layer
dataTypes.clear();
dataTypes << QgsProcessing::TypeMapLayer;
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVector ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorAnyGeometry ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorPoint ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorLine ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeVectorPolygon ) );
QVERIFY( QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType( dataTypes, QgsProcessing::TypeMapLayer ) );
}

void TestQgsProcessing::modelAcceptableValues()
{
QgsProcessingModelAlgorithm m;
Expand Down

0 comments on commit 3791bac

Please sign in to comment.