From 54788583a4155b136f3c6c75f10283a268dd4421 Mon Sep 17 00:00:00 2001 From: Joppe Blondel Date: Mon, 27 Jul 2026 14:48:50 +0200 Subject: [PATCH] Get bounding box of an icon --- src/bedit_gui/utils/icon.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/bedit_gui/utils/icon.py diff --git a/src/bedit_gui/utils/icon.py b/src/bedit_gui/utils/icon.py new file mode 100644 index 0000000..51acf79 --- /dev/null +++ b/src/bedit_gui/utils/icon.py @@ -0,0 +1,32 @@ +from __future__ import annotations + +from PySide6.QtCore import QRectF + +from bedit_gui.models import Icon, Line, Rectangle, Text + +PORT_SIZE = 16 + + +def get_bounding_box(icon: Icon) -> QRectF: + points: list[tuple[float, float]] = [] + + for shape in icon.shapes.values(): + x, y = shape.pos + points.append((x, y)) + if isinstance(shape, (Rectangle, Text)): + points.append((x + shape.width, y + shape.height)) + elif isinstance(shape, Line): + points.append(shape.end) + + for x, y in icon.port_positions.values(): + points.append((x, y)) + points.append((x + PORT_SIZE, y + PORT_SIZE)) + + if not points: + return QRectF() + + left = min(point[0] for point in points) + top = min(point[1] for point in points) + right = max(point[0] for point in points) + bottom = max(point[1] for point in points) + return QRectF(left, top, right - left, bottom - top)