-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_edge.py
128 lines (103 loc) · 4.26 KB
/
node_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
from node_graphics_edge import *
from collections import OrderedDict
from node_serializable import Serializable
from utility import print_traceback
EGDE_DIRECT = 1
EDGE_BEZIER = 2
class Edge(Serializable):
def __init__(self, scene, start_socket=None, end_socket=None, type_edge = EGDE_DIRECT) -> None:
super().__init__()
self.scene = scene
self._start_socket = None
self._end_socket = None
self.start_socket = start_socket
self.end_socket = end_socket
self.edge_type = type_edge
self.scene.add_edge(self)
def __str__(self) -> str:
return "<Edge %s>" % (hex(id(self)))
def getOtherSocket(self, known_socket):
return self.start_socket if known_socket == self.end_socket else self.end_socket
@property
def start_socket(self):return self._start_socket
@start_socket.setter
def start_socket(self, value):
# if we were assigned to some socket before, delete us from the socket
if self._start_socket is not None:
self._start_socket.remove_edge(self)
# assign new start socket
self._start_socket = value
if self.start_socket is not None:
self.start_socket.add_edge(self)
@property
def end_socket(self):return self._end_socket
@end_socket.setter
def end_socket(self, value):
# if we were assigned to some socket before, delete us from the socket
if self._end_socket is not None:
self._end_socket.remove_edge(self)
# assign new end socket
self._end_socket = value
if self.end_socket is not None:
self.end_socket.add_edge(self)
@property
def edge_type(self): return self._edge_type
@edge_type.setter
def edge_type(self, value):
if hasattr(self, 'graphical_edge') and self.graphical_edge is not None:
self.scene.grscene.removeItem(self.graphical_edge)
self._edge_type = value
if self.edge_type == EDGE_BEZIER:
self.graphical_edge = QGraphics_edge_bezier(self)
elif self.edge_type == EGDE_DIRECT:
self.graphical_edge = QGraphics_edge_direct(self)
else:
self.graphical_edge = QGraphics_edge_bezier(self)
self.scene.grscene.addItem(self.graphical_edge)
if self.start_socket is not None:
self.update_positions()
def update_positions(self):
source_position = self.start_socket.get_socket_position()
source_position[0] += self.start_socket.node.graphical_node.pos().x()
source_position[1] += self.start_socket.node.graphical_node.pos().y()
self.graphical_edge.set_source(*source_position)
if self.end_socket is not None:
end_position = self.end_socket.get_socket_position()
end_position[0] += self.end_socket.node.graphical_node.pos().x()
end_position[1] += self.end_socket.node.graphical_node.pos().y()
self.graphical_edge.set_destination(*end_position)
else:
self.graphical_edge.set_destination(*source_position)
self.graphical_edge.update()
def remove_from_socket(self):
self.end_socket = None
self.start_socket = None
def remove(self):
old_sockets = [self.start_socket, self.end_socket]
self.remove_from_socket()
self.scene.grscene.removeItem(self.graphical_edge)
self.graphical_edge = None
try:
self.scene.remove_edge(self)
except ValueError:
pass
try:
for socket in old_sockets:
if socket and socket.node:
socket.node.onEdgeConnectionChanged(self)
if socket.is_input:
socket.node.onInputChanged(self)
except Exception as e:
print_traceback(e)
def serialize(self):
return OrderedDict([
('id', self.id),
('edge_type', self.edge_type),
('start', self.start_socket.id),
('end', self.end_socket.id),
])
def deserialize(self, data, hashmap={}, restore_id = True):
if restore_id: self.id = data['id']
self.start_socket = hashmap[data['start']]
self.end_socket = hashmap[data['end']]
self.edge_type = data['edge_type']