Skip to content

Commit

Permalink
feat: Add unused ports flag and simplify height calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
huyenngn committed Nov 5, 2024
1 parent 2b3b677 commit e3f8d64
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
43 changes: 22 additions & 21 deletions capellambse_context_diagrams/collectors/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"

Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions capellambse_context_diagrams/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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_):
Expand Down

0 comments on commit e3f8d64

Please sign in to comment.