Skip to content

Commit

Permalink
scope enum for QgsProcessing::LayerHint (#9639)
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Mar 27, 2019
1 parent 7fb752e commit 631c701
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 41 deletions.
12 changes: 12 additions & 0 deletions python/core/auto_additions/qgsprocessingutils.py
@@ -0,0 +1,12 @@
# The following has been generated automatically from src/core/processing/qgsprocessingutils.h
# monkey patching scoped based enum
QgsProcessingUtils.UnknownType = QgsProcessingUtils.LayerHint.UnknownType
QgsProcessingUtils.LayerHint.UnknownType.__doc__ = "Unknown layer type"
QgsProcessingUtils.Vector = QgsProcessingUtils.LayerHint.Vector
QgsProcessingUtils.LayerHint.Vector.__doc__ = "Vector layer type"
QgsProcessingUtils.Raster = QgsProcessingUtils.LayerHint.Raster
QgsProcessingUtils.LayerHint.Raster.__doc__ = "Raster layer type"
QgsProcessingUtils.Mesh = QgsProcessingUtils.LayerHint.Mesh
QgsProcessingUtils.LayerHint.Mesh.__doc__ = "Mesh layer type \since QGIS 3.6"
QgsProcessingUtils.LayerHint.__doc__ = 'Layer type hints.\n\n.. versionadded:: 3.4\n\n' + '* ``UnknownType``: ' + QgsProcessingUtils.LayerHint.UnknownType.__doc__ + '\n' + '* ``Vector``: ' + QgsProcessingUtils.LayerHint.Vector.__doc__ + '\n' + '* ``Raster``: ' + QgsProcessingUtils.LayerHint.Raster.__doc__ + '\n' + '* ``Mesh``: ' + QgsProcessingUtils.LayerHint.Mesh.__doc__
# --
Expand Up @@ -126,7 +126,7 @@ Details for layers to load into projects.
%End
public:

LayerDetails( const QString &name, QgsProject *project, const QString &outputName = QString(), QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::UnknownType );
LayerDetails( const QString &name, QgsProject *project, const QString &outputName = QString(), QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::LayerHint::UnknownType );
%Docstring
Constructor for LayerDetails.
%End
Expand Down
Expand Up @@ -92,15 +92,15 @@ value.
.. seealso:: :py:func:`compatibleVectorLayers`
%End

enum LayerHint
enum class LayerHint
{
UnknownType,
Vector,
Raster,
Mesh,
};

static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, LayerHint typeHint = UnknownType );
static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
%Docstring
Interprets a string as a map layer within the supplied ``context``.

Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Expand Up @@ -808,7 +808,7 @@ QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> QgsProcessingMode
}
if ( !featureSource )
{
if ( QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( value.toString(), context, true, QgsProcessingUtils::Vector ) ) )
if ( QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( value.toString(), context, true, QgsProcessingUtils::LayerHint::Vector ) ) )
featureSource = vl;
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/processing/qgsprocessingcontext.h
Expand Up @@ -170,7 +170,7 @@ class CORE_EXPORT QgsProcessingContext
/**
* Constructor for LayerDetails.
*/
LayerDetails( const QString &name, QgsProject *project, const QString &outputName = QString(), QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::UnknownType )
LayerDetails( const QString &name, QgsProject *project, const QString &outputName = QString(), QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::LayerHint::UnknownType )
: name( name )
, outputName( outputName )
, layerTypeHint( layerTypeHint )
Expand All @@ -191,7 +191,7 @@ class CORE_EXPORT QgsProcessingContext
*
* \since QGIS 3.4
*/
QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::UnknownType;
QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::LayerHint::UnknownType;

/**
* Layer post-processor. May be NULLPTR if no post-processing is required.
Expand Down
22 changes: 11 additions & 11 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -427,7 +427,7 @@ QgsFeatureSink *QgsProcessingParameters::parameterAsSink( const QgsProcessingPar
QString outputName;
if ( definition )
outputName = definition->name();
context.addLayerToLoadOnCompletion( destinationIdentifier, QgsProcessingContext::LayerDetails( destName, destinationProject, outputName, QgsProcessingUtils::Vector ) );
context.addLayerToLoadOnCompletion( destinationIdentifier, QgsProcessingContext::LayerDetails( destName, destinationProject, outputName, QgsProcessingUtils::LayerHint::Vector ) );
}

return sink.release();
Expand Down Expand Up @@ -506,7 +506,7 @@ QString QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( const Qgs
if ( layerRef.isEmpty() )
return QString();

vl = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( layerRef, context, true, QgsProcessingUtils::Vector ) );
vl = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( layerRef, context, true, QgsProcessingUtils::LayerHint::Vector ) );
}
}

Expand Down Expand Up @@ -652,11 +652,11 @@ QString QgsProcessingParameters::parameterAsOutputLayer( const QgsProcessingPara
if ( definition )
outputName = definition->name();

QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::UnknownType;
QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::LayerHint::UnknownType;
if ( definition->type() == QgsProcessingParameterVectorDestination::typeName() )
layerTypeHint = QgsProcessingUtils::Vector;
layerTypeHint = QgsProcessingUtils::LayerHint::Vector;
else if ( definition->type() == QgsProcessingParameterRasterDestination::typeName() )
layerTypeHint = QgsProcessingUtils::Raster;
layerTypeHint = QgsProcessingUtils::LayerHint::Raster;

context.addLayerToLoadOnCompletion( dest, QgsProcessingContext::LayerDetails( destName, destinationProject, outputName, layerTypeHint ) );
}
Expand Down Expand Up @@ -2976,7 +2976,7 @@ bool QgsProcessingParameterRasterLayer::checkValueIsAcceptable( const QVariant &
}

// try to load as layer
if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context, true, QgsProcessingUtils::Raster ) )
if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context, true, QgsProcessingUtils::LayerHint::Raster ) )
return true;

return false;
Expand Down Expand Up @@ -3496,7 +3496,7 @@ bool QgsProcessingParameterVectorLayer::checkValueIsAcceptable( const QVariant &
}

// try to load as layer
if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context, true, QgsProcessingUtils::Vector ) )
if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context, true, QgsProcessingUtils::LayerHint::Vector ) )
return true;

return false;
Expand Down Expand Up @@ -3629,7 +3629,7 @@ bool QgsProcessingParameterMeshLayer::checkValueIsAcceptable( const QVariant &v,
}

// try to load as layer
if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context, true, QgsProcessingUtils::Mesh ) )
if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context, true, QgsProcessingUtils::LayerHint::Mesh ) )
return true;

return false;
Expand Down Expand Up @@ -3969,7 +3969,7 @@ bool QgsProcessingParameterFeatureSource::checkValueIsAcceptable( const QVariant
}

// try to load as layer
if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context, true, QgsProcessingUtils::Vector ) )
if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context, true, QgsProcessingUtils::LayerHint::Vector ) )
return true;

return false;
Expand All @@ -3996,7 +3996,7 @@ QString QgsProcessingParameterFeatureSource::valueAsPythonString( const QVariant
{
QString layerString = fromVar.source.staticValue().toString();
// prefer to use layer source instead of id if possible (since it's persistent)
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerString, context, true, QgsProcessingUtils::Vector ) ) )
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerString, context, true, QgsProcessingUtils::LayerHint::Vector ) ) )
layerString = layer->source();
return QgsProcessingUtils::stringToPythonLiteral( layerString );
}
Expand All @@ -4021,7 +4021,7 @@ QString QgsProcessingParameterFeatureSource::valueAsPythonString( const QVariant
QString layerString = value.toString();

// prefer to use layer source if possible (since it's persistent)
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerString, context, true, QgsProcessingUtils::Vector ) ) )
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerString, context, true, QgsProcessingUtils::LayerHint::Vector ) ) )
layerString = layer->source();

return QgsProcessingUtils::stringToPythonLiteral( layerString );
Expand Down
16 changes: 8 additions & 8 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -159,16 +159,16 @@ QgsMapLayer *QgsProcessingUtils::mapLayerFromStore( const QString &string, QgsMa
{
switch ( typeHint )
{
case UnknownType:
case LayerHint::UnknownType:
return true;

case Vector:
case LayerHint::Vector:
return l->type() == QgsMapLayerType::VectorLayer;

case Raster:
case LayerHint::Raster:
return l->type() == QgsMapLayerType::RasterLayer;

case Mesh:
case LayerHint::Mesh:
return l->type() == QgsMapLayerType::MeshLayer;
}
return true;
Expand Down Expand Up @@ -235,7 +235,7 @@ QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string,
QString name = fi.baseName();

// brute force attempt to load a matching layer
if ( typeHint == UnknownType || typeHint == Vector )
if ( typeHint == LayerHint::UnknownType || typeHint == LayerHint::Vector )
{
QgsVectorLayer::LayerOptions options;
options.loadDefaultStyle = false;
Expand All @@ -245,7 +245,7 @@ QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string,
return layer.release();
}
}
if ( typeHint == UnknownType || typeHint == Raster )
if ( typeHint == LayerHint::UnknownType || typeHint == LayerHint::Raster )
{
QgsRasterLayer::LayerOptions rasterOptions;
rasterOptions.loadDefaultStyle = false;
Expand All @@ -255,7 +255,7 @@ QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string,
return rasterLayer.release();
}
}
if ( typeHint == UnknownType || typeHint == Mesh )
if ( typeHint == LayerHint::UnknownType || typeHint == LayerHint::Mesh )
{
QgsMeshLayer::LayerOptions meshOptions;
std::unique_ptr< QgsMeshLayer > meshLayer( new QgsMeshLayer( string, name, QStringLiteral( "mdal" ), meshOptions ) );
Expand Down Expand Up @@ -346,7 +346,7 @@ QgsProcessingFeatureSource *QgsProcessingUtils::variantToSource( const QVariant
if ( layerRef.isEmpty() )
return nullptr;

QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( layerRef, context, true, Vector ) );
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( layerRef, context, true, LayerHint::Vector ) );
if ( !vl )
return nullptr;

Expand Down
8 changes: 4 additions & 4 deletions src/core/processing/qgsprocessingutils.h
Expand Up @@ -109,7 +109,7 @@ class CORE_EXPORT QgsProcessingUtils
* Layer type hints.
* \since QGIS 3.4
*/
enum LayerHint
enum class LayerHint SIP_MONKEYPATCH_SCOPEENUM : int
{
UnknownType, //!< Unknown layer type
Vector, //!< Vector layer type
Expand All @@ -129,7 +129,7 @@ class CORE_EXPORT QgsProcessingUtils
*
* The \a typeHint can be used to dictate the type of map layer expected.
*/
static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, LayerHint typeHint = UnknownType );
static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );

/**
* Converts a variant \a value to a new feature source.
Expand Down Expand Up @@ -308,15 +308,15 @@ class CORE_EXPORT QgsProcessingUtils
* returned.
* \see mapLayerFromString()
*/
static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store, LayerHint typeHint = UnknownType );
static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );

/**
* Interprets a string as a map layer. The method will attempt to
* load a layer matching the passed \a string. E.g. if the string is a file path,
* then the layer at this file path will be loaded.
* The caller takes responsibility for deleting the returned map layer.
*/
static QgsMapLayer *loadMapLayerFromString( const QString &string, LayerHint typeHint = UnknownType );
static QgsMapLayer *loadMapLayerFromString( const QString &string, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );

static void parseDestinationString( QString &destination, QString &providerKey, QString &uri, QString &layerName, QString &format, QMap<QString, QVariant> &options, bool &useWriter, QString &extension );

Expand Down
24 changes: 12 additions & 12 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -1141,20 +1141,20 @@ void TestQgsProcessing::mapLayerFromString()
QVERIFY( !c.getMapLayer( QString() ) );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( raster1, c ), r1 );
QCOMPARE( c.getMapLayer( raster1 ), r1 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( raster1, c, true, QgsProcessingUtils::Raster ), r1 );
QVERIFY( !QgsProcessingUtils::mapLayerFromString( raster1, c, true, QgsProcessingUtils::Vector ) );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( raster1, c, true, QgsProcessingUtils::LayerHint::Raster ), r1 );
QVERIFY( !QgsProcessingUtils::mapLayerFromString( raster1, c, true, QgsProcessingUtils::LayerHint::Vector ) );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( raster2, c ), r2 );
QCOMPARE( c.getMapLayer( raster2 ), r2 );
QVERIFY( !QgsProcessingUtils::mapLayerFromString( raster2, c, true, QgsProcessingUtils::Vector ) );
QVERIFY( !QgsProcessingUtils::mapLayerFromString( raster2, c, true, QgsProcessingUtils::LayerHint::Vector ) );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "R1", c ), r1 );
QCOMPARE( c.getMapLayer( "R1" ), r1 );
QVERIFY( !QgsProcessingUtils::mapLayerFromString( "R1", c, true, QgsProcessingUtils::Vector ) );
QVERIFY( !QgsProcessingUtils::mapLayerFromString( "R1", c, true, QgsProcessingUtils::LayerHint::Vector ) );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "ar2", c ), r2 );
QCOMPARE( c.getMapLayer( "ar2" ), r2 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "V4", c ), v1 );
QCOMPARE( c.getMapLayer( "V4" ), v1 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "V4", c, true, QgsProcessingUtils::Vector ), v1 );
QVERIFY( !QgsProcessingUtils::mapLayerFromString( "V4", c, true, QgsProcessingUtils::Raster ) );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "V4", c, true, QgsProcessingUtils::LayerHint::Vector ), v1 );
QVERIFY( !QgsProcessingUtils::mapLayerFromString( "V4", c, true, QgsProcessingUtils::LayerHint::Raster ) );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "v1", c ), v2 );
QCOMPARE( c.getMapLayer( "v1" ), v2 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( r1->id(), c ), r1 );
Expand All @@ -1168,8 +1168,8 @@ void TestQgsProcessing::mapLayerFromString()
c.temporaryLayerStore()->addMapLayers( QList<QgsMapLayer *>() << v5 << v6 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "V5", c ), v5 );
QCOMPARE( c.getMapLayer( "V5" ), v5 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "V5", c, true, QgsProcessingUtils::Vector ), v5 );
QVERIFY( !QgsProcessingUtils::mapLayerFromString( "V5", c, true, QgsProcessingUtils::Raster ) );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "V5", c, true, QgsProcessingUtils::LayerHint::Vector ), v5 );
QVERIFY( !QgsProcessingUtils::mapLayerFromString( "V5", c, true, QgsProcessingUtils::LayerHint::Raster ) );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "v6", c ), v6 );
QCOMPARE( c.getMapLayer( "v6" ), v6 );
QCOMPARE( QgsProcessingUtils::mapLayerFromString( v5->id(), c ), v5 );
Expand Down Expand Up @@ -5413,7 +5413,7 @@ void TestQgsProcessing::parameterVectorOut()
QCOMPARE( context.layersToLoadOnCompletion().size(), 1 );
QCOMPARE( context.layersToLoadOnCompletion().keys().at( 0 ), QStringLiteral( "test.shp" ) );
QCOMPARE( context.layersToLoadOnCompletion().values().at( 0 ).name, QStringLiteral( "desc" ) );
QCOMPARE( context.layersToLoadOnCompletion().values().at( 0 ).layerTypeHint, QgsProcessingUtils::Vector );
QCOMPARE( context.layersToLoadOnCompletion().values().at( 0 ).layerTypeHint, QgsProcessingUtils::LayerHint::Vector );

// with name overloading
QgsProcessingContext context2;
Expand All @@ -5426,7 +5426,7 @@ void TestQgsProcessing::parameterVectorOut()
QCOMPARE( context2.layersToLoadOnCompletion().keys().at( 0 ), QStringLiteral( "test.shp" ) );
QCOMPARE( context2.layersToLoadOnCompletion().values().at( 0 ).name, QStringLiteral( "my_dest" ) );
QCOMPARE( context2.layersToLoadOnCompletion().values().at( 0 ).outputName, QStringLiteral( "x" ) );
QCOMPARE( context2.layersToLoadOnCompletion().values().at( 0 ).layerTypeHint, QgsProcessingUtils::Vector );
QCOMPARE( context2.layersToLoadOnCompletion().values().at( 0 ).layerTypeHint, QgsProcessingUtils::LayerHint::Vector );

QgsProcessingContext context3;
params.insert( QStringLiteral( "x" ), QgsProcessing::TEMPORARY_OUTPUT );
Expand Down Expand Up @@ -5590,7 +5590,7 @@ void TestQgsProcessing::parameterRasterOut()
QCOMPARE( context.layersToLoadOnCompletion().size(), 1 );
QCOMPARE( context.layersToLoadOnCompletion().keys().at( 0 ), QStringLiteral( "test.tif" ) );
QCOMPARE( context.layersToLoadOnCompletion().values().at( 0 ).name, QStringLiteral( "desc" ) );
QCOMPARE( context.layersToLoadOnCompletion().values().at( 0 ).layerTypeHint, QgsProcessingUtils::Raster );
QCOMPARE( context.layersToLoadOnCompletion().values().at( 0 ).layerTypeHint, QgsProcessingUtils::LayerHint::Raster );

// with name overloading
QgsProcessingContext context2;
Expand All @@ -5603,7 +5603,7 @@ void TestQgsProcessing::parameterRasterOut()
QCOMPARE( context2.layersToLoadOnCompletion().keys().at( 0 ), QStringLiteral( "test.tif" ) );
QCOMPARE( context2.layersToLoadOnCompletion().values().at( 0 ).name, QStringLiteral( "my_dest" ) );
QCOMPARE( context2.layersToLoadOnCompletion().values().at( 0 ).outputName, QStringLiteral( "x" ) );
QCOMPARE( context2.layersToLoadOnCompletion().values().at( 0 ).layerTypeHint, QgsProcessingUtils::Raster );
QCOMPARE( context2.layersToLoadOnCompletion().values().at( 0 ).layerTypeHint, QgsProcessingUtils::LayerHint::Raster );
}

void TestQgsProcessing::parameterFileOut()
Expand Down

0 comments on commit 631c701

Please sign in to comment.