Skip to content

Commit

Permalink
Add method to convert QgsProcessing::SourceType to string representation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 29, 2019
1 parent 7f7c7a9 commit d4b262c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/processing/qgsprocessing.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ and parameters.
TypeMesh
};

static QString sourceTypeToString( SourceType type );
%Docstring
Converts a source ``type`` to a string representation.

.. versionadded:: 3.6
%End

static const QString TEMPORARY_OUTPUT;
};

Expand Down
31 changes: 31 additions & 0 deletions src/core/processing/qgsprocessing.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,37 @@ class CORE_EXPORT QgsProcessing
TypeMesh = 6 //!< Mesh layers \since QGIS 3.6
};

/**
* Converts a source \a type to a string representation.
*
* \since QGIS 3.6
*/
static QString sourceTypeToString( SourceType type )
{
switch ( type )
{
case QgsProcessing::TypeMapLayer:
return QStringLiteral( "TypeMapLayer" );
case QgsProcessing::TypeVectorAnyGeometry:
return QStringLiteral( "TypeVectorAnyGeometry" );
case QgsProcessing::TypeVectorPoint:
return QStringLiteral( "TypeVectorPoint" );
case QgsProcessing::TypeVectorLine:
return QStringLiteral( "TypeVectorLine" );
case QgsProcessing::TypeVectorPolygon:
return QStringLiteral( "TypeVectorPolygon" );
case QgsProcessing::TypeRaster:
return QStringLiteral( "TypeRaster" );
case QgsProcessing::TypeFile:
return QStringLiteral( "TypeFile" );
case QgsProcessing::TypeVector:
return QStringLiteral( "TypeVector" );
case QgsProcessing::TypeMesh:
return QStringLiteral( "TypeMesh" );
}
return QString();
}

/**
* Constant used to indicate that a Processing algorithm output should be a temporary layer/file.
*
Expand Down
27 changes: 27 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,8 @@ class TestQgsProcessing: public QObject
void removeParameterType();
void parameterTypes();
void parameterType();
void sourceTypeToString_data();
void sourceTypeToString();

private:

Expand Down Expand Up @@ -7403,5 +7405,30 @@ void TestQgsProcessing::parameterType()
QCOMPARE( reg.parameterType( QStringLiteral( "paramType" ) ), paramType );
}

void TestQgsProcessing::sourceTypeToString_data()
{
QTest::addColumn<int>( "type" );
QTest::addColumn<QString>( "expected" );

// IMPORTANT -- these must match the original enum values!
QTest::newRow( "map layer" ) << static_cast< int >( QgsProcessing::TypeMapLayer ) << QStringLiteral( "TypeMapLayer" );
QTest::newRow( "map layer" ) << static_cast< int >( QgsProcessing::TypeVectorAnyGeometry ) << QStringLiteral( "TypeVectorAnyGeometry" );
QTest::newRow( "map layer" ) << static_cast< int >( QgsProcessing::TypeVectorPoint ) << QStringLiteral( "TypeVectorPoint" );
QTest::newRow( "map layer" ) << static_cast< int >( QgsProcessing::TypeVectorLine ) << QStringLiteral( "TypeVectorLine" );
QTest::newRow( "map layer" ) << static_cast< int >( QgsProcessing::TypeVectorPolygon ) << QStringLiteral( "TypeVectorPolygon" );
QTest::newRow( "map layer" ) << static_cast< int >( QgsProcessing::TypeRaster ) << QStringLiteral( "TypeRaster" );
QTest::newRow( "map layer" ) << static_cast< int >( QgsProcessing::TypeFile ) << QStringLiteral( "TypeFile" );
QTest::newRow( "map layer" ) << static_cast< int >( QgsProcessing::TypeMesh ) << QStringLiteral( "TypeMesh" );
}

void TestQgsProcessing::sourceTypeToString()
{
QFETCH( int, type );
QFETCH( QString, expected );

const QgsProcessing::SourceType sourceType = static_cast< QgsProcessing::SourceType >( type );
QCOMPARE( QgsProcessing::sourceTypeToString( sourceType ), expected );
}

QGSTEST_MAIN( TestQgsProcessing )
#include "testqgsprocessing.moc"

0 comments on commit d4b262c

Please sign in to comment.