From 55b6cbbd90331745c1144f04d6ac7da4bf34174c Mon Sep 17 00:00:00 2001 From: Huy Pham <42948410+hpham@users.noreply.github.com> Date: Thu, 13 Feb 2020 12:15:56 -0800 Subject: [PATCH] sacle toolbar button after choosing a node from node picker, scale canvas nodes and canvas node text --- daemon/core/gui/dialogs/preferences.py | 2 ++ daemon/core/gui/graph/graph.py | 13 ++++++++++++- daemon/core/gui/graph/node.py | 6 ++++++ daemon/core/gui/images.py | 21 +++++++++++++++++++++ daemon/core/gui/toolbar.py | 13 +++---------- 5 files changed, 44 insertions(+), 11 deletions(-) diff --git a/daemon/core/gui/dialogs/preferences.py b/daemon/core/gui/dialogs/preferences.py index 654f9e32..415fbe9a 100644 --- a/daemon/core/gui/dialogs/preferences.py +++ b/daemon/core/gui/dialogs/preferences.py @@ -137,3 +137,5 @@ class PreferencesDialog(Dialog): # scale toolbar icons and picker icons self.app.toolbar.scale() + + self.app.canvas.scale_graph() diff --git a/daemon/core/gui/graph/graph.py b/daemon/core/gui/graph/graph.py index da394503..a4ddd0b7 100644 --- a/daemon/core/gui/graph/graph.py +++ b/daemon/core/gui/graph/graph.py @@ -12,7 +12,7 @@ from core.gui.graph.enums import GraphMode, ScaleOption from core.gui.graph.node import CanvasNode from core.gui.graph.shape import Shape from core.gui.graph.shapeutils import ShapeType, is_draw_shape, is_marker -from core.gui.images import ImageEnum, Images +from core.gui.images import ImageEnum, Images, TypeToImage from core.gui.nodeutils import EdgeUtils, NodeUtils if TYPE_CHECKING: @@ -914,3 +914,14 @@ class CanvasGraph(tk.Canvas): width=self.itemcget(edge.id, "width"), fill=self.itemcget(edge.id, "fill"), ) + + def scale_graph(self): + for nid, canvas_node in self.nodes.items(): + image_enum = TypeToImage.get( + canvas_node.core_node.type, canvas_node.core_node.model + ) + img = Images.get(image_enum, int(ICON_SIZE * self.app_scale)) + self.itemconfig(nid, image=img) + canvas_node.image = img + + canvas_node.scale_text() diff --git a/daemon/core/gui/graph/node.py b/daemon/core/gui/graph/node.py index dbd8827b..b8faef13 100644 --- a/daemon/core/gui/graph/node.py +++ b/daemon/core/gui/graph/node.py @@ -106,6 +106,12 @@ class CanvasNode: image_box = self.canvas.bbox(self.id) return image_box[3] + NODE_TEXT_OFFSET + def scale_text(self): + text_bound = self.canvas.bbox(self.text_id) + prev_y = (text_bound[3] + text_bound[1]) / 2 + new_y = self._get_label_y() + self.canvas.move(self.text_id, 0, new_y - prev_y) + def move(self, x: int, y: int): x, y = self.canvas.get_scaled_coords(x, y) current_x, current_y = self.canvas.coords(self.id) diff --git a/daemon/core/gui/images.py b/daemon/core/gui/images.py index cd472764..f299c5a5 100644 --- a/daemon/core/gui/images.py +++ b/daemon/core/gui/images.py @@ -3,6 +3,7 @@ from tkinter import messagebox from PIL import Image, ImageTk +from core.api.grpc import core_pb2 from core.gui.appconfig import LOCAL_ICONS_PATH @@ -90,3 +91,23 @@ class ImageEnum(Enum): SHUTDOWN = "shutdown" CANCEL = "cancel" ERROR = "error" + + +class TypeToImage: + type_to_image = { + (core_pb2.NodeType.DEFAULT, "router"): ImageEnum.ROUTER, + (core_pb2.NodeType.DEFAULT, "PC"): ImageEnum.PC, + (core_pb2.NodeType.DEFAULT, "host"): ImageEnum.HOST, + (core_pb2.NodeType.DEFAULT, "mdr"): ImageEnum.MDR, + (core_pb2.NodeType.DEFAULT, "prouter"): ImageEnum.PROUTER, + (core_pb2.NodeType.HUB, ""): ImageEnum.HUB, + (core_pb2.NodeType.SWITCH, ""): ImageEnum.SWITCH, + (core_pb2.NodeType.WIRELESS_LAN, ""): ImageEnum.WLAN, + (core_pb2.NodeType.EMANE, ""): ImageEnum.EMANE, + (core_pb2.NodeType.RJ45, ""): ImageEnum.RJ45, + (core_pb2.NodeType.TUNNEL, ""): ImageEnum.TUNNEL, + } + + @classmethod + def get(cls, node_type, model): + return cls.type_to_image.get((node_type, model), None) diff --git a/daemon/core/gui/toolbar.py b/daemon/core/gui/toolbar.py index 1df24993..10075b74 100644 --- a/daemon/core/gui/toolbar.py +++ b/daemon/core/gui/toolbar.py @@ -103,21 +103,18 @@ class Toolbar(ttk.Frame): self.design_frame.columnconfigure(0, weight=1) self.play_button = self.create_button( self.design_frame, - # icon(ImageEnum.START), self.get_icon(ImageEnum.START), self.click_start, "start the session", ) self.select_button = self.create_button( self.design_frame, - # icon(ImageEnum.SELECT), self.get_icon(ImageEnum.SELECT), self.click_selection, "selection tool", ) self.link_button = self.create_button( self.design_frame, - # icon(ImageEnum.LINK), self.get_icon(ImageEnum.LINK), self.click_link, "link tool", @@ -152,21 +149,18 @@ class Toolbar(ttk.Frame): self.stop_button = self.create_button( self.runtime_frame, - # icon(ImageEnum.STOP), self.get_icon(ImageEnum.STOP), self.click_stop, "stop the session", ) self.runtime_select_button = self.create_button( self.runtime_frame, - # icon(ImageEnum.SELECT), self.get_icon(ImageEnum.SELECT), self.click_runtime_selection, "selection tool", ) self.plot_button = self.create_button( self.runtime_frame, - # icon(ImageEnum.PLOT), self.get_icon(ImageEnum.PLOT), self.click_plot_button, "plot", @@ -192,8 +186,7 @@ class Toolbar(ttk.Frame): self.node_picker = ttk.Frame(self.master) # draw default nodes for node_draw in NodeUtils.NODES: - toolbar_image = icon(node_draw.image_enum) - # image = icon(node_draw.image_enum, PICKER_SIZE) + toolbar_image = self.get_icon(node_draw.image_enum, TOOLBAR_SIZE) image = self.get_icon(node_draw.image_enum, PICKER_SIZE) func = partial( self.update_button, @@ -372,7 +365,7 @@ class Toolbar(ttk.Frame): self.hide_pickers() self.network_picker = ttk.Frame(self.master) for node_draw in NodeUtils.NETWORK_NODES: - toolbar_image = icon(node_draw.image_enum) + toolbar_image = self.get_icon(node_draw.image_enum, TOOLBAR_SIZE) image = self.get_icon(node_draw.image_enum, PICKER_SIZE) self.create_picker_button( image, @@ -419,7 +412,7 @@ class Toolbar(ttk.Frame): (ImageEnum.TEXT, ShapeType.TEXT), ] for image_enum, shape_type in nodes: - toolbar_image = icon(image_enum) + toolbar_image = self.get_icon(image_enum, TOOLBAR_SIZE) image = self.get_icon(image_enum, PICKER_SIZE) self.create_picker_button( image,