Skip to content
Permalink
Browse files

Restore correct layer naming when loading results

  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent 5b9d925 commit 46596c50f26a01c1bb8f403909c9277ff5ada8c3
@@ -83,24 +83,25 @@ class QgsProcessingContext
:rtype: QgsMapLayerStore
%End

QStringList layersToLoadOnCompletion() const;
QgsStringMap layersToLoadOnCompletion() const;
%Docstring
Returns a list of layers (by ID or datasource) to load into the canvas upon completion of the algorithm or model.
Returns a map of layers (by ID or datasource) to friendly layer name, to load into the canvas upon completion of the algorithm or model.
.. seealso:: setLayersToLoadOnCompletion()
.. seealso:: addLayerToLoadOnCompletion()
:rtype: list of str
:rtype: QgsStringMap
%End

void setLayersToLoadOnCompletion( const QStringList &layers );
void setLayersToLoadOnCompletion( const QgsStringMap &layers );
%Docstring
Sets the list of ``layers`` (by ID or datasource) to load into the canvas upon completion of the algorithm or model.
Sets the map of ``layers`` (by ID or datasource) to friendly layer name, to load into the canvas upon completion of the algorithm or model.
.. seealso:: addLayerToLoadOnCompletion()
.. seealso:: layersToLoadOnCompletion()
%End

void addLayerToLoadOnCompletion( const QString &layer );
void addLayerToLoadOnCompletion( const QString &layer, const QString &name );
%Docstring
Adds a ``layer`` to load (by ID or datasource) into the canvas upon completion of the algorithm or model.
The ``name`` parameter dictates a friendly display name for the layer.
.. seealso:: setLayersToLoadOnCompletion()
.. seealso:: layersToLoadOnCompletion()
%End
@@ -55,24 +55,19 @@ def handleAlgorithmResults(alg, context, feedback=None, showResults=True):
feedback = QgsProcessingFeedback()
feedback.setProgressText(QCoreApplication.translate('Postprocessing', 'Loading resulting layers'))
i = 0
for l in context.layersToLoadOnCompletion():
for l, name in context.layersToLoadOnCompletion().items():
feedback.setProgress(100 * i / float(len(context.layersToLoadOnCompletion())))
try:
layer = QgsProcessingUtils.mapLayerFromString(l, context)
if layer:
#TODO
#layer.setName(out.description())
layer.setName(name)
QgsProject.instance().addMapLayer(context.temporaryLayerStore().takeMapLayer(layer))
else:
if ProcessingConfig.getSetting(
ProcessingConfig.USE_FILENAME_AS_LAYER_NAME):
name = os.path.basename(l)
else:
#TODO
name = l # out.description
dataobjects.load(l, name, alg.crs,
RenderingStyles.getStyle(alg.id(), l))
#out.name))
except Exception:
QgsMessageLog.logMessage("Error loading result layer:\n" + traceback.format_exc(), 'Processing', QgsMessageLog.CRITICAL)
#wrongLayers.append(out.description())
@@ -102,33 +102,34 @@ class CORE_EXPORT QgsProcessingContext
QgsMapLayerStore *temporaryLayerStore() { return &tempLayerStore; }

/**
* Returns a list of layers (by ID or datasource) to load into the canvas upon completion of the algorithm or model.
* Returns a map of layers (by ID or datasource) to friendly layer name, to load into the canvas upon completion of the algorithm or model.
* \see setLayersToLoadOnCompletion()
* \see addLayerToLoadOnCompletion()
*/
QStringList layersToLoadOnCompletion() const
QgsStringMap layersToLoadOnCompletion() const
{
return mLayersToLoadOnCompletion;
}

/**
* Sets the list of \a layers (by ID or datasource) to load into the canvas upon completion of the algorithm or model.
* Sets the map of \a layers (by ID or datasource) to friendly layer name, to load into the canvas upon completion of the algorithm or model.
* \see addLayerToLoadOnCompletion()
* \see layersToLoadOnCompletion()
*/
void setLayersToLoadOnCompletion( const QStringList &layers )
void setLayersToLoadOnCompletion( const QgsStringMap &layers )
{
mLayersToLoadOnCompletion = layers;
}

/**
* Adds a \a layer to load (by ID or datasource) into the canvas upon completion of the algorithm or model.
* The \a name parameter dictates a friendly display name for the layer.
* \see setLayersToLoadOnCompletion()
* \see layersToLoadOnCompletion()
*/
void addLayerToLoadOnCompletion( const QString &layer )
void addLayerToLoadOnCompletion( const QString &layer, const QString &name )
{
mLayersToLoadOnCompletion << layer;
mLayersToLoadOnCompletion.insert( layer, name );
}

/**
@@ -208,7 +209,7 @@ class CORE_EXPORT QgsProcessingContext
QgsFeatureRequest::InvalidGeometryCheck mInvalidGeometryCheck = QgsFeatureRequest::GeometryNoCheck;
std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
QString mDefaultEncoding;
QStringList mLayersToLoadOnCompletion;
QgsStringMap mLayersToLoadOnCompletion;

#ifdef SIP_RUN
QgsProcessingContext( const QgsProcessingContext &other );
@@ -243,7 +243,7 @@ QgsFeatureSink *QgsProcessingParameters::parameterAsSink( const QgsProcessingPar
destinationIdentifier = dest;

if ( loadIntoProject )
context.addLayerToLoadOnCompletion( destinationIdentifier );
context.addLayerToLoadOnCompletion( destinationIdentifier, definition ? definition->description() : QString() );

return sink.release();
}
@@ -467,16 +467,28 @@ void TestQgsProcessing::context()
QgsVectorLayer *v1 = new QgsVectorLayer( "Polygon", "V1", "memory" );
QgsVectorLayer *v2 = new QgsVectorLayer( "Polygon", "V2", "memory" );
QVERIFY( context.layersToLoadOnCompletion().isEmpty() );
context.setLayersToLoadOnCompletion( QStringList() << v1->id() );
QgsStringMap layers;
layers.insert( v1->id(), QStringLiteral( "v1" ) );
context.setLayersToLoadOnCompletion( layers );
QCOMPARE( context.layersToLoadOnCompletion().count(), 1 );
QCOMPARE( context.layersToLoadOnCompletion().at( 0 ), v1->id() );
context.addLayerToLoadOnCompletion( v2->id() );
QCOMPARE( context.layersToLoadOnCompletion().keys().at( 0 ), v1->id() );
QCOMPARE( context.layersToLoadOnCompletion().values().at( 0 ), QStringLiteral( "v1" ) );
context.addLayerToLoadOnCompletion( v2->id(), QStringLiteral( "v2" ) );
QCOMPARE( context.layersToLoadOnCompletion().count(), 2 );
QCOMPARE( context.layersToLoadOnCompletion().at( 0 ), v1->id() );
QCOMPARE( context.layersToLoadOnCompletion().at( 1 ), v2->id() );
context.setLayersToLoadOnCompletion( QStringList() << v2->id() );
QCOMPARE( context.layersToLoadOnCompletion().keys().at( 0 ), v1->id() );
QCOMPARE( context.layersToLoadOnCompletion().values().at( 0 ), QStringLiteral( "v1" ) );
QCOMPARE( context.layersToLoadOnCompletion().keys().at( 1 ), v2->id() );
QCOMPARE( context.layersToLoadOnCompletion().values().at( 1 ), QStringLiteral( "v2" ) );
layers.clear();
layers.insert( v2->id(), QStringLiteral( "v2" ) );
context.setLayersToLoadOnCompletion( layers );
QCOMPARE( context.layersToLoadOnCompletion().count(), 1 );
QCOMPARE( context.layersToLoadOnCompletion().at( 0 ), v2->id() );
QCOMPARE( context.layersToLoadOnCompletion().keys().at( 0 ), v2->id() );
QCOMPARE( context.layersToLoadOnCompletion().values().at( 0 ), QStringLiteral( "v2" ) );
context.addLayerToLoadOnCompletion( v1->id(), QString() );
QCOMPARE( context.layersToLoadOnCompletion().count(), 2 );
QCOMPARE( context.layersToLoadOnCompletion().keys().at( 0 ), v1->id() );
QCOMPARE( context.layersToLoadOnCompletion().keys().at( 1 ), v2->id() );
delete v1;
delete v2;
}
@@ -1027,7 +1039,7 @@ void TestQgsProcessing::parameters()
QVERIFY( !QgsProcessingParameters::isDynamic( params, QStringLiteral( "bad" ) ) );

// parameterAsString
def = new QgsProcessingParameterString( QStringLiteral( "string" ) );
def = new QgsProcessingParameterString( QStringLiteral( "string" ), QStringLiteral( "desc" ) );
QCOMPARE( QgsProcessingParameters::parameterAsString( def, params, context ), QStringLiteral( "a string" ) );
def->setName( QStringLiteral( "double" ) );
QCOMPARE( QgsProcessingParameters::parameterAsString( def, params, context ).left( 3 ), QStringLiteral( "5.2" ) );
@@ -1154,7 +1166,8 @@ void TestQgsProcessing::parameters()

// make sure layer was automatically added to list to load on completion
QCOMPARE( context.layersToLoadOnCompletion().size(), 1 );
QCOMPARE( context.layersToLoadOnCompletion().at( 0 ), destId );
QCOMPARE( context.layersToLoadOnCompletion().keys().at( 0 ), destId );
QCOMPARE( context.layersToLoadOnCompletion().values().at( 0 ), QStringLiteral( "desc" ) );

delete def;
}

0 comments on commit 46596c5

Please sign in to comment.
You can’t perform that action at this time.