Get bounding box of an icon

This commit is contained in:
2026-07-27 14:48:50 +02:00
parent e5968e165e
commit 54788583a4

View File

@@ -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)