pygui: updated edge tokens to use core node ids instead of canvas ids, updated link config to properly set/load width/color, updated metadata to store/load edge width/color when switching sessions and saving xml

This commit is contained in:
Blake Harnden 2020-10-13 10:25:40 -07:00
parent 68934da204
commit aa40229495
4 changed files with 35 additions and 20 deletions

View file

@ -225,13 +225,9 @@ class CoreClient:
self.app.canvas.add_wired_edge(canvas_node1, canvas_node2, event.link)
self.app.canvas.organize()
elif event.message_type == MessageType.DELETE:
self.app.canvas.delete_wired_edge(
canvas_node1, canvas_node2, event.link
)
self.app.canvas.delete_wired_edge(event.link)
elif event.message_type == MessageType.NONE:
self.app.canvas.update_wired_edge(
canvas_node1, canvas_node2, event.link
)
self.app.canvas.update_wired_edge(event.link)
else:
logging.warning("unknown link event: %s", event)
@ -385,6 +381,17 @@ class CoreClient:
except ValueError:
logging.exception("unknown shape: %s", shape_type)
# load edges config
edges_config = config.get("edges")
if edges_config:
edges_config = json.loads(edges_config)
logging.info("edges config: %s", edges_config)
for edge_config in edges_config:
edge = self.links[edge_config["token"]]
edge.width = edge_config["width"]
edge.color = edge_config["color"]
edge.redraw()
def create_new_session(self) -> None:
"""
Create a new session
@ -572,7 +579,15 @@ class CoreClient:
shapes.append(shape.metadata())
shapes = json.dumps(shapes)
metadata = {"canvas": canvas_config, "shapes": shapes}
# create edges config
edges_config = []
for edge in self.links.values():
edge_config = dict(token=edge.token, width=edge.width, color=edge.color)
edges_config.append(edge_config)
edges_config = json.dumps(edges_config)
# save metadata
metadata = dict(canvas=canvas_config, shapes=shapes, edges=edges_config)
response = self.client.set_session_metadata(self.session.id, metadata)
logging.debug("set session metadata %s, result: %s", metadata, response)

View file

@ -56,9 +56,9 @@ class LinkConfigurationDialog(Dialog):
self.down_loss: tk.StringVar = tk.StringVar()
self.down_duplicate: tk.StringVar = tk.StringVar()
self.color: tk.StringVar = tk.StringVar(value="#000000")
self.color: tk.StringVar = tk.StringVar(value=self.edge.color)
self.color_button: Optional[tk.Button] = None
self.width: tk.DoubleVar = tk.DoubleVar()
self.width: tk.DoubleVar = tk.DoubleVar(value=self.edge.width)
self.load_link_config()
self.symmetric_frame: Optional[ttk.Frame] = None
@ -217,8 +217,8 @@ class LinkConfigurationDialog(Dialog):
self.color_button.config(background=color)
def click_apply(self) -> None:
self.app.canvas.itemconfigure(self.edge.id, width=self.width.get())
self.app.canvas.itemconfigure(self.edge.id, fill=self.color.get())
self.edge.width = self.width.get()
self.edge.color = self.color.get()
link = self.edge.link
bandwidth = get_int(self.bandwidth)
jitter = get_int(self.jitter)
@ -269,7 +269,7 @@ class LinkConfigurationDialog(Dialog):
self.app.core.edit_link(self.edge.asymmetric_link)
# update edge label
self.edge.draw_link_options()
self.edge.redraw()
self.edge.check_options()
self.destroy()

View file

@ -27,10 +27,10 @@ def create_wireless_token(src: int, dst: int, network: int) -> str:
return f"{src}-{dst}-{network}"
def create_edge_token(src: int, dst: int, link: Link) -> str:
def create_edge_token(link: Link) -> str:
iface1_id = link.iface1.id if link.iface1 else None
iface2_id = link.iface2.id if link.iface2 else None
return f"{src}-{iface1_id}-{dst}-{iface2_id}"
return f"{link.node1_id}-{iface1_id}-{link.node2_id}-{iface2_id}"
def arc_edges(edges) -> None:

View file

@ -229,7 +229,7 @@ class CanvasGraph(tk.Canvas):
self.tag_lower(self.rect)
def add_wired_edge(self, src: CanvasNode, dst: CanvasNode, link: Link) -> None:
token = create_edge_token(src.id, dst.id, link)
token = create_edge_token(link)
if token in self.edges and link.options.unidirectional:
edge = self.edges[token]
edge.asymmetric_link = link
@ -241,14 +241,14 @@ class CanvasGraph(tk.Canvas):
edge = CanvasEdge(self, src.id, src_pos, dst_pos)
self.complete_edge(src, dst, edge, link)
def delete_wired_edge(self, src: CanvasNode, dst: CanvasNode, link: Link) -> None:
token = create_edge_token(src.id, dst.id, link)
def delete_wired_edge(self, link: Link) -> None:
token = create_edge_token(link)
edge = self.edges.get(token)
if edge:
self.delete_edge(edge)
def update_wired_edge(self, src: CanvasNode, dst: CanvasNode, link: Link) -> None:
token = create_edge_token(src.id, dst.id, link)
def update_wired_edge(self, link: Link) -> None:
token = create_edge_token(link)
edge = self.edges.get(token)
if edge:
edge.link.options = deepcopy(link.options)
@ -915,7 +915,7 @@ class CanvasGraph(tk.Canvas):
dst.ifaces[iface2.id] = iface2
src.edges.add(edge)
dst.edges.add(edge)
edge.token = create_edge_token(src.id, dst.id, edge.link)
edge.token = create_edge_token(edge.link)
self.arc_common_edges(edge)
edge.draw_labels()
edge.check_options()