Skip to content

Commit

Permalink
feat: Display port labels
Browse files Browse the repository at this point in the history
  • Loading branch information
huyenngn committed Sep 16, 2024
1 parent 03ec7d5 commit 4daeaf4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 53 deletions.
3 changes: 0 additions & 3 deletions capellambse_context_diagrams/_elkjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
25 changes: 7 additions & 18 deletions capellambse_context_diagrams/collectors/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -208,27 +204,20 @@ 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(
owner,
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
Expand Down
3 changes: 0 additions & 3 deletions capellambse_context_diagrams/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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_):
Expand Down
57 changes: 28 additions & 29 deletions capellambse_context_diagrams/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,43 +215,42 @@ 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":
uuid = uuid.rsplit("_", maxsplit=1)[0]
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(
Expand Down

0 comments on commit 4daeaf4

Please sign in to comment.