diff --git a/gaphor/core/modeling/presentation.py b/gaphor/core/modeling/presentation.py index 9f2c53ec9..0d831685f 100644 --- a/gaphor/core/modeling/presentation.py +++ b/gaphor/core/modeling/presentation.py @@ -102,7 +102,13 @@ class Presentation(Matrices, Element, Generic[S]): self.parent.matrix_i2c.remove_handler(self._on_matrix_changed) if diagram := self._original_diagram: + connecting_items = list( + cinfo.item + for cinfo in diagram.connections.get_connections(connected=self) + ) diagram.connections.remove_connections_to_item(self) + for item in connecting_items: + item.unlink() self._original_diagram = None super().inner_unlink(UnlinkEvent(self, diagram=diagram)) diff --git a/gaphor/diagram/general/tests/test_comment.py b/gaphor/diagram/general/tests/test_comment.py index 4e9e9eee0..651063e32 100644 --- a/gaphor/diagram/general/tests/test_comment.py +++ b/gaphor/diagram/general/tests/test_comment.py @@ -182,7 +182,8 @@ def test_commentline_element_unlink(create, diagram): assert clazz not in diagram.ownedPresentation assert not clazz.diagram - assert line.diagram + assert line not in diagram.ownedPresentation + assert not line.diagram assert not comment.subject.annotatedElement assert len(clazz_subject.comment) == 0 diff --git a/gaphor/diagram/tests/test_presentation.py b/gaphor/diagram/tests/test_presentation.py index 1ba7347e2..9dd33920d 100644 --- a/gaphor/diagram/tests/test_presentation.py +++ b/gaphor/diagram/tests/test_presentation.py @@ -1,5 +1,7 @@ from gaphor import UML from gaphor.diagram.presentation import ElementPresentation, LinePresentation +from gaphor.diagram.tests.fixtures import connect +from gaphor.UML import diagramitems class DummyVisualComponent: @@ -133,3 +135,18 @@ def test_line_loading(element_factory, diagram): assert tuple(p.handles()[0].pos) == (1.0, 2.0) assert tuple(p.handles()[1].pos) == (3.0, 4.0) assert p.subject is subject + + +def test_remove_connected_items_on_unlink(create, diagram): + class_a_item = create(diagramitems.ClassItem, UML.Class) + class_b_item = create(diagramitems.ClassItem, UML.Class) + association_item = create(diagramitems.AssociationItem) + + connect(association_item, association_item.head, class_a_item) + connect(association_item, association_item.tail, class_b_item) + + class_a_item.subject.unlink() + + assert class_a_item not in diagram.ownedPresentation + assert association_item not in diagram.ownedPresentation + assert class_b_item in diagram.ownedPresentation diff --git a/tests/test_undo.py b/tests/test_undo.py index 30373e8d2..f84b75d0d 100644 --- a/tests/test_undo.py +++ b/tests/test_undo.py @@ -42,6 +42,12 @@ def undo_manager(session): def test_class_association_undo_redo(event_manager, element_factory, undo_manager): diagram, ci1, ci2, a = set_up_class_and_association(event_manager, element_factory) + def get_connected(handle): + """Get item connected to line via handle.""" + if cinfo := diagram.connections.get_connection(handle): + return cinfo.connected + return None + undo_manager.clear_undo_stack() assert not undo_manager.can_undo() @@ -50,22 +56,14 @@ def test_class_association_undo_redo(event_manager, element_factory, undo_manage assert undo_manager.can_undo() - def get_connected(handle): - """Get item connected to line via handle.""" - if cinfo := diagram.connections.get_connection(handle): - return cinfo.connected - return None - - assert ci1 == get_connected(a.head) - assert None is get_connected(a.tail) - for i in range(3): - assert 9 == len(diagram.connections.solver.constraints) + assert 8 == len(diagram.connections.solver.constraints) undo_manager.undo_transaction() assert 18 == len(diagram.connections.solver.constraints) + a = next(element_factory.select(AssociationItem)) assert ci1 == get_connected(a.head) assert ci2.id == get_connected(a.tail).id