Skip to content

Commit

Permalink
Fix use of direct map layers as inputs for source parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 28, 2017
1 parent 891481d commit d2b9652
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
12 changes: 10 additions & 2 deletions python/plugins/processing/tests/AlgorithmsTestBase.py
Expand Up @@ -59,6 +59,7 @@

from qgis.core import (QgsVectorLayer,
QgsRasterLayer,
QgsMapLayer,
QgsProject,
QgsApplication,
QgsProcessingContext,
Expand Down Expand Up @@ -241,7 +242,10 @@ def check_results(self, results, context, params, expected):
if expected_result['type'] in ('vector', 'table'):
if 'compare' in expected_result and not expected_result['compare']:
# skipping the comparison, so just make sure output is valid
result_lyr = QgsProcessingUtils.mapLayerFromString(results[id], context)
if isinstance(results[id], QgsMapLayer):
result_lyr = results[id]
else:
result_lyr = QgsProcessingUtils.mapLayerFromString(results[id], context)
self.assertTrue(result_lyr.isValid())
continue

Expand All @@ -254,7 +258,11 @@ def check_results(self, results, context, params, expected):
except KeyError as e:
raise KeyError('Expected result {} does not exist in {}'.format(str(e), list(results.keys())))

result_lyr = QgsProcessingUtils.mapLayerFromString(results[id], context)
if isinstance(results[id], QgsMapLayer):
assert False
result_lyr = results[id]
else:
result_lyr = QgsProcessingUtils.mapLayerFromString(results[id], context)

compare = expected_result.get('compare', {})

Expand Down
10 changes: 10 additions & 0 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -277,6 +277,11 @@ QgsProcessingFeatureSource *QgsProcessingParameters::parameterAsSource( const Qg
val = fromVar.source;
}

if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) ) )
{
return new QgsProcessingFeatureSource( layer, context );
}

QString layerRef;
if ( val.canConvert<QgsProperty>() )
{
Expand All @@ -285,6 +290,11 @@ QgsProcessingFeatureSource *QgsProcessingParameters::parameterAsSource( const Qg
else if ( !val.isValid() || val.toString().isEmpty() )
{
// fall back to default
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( definition->defaultValue() ) ) )
{
return new QgsProcessingFeatureSource( layer, context );
}

layerRef = definition->defaultValue().toString();
}
else
Expand Down
9 changes: 9 additions & 0 deletions tests/src/core/testqgsprocessing.cpp
Expand Up @@ -3924,6 +3924,15 @@ void TestQgsProcessing::processingFeatureSource()
QVERIFY( source->getFeatures().nextFeature( f2 ) );
QCOMPARE( f2.geometry(), f.geometry() );

// direct map layer
params.insert( QStringLiteral( "layer" ), QVariant::fromValue( layer ) );
source.reset( QgsProcessingParameters::parameterAsSource( def.get(), params, context ) );
// can't directly match it to layer, so instead just get the feature and test that it matches what we expect
QVERIFY( source.get() );
QVERIFY( source->getFeatures().nextFeature( f2 ) );
QCOMPARE( f2.geometry(), f.geometry() );


// next using property based definition
params.insert( QStringLiteral( "layer" ), QgsProcessingFeatureSourceDefinition( QgsProperty::fromExpression( QStringLiteral( "trim('%1' + ' ')" ).arg( layer->id() ) ), false ) );
source.reset( QgsProcessingParameters::parameterAsSource( def.get(), params, context ) );
Expand Down

0 comments on commit d2b9652

Please sign in to comment.