[detectionview] support single item selection, hightlight selected items

This commit is contained in:
Jan Grewe 2025-02-02 18:44:54 +01:00
parent 4f18387c2f
commit ae0df7b4c3

View File

@ -15,9 +15,15 @@ class Detection(QGraphicsEllipseItem):
super().__init__(x, y, width, height)
self.setBrush(brush)
self.setAcceptHoverEvents(True) # Enable hover events if needed
self.setFlags(QGraphicsRectItem.ItemIsSelectable)
def mousePressEvent(self, event):
self.signals.clicked.emit(self.data(0), QPointF(event.scenePos().x(), event.scenePos().y()))
# item = self.scene().itemAt(event.scenePos(), self.views()[0].transform())
# if item:
# item.setSelected(True)
# print(f"Rectangle clicked at: {event.scenePos().x()}, {event.scenePos().y()}")
def hoverEnterEvent(self, event):
@ -54,20 +60,22 @@ class DetectionScene(QGraphicsScene):
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton and self.selection_rect is not None:
# Perform selection of items within the selection rectangle
rect = self.selection_rect.rect()
# Remove the temporary selection rectangle
self.removeItem(self.selection_rect)
self.selection_rect = None
# Find all items that intersect with the selection rectangle
selected_items = self.items(rect, Qt.IntersectsItemShape)
for item in selected_items:
if not isinstance(item, Detection):
selected_items.remove(item)
item.setSelected(True) # Mark the item as selected
self.signals.itemsSelected.emit(selected_items)
if rect.width() > 0.0:
# Find all items that intersect with the selection rectangle
selected_items = self.items(rect, Qt.IntersectsItemShape)
for item in selected_items:
if not isinstance(item, Detection):
selected_items.remove(item)
item.setSelected(True) # Mark the item as selected
self.signals.itemsSelected.emit(selected_items)
else:
item = self.itemAt(event.scenePos(), self.views()[0].transform())
if item:
item.setSelected(True)
self.signals.itemsSelected.emit(self.selectedItems())
super().mouseReleaseEvent(event)
@ -133,7 +141,7 @@ class DetectionView(QWidget):
def main():
def items_selected(items):
print(items)
print("items selected", items)
import pickle
import numpy as np
@ -141,7 +149,7 @@ def main():
from PySide6.QtWidgets import QApplication
datafile = PACKAGE_ROOT / "data/merged_small.pkl"
imgfile = PACKAGE_ROOT / "data/merged.png"
imgfile = PACKAGE_ROOT / "data/merged2.png"
print(datafile)
with open(datafile, "rb") as f:
df = pickle.load(f)