From c1755afb2f6ddddf7ef9748a289ba295e741db72 Mon Sep 17 00:00:00 2001 From: Huy Pham <42948410+hpham@users.noreply.github.com> Date: Thu, 19 Dec 2019 16:15:29 -0800 Subject: [PATCH] copy node and links --- daemon/core/gui/coreclient.py | 7 ++--- daemon/core/gui/graph/graph.py | 48 +++++++++++++++++++++++++++++++++- daemon/core/gui/menuaction.py | 9 +++++++ daemon/core/gui/menubar.py | 11 ++++++-- 4 files changed, 69 insertions(+), 6 deletions(-) diff --git a/daemon/core/gui/coreclient.py b/daemon/core/gui/coreclient.py index dee94dbf..f226d636 100644 --- a/daemon/core/gui/coreclient.py +++ b/daemon/core/gui/coreclient.py @@ -760,9 +760,10 @@ class CoreClient: if edge in edges: continue edges.add(edge) - if edge.token not in self.links: - logging.error("unknown edge: %s", edge.token) - del self.links[edge.token] + # + # if edge.token not in self.links: + # logging.error("unknown edge: %s", edge.token) + self.links.pop(edge.token, None) def create_interface(self, canvas_node): node = canvas_node.core_node diff --git a/daemon/core/gui/graph/graph.py b/daemon/core/gui/graph/graph.py index 6b8c55ef..453f35c2 100644 --- a/daemon/core/gui/graph/graph.py +++ b/daemon/core/gui/graph/graph.py @@ -46,6 +46,7 @@ class CanvasGraph(tk.Canvas): self.offset = (0, 0) self.cursor = (0, 0) self.marker_tool = None + self.to_copy = [] # background related self.wallpaper_id = None @@ -441,7 +442,8 @@ class CanvasGraph(tk.Canvas): continue edges.add(edge) self.throughput_draw.delete(edge) - del self.edges[edge.token] + self.edges.pop(edge.token, None) + # del self.edges[edge.token] edge.delete() # update node connected to edge being deleted @@ -838,3 +840,47 @@ class CanvasGraph(tk.Canvas): self.nodes[source.id].edges.add(edge) self.nodes[dest.id].edges.add(edge) self.core.create_link(edge, source, dest) + + def copy(self): + if self.selection: + logging.debug( + "store current selection to to_copy, number of nodes: %s", + len(self.selection), + ) + self.to_copy = self.selection.keys() + + def paste(self): + logging.debug("copy") + # maps original node canvas id to copy node canvas id + copy_map = {} + # the edges that will be copy over + to_copy_edges = [] + for canvas_nid in self.to_copy: + core_node = self.nodes[canvas_nid].core_node + actual_x = core_node.position.x + 50 + actual_y = core_node.position.y + 50 + scaled_x, scaled_y = self.get_scaled_coords(actual_x, actual_y) + + copy = self.core.create_node( + actual_x, actual_y, core_node.type, core_node.model + ) + node = CanvasNode( + self.master, scaled_x, scaled_y, copy, self.nodes[canvas_nid].image + ) + copy_map[canvas_nid] = node.id + self.core.canvas_nodes[copy.id] = node + self.nodes[node.id] = node + + edges = self.nodes[canvas_nid].edges + for edge in edges: + if edge.src not in self.to_copy or edge.dst not in self.to_copy: + if canvas_nid == edge.src: + self.create_edge(node, self.nodes[edge.dst]) + elif canvas_nid == edge.dst: + self.create_edge(self.nodes[edge.src], node) + else: + to_copy_edges.append(tuple([edge.src, edge.dst])) + for e in to_copy_edges: + source_node_copy = self.nodes[copy_map[e[0]]] + dest_node_copy = self.nodes[copy_map[e[1]]] + self.create_edge(source_node_copy, dest_node_copy) diff --git a/daemon/core/gui/menuaction.py b/daemon/core/gui/menuaction.py index be532218..8d45c33a 100644 --- a/daemon/core/gui/menuaction.py +++ b/daemon/core/gui/menuaction.py @@ -30,6 +30,7 @@ class MenuAction: def __init__(self, app, master): self.master = master self.app = app + self.canvas = app.canvas def cleanup_old_session(self, quitapp=False): logging.info("cleaning up old session") @@ -155,3 +156,11 @@ class MenuAction: self.app.core.enable_throughputs() else: self.app.core.cancel_throughputs() + + def copy(self, event=None): + logging.debug("copy") + self.app.canvas.copy() + + def paste(self, event=None): + logging.debug("paste") + self.app.canvas.paste() diff --git a/daemon/core/gui/menubar.py b/daemon/core/gui/menubar.py index c2a4e353..d797097f 100644 --- a/daemon/core/gui/menubar.py +++ b/daemon/core/gui/menubar.py @@ -90,8 +90,12 @@ class Menubar(tk.Menu): menu.add_command(label="Redo", accelerator="Ctrl+Y", state=tk.DISABLED) menu.add_separator() menu.add_command(label="Cut", accelerator="Ctrl+X", state=tk.DISABLED) - menu.add_command(label="Copy", accelerator="Ctrl+C", state=tk.DISABLED) - menu.add_command(label="Paste", accelerator="Ctrl+V", state=tk.DISABLED) + menu.add_command( + label="Copy", accelerator="Ctrl+C", command=self.menuaction.copy + ) + menu.add_command( + label="Paste", accelerator="Ctrl+V", command=self.menuaction.paste + ) menu.add_separator() menu.add_command(label="Select all", accelerator="Ctrl+A", state=tk.DISABLED) menu.add_command( @@ -102,6 +106,9 @@ class Menubar(tk.Menu): menu.add_command(label="Clear marker", state=tk.DISABLED) self.add_cascade(label="Edit", menu=menu) + self.app.master.bind_all("", self.menuaction.copy) + self.app.master.bind_all("", self.menuaction.paste) + def draw_canvas_menu(self): """ Create canvas menu