From e3f8d64c26ee23a90bf29dd0997b147adcc0bacc Mon Sep 17 00:00:00 2001 From: huyenngn Date: Tue, 5 Nov 2024 07:55:01 +0100 Subject: [PATCH] feat: Add unused ports flag and simplify height calculation --- .../collectors/default.py | 43 ++++++++++--------- capellambse_context_diagrams/context.py | 3 ++ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/capellambse_context_diagrams/collectors/default.py b/capellambse_context_diagrams/collectors/default.py index 0964e81..6e4fb95 100644 --- a/capellambse_context_diagrams/collectors/default.py +++ b/capellambse_context_diagrams/collectors/default.py @@ -69,11 +69,7 @@ def process_context(self): box.children = [self.centerbox] del self.data.children[0] - stack_heights: dict[str, float | int] = { - "input": -makers.NEIGHBOR_VMARGIN, - "output": -makers.NEIGHBOR_VMARGIN, - } - self._process_ports(stack_heights) + self._process_ports() if self.diagram._display_parent_relation and self.diagram.target.owner: current = self.diagram.target.owner @@ -102,9 +98,6 @@ def process_context(self): ) generic.move_edges(owner_boxes, self.exchanges.values(), self.data) - self.centerbox.height = max( - self.centerbox.height, *stack_heights.values() - ) if self.diagram._hide_direct_children: self.centerbox.children = [] hidden = set(edge.id for edge in self.centerbox.edges) @@ -210,46 +203,56 @@ def _process_exchanges( except AttributeError: continue - for p in inc + out: + if not self.diagram._display_unused_ports: + ports = [ + p + for p in inc + out + if (inc_c.get(p.uuid) or out_c.get(p.uuid)) + ] + else: + ports = inc + out + + self.centerbox.height = max( + self.centerbox.height, + (makers.PORT_SIZE + 2 * makers.PORT_PADDING) * (len(ports) + 1), + ) + for p in ports: port = makers.make_port(p.uuid) if self.diagram._display_port_labels: port.labels = makers.make_label(p.name) + label_height = sum(label.height for label in port.labels) + self.centerbox.height += label_height self.centerbox.ports.append(port) self.centerbox.layoutOptions["portLabels.placement"] = "OUTSIDE" - return (inc + out), ex_datas + return ports, ex_datas - def _process_ports(self, stack_heights: dict[str, float | int]) -> None: + def _process_ports(self) -> None: ports, ex_datas = self._process_exchanges() for owner, local_ports, side in port_context_collector( ex_datas, ports ): - _, label_height = helpers.get_text_extent(owner.name) - height = max( - label_height + 2 * makers.LABEL_VPAD, - makers.PORT_PADDING - + (makers.PORT_SIZE + makers.PORT_PADDING) * len(local_ports), - ) local_port_objs = [] + label_heights = 0.0 for j in local_ports: port = makers.make_port(j.uuid) if self.diagram._display_port_labels: port.labels = makers.make_label(j.name) + label_heights += sum(label.height for label in port.labels) local_port_objs.append(port) if box := self.global_boxes.get(owner.uuid): # type: ignore[assignment] if box is self.centerbox: continue box.ports.extend(local_port_objs) - box.height += height else: box = self._make_box( owner, - height=height, no_symbol=self.diagram._display_symbols_as_boxes, ) box.ports = local_port_objs + box.height += label_heights box.layoutOptions["portLabels.placement"] = "OUTSIDE" @@ -264,8 +267,6 @@ def _process_ports(self, stack_heights: dict[str, float | int]) -> None: current = self._make_owner_box(self.diagram, current) self.common_owners.add(current.uuid) - stack_heights[side] += makers.NEIGHBOR_VMARGIN + height - def _make_box( self, obj: t.Any, diff --git a/capellambse_context_diagrams/context.py b/capellambse_context_diagrams/context.py index ff18998..2e143da 100644 --- a/capellambse_context_diagrams/context.py +++ b/capellambse_context_diagrams/context.py @@ -251,6 +251,7 @@ class ContextDiagram(m.AbstractDiagram): [`PORT_LABEL_POSITION`][capellambse_context_diagrams.context._elkjs.PORT_LABEL_POSITION]. * hide_direct_children - Hide direct children of the object of interest. + * display_unused_ports - Display ports that are not connected to an edge. """ _display_symbols_as_boxes: bool @@ -261,6 +262,7 @@ class ContextDiagram(m.AbstractDiagram): _display_port_labels: bool _port_label_position: str _transparent_background: bool + _display_unused_ports: bool def __init__( self, @@ -288,6 +290,7 @@ def __init__( "display_port_labels": False, "port_label_position": _elkjs.PORT_LABEL_POSITION.OUTSIDE.name, "transparent_background": False, + "display_unused_ports": False, } | default_render_parameters if standard_filter := STANDARD_FILTERS.get(class_):