Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[elevation Profile] Correctly add a layer generated by a processing #59850

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ Set where the new layers should be inserted - can be used to follow current sele
By default it is root group with zero index.

.. versionadded:: 3.10
%End

InsertionPoint layerInsertionPoint() const;
%Docstring
Returns the insertion point used to add layers to the tree

.. versionadded:: 3.42
%End

void setLayerInsertionMethod( Qgis::LayerTreeInsertionMethod method );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ Set where the new layers should be inserted - can be used to follow current sele
By default it is root group with zero index.

.. versionadded:: 3.10
%End

InsertionPoint layerInsertionPoint() const;
%Docstring
Returns the insertion point used to add layers to the tree

.. versionadded:: 3.42
%End

void setLayerInsertionMethod( Qgis::LayerTreeInsertionMethod method );
Expand Down
32 changes: 26 additions & 6 deletions python/plugins/processing/gui/Postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
QgsLayerTreeLayer,
QgsLayerTreeGroup,
QgsLayerTreeNode,
QgsLayerTreeRegistryBridge,
)
from qgis.utils import iface

Expand Down Expand Up @@ -237,8 +238,6 @@ 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)

# we don't add the layer to the tree yet -- that's done
# later, after we've sorted all added layers
layer_tree_layer = create_layer_tree_layer(owned_map_layer, details)
Expand Down Expand Up @@ -277,10 +276,15 @@ def handleAlgorithmResults(
current_selected_node = iface.layerTreeView().currentNode()
iface.layerTreeView().setUpdatesEnabled(False)

# store the current intersection point to restore it later
previous_insertion_point = (
details.project.layerTreeRegistryBridge().layerInsertionPoint()
)
for group, layer_node in sorted_layer_tree_layers:
layer_node.removeCustomProperty(SORT_ORDER_CUSTOM_PROPERTY)
insertion_point: Optional[QgsLayerTreeRegistryBridge.InsertionPoint] = None
if group is not None:
group.insertChildNode(0, layer_node)
insertion_point = QgsLayerTreeRegistryBridge.InsertionPoint(group, 0)
else:
# no destination group for this layer, so should be placed
# above the current layer
Expand All @@ -289,16 +293,32 @@ def handleAlgorithmResults(
current_node_index = current_node_group.children().index(
current_selected_node
)
current_node_group.insertChildNode(current_node_index, layer_node)
insertion_point = QgsLayerTreeRegistryBridge.InsertionPoint(
current_node_group, current_node_index
)
elif isinstance(current_selected_node, QgsLayerTreeGroup):
current_selected_node.insertChildNode(0, layer_node)
insertion_point = QgsLayerTreeRegistryBridge.InsertionPoint(
current_selected_node, 0
)
elif context.project():
context.project().layerTreeRoot().insertChildNode(0, layer_node)
insertion_point = QgsLayerTreeRegistryBridge.InsertionPoint()

if insertion_point:
details.project.layerTreeRegistryBridge().setLayerInsertionPoint(
insertion_point
)
nyalldawson marked this conversation as resolved.
Show resolved Hide resolved

details.project.addMapLayer(layer_node.layer())

if not have_set_active_layer and iface is not None:
iface.setActiveLayer(layer_node.layer())
have_set_active_layer = True

# reset to the previous insertion point
details.project.layerTreeRegistryBridge().setLayerInsertionPoint(
previous_insertion_point
)

# all layers have been added to the layer tree, so safe to call
# postProcessors now
for layer, details in layers_to_post_process:
Expand Down
6 changes: 6 additions & 0 deletions src/core/layertree/qgslayertreeregistrybridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ void QgsLayerTreeRegistryBridge::setLayerInsertionPoint( const InsertionPoint &i
mInsertionPointPosition = insertionPoint.position;
}

QgsLayerTreeRegistryBridge::InsertionPoint QgsLayerTreeRegistryBridge::layerInsertionPoint() const
{
QgsLayerTreeGroup *group = mInsertionPointGroup.isNull() ? mRoot : mInsertionPointGroup.data();
return InsertionPoint( group, mInsertionPointPosition );
}

void QgsLayerTreeRegistryBridge::layersAdded( const QList<QgsMapLayer *> &layers )
{
if ( !mEnabled )
Expand Down
6 changes: 6 additions & 0 deletions src/core/layertree/qgslayertreeregistrybridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ class CORE_EXPORT QgsLayerTreeRegistryBridge : public QObject
*/
void setLayerInsertionPoint( const InsertionPoint &insertionPoint );

/**
* Returns the insertion point used to add layers to the tree
* \since QGIS 3.42
*/
InsertionPoint layerInsertionPoint() const;

/**
* Sets the insertion \a method used to add layers to the tree
* \since QGIS 3.30
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ ADD_PYTHON_TEST(PyQgsLayerMetadataProviderPython test_qgslayermetadataprovider_p
ADD_PYTHON_TEST(PyQgsLayerMetadataProviderOgr test_qgslayermetadataprovider_ogr.py)
ADD_PYTHON_TEST(PyQgsLayerTree test_qgslayertree.py)
ADD_PYTHON_TEST(PyQgsLayerTreeFilterProxyModel test_qgslayertreefilterproxymodel.py)
ADD_PYTHON_TEST(PyQgsLayerTreeRegistryBridge test_qgslayertreeregistrybridge.py)
ADD_PYTHON_TEST(PyQgsLayout test_qgslayout.py)
ADD_PYTHON_TEST(PyQgsLayoutAlign test_qgslayoutaligner.py)
ADD_PYTHON_TEST(PyQgsLayoutAtlas test_qgslayoutatlas.py)
Expand Down
62 changes: 62 additions & 0 deletions tests/src/python/test_qgslayertreeregistrybridge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""QGIS Unit tests for QgsLayerTreeRegistryBridge

.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""

__author__ = "Jean Felder"
__date__ = "05/02/2025"
__copyright__ = "Copyright 2025, The QGIS Project"

from qgis.core import (
QgsLayerTreeRegistryBridge,
QgsLayerTree,
QgsProject,
QgsLayerTreeGroup,
)

import unittest
from qgis.testing import start_app, QgisTestCase

start_app()


class TestQgsLayerTreeRegistryBridge(QgisTestCase):

def test_constructor(self):
project = QgsProject()
root_group = QgsLayerTree()
bridge = QgsLayerTreeRegistryBridge(root_group, project)

self.assertTrue(bridge.isEnabled())
self.assertTrue(bridge.newLayersVisible())
self.assertEqual(bridge.layerInsertionPoint().group, root_group)
self.assertEqual(bridge.layerInsertionPoint().position, 0)

def test_insertion_point(self):
project = QgsProject()
root_group = QgsLayerTree()
bridge = QgsLayerTreeRegistryBridge(root_group, project)

self.assertEqual(bridge.layerInsertionPoint().group, root_group)
self.assertEqual(bridge.layerInsertionPoint().position, 0)

group_node = QgsLayerTreeGroup()
root_group.addChildNode(group_node)
bridge.setLayerInsertionPoint(
QgsLayerTreeRegistryBridge.InsertionPoint(group_node, 4)
)
self.assertEqual(bridge.layerInsertionPoint().group, group_node)
self.assertEqual(bridge.layerInsertionPoint().position, 4)

bridge.setLayerInsertionPoint(
QgsLayerTreeRegistryBridge.InsertionPoint(root_group, 0)
)
self.assertEqual(bridge.layerInsertionPoint().group, root_group)
self.assertEqual(bridge.layerInsertionPoint().position, 0)


if __name__ == "__main__":
unittest.main()