diff --git a/coretk/coretk/app.py b/coretk/coretk/app.py index 5481ad19..11fad378 100644 --- a/coretk/coretk/app.py +++ b/coretk/coretk/app.py @@ -15,21 +15,19 @@ from coretk.toolbar import Toolbar class Application(tk.Frame): def __init__(self, master=None): super().__init__(master) - self.config = appconfig.read() - self.style = ttk.Style() - self.setup_theme() + # widgets self.menubar = None self.toolbar = None self.canvas = None self.statusbar = None - self.is_open_xml = False - self.size_and_scale = None + + # variables self.set_wallpaper = None - self.wallpaper_id = None - self.current_wallpaper = None - self.radiovar = tk.IntVar(value=1) - self.show_grid_var = tk.IntVar(value=1) - self.adjust_to_dim_var = tk.IntVar(value=0) + + # setup + self.config = appconfig.read() + self.style = ttk.Style() + self.setup_theme() self.core = CoreClient(self) self.setup_app() self.draw() @@ -43,12 +41,21 @@ class Application(tk.Frame): def setup_app(self): self.master.title("CORE") - self.master.geometry("1000x800") + self.center() self.master.protocol("WM_DELETE_WINDOW", self.on_closing) image = Images.get(ImageEnum.CORE, 16) self.master.tk.call("wm", "iconphoto", self.master._w, image) self.pack(fill=tk.BOTH, expand=True) + def center(self): + width = 1000 + height = 800 + screen_width = self.master.winfo_screenwidth() + screen_height = self.master.winfo_screenheight() + x = int((screen_width / 2) - (width / 2)) + y = int((screen_height / 2) - (height / 2)) + self.master.geometry(f"{width}x{height}+{x}+{y}") + def draw(self): self.master.option_add("*tearOff", tk.FALSE) self.menubar = Menubar(self.master, self) diff --git a/coretk/coretk/dialogs/canvasbackground.py b/coretk/coretk/dialogs/canvasbackground.py index 7b1dd186..80b1ae5f 100644 --- a/coretk/coretk/dialogs/canvasbackground.py +++ b/coretk/coretk/dialogs/canvasbackground.py @@ -10,6 +10,7 @@ from PIL import Image, ImageTk from coretk.appconfig import BACKGROUNDS_PATH from coretk.dialogs.dialog import Dialog +from coretk.images import Images PADX = 5 @@ -31,11 +32,11 @@ class CanvasBackgroundDialog(Dialog): """ super().__init__(master, app, "Canvas Background", modal=True) self.canvas = self.app.canvas - self.radiovar = tk.IntVar(value=self.app.radiovar.get()) - self.show_grid_var = tk.IntVar(value=self.app.show_grid_var.get()) - self.adjust_to_dim_var = tk.IntVar(value=self.app.adjust_to_dim_var.get()) + self.scale_option = tk.IntVar(value=self.canvas.scale_option.get()) + self.show_grid = tk.IntVar(value=self.canvas.show_grid.get()) + self.adjust_to_dim = tk.IntVar(value=self.canvas.adjust_to_dim.get()) + self.filename = tk.StringVar(value=self.canvas.wallpaper_file) self.image_label = None - self.file_name = tk.StringVar() self.options = [] self.draw() @@ -57,6 +58,8 @@ class CanvasBackgroundDialog(Dialog): def draw_image_label(self): label = ttk.Label(self.top, text="Image filename: ") label.grid(row=1, column=0, sticky="ew") + if self.filename.get(): + self.draw_preview() def draw_image_selection(self): frame = ttk.Frame(self.top) @@ -65,7 +68,7 @@ class CanvasBackgroundDialog(Dialog): frame.columnconfigure(2, weight=1) frame.grid(row=2, column=0, sticky="ew") - entry = ttk.Entry(frame, textvariable=self.file_name) + entry = ttk.Entry(frame, textvariable=self.filename) entry.focus() entry.grid(row=0, column=0, sticky="ew", padx=PADX) @@ -84,41 +87,45 @@ class CanvasBackgroundDialog(Dialog): frame.grid(row=3, column=0, sticky="ew") button = ttk.Radiobutton( - frame, text="upper-left", value=1, variable=self.radiovar + frame, text="upper-left", value=1, variable=self.scale_option ) button.grid(row=0, column=0, sticky="ew") self.options.append(button) button = ttk.Radiobutton( - frame, text="centered", value=2, variable=self.radiovar + frame, text="centered", value=2, variable=self.scale_option ) button.grid(row=0, column=1, sticky="ew") self.options.append(button) - button = ttk.Radiobutton(frame, text="scaled", value=3, variable=self.radiovar) + button = ttk.Radiobutton( + frame, text="scaled", value=3, variable=self.scale_option + ) button.grid(row=0, column=2, sticky="ew") self.options.append(button) - button = ttk.Radiobutton(frame, text="titled", value=4, variable=self.radiovar) + button = ttk.Radiobutton( + frame, text="titled", value=4, variable=self.scale_option + ) button.grid(row=0, column=3, sticky="ew") self.options.append(button) def draw_additional_options(self): checkbutton = ttk.Checkbutton( - self.top, text="Show grid", variable=self.show_grid_var + self.top, text="Show grid", variable=self.show_grid ) checkbutton.grid(row=4, column=0, sticky="ew", padx=PADX) checkbutton = ttk.Checkbutton( self.top, text="Adjust canvas size to image dimensions", - variable=self.adjust_to_dim_var, + variable=self.adjust_to_dim, command=self.click_adjust_canvas, ) checkbutton.grid(row=5, column=0, sticky="ew", padx=PADX) - self.show_grid_var.set(1) - self.adjust_to_dim_var.set(0) + self.show_grid.set(1) + self.adjust_to_dim.set(0) def draw_buttons(self): frame = ttk.Frame(self.top) @@ -142,13 +149,13 @@ class CanvasBackgroundDialog(Dialog): ), ) if filename: - self.file_name.set(filename) - width, height = 250, 135 - img = Image.open(filename) - img = img.resize((width, height), Image.ANTIALIAS) - tk_img = ImageTk.PhotoImage(img) - self.image_label.config(image=tk_img, width=width) - self.image_label.image = tk_img + self.filename.set(filename) + self.draw_preview() + + def draw_preview(self): + image = Images.create(self.filename.get(), 250, 135) + self.image_label.config(image=image) + self.image_label.image = image def click_clear(self): """ @@ -157,19 +164,20 @@ class CanvasBackgroundDialog(Dialog): :return: nothing """ # delete entry - self.file_name.set("") + self.filename.set("") # delete display image self.image_label.config(image="", width=32) + self.image_label.image = None def click_adjust_canvas(self): # deselect all radio buttons and grey them out - if self.adjust_to_dim_var.get() == 1: - self.radiovar.set(0) + if self.adjust_to_dim.get() == 1: + self.scale_option.set(0) for option in self.options: option.config(state=tk.DISABLED) # turn back the radio button to active state so that user can choose again - elif self.adjust_to_dim_var.get() == 0: - self.radiovar.set(1) + elif self.adjust_to_dim.get() == 0: + self.scale_option.set(1) for option in self.options: option.config(state=tk.NORMAL) else: @@ -192,9 +200,8 @@ class CanvasBackgroundDialog(Dialog): :return: nothing """ - canvas = self.app.canvas - grid = canvas.find_withtag("rectangle")[0] - x0, y0, x1, y1 = canvas.coords(grid) + grid = self.canvas.find_withtag("rectangle")[0] + x0, y0, x1, y1 = self.canvas.coords(grid) canvas_w = abs(x0 - x1) canvas_h = abs(y0 - y1) return canvas_w, canvas_h @@ -224,15 +231,12 @@ class CanvasBackgroundDialog(Dialog): cropped_tk = ImageTk.PhotoImage(cropped) # place left corner of image to the left corner of the canvas - self.app.croppedwallpaper = cropped_tk - + self.canvas.wallpaper_drawn = cropped_tk self.delete_canvas_components(["wallpaper"]) - # self.delete_previous_wallpaper() - wid = self.canvas.create_image( (cropx / 2, cropy / 2), image=cropped_tk, tags="wallpaper" ) - self.app.wallpaper_id = wid + self.canvas.wallpaper_id = wid def center(self, img): """ @@ -261,13 +265,13 @@ class CanvasBackgroundDialog(Dialog): cropped_tk = ImageTk.PhotoImage(cropped) # place the center of the image at the center of the canvas - self.app.croppedwallpaper = cropped_tk self.delete_canvas_components(["wallpaper"]) # self.delete_previous_wallpaper() wid = self.canvas.create_image( (canvas_w / 2, canvas_h / 2), image=cropped_tk, tags="wallpaper" ) - self.app.wallpaper_id = wid + self.canvas.wallpaper_id = wid + self.canvas.wallpaper_drawn = cropped_tk def scaled(self, img): """ @@ -279,15 +283,12 @@ class CanvasBackgroundDialog(Dialog): canvas_w, canvas_h = self.get_canvas_width_and_height() resized_image = img.resize((int(canvas_w), int(canvas_h)), Image.ANTIALIAS) image_tk = ImageTk.PhotoImage(resized_image) - self.app.croppedwallpaper = image_tk - self.delete_canvas_components(["wallpaper"]) - # self.delete_previous_wallpaper() - wid = self.canvas.create_image( (canvas_w / 2, canvas_h / 2), image=image_tk, tags="wallpaper" ) - self.app.wallpaper_id = wid + self.canvas.wallpaper_id = wid + self.canvas.wallpaper_drawn = image_tk def tiled(self, img): return @@ -310,20 +311,15 @@ class CanvasBackgroundDialog(Dialog): self.delete_canvas_components(["wallpaper"]) self.draw_new_canvas(img_w, img_h) wid = self.canvas.create_image((img_w / 2, img_h / 2), image=image_tk) - self.app.croppedwallpaper = image_tk - self.app.wallpaper_id = wid + self.canvas.wallpaper_id = wid + self.canvas.wallpaper_drawn = image_tk - def show_grid(self): - """ - - :return: nothing - """ - self.app.adjust_to_dim_var.set(self.adjust_to_dim_var.get()) - - if self.show_grid_var.get() == 0: + def draw_grid(self): + self.canvas.adjust_to_dim.set(self.adjust_to_dim.get()) + if self.show_grid.get() == 0: for i in self.canvas.find_withtag("gridline"): self.canvas.itemconfig(i, state=tk.HIDDEN) - elif self.show_grid_var.get() == 1: + elif self.show_grid.get() == 1: for i in self.canvas.find_withtag("gridline"): self.canvas.itemconfig(i, state=tk.NORMAL) self.canvas.lift(i) @@ -331,33 +327,36 @@ class CanvasBackgroundDialog(Dialog): logging.error("canvasbackground.py show_grid invalid value") def save_wallpaper_options(self): - self.app.radiovar.set(self.radiovar.get()) - self.app.show_grid_var.set(self.show_grid_var.get()) - self.app.adjust_to_dim_var.set(self.adjust_to_dim_var.get()) + self.canvas.scale_option.set(self.scale_option.get()) + self.canvas.show_grid.set(self.show_grid.get()) + self.canvas.adjust_to_dim.set(self.adjust_to_dim.get()) def click_apply(self): - filename = self.file_name.get() + filename = self.filename.get() if not filename: self.delete_canvas_components(["wallpaper"]) self.destroy() - self.app.current_wallpaper = None + self.canvas.wallpaper = None + self.canvas.wallpaper_file = None self.save_wallpaper_options() return try: img = Image.open(filename) - self.app.current_wallpaper = img + self.canvas.wallpaper = img + self.canvas.wallpaper_file = filename except FileNotFoundError: logging.error("invalid background: %s", filename) - if self.app.wallpaper_id: - self.canvas.delete(self.app.wallpaper_id) + if self.canvas.wallpaper_id: + self.canvas.delete(self.canvas.wallpaper_id) + self.canvas.wallpaper_id = None self.destroy() return - self.app.adjust_to_dim_var.set(self.adjust_to_dim_var.get()) - if self.adjust_to_dim_var.get() == 0: - self.app.radiovar.set(self.radiovar.get()) - option = ScaleOption(self.radiovar.get()) + self.canvas.adjust_to_dim.set(self.adjust_to_dim.get()) + if self.adjust_to_dim.get() == 0: + self.canvas.scale_option.set(self.scale_option.get()) + option = ScaleOption(self.scale_option.get()) if option == ScaleOption.UPPER_LEFT: self.upper_left(img) elif option == ScaleOption.CENTERED: @@ -365,10 +364,9 @@ class CanvasBackgroundDialog(Dialog): elif option == ScaleOption.SCALED: self.scaled(img) elif option == ScaleOption.TILED: - print("not implemented yet") - - elif self.adjust_to_dim_var.get() == 1: + logging.warning("tiled background not implemented yet") + elif self.adjust_to_dim.get() == 1: self.canvas_to_image_dimension(img) - self.show_grid() + self.draw_grid() self.destroy() diff --git a/coretk/coretk/dialogs/canvassizeandscale.py b/coretk/coretk/dialogs/canvassizeandscale.py index 5bbd2ec2..598d8d37 100644 --- a/coretk/coretk/dialogs/canvassizeandscale.py +++ b/coretk/coretk/dialogs/canvassizeandscale.py @@ -1,6 +1,7 @@ """ size and scale """ +import logging import tkinter as tk from tkinter import font, ttk @@ -9,7 +10,6 @@ from coretk.dialogs.dialog import Dialog DRAW_OBJECT_TAGS = ["edge", "node", "nodename", "linkinfo", "antenna"] FRAME_PAD = 5 -PAD = (0, 0, 5, 0) PADX = 5 @@ -21,13 +21,13 @@ class SizeAndScaleDialog(Dialog): :param app: main application """ super().__init__(master, app, "Canvas Size and Scale", modal=True) - self.meter_per_pixel = self.app.canvas.meters_per_pixel + self.canvas = self.app.canvas + self.meter_per_pixel = self.canvas.meters_per_pixel self.section_font = font.Font(weight="bold") # get current canvas dimensions - canvas = self.app.canvas - plot = canvas.find_withtag("rectangle") - x0, y0, x1, y1 = canvas.bbox(plot[0]) + plot = self.canvas.find_withtag("rectangle") + x0, y0, x1, y1 = self.canvas.bbox(plot[0]) width = abs(x0 - x1) - 2 height = abs(y0 - y1) - 2 self.pixel_width = tk.IntVar(value=width) @@ -122,14 +122,12 @@ class SizeAndScaleDialog(Dialog): label = ttk.Label(frame, text="X") label.grid(row=0, column=0, sticky="w", padx=PADX) - x_var = tk.StringVar(value=0) - entry = ttk.Entry(frame, textvariable=x_var) + entry = ttk.Entry(frame, textvariable=self.x) entry.grid(row=0, column=1, sticky="ew", padx=PADX) label = ttk.Label(frame, text="Y") label.grid(row=0, column=2, sticky="w", padx=PADX) - y_var = tk.StringVar(value=0) - entry = ttk.Entry(frame, textvariable=y_var) + entry = ttk.Entry(frame, textvariable=self.y) entry.grid(row=0, column=3, sticky="ew", padx=PADX) label = ttk.Label(label_frame, text="Translates To") @@ -181,40 +179,40 @@ class SizeAndScaleDialog(Dialog): :return: nothing """ width, height = self.pixel_width.get(), self.pixel_height.get() - - canvas = self.app.canvas - canvas.config(scrollregion=(0, 0, width + 200, height + 200)) + self.canvas.config(scrollregion=(0, 0, width + 200, height + 200)) # delete old plot and redraw - for i in canvas.find_withtag("gridline"): - canvas.delete(i) - for i in canvas.find_withtag("rectangle"): - canvas.delete(i) + for i in self.canvas.find_withtag("gridline"): + self.canvas.delete(i) + for i in self.canvas.find_withtag("rectangle"): + self.canvas.delete(i) - canvas.draw_grid(width=width, height=height) + self.canvas.draw_grid(width=width, height=height) # lift anything that is drawn on the plot before for tag in DRAW_OBJECT_TAGS: - for i in canvas.find_withtag(tag): - canvas.lift(i) + for i in self.canvas.find_withtag(tag): + self.canvas.lift(i) def click_apply(self): meter_per_pixel = float(self.scale.get()) / 100 - self.app.canvas.meters_per_pixel = meter_per_pixel + self.canvas.meters_per_pixel = meter_per_pixel self.redraw_grid() - # if there is a current wallpaper showing, redraw it based on current wallpaper options + # if there is a current wallpaper showing, redraw it based on current + # wallpaper options wallpaper_tool = self.app.set_wallpaper - current_wallpaper = self.app.current_wallpaper - if current_wallpaper: - if self.app.adjust_to_dim_var.get() == 0: - if self.app.radiovar.get() == ScaleOption.UPPER_LEFT.value: - wallpaper_tool.upper_left(current_wallpaper) - elif self.app.radiovar.get() == ScaleOption.CENTERED.value: - wallpaper_tool.center(current_wallpaper) - elif self.app.radiovar.get() == ScaleOption.SCALED.value: - wallpaper_tool.scaled(current_wallpaper) - elif self.app.radiovar.get() == ScaleOption.TILED.value: - print("not implemented") - elif self.app.adjust_to_dim_var.get() == 1: - wallpaper_tool.canvas_to_image_dimension(current_wallpaper) + wallpaper = self.canvas.wallpaper + if wallpaper: + if self.canvas.adjust_to_dim.get() == 0: + scale_option = ScaleOption(self.canvas.scale_option.get()) + if scale_option == ScaleOption.UPPER_LEFT: + wallpaper_tool.upper_left(wallpaper) + elif scale_option == ScaleOption.CENTERED: + wallpaper_tool.center(wallpaper) + elif scale_option == ScaleOption.SCALED: + wallpaper_tool.scaled(wallpaper) + elif scale_option == ScaleOption.TILED: + logging.warning("tiled background not implemented") + elif self.canvas.adjust_to_dim.get() == 1: + wallpaper_tool.canvas_to_image_dimension(wallpaper) wallpaper_tool.show_grid() self.destroy() diff --git a/coretk/coretk/graph.py b/coretk/coretk/graph.py index 5cdd2eec..1bec481b 100644 --- a/coretk/coretk/graph.py +++ b/coretk/coretk/graph.py @@ -43,11 +43,8 @@ class CanvasGraph(tk.Canvas): self.drawing_edge = None self.grid = None self.meters_per_pixel = 1.5 - self.canvas_management = CanvasComponentManagement(self, core) - self.canvas_action = CanvasAction(master, self) - self.setup_menus() self.setup_bindings() self.draw_grid() @@ -57,6 +54,15 @@ class CanvasGraph(tk.Canvas): self.wireless_draw = WirelessConnection(self, core) self.is_node_context_opened = False + # background related + self.wallpaper_id = None + self.wallpaper = None + self.wallpaper_drawn = None + self.wallpaper_file = "" + self.scale_option = tk.IntVar(value=1) + self.show_grid = tk.IntVar(value=1) + self.adjust_to_dim = tk.IntVar(value=0) + def setup_menus(self): self.node_context = tk.Menu(self.master) self.node_context.add_command( @@ -95,8 +101,6 @@ class CanvasGraph(tk.Canvas): self.drawing_edge = None self.draw_existing_component(session) - # self.grpc_manager.wlanconfig_management.load_wlan_configurations(self.core_grpc) - def setup_bindings(self): """ Bind any mouse events or hot keys to the matching action diff --git a/coretk/coretk/menuaction.py b/coretk/coretk/menuaction.py index 98b3d254..b47c6ef2 100644 --- a/coretk/coretk/menuaction.py +++ b/coretk/coretk/menuaction.py @@ -73,7 +73,6 @@ class MenuAction: def file_open_xml(self, event=None): logging.info("menuaction.py file_open_xml()") - self.app.is_open_xml = True file_path = filedialog.askopenfilename( initialdir=str(XML_PATH), title="Open",