pygui: moved complete edge logic into the edge itself

This commit is contained in:
Blake Harnden 2021-01-15 23:39:38 -08:00
parent fb3d593751
commit e9bf50b3ff
5 changed files with 35 additions and 41 deletions

View file

@ -837,12 +837,10 @@ class CoreClient:
links.append(edge.link)
self.ifaces_manager.removed(links)
def save_edge(
self, edge: CanvasEdge, canvas_src_node: CanvasNode, canvas_dst_node: CanvasNode
) -> None:
def save_edge(self, edge: CanvasEdge) -> None:
self.links[edge.token] = edge
src_node = canvas_src_node.core_node
dst_node = canvas_dst_node.core_node
src_node = edge.src.core_node
dst_node = edge.dst.core_node
if NodeUtils.is_container_node(src_node):
src_iface_id = edge.link.iface1.id
self.iface_to_edge[(src_node.id, src_iface_id)] = edge

View file

@ -400,7 +400,6 @@ class Edge:
self.src_label2 = None
self.dst_label = None
self.dst_label2 = None
self.manager.edges.pop(self.token, None)
def hide(self) -> None:
self.hidden = True
@ -603,12 +602,37 @@ class CanvasEdge(Edge):
if not self.linked_wireless:
self.draw_link_options()
def complete(self, dst: "CanvasNode") -> None:
def complete(self, dst: "CanvasNode", link: Link = None) -> None:
logging.debug(
"completing wired link from node(%s) to node(%s)",
self.src.core_node.name,
dst.core_node.name,
)
self.dst = dst
self.linked_wireless = self.src.is_wireless() or self.dst.is_wireless()
self.set_bindings()
self.check_wireless()
logging.debug("draw wired link from node %s to node %s", self.src, dst)
if link is None:
link = self.app.core.ifaces_manager.create_link(self)
if link.iface1:
iface1 = link.iface1
self.src.ifaces[iface1.id] = iface1
if link.iface2:
iface2 = link.iface2
self.dst.ifaces[iface2.id] = iface2
self.token = create_edge_token(link)
self.link = link
self.src.edges.add(self)
self.dst.edges.add(self)
if not self.linked_wireless:
self.arc_common_edges()
self.draw_labels()
self.check_options()
self.app.core.save_edge(self)
self.src.canvas.organize()
if self.has_shadows():
self.dst.canvas.organize()
self.manager.edges[self.token] = self
def check_wireless(self) -> None:
if not self.linked_wireless:
@ -715,3 +739,4 @@ class CanvasEdge(Edge):
super().delete()
if self.dst:
self.arc_common_edges()
self.manager.edges.pop(self.token, None)

View file

@ -240,7 +240,7 @@ class CanvasGraph(tk.Canvas):
edge.delete()
return
# finalize edge creation
self.manager.complete_edge(edge, dst_node)
edge.complete(dst_node)
def select_object(self, object_id: int, choose_multiple: bool = False) -> None:
"""
@ -677,7 +677,7 @@ class CanvasGraph(tk.Canvas):
create an edge between source node and destination node
"""
edge = CanvasEdge(self.app, src)
self.manager.complete_edge(edge, dst)
edge.complete(dst)
return edge
def copy(self) -> None:

View file

@ -323,7 +323,7 @@ class CanvasManager:
edge.asymmetric_link = link
elif token not in self.edges:
edge = CanvasEdge(self.app, src, dst)
self.complete_edge(edge, dst, link)
edge.complete(dst, link)
def add_wireless_edge(self, src: CanvasNode, dst: CanvasNode, link: Link) -> None:
network_id = link.network_id if link.network_id else None
@ -356,32 +356,3 @@ class CanvasManager:
else:
edge = self.wireless_edges[token]
edge.middle_label_text(link.label)
# TODO: look into properly moving this into the edge itself and complete when
# the destination is already provided
def complete_edge(
self, edge: CanvasEdge, dst: CanvasNode, link: Optional[Link] = None
) -> None:
src = edge.src
edge.complete(dst)
if link is None:
link = self.core.ifaces_manager.create_link(edge)
edge.link = link
if link.iface1:
iface1 = link.iface1
src.ifaces[iface1.id] = iface1
if link.iface2:
iface2 = link.iface2
dst.ifaces[iface2.id] = iface2
src.edges.add(edge)
dst.edges.add(edge)
edge.token = create_edge_token(edge.link)
if not edge.linked_wireless:
edge.arc_common_edges()
edge.draw_labels()
edge.check_options()
self.edges[edge.token] = edge
self.core.save_edge(edge, src, dst)
edge.src.canvas.organize()
if edge.has_shadows():
edge.dst.canvas.organize()

View file

@ -319,7 +319,7 @@ class CanvasNode:
def click_link(self, node: "CanvasNode") -> None:
edge = CanvasEdge(self.app, self, node)
self.app.manager.complete_edge(edge, node)
edge.complete(node)
def canvas_delete(self) -> None:
self.canvas.clear_selection()