-
Notifications
You must be signed in to change notification settings - Fork 0
/
Myview.py
160 lines (142 loc) · 6.18 KB
/
Myview.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from PyQt5.QtWidgets import QGraphicsView, QGraphicsRectItem
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter
from item import GraphicItem
from edge import Edge
from key_point_item import EllipseItem
class GraphicView(QGraphicsView):
def __init__(self, graphic_scene, parent=None):
super().__init__(parent)
self.gr_scene = graphic_scene
self.parent = parent
self.edge_enable = True
self.drag_edge = None
self.init_ui()
self.key_point_list_total = []
def init_ui(self):
self.setScene(self.gr_scene)
self.setRenderHints(QPainter.Antialiasing |
QPainter.HighQualityAntialiasing |
QPainter.TextAntialiasing |
QPainter.SmoothPixmapTransform |
QPainter.LosslessImageRendering)
self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setTransformationAnchor(self.AnchorUnderMouse)
self.setDragMode(self.RubberBandDrag)
def one_faical_feature(self, point_list, color, end2start=False):
self.key_point_list = []
if end2start:
for [x, y] in point_list:
item = EllipseItem(r=10)
item.setPos(x, y)
item.setBrush(color)
self.gr_scene.add_node(item)
self.key_point_list.append(item)
self.key_point_list_total.append(self.key_point_list)
for i in range(len(self.key_point_list) - 1):
item_s = self.key_point_list[i]
item_e = self.key_point_list[i + 1]
# print("start {}, end {}".format(item_s.pos(), item_e.pos()))
self.edge_drag_start(item_s)
self.edge_drag_end(item_e)
l = len(self.key_point_list) - 1
self.edge_drag_start(self.key_point_list[l])
self.edge_drag_end(self.key_point_list[0])
else:
for [x, y] in point_list:
item = EllipseItem(r=10)
item.setPos(x, y)
item.setBrush(color)
self.gr_scene.add_node(item)
self.key_point_list.append(item)
self.key_point_list_total.append(self.key_point_list)
for i in range(len(self.key_point_list) - 1):
item_s = self.key_point_list[i]
item_e = self.key_point_list[i + 1]
# print("start {}, end {}".format(item_s.pos(), item_e.pos()))
self.edge_drag_start(item_s)
self.edge_drag_end(item_e)
def create_key_point_by_point_list(self, point_list, color_list, start2end_list):
self.all_facial_key_point = []
for n in range(len(point_list)):
one_facial_feature = point_list[n]
color = color_list[n]
start2end = start2end_list[n]
self.one_faical_feature(one_facial_feature, color, start2end)
self.all_facial_key_point.append(self.key_point_list)
print(self.all_facial_key_point)
def create_key_point(self):
self.key_point_list = []
for x, y in zip([100, 1, 200, 500], [100, 1, 200, 400]):
item = EllipseItem(r=10)
item.setPos(x, y)
self.gr_scene.add_node(item)
self.key_point_list.append(item)
# for x, y in zip([10, 1, 200, 500], [10, 1, 200, 400]):
# item2 = EllipseItem(r=10)
# item2.setPos(x, y)
# self.gr_scene.add_node(item2)
self.edge_enable = True
for i in range(len(self.key_point_list) - 1):
item_s = self.key_point_list[i]
item_e = self.key_point_list[i + 1]
# print("start {}, end {}".format(item_s.pos(), item_e.pos()))
self.edge_drag_start(item_s)
self.edge_drag_end(item_e)
def keyPressEvent(self, event):
if event.key() == Qt.Key_N:
item = GraphicItem()
item.setPos(0, 0)
self.gr_scene.add_node(item)
if event.key() == Qt.Key_E:
self.edge_enable = ~self.edge_enable
def mousePressEvent(self, event):
item = self.get_item_at_click(event)
if event.button() == Qt.RightButton:
if isinstance(item, GraphicItem):
self.gr_scene.remove_node(item)
elif self.edge_enable:
if isinstance(item, GraphicItem):
self.edge_drag_start(item)
else:
super().mousePressEvent(event)
def get_item_at_click(self, event):
""" Return the object that clicked on. """
pos = event.pos()
item = self.itemAt(pos)
return item
def get_items_at_rubber(self):
""" Get group select items. """
area = self.rubberBandRect()
return self.items(area)
def mouseMoveEvent(self, event):
pos = event.pos()
if self.edge_enable and self.drag_edge is not None:
sc_pos = self.mapToScene(pos)
self.drag_edge.gr_edge.set_dst(sc_pos.x(), sc_pos.y())
self.drag_edge.gr_edge.update()
super().mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
try:
if self.edge_enable:
self.edge_enable = False
item = self.get_item_at_click(event)
if isinstance(item, GraphicItem) and item is not self.drag_start_item:
self.edge_drag_end(item)
else:
self.drag_edge.remove()
self.drag_edge = None
else:
super().mouseReleaseEvent(event)
except:
pass
def edge_drag_start(self, item):
self.drag_start_item = item
self.drag_edge = Edge(self.gr_scene, self.drag_start_item, None)
def edge_drag_end(self, item):
new_edge = Edge(self.gr_scene, self.drag_start_item, item)
self.drag_edge.remove()
self.drag_edge = None
new_edge.store()