Remove connected items from the diagram

E.g. when a class is removed, also remove the connected associations and
dependencies.
This commit is contained in:
Arjan Molenaar 2022-09-10 11:49:08 +02:00
parent 6cf0486e97
commit 7103e5a1f4
No known key found for this signature in database
GPG Key ID: BF977B918996CB13
4 changed files with 33 additions and 11 deletions

View File

@ -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))

View File

@ -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

View File

@ -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

View File

@ -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