Skip to content

Commit

Permalink
Restore correct layer naming when loading results
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent 5b9d925 commit 46596c5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 30 deletions.
13 changes: 7 additions & 6 deletions python/core/processing/qgsprocessingcontext.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions python/plugins/processing/gui/Postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
15 changes: 8 additions & 7 deletions src/core/processing/qgsprocessingcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/qgsprocessingparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
31 changes: 22 additions & 9 deletions tests/src/core/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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" ) );
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 46596c5

Please sign in to comment.