From 4daeaf44ce2908a23bd577b45ae5cc6021665bcb Mon Sep 17 00:00:00 2001 From: huyenngn Date: Mon, 16 Sep 2024 12:04:13 +0200 Subject: [PATCH] feat: Display port labels --- capellambse_context_diagrams/_elkjs.py | 3 - .../collectors/default.py | 25 +++----- capellambse_context_diagrams/context.py | 3 - capellambse_context_diagrams/serializers.py | 57 +++++++++---------- 4 files changed, 35 insertions(+), 53 deletions(-) diff --git a/capellambse_context_diagrams/_elkjs.py b/capellambse_context_diagrams/_elkjs.py index c96c3416..f2548773 100644 --- a/capellambse_context_diagrams/_elkjs.py +++ b/capellambse_context_diagrams/_elkjs.py @@ -146,9 +146,6 @@ class ELKInputPort(BaseELKModel): layoutOptions: LayoutOptions = pydantic.Field(default_factory=dict) width: t.Union[int, float] height: t.Union[int, float] - labels: cabc.MutableSequence[ELKInputLabel] = pydantic.Field( - default_factory=list - ) class ELKInputEdge(BaseELKModel): diff --git a/capellambse_context_diagrams/collectors/default.py b/capellambse_context_diagrams/collectors/default.py index 8a8b60ba..76f6c8dc 100644 --- a/capellambse_context_diagrams/collectors/default.py +++ b/capellambse_context_diagrams/collectors/default.py @@ -188,13 +188,9 @@ def _process_exchanges(self) -> tuple[ except AttributeError: continue - for p in inc + out: - port = makers.make_port(p.uuid) - if self.diagram._display_port_labels: - port.labels = makers.make_label(p.name) - self.centerbox.ports.append(port) - self.centerbox.layoutOptions["portLabels.placement"] = "OUTSIDE" - + self.centerbox.ports = [ + makers.make_port(uuid) for uuid in {**inc_c, **out_c} + ] return (inc + out), ex_datas def _process_ports(self, stack_heights: dict[str, float | int]) -> None: @@ -208,17 +204,12 @@ def _process_ports(self, stack_heights: dict[str, float | int]) -> None: makers.PORT_PADDING + (makers.PORT_SIZE + makers.PORT_PADDING) * len(local_ports), ) - local_port_objs = [] - for j in local_ports: - port = makers.make_port(j.uuid) - if self.diagram._display_port_labels: - port.labels = makers.make_label(j.name) - 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.ports.extend( + [makers.make_port(j.uuid) for j in local_ports] + ) box.height += height else: box = self._make_box( @@ -226,9 +217,7 @@ def _process_ports(self, stack_heights: dict[str, float | int]) -> None: height=height, no_symbol=self.diagram._display_symbols_as_boxes, ) - box.ports = local_port_objs - - box.layoutOptions["portLabels.placement"] = "OUTSIDE" + box.ports = [makers.make_port(j.uuid) for j in local_ports] if self.diagram._display_parent_relation: current = owner diff --git a/capellambse_context_diagrams/context.py b/capellambse_context_diagrams/context.py index 908b718f..38e20c27 100644 --- a/capellambse_context_diagrams/context.py +++ b/capellambse_context_diagrams/context.py @@ -229,14 +229,12 @@ class ContextDiagram(diagram.AbstractDiagram): * slim_center_box — Minimal width for the center box, containing just the icon and the label. This is False if hierarchy was identified. - * display_port_labels — Display port labels on the diagram. """ _display_symbols_as_boxes: bool _display_parent_relation: bool _display_derived_interfaces: bool _slim_center_box: bool - _display_port_labels: bool def __init__( self, @@ -258,7 +256,6 @@ def __init__( "display_parent_relation": False, "display_derived_interfaces": False, "slim_center_box": True, - "display_port_labels": False, } | default_render_parameters if standard_filter := STANDARD_FILTERS.get(class_): diff --git a/capellambse_context_diagrams/serializers.py b/capellambse_context_diagrams/serializers.py index 6f811b0d..4df27002 100644 --- a/capellambse_context_diagrams/serializers.py +++ b/capellambse_context_diagrams/serializers.py @@ -215,35 +215,35 @@ class type that stores all previously named classes. self._cache[uuid] = element elif child.type == "label": assert parent is not None - # if not parent.port: - if parent.JSON_TYPE != "symbol": - parent.styleoverrides |= styleoverrides - - if isinstance(parent, diagram.Box): - attr_name = "floating_labels" - else: - attr_name = "labels" - - if labels := getattr(parent, attr_name): - label_box = labels[-1] - label_box.label += " " + child.text - label_box.size = diagram.Vector2D( - max(label_box.size.x, child.size.width), - label_box.size.y + child.size.height, - ) - label_box.pos = diagram.Vector2D( - min(label_box.pos.x, ref.x + child.position.x), - label_box.pos.y, - ) - else: - labels.append( - diagram.Box( - ref + (child.position.x, child.position.y), - (child.size.width, child.size.height), - label=child.text, - styleoverrides=styleoverrides, + if not parent.port: + if parent.JSON_TYPE != "symbol": + parent.styleoverrides |= styleoverrides + + if isinstance(parent, diagram.Box): + attr_name = "floating_labels" + else: + attr_name = "labels" + + if labels := getattr(parent, attr_name): + label_box = labels[-1] + label_box.label += " " + child.text + label_box.size = diagram.Vector2D( + max(label_box.size.x, child.size.width), + label_box.size.y + child.size.height, + ) + label_box.pos = diagram.Vector2D( + min(label_box.pos.x, ref.x + child.position.x), + label_box.pos.y, + ) + else: + labels.append( + diagram.Box( + ref + (child.position.x, child.position.y), + (child.size.width, child.size.height), + label=child.text, + styleoverrides=styleoverrides, + ) ) - ) element = parent elif child.type == "junction": @@ -251,7 +251,6 @@ class type that stores all previously named classes. pos = diagram.Vector2D(child.position.x, child.position.y) if self._is_hierarchical(uuid): # FIXME should this use `parent` instead? - pos += self.diagram[self._diagram.target.uuid].pos element = diagram.Circle(