-
Notifications
You must be signed in to change notification settings - Fork 0
/
edge.py
144 lines (110 loc) · 4.52 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
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
import logging
import math
from socket_ import SocketPosition
try:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
except ImportError:
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
EDGE_ROUNDNESS = 100
class Edge:
def __init__(self, scene, start_socket, end_socket):
self.scene = scene
self.start_socket = start_socket
self.end_socket = end_socket
self.start_socket.edge = self
if self.end_socket is not None:
self.end_socket.edge = self
self.graphic_edge = GraphicalPath(self)
self.update_positions()
self.scene.graphic_scene.addItem(self.graphic_edge)
self.scene.add_edge(self)
def __str__(self):
return "<Edge %s..%s>" % (hex(id(self))[2:5], hex(id(self))[-3:])
def update_positions(self):
source_posx, source_posy = self.start_socket.get_socket_position()
source_posx += self.start_socket.node.graphic_node.pos().x()
source_posy += self.start_socket.node.graphic_node.pos().y()
self.graphic_edge.set_source(source_posx, source_posy)
if self.end_socket is not None:
end_posx, end_posy = self.end_socket.get_socket_position()
end_posx += self.end_socket.node.graphic_node.pos().x()
end_posy += self.end_socket.node.graphic_node.pos().y()
self.graphic_edge.set_destination(end_posx, end_posy)
else:
self.graphic_edge.set_destination(source_posx, source_posy)
self.graphic_edge.update()
def remove_from_sockets(self):
if self.start_socket is not None:
self.start_socket.set_edge(None)
if self.end_socket is not None:
self.end_socket.set_edge(None)
self.end_socket = None
self.start_socket = None
def remove(self):
logger.debug(" Removing Edge {}".format(self))
self.remove_from_sockets()
self.scene.graphic_scene.removeItem(self.graphic_edge)
self.graphic_edge = None
try:
self.scene.remove_edge(self)
except ValueError:
pass
try:
self.scene.parent_widget.edges.remove(self)
except ValueError:
pass
class GraphicalPath(QGraphicsPathItem):
def __init__(self, edge, parent=None):
super().__init__(parent)
self.edge = edge
self._color = QColor("#001000")
self._color_selected = QColor("#00ff00")
self._pen = QPen(self._color)
self._pen_selected = QPen(self._color_selected)
self._pen_dragging = QPen(self._color)
self._pen_dragging.setStyle(Qt.DashLine)
self._pen.setWidthF(2.0)
self._pen_selected.setWidthF(2.0)
self._pen_dragging.setWidthF(2.0)
self.setFlag(QGraphicsItem.ItemIsSelectable)
self.setZValue(-1)
self.pos_source = 0, 0
self.pos_destination = 200, 100
def set_source(self, x, y):
self.pos_source = x, y
def set_destination(self, x, y):
self.pos_destination = x, y
def paint(self, painter, graphics_item, widget=None):
self.setPath(self.calc_path())
if self.edge.end_socket is None:
painter.setPen(self._pen_dragging)
else:
painter.setPen(self._pen if not self.isSelected() else self._pen_selected)
painter.setBrush(Qt.NoBrush)
painter.drawPath(self.path())
def intersects_with(self, p1, p2):
cutpath = QPainterPath(p1)
cutpath.lineTo(p2)
path = self.calc_path()
return cutpath.intersects(path)
def calc_path(self):
s, d = self.pos_source, self.pos_destination
dist = (d[0] - s[0]) * 0.5
cpx_s, cpx_d = +dist, -dist
cpy_s, cpy_d = 0, 0
sspos = self.edge.start_socket.position
if (s[0] > d[0] and sspos == SocketPosition.RIGHT) or (s[0] < d[0] and sspos == SocketPosition.LEFT):
cpx_d *= -1
cpx_s *= -1
cpy_d = ((s[1] - d[1]) / math.fabs((s[1] - d[1]) if (s[1] - d[1]) != 0 else 0.00001)) * EDGE_ROUNDNESS
cpy_s = ((d[1] - s[1]) / math.fabs((d[1] - s[1]) if (d[1] - s[1]) != 0 else 0.00001)) * EDGE_ROUNDNESS
path = QPainterPath(QPointF(self.pos_source[0], self.pos_source[1]))
path.cubicTo(s[0] + cpx_s, s[1] + cpy_s, d[0] + cpx_d, d[1] + cpy_d, self.pos_destination[0],
self.pos_destination[1])
return path