Get bounding box of an icon
This commit is contained in:
32
src/bedit_gui/utils/icon.py
Normal file
32
src/bedit_gui/utils/icon.py
Normal 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)
|
||||
Reference in New Issue
Block a user