-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedge.py
110 lines (85 loc) · 3.42 KB
/
edge.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
import math
from PyQt5.QtWidgets import QGraphicsPathItem, QGraphicsItem
from PyQt5.QtGui import QColor, QPen, QBrush, QPainterPath
from PyQt5.QtCore import Qt, QPointF
class Edge:
def __init__(self, scene, start_item, end_item):
super().__init__()
self.scene = scene
self.start_item = start_item
self.end_item = end_item
self.gr_edge = GraphicEdge(self)
# add edge on graphic scene
self.scene.add_edge(self.gr_edge)
if self.start_item is not None:
self.update_positions()
def store(self):
self.scene.add_edge(self.gr_edge)
def update_positions(self):
patch = self.start_item.width / 2
src_pos = self.start_item.pos()
self.gr_edge.set_src(src_pos.x()+patch, src_pos.y()+patch)
if self.end_item is not None:
end_pos = self.end_item.pos()
# print("start {}, end {}".format(src_pos, end_pos))
self.gr_edge.set_dst(end_pos.x()+patch, end_pos.y()+patch)
else:
self.gr_edge.set_dst(src_pos.x()+patch, src_pos.y()+patch)
self.gr_edge.update()
def remove_from_current_items(self):
self.end_item = None
self.start_item = None
def remove(self):
self.remove_from_current_items()
self.scene.remove_edge(self.gr_edge)
self.gr_edge = None
class GraphicEdge(QGraphicsPathItem):
def __init__(self, edge_wrap, parent=None):
super().__init__(parent)
self.edge_wrap = edge_wrap
self.width = 3.0
self.pos_src = [0, 0]
self.pos_dst = [0, 0]
self._pen = QPen(QColor(255, 0, 0))
self._pen.setWidthF(self.width)
self._pen_dragging = QPen(QColor("#000"))
#self._pen_dragging.setStyle(Qt.DashDotLine)
self._pen_dragging.setWidthF(self.width)
# self._mark_pen = QPen(Qt.green)
# self._mark_pen.setWidthF(self.width)
# self._mark_brush = QBrush()
# self._mark_brush.setColor(Qt.green)
# self._mark_brush.setStyle(Qt.SolidPattern)
self.setFlag(QGraphicsItem.ItemIsSelectable)
self.setZValue(1)
def set_src(self, x, y):
self.pos_src = [x, y]
def set_dst(self, x, y):
self.pos_dst = [x, y]
def calc_path(self):
path = QPainterPath(QPointF(self.pos_src[0], self.pos_src[1]))
path.lineTo(self.pos_dst[0], self.pos_dst[1])
return path
def boundingRect(self):
return self.shape().boundingRect()
def shape(self):
return self.calc_path()
def paint(self, painter, graphics_item, widget=None):
self.setPath(self.calc_path())
path = self.path()
if self.edge_wrap.end_item is None:
painter.setPen(self._pen_dragging)
painter.drawPath(path)
else:
x1, y1 = self.pos_src
x2, y2 = self.pos_dst
radius = 5 # marker radius
length = 70 # marker length
k = math.atan2(y2 - y1, x2 - x1)
new_x = x2 - length * math.cos(k) - self.width
new_y = y2 - length * math.sin(k) - self.width
painter.setPen(self._pen)
painter.drawPath(path)
# painter.setPen(self._mark_pen)
# painter.setBrush(self._mark_brush)
# painter.drawEllipse(new_x, new_y, radius, radius)