From e9bf50b3ff4b2970c1a91e93d333ba9fccbb76b1 Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Fri, 15 Jan 2021 23:39:38 -0800 Subject: [PATCH] pygui: moved complete edge logic into the edge itself --- daemon/core/gui/coreclient.py | 8 +++----- daemon/core/gui/graph/edges.py | 31 ++++++++++++++++++++++++++++--- daemon/core/gui/graph/graph.py | 4 ++-- daemon/core/gui/graph/manager.py | 31 +------------------------------ daemon/core/gui/graph/node.py | 2 +- 5 files changed, 35 insertions(+), 41 deletions(-) diff --git a/daemon/core/gui/coreclient.py b/daemon/core/gui/coreclient.py index d9214753..74bc56fb 100644 --- a/daemon/core/gui/coreclient.py +++ b/daemon/core/gui/coreclient.py @@ -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 diff --git a/daemon/core/gui/graph/edges.py b/daemon/core/gui/graph/edges.py index 0736b53b..7f8cd7f2 100644 --- a/daemon/core/gui/graph/edges.py +++ b/daemon/core/gui/graph/edges.py @@ -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) diff --git a/daemon/core/gui/graph/graph.py b/daemon/core/gui/graph/graph.py index 19c88c4f..d5604e28 100644 --- a/daemon/core/gui/graph/graph.py +++ b/daemon/core/gui/graph/graph.py @@ -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: diff --git a/daemon/core/gui/graph/manager.py b/daemon/core/gui/graph/manager.py index f7f17e81..ae528ed0 100644 --- a/daemon/core/gui/graph/manager.py +++ b/daemon/core/gui/graph/manager.py @@ -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() diff --git a/daemon/core/gui/graph/node.py b/daemon/core/gui/graph/node.py index 31f8af5e..1883e901 100644 --- a/daemon/core/gui/graph/node.py +++ b/daemon/core/gui/graph/node.py @@ -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()