Skip to content

Commit

Permalink
postprocessing: Correctly call addMapLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitjano committed Dec 12, 2024
1 parent b6e18bb commit 40f5d51
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions python/plugins/processing/gui/Postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ def handleAlgorithmResults(
# output layer already exists in the destination project
owned_map_layer = context.temporaryLayerStore().takeMapLayer(layer)
if owned_map_layer:
details.project.addMapLayer(owned_map_layer, False)
# If the layer is part of a group, do not add it to the legend
add_to_legend = results_group is None
details.project.addMapLayer(owned_map_layer, add_to_legend)

# we don't add the layer to the tree yet -- that's done
# later, after we've sorted all added layers
Expand Down Expand Up @@ -282,6 +284,12 @@ def handleAlgorithmResults(
if group is not None:
group.insertChildNode(0, layer_node)
else:
# a new node has already been created by the `addMapLayer` call
# move the cloned node to the correct location and the remove
# the first one
root_node = context.project().layerTreeRoot()
previous_layer_node = root_node.findLayer(layer_node.layerId())
previous_layer_group: Optional[QgsLayerTreeGroup] = None
# no destination group for this layer, so should be placed
# above the current layer
if isinstance(current_selected_node, QgsLayerTreeLayer):
Expand All @@ -290,10 +298,17 @@ def handleAlgorithmResults(
current_selected_node
)
current_node_group.insertChildNode(current_node_index, layer_node)
previous_layer_group = current_node_group
elif isinstance(current_selected_node, QgsLayerTreeGroup):
current_selected_node.insertChildNode(0, layer_node)
previous_layer_group = current_selected_node
elif context.project():
context.project().layerTreeRoot().insertChildNode(0, layer_node)
root_node.insertChildNode(0, layer_node)
previous_layer_group = root_node

# remove the previous node added by the `addMapLayer` call
if previous_layer_group and previous_layer_node:
previous_layer_group.removeChildNode(previous_layer_node)

if not have_set_active_layer and iface is not None:
iface.setActiveLayer(layer_node.layer())
Expand Down

0 comments on commit 40f5d51

Please sign in to comment.