diff --git a/daemon/core/gui/coreclient.py b/daemon/core/gui/coreclient.py index 3c4690c3..65d51fb0 100644 --- a/daemon/core/gui/coreclient.py +++ b/daemon/core/gui/coreclient.py @@ -43,7 +43,6 @@ from core.gui.dialogs.mobilityplayer import MobilityPlayer from core.gui.dialogs.sessions import SessionsDialog from core.gui.graph.edges import CanvasEdge from core.gui.graph.node import CanvasNode -from core.gui.graph.shape import Shape from core.gui.interface import InterfaceManager from core.gui.nodeutils import NodeDraw @@ -317,45 +316,6 @@ class CoreClient: def is_runtime(self) -> bool: return self.session and self.session.state == SessionState.RUNTIME - def parse_metadata(self) -> None: - # canvas setting - config = self.session.metadata - canvas_config = config.get("canvas") - logger.debug("canvas metadata: %s", canvas_config) - if canvas_config: - canvas_config = json.loads(canvas_config) - self.app.manager.parse_metadata(canvas_config) - - # load saved shapes - shapes_config = config.get("shapes") - if shapes_config: - shapes_config = json.loads(shapes_config) - for shape_config in shapes_config: - logger.debug("loading shape: %s", shape_config) - Shape.from_metadata(self.app, shape_config) - - # load edges config - edges_config = config.get("edges") - if edges_config: - edges_config = json.loads(edges_config) - logger.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() - - # read hidden nodes - hidden = config.get("hidden") - if hidden: - hidden = json.loads(hidden) - for _id in hidden: - canvas_node = self.canvas_nodes.get(_id) - if canvas_node: - canvas_node.hide() - else: - logger.warning("invalid node to hide: %s", _id) - def create_new_session(self) -> None: """ Create a new session @@ -439,7 +399,7 @@ class CoreClient: except grpc.RpcError as e: self.app.show_grpc_exception("Edit Node Error", e) - def get_links(self, definition: bool = False) -> Tuple[List[Link], List[Link]]: + def get_links(self, definition: bool = False) -> List[Link]: if not definition: self.ifaces_manager.set_macs([x.link for x in self.links.values()]) links = [] diff --git a/daemon/core/gui/graph/manager.py b/daemon/core/gui/graph/manager.py index 7a2c0ed8..08352a25 100644 --- a/daemon/core/gui/graph/manager.py +++ b/daemon/core/gui/graph/manager.py @@ -1,3 +1,4 @@ +import json import logging import tkinter as tk from copy import deepcopy @@ -16,6 +17,7 @@ from core.gui.graph.edges import ( from core.gui.graph.enums import GraphMode from core.gui.graph.graph import CanvasGraph from core.gui.graph.node import CanvasNode +from core.gui.graph.shape import Shape from core.gui.graph.shapeutils import ShapeType from core.gui.nodeutils import NodeDraw @@ -207,7 +209,7 @@ class CanvasManager: edge.delete() def join(self, session: Session) -> None: - # clear out all canvas + # clear out all canvases for canvas_id in self.notebook.tabs(): self.notebook.forget(canvas_id) self.canvases.clear() @@ -234,6 +236,10 @@ class CanvasManager: self.draw_session(session) def draw_session(self, session: Session) -> None: + # draw canvas configurations and shapes + self.parse_metadata_canvas(session.metadata) + self.parse_metadata_shapes(session.metadata) + # create session nodes for core_node in session.nodes.values(): # add node, avoiding ignored nodes @@ -256,11 +262,14 @@ class CanvasManager: else: self.add_wired_edge(node1, node2, link) - # parse metadata and organize canvases - self.core.parse_metadata() + # organize canvas order for canvas in self.canvases.values(): canvas.organize() + # parse metada for edge configs and hidden nodes + self.parse_metadata_edges(session.metadata) + self.parse_metadata_hidden(session.metadata) + # create a default canvas if none were created prior if not self.canvases: self.add_canvas() @@ -279,16 +288,22 @@ class CanvasManager: canvases=canvases, ) - def parse_metadata(self, config: Dict[str, Any]) -> None: + def parse_metadata_canvas(self, metadata: Dict[str, Any]) -> None: + # canvas setting + canvas_config = metadata.get("canvas") + logger.debug("canvas metadata: %s", canvas_config) + if not canvas_config: + return + canvas_config = json.loads(canvas_config) # get configured dimensions and gridlines option dimensions = self.default_dimensions - dimensions = config.get("dimensions", dimensions) - gridlines = config.get("gridlines", True) + dimensions = canvas_config.get("dimensions", dimensions) + gridlines = canvas_config.get("gridlines", True) self.show_grid.set(gridlines) self.redraw_canvases(dimensions) # get background configurations - for canvas_config in config.get("canvases", []): + for canvas_config in canvas_config.get("canvases", []): canvas_id = canvas_config.get("id") if canvas_id is None: logger.error("canvas config id not provided") @@ -296,6 +311,46 @@ class CanvasManager: canvas = self.get(canvas_id) canvas.parse_metadata(canvas_config) + def parse_metadata_shapes(self, metadata: Dict[str, Any]) -> None: + # load saved shapes + shapes_config = metadata.get("shapes") + if not shapes_config: + return + shapes_config = json.loads(shapes_config) + for shape_config in shapes_config: + logger.debug("loading shape: %s", shape_config) + Shape.from_metadata(self.app, shape_config) + + def parse_metadata_edges(self, metadata: Dict[str, Any]) -> None: + # load edges config + edges_config = metadata.get("edges") + if not edges_config: + return + edges_config = json.loads(edges_config) + logger.info("edges config: %s", edges_config) + for edge_config in edges_config: + edge_token = edge_config["token"] + edge = self.core.links.get(edge_token) + if edge: + edge.width = edge_config["width"] + edge.color = edge_config["color"] + edge.redraw() + else: + logger.warning("invalid edge token to configure: %s", edge_token) + + def parse_metadata_hidden(self, metadata: Dict[str, Any]) -> None: + # read hidden nodes + hidden_config = metadata.get("hidden") + if not hidden_config: + return + hidden_config = json.loads(hidden_config) + for node_id in hidden_config: + canvas_node = self.core.canvas_nodes.get(node_id) + if canvas_node: + canvas_node.hide() + else: + logger.warning("invalid node to hide: %s", node_id) + def add_core_node(self, core_node: Node) -> None: # get canvas tab for node canvas_id = core_node.canvas if core_node.canvas > 0 else 1