-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.py
112 lines (93 loc) · 3.87 KB
/
view.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
from PyQt5.QtWidgets import QGraphicsView
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 = False
self.edge_enable = True
self.drag_edge = None
#self.create_key_point()
self.init_ui()
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 create_key_point(self):
self.key_point_list = []
for x, y in zip([100, 1, 200, 500], [100, 1, 200, 400]):
item = EllipseItem()
item.setPos(x, y)
self.gr_scene.add_node(item)
self.key_point_list.append(item)
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):
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)
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()