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)