Skip to content

Commit 46596c5

Browse files
committedJun 5, 2017
Restore correct layer naming when loading results
1 parent 5b9d925 commit 46596c5

File tree

5 files changed

+40
-30
lines changed

5 files changed

+40
-30
lines changed
 

‎python/core/processing/qgsprocessingcontext.sip

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,25 @@ class QgsProcessingContext
8383
:rtype: QgsMapLayerStore
8484
%End
8585

86-
QStringList layersToLoadOnCompletion() const;
86+
QgsStringMap layersToLoadOnCompletion() const;
8787
%Docstring
88-
Returns a list of layers (by ID or datasource) to load into the canvas upon completion of the algorithm or model.
88+
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.
8989
.. seealso:: setLayersToLoadOnCompletion()
9090
.. seealso:: addLayerToLoadOnCompletion()
91-
:rtype: list of str
91+
:rtype: QgsStringMap
9292
%End
9393

94-
void setLayersToLoadOnCompletion( const QStringList &layers );
94+
void setLayersToLoadOnCompletion( const QgsStringMap &layers );
9595
%Docstring
96-
Sets the list of ``layers`` (by ID or datasource) to load into the canvas upon completion of the algorithm or model.
96+
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.
9797
.. seealso:: addLayerToLoadOnCompletion()
9898
.. seealso:: layersToLoadOnCompletion()
9999
%End
100100

101-
void addLayerToLoadOnCompletion( const QString &layer );
101+
void addLayerToLoadOnCompletion( const QString &layer, const QString &name );
102102
%Docstring
103103
Adds a ``layer`` to load (by ID or datasource) into the canvas upon completion of the algorithm or model.
104+
The ``name`` parameter dictates a friendly display name for the layer.
104105
.. seealso:: setLayersToLoadOnCompletion()
105106
.. seealso:: layersToLoadOnCompletion()
106107
%End

‎python/plugins/processing/gui/Postprocessing.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,19 @@ def handleAlgorithmResults(alg, context, feedback=None, showResults=True):
5555
feedback = QgsProcessingFeedback()
5656
feedback.setProgressText(QCoreApplication.translate('Postprocessing', 'Loading resulting layers'))
5757
i = 0
58-
for l in context.layersToLoadOnCompletion():
58+
for l, name in context.layersToLoadOnCompletion().items():
5959
feedback.setProgress(100 * i / float(len(context.layersToLoadOnCompletion())))
6060
try:
6161
layer = QgsProcessingUtils.mapLayerFromString(l, context)
6262
if layer:
63-
#TODO
64-
#layer.setName(out.description())
63+
layer.setName(name)
6564
QgsProject.instance().addMapLayer(context.temporaryLayerStore().takeMapLayer(layer))
6665
else:
6766
if ProcessingConfig.getSetting(
6867
ProcessingConfig.USE_FILENAME_AS_LAYER_NAME):
6968
name = os.path.basename(l)
70-
else:
71-
#TODO
72-
name = l # out.description
7369
dataobjects.load(l, name, alg.crs,
7470
RenderingStyles.getStyle(alg.id(), l))
75-
#out.name))
7671
except Exception:
7772
QgsMessageLog.logMessage("Error loading result layer:\n" + traceback.format_exc(), 'Processing', QgsMessageLog.CRITICAL)
7873
#wrongLayers.append(out.description())

‎src/core/processing/qgsprocessingcontext.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,33 +102,34 @@ class CORE_EXPORT QgsProcessingContext
102102
QgsMapLayerStore *temporaryLayerStore() { return &tempLayerStore; }
103103

104104
/**
105-
* Returns a list of layers (by ID or datasource) to load into the canvas upon completion of the algorithm or model.
105+
* 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.
106106
* \see setLayersToLoadOnCompletion()
107107
* \see addLayerToLoadOnCompletion()
108108
*/
109-
QStringList layersToLoadOnCompletion() const
109+
QgsStringMap layersToLoadOnCompletion() const
110110
{
111111
return mLayersToLoadOnCompletion;
112112
}
113113

114114
/**
115-
* Sets the list of \a layers (by ID or datasource) to load into the canvas upon completion of the algorithm or model.
115+
* 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.
116116
* \see addLayerToLoadOnCompletion()
117117
* \see layersToLoadOnCompletion()
118118
*/
119-
void setLayersToLoadOnCompletion( const QStringList &layers )
119+
void setLayersToLoadOnCompletion( const QgsStringMap &layers )
120120
{
121121
mLayersToLoadOnCompletion = layers;
122122
}
123123

124124
/**
125125
* Adds a \a layer to load (by ID or datasource) into the canvas upon completion of the algorithm or model.
126+
* The \a name parameter dictates a friendly display name for the layer.
126127
* \see setLayersToLoadOnCompletion()
127128
* \see layersToLoadOnCompletion()
128129
*/
129-
void addLayerToLoadOnCompletion( const QString &layer )
130+
void addLayerToLoadOnCompletion( const QString &layer, const QString &name )
130131
{
131-
mLayersToLoadOnCompletion << layer;
132+
mLayersToLoadOnCompletion.insert( layer, name );
132133
}
133134

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

213214
#ifdef SIP_RUN
214215
QgsProcessingContext( const QgsProcessingContext &other );

‎src/core/processing/qgsprocessingparameters.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ QgsFeatureSink *QgsProcessingParameters::parameterAsSink( const QgsProcessingPar
243243
destinationIdentifier = dest;
244244

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

248248
return sink.release();
249249
}

‎tests/src/core/testqgsprocessing.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -467,16 +467,28 @@ void TestQgsProcessing::context()
467467
QgsVectorLayer *v1 = new QgsVectorLayer( "Polygon", "V1", "memory" );
468468
QgsVectorLayer *v2 = new QgsVectorLayer( "Polygon", "V2", "memory" );
469469
QVERIFY( context.layersToLoadOnCompletion().isEmpty() );
470-
context.setLayersToLoadOnCompletion( QStringList() << v1->id() );
470+
QgsStringMap layers;
471+
layers.insert( v1->id(), QStringLiteral( "v1" ) );
472+
context.setLayersToLoadOnCompletion( layers );
471473
QCOMPARE( context.layersToLoadOnCompletion().count(), 1 );
472-
QCOMPARE( context.layersToLoadOnCompletion().at( 0 ), v1->id() );
473-
context.addLayerToLoadOnCompletion( v2->id() );
474+
QCOMPARE( context.layersToLoadOnCompletion().keys().at( 0 ), v1->id() );
475+
QCOMPARE( context.layersToLoadOnCompletion().values().at( 0 ), QStringLiteral( "v1" ) );
476+
context.addLayerToLoadOnCompletion( v2->id(), QStringLiteral( "v2" ) );
474477
QCOMPARE( context.layersToLoadOnCompletion().count(), 2 );
475-
QCOMPARE( context.layersToLoadOnCompletion().at( 0 ), v1->id() );
476-
QCOMPARE( context.layersToLoadOnCompletion().at( 1 ), v2->id() );
477-
context.setLayersToLoadOnCompletion( QStringList() << v2->id() );
478+
QCOMPARE( context.layersToLoadOnCompletion().keys().at( 0 ), v1->id() );
479+
QCOMPARE( context.layersToLoadOnCompletion().values().at( 0 ), QStringLiteral( "v1" ) );
480+
QCOMPARE( context.layersToLoadOnCompletion().keys().at( 1 ), v2->id() );
481+
QCOMPARE( context.layersToLoadOnCompletion().values().at( 1 ), QStringLiteral( "v2" ) );
482+
layers.clear();
483+
layers.insert( v2->id(), QStringLiteral( "v2" ) );
484+
context.setLayersToLoadOnCompletion( layers );
478485
QCOMPARE( context.layersToLoadOnCompletion().count(), 1 );
479-
QCOMPARE( context.layersToLoadOnCompletion().at( 0 ), v2->id() );
486+
QCOMPARE( context.layersToLoadOnCompletion().keys().at( 0 ), v2->id() );
487+
QCOMPARE( context.layersToLoadOnCompletion().values().at( 0 ), QStringLiteral( "v2" ) );
488+
context.addLayerToLoadOnCompletion( v1->id(), QString() );
489+
QCOMPARE( context.layersToLoadOnCompletion().count(), 2 );
490+
QCOMPARE( context.layersToLoadOnCompletion().keys().at( 0 ), v1->id() );
491+
QCOMPARE( context.layersToLoadOnCompletion().keys().at( 1 ), v2->id() );
480492
delete v1;
481493
delete v2;
482494
}
@@ -1027,7 +1039,7 @@ void TestQgsProcessing::parameters()
10271039
QVERIFY( !QgsProcessingParameters::isDynamic( params, QStringLiteral( "bad" ) ) );
10281040

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

11551167
// make sure layer was automatically added to list to load on completion
11561168
QCOMPARE( context.layersToLoadOnCompletion().size(), 1 );
1157-
QCOMPARE( context.layersToLoadOnCompletion().at( 0 ), destId );
1169+
QCOMPARE( context.layersToLoadOnCompletion().keys().at( 0 ), destId );
1170+
QCOMPARE( context.layersToLoadOnCompletion().values().at( 0 ), QStringLiteral( "desc" ) );
11581171

11591172
delete def;
11601173
}

0 commit comments

Comments
 (0)
Please sign in to comment.