-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImgScene.py
81 lines (65 loc) · 2.61 KB
/
ImgScene.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import sys
from os import listdir, system, execl
import os.path
import PySide2
import copy
from PySide2.QtWidgets import (QGraphicsScene)
from PySide2.QtGui import (QBrush, QPen, QFont)
from PySide2.QtCore import QLineF, QPointF, Qt
from bbox import BBox
class ImgScene(QGraphicsScene):
def __init__(self, parent):
super().__init__(parent)
# internal flags for mouse event sync
self.mouseDown = False
self.targetCreated = False
self.mouseEventHandled = False
self.newBboxes = []
def set_dataScene(self, dscene_ref):
self.dscene = dscene_ref
# To correctly capture a click and drag event
# we need to intercept the floowing three events
def mousePressEvent(self, event):
self.mouseEventHandled = False
super().mousePressEvent(event)
if (event.button() == Qt.LeftButton) \
and (not self.mouseEventHandled):
self.mouseDown = True
self.mouseEventHandled = False
# print(type(self.height()))
# print(self.height())
def mouseMoveEvent(self, event):
super().mouseMoveEvent(event)
x = event.scenePos().x()
y = event.scenePos().y()
# at the begaining of a click & drag: create a new bbox
if self.mouseDown and (not self.targetCreated):
# if started outside the img, don't create nothing
if (0 > x) or (0 > y) or (x > self.dscene.backgroundSize[0]) \
or (y > self.dscene.backgroundSize[1]):
self.mouseDown = False
return
newBbox = BBox([x, y, 0, 0],
self.dscene.backgroundSize,
self.dscene.last_cls)
newBbox.drew_in_scene(self, self.dscene, -1)
newBbox.br.mouseMoveEvent(event, \
passed_by_scene=True)
self.newBboxes.append(newBbox)
self.targetCreated = True
# a new bbox is already created for this click & drag action
# pass the mouse event to the botton right ancker so a bbox
# can be dragged out
elif self.mouseDown:
self.newBboxes[-1].br.mouseMoveEvent(event, \
passed_by_scene=True)
def mouseReleaseEvent(self, event):
super().mouseReleaseEvent(event)
# if a new bbox was dragged out, save it
if self.mouseDown and self.targetCreated:
self.mouseDown = False
self.targetCreated = False
self.newBboxes[-1].update(verify=True)
self.dscene.record_new_target(self.newBboxes[-1])
else:
self.mouseDown = False