Skip to content

Commit

Permalink
Refactor: Update ResizableRect and ResizableRectWithNameTypeAndResult…
Browse files Browse the repository at this point in the history
… classes
  • Loading branch information
royshil committed Oct 20, 2024
1 parent 3dcdde6 commit e1f320e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
36 changes: 30 additions & 6 deletions src/resizable_rect.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Callable
from PySide6.QtCore import QPointF, QRectF, Qt
from PySide6.QtGui import QBrush, QColor, QFont, QPen
from PySide6.QtWidgets import (
Expand Down Expand Up @@ -151,7 +152,7 @@ def hoverMoveEvent(self, event):


class MiniRect(ResizableRect):
def __init__(self, x, y, width, height, parent=None):
def __init__(self, x, y, width, height, boxChangedCallback, parent=None):
super().__init__(x, y, width, height)
self.setPen(QPen(QColor(255, 0, 0)))
self.setBrush(QBrush(QColor(255, 0, 0, 50)))
Expand All @@ -160,6 +161,7 @@ def __init__(self, x, y, width, height, parent=None):
QGraphicsItem.GraphicsItemFlag.ItemIsMovable
| QGraphicsItem.GraphicsItemFlag.ItemIsSelectable
)
self.boxChangedCallback = boxChangedCallback

def mousePressEvent(self, event):
super().mousePressEvent(event)
Expand All @@ -168,6 +170,7 @@ def mousePressEvent(self, event):
def mouseReleaseEvent(self, event):
super().mouseReleaseEvent(event)
self.unsetCursor()
self.boxChangedCallback()


class ResizableRectWithNameTypeAndResult(ResizableRect):
Expand All @@ -177,7 +180,7 @@ def __init__(
image_size,
result="",
onCenter=False,
boxChangedCallback=None,
boxChangedCallback: Callable[[str, QRectF, list[QRectF]], None] = None,
itemSelectedCallback=None,
boxDisplayStyle: int = 1,
):
Expand All @@ -204,7 +207,14 @@ def __init__(

# Mini-rect related attributes
self.mini_rects = [
MiniRect(r.x(), r.y(), r.width(), r.height(), parent=self)
MiniRect(
r.x(),
r.y(),
r.width(),
r.height(),
self.sendBoxChangedCallback,
parent=self,
)
for r in detectionTarget.mini_rects
]
self.mini_rect_mode = False
Expand Down Expand Up @@ -422,6 +432,7 @@ def startCreateMiniRect(self, rect: QRectF):
rect.y(),
rect.width(),
rect.height(),
self.sendBoxChangedCallback,
parent=self,
)
self.mini_rects.append(new_mini_rect)
Expand All @@ -434,16 +445,26 @@ def clearMiniRects(self):
def getMiniRects(self):
return [rect.rect() for rect in self.mini_rects]

def mouseReleaseEvent(self, event):
super().mouseReleaseEvent(event)
def sendBoxChangedCallback(self):
origRect = self.getRect()
boxRect = QRectF(
origRect.x() + self.x(),
origRect.y() + self.y(),
origRect.width(),
origRect.height(),
)
self.boxChangedCallback(self.name, boxRect, self.mini_rects)
self.boxChangedCallback(
self.name,
boxRect,
[
QRectF(r.x(), r.y(), r.rect().width(), r.rect().height())
for r in self.mini_rects
],
)

def mouseReleaseEvent(self, event):
super().mouseReleaseEvent(event)
self.sendBoxChangedCallback()

def mousePressEvent(self, event):
if self.mini_rect_mode:
Expand All @@ -457,6 +478,9 @@ def mousePressEvent(self, event):
)
)
else:
# deselect all mini rects
for mini_rect in self.mini_rects:
mini_rect.setSelected(False)
super().mousePressEvent(event)
else:
super().mousePressEvent(event)
Expand Down
6 changes: 1 addition & 5 deletions src/source_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ def detectionTargetsChanged(self):
boxFound = self.findBox(detectionTarget.name)
if boxFound is None:
boxFound = ResizableRectWithNameTypeAndResult(
detectionTarget.x(),
detectionTarget.y(),
detectionTarget.width(),
detectionTarget.height(),
detectionTarget.name,
detectionTarget,
# image size
self.scene.sceneRect().width(),
onCenter=False,
Expand Down

0 comments on commit e1f320e

Please sign in to comment.