pygui: revamped config to leverage classes mapped to yaml, removes need for using keys all over and type hinting on glasses, future changes should support defaults better

This commit is contained in:
Blake Harnden 2020-05-08 00:16:25 -07:00
parent d9f48d14a7
commit 86ae87eafe
16 changed files with 251 additions and 208 deletions

View file

@ -241,16 +241,16 @@ class SizeAndScaleDialog(Dialog):
location.alt = self.alt.get()
location.scale = self.scale.get()
if self.save_default.get():
location_config = self.app.guiconfig["location"]
location_config["x"] = location.x
location_config["y"] = location.y
location_config["z"] = location.z
location_config["lat"] = location.lat
location_config["lon"] = location.lon
location_config["alt"] = location.alt
location_config["scale"] = location.scale
preferences = self.app.guiconfig["preferences"]
preferences["width"] = width
preferences["height"] = height
location_config = self.app.guiconfig.location
location_config.x = location.x
location_config.y = location.y
location_config.z = location.z
location_config.lat = location.lat
location_config.lon = location.lon
location_config.alt = location.alt
location_config.scale = location.scale
preferences = self.app.guiconfig.preferences
preferences.width = width
preferences.height = height
self.app.save_config()
self.destroy()

View file

@ -5,7 +5,7 @@ from tkinter import ttk
from typing import TYPE_CHECKING, Set
from core.gui import nodeutils
from core.gui.appconfig import ICONS_PATH
from core.gui.appconfig import ICONS_PATH, CustomNode
from core.gui.dialogs.dialog import Dialog
from core.gui.images import Images
from core.gui.nodeutils import NodeDraw
@ -201,17 +201,12 @@ class CustomNodesDialog(Dialog):
self.services.update(dialog.current_services)
def click_save(self):
self.app.guiconfig["nodes"].clear()
for name in sorted(self.app.core.custom_nodes):
self.app.guiconfig.nodes.clear()
for name in self.app.core.custom_nodes:
node_draw = self.app.core.custom_nodes[name]
self.app.guiconfig["nodes"].append(
{
"name": name,
"image": node_draw.image_file,
"services": list(node_draw.services),
}
)
logging.info("saving custom nodes: %s", self.app.guiconfig["nodes"])
custom_node = CustomNode(name, node_draw.image_file, node_draw.services)
self.app.guiconfig.nodes.append(custom_node)
logging.info("saving custom nodes: %s", self.app.guiconfig.nodes)
self.app.save_config()
self.destroy()
@ -219,7 +214,8 @@ class CustomNodesDialog(Dialog):
name = self.name.get()
if name not in self.app.core.custom_nodes:
image_file = Path(self.image_file).stem
node_draw = NodeDraw.from_custom(name, image_file, set(self.services))
custom_node = CustomNode(name, image_file, list(self.services))
node_draw = NodeDraw.from_custom(custom_node)
logging.info(
"created new custom node (%s), image file (%s), services: (%s)",
name,

View file

@ -45,7 +45,7 @@ class FindDialog(Dialog):
)
self.tree.grid(sticky="nsew", pady=PADY)
style = ttk.Style()
heading_size = int(self.app.guiconfig["scale"] * 10)
heading_size = int(self.app.guiconfig.scale * 10)
style.configure("Treeview.Heading", font=(None, heading_size, "bold"))
self.tree.column("nodeid", stretch=tk.YES, anchor="center")
self.tree.heading("nodeid", text="Node ID")
@ -124,7 +124,7 @@ class FindDialog(Dialog):
canvas_node = self.app.core.canvas_nodes[node_id]
x0, y0, x1, y1 = self.app.canvas.bbox(canvas_node.id)
dist = 5 * self.app.guiconfig["scale"]
dist = 5 * self.app.guiconfig.scale
self.app.canvas.create_oval(
x0 - dist,
y0 - dist,
@ -132,7 +132,7 @@ class FindDialog(Dialog):
y1 + dist,
tags="find",
outline="red",
width=3.0 * self.app.guiconfig["scale"],
width=3.0 * self.app.guiconfig.scale,
)
_x, _y, _, _ = self.app.canvas.bbox(canvas_node.id)

View file

@ -4,7 +4,6 @@ from typing import TYPE_CHECKING
import netaddr
from core.gui import appconfig
from core.gui.dialogs.dialog import Dialog
from core.gui.themes import FRAME_PAD, PADX, PADY
from core.gui.widgets import ListboxScroll
@ -16,11 +15,10 @@ if TYPE_CHECKING:
class IpConfigDialog(Dialog):
def __init__(self, app: "Application") -> None:
super().__init__(app, "IP Configuration")
ip_config = self.app.guiconfig.setdefault("ips")
self.ip4 = ip_config.setdefault("ip4", appconfig.DEFAULT_IP4)
self.ip6 = ip_config.setdefault("ip6", appconfig.DEFAULT_IP6)
self.ip4s = ip_config.setdefault("ip4s", appconfig.DEFAULT_IP4S)
self.ip6s = ip_config.setdefault("ip6s", appconfig.DEFAULT_IP6S)
self.ip4 = self.app.guiconfig.ips.ip4
self.ip6 = self.app.guiconfig.ips.ip6
self.ip4s = self.app.guiconfig.ips.ip4s
self.ip6s = self.app.guiconfig.ips.ip6s
self.ip4_entry = None
self.ip4_listbox = None
self.ip6_entry = None
@ -143,11 +141,11 @@ class IpConfigDialog(Dialog):
for index in range(self.ip6_listbox.listbox.size()):
ip6 = self.ip6_listbox.listbox.get(index)
ip6s.append(ip6)
ip_config = self.app.guiconfig["ips"]
ip_config["ip4"] = self.ip4
ip_config["ip6"] = self.ip6
ip_config["ip4s"] = ip4s
ip_config["ip6s"] = ip6s
ip_config = self.app.guiconfig.ips
ip_config.ip4 = self.ip4
ip_config.ip6 = self.ip6
ip_config.ip4s = ip4s
ip_config.ip6s = ip6s
self.app.core.interfaces_manager.update_ips(self.ip4, self.ip6)
self.app.save_config()
self.destroy()

View file

@ -4,7 +4,6 @@ from typing import TYPE_CHECKING
import netaddr
from core.gui import appconfig
from core.gui.dialogs.dialog import Dialog
from core.gui.themes import PADX, PADY
@ -15,7 +14,7 @@ if TYPE_CHECKING:
class MacConfigDialog(Dialog):
def __init__(self, app: "Application") -> None:
super().__init__(app, "MAC Configuration")
mac = self.app.guiconfig.get("mac", appconfig.DEFAULT_MAC)
mac = self.app.guiconfig.mac
self.mac_var = tk.StringVar(value=mac)
self.draw()
@ -57,6 +56,6 @@ class MacConfigDialog(Dialog):
messagebox.showerror("MAC Error", f"{mac} is an invalid mac")
else:
self.app.core.interfaces_manager.mac = netaddr.EUI(mac)
self.app.guiconfig["mac"] = mac
self.app.guiconfig.mac = mac
self.app.save_config()
self.destroy()

View file

@ -2,7 +2,7 @@ import tkinter as tk
from tkinter import messagebox, ttk
from typing import TYPE_CHECKING
from core.gui.coreclient import Observer
from core.gui.appconfig import Observer
from core.gui.dialogs.dialog import Dialog
from core.gui.themes import PADX, PADY
from core.gui.widgets import ListboxScroll
@ -89,11 +89,9 @@ class ObserverDialog(Dialog):
button.grid(row=0, column=1, sticky="ew")
def click_save_config(self):
observers = []
for name in sorted(self.app.core.custom_observers):
observer = self.app.core.custom_observers[name]
observers.append({"name": observer.name, "cmd": observer.cmd})
self.app.guiconfig["observers"] = observers
self.app.guiconfig.observers.clear()
for observer in self.app.core.custom_observers.values():
self.app.guiconfig.observers.append(observer)
self.app.save_config()
self.destroy()

View file

@ -19,11 +19,11 @@ class PreferencesDialog(Dialog):
def __init__(self, app: "Application"):
super().__init__(app, "Preferences")
self.gui_scale = tk.DoubleVar(value=self.app.app_scale)
preferences = self.app.guiconfig["preferences"]
self.editor = tk.StringVar(value=preferences["editor"])
self.theme = tk.StringVar(value=preferences["theme"])
self.terminal = tk.StringVar(value=preferences["terminal"])
self.gui3d = tk.StringVar(value=preferences["gui3d"])
preferences = self.app.guiconfig.preferences
self.editor = tk.StringVar(value=preferences.editor)
self.theme = tk.StringVar(value=preferences.theme)
self.terminal = tk.StringVar(value=preferences.terminal)
self.gui3d = tk.StringVar(value=preferences.gui3d)
self.draw()
def draw(self):
@ -110,15 +110,14 @@ class PreferencesDialog(Dialog):
self.app.style.theme_use(theme)
def click_save(self):
preferences = self.app.guiconfig["preferences"]
preferences["terminal"] = self.terminal.get()
preferences["editor"] = self.editor.get()
preferences["gui3d"] = self.gui3d.get()
preferences["theme"] = self.theme.get()
preferences = self.app.guiconfig.preferences
preferences.terminal = self.terminal.get()
preferences.editor = self.editor.get()
preferences.gui3d = self.gui3d.get()
preferences.theme = self.theme.get()
self.gui_scale.set(round(self.gui_scale.get(), 2))
app_scale = self.gui_scale.get()
self.app.guiconfig["scale"] = app_scale
self.app.guiconfig.scale = app_scale
self.app.save_config()
self.scale_adjust()
self.destroy()

View file

@ -2,7 +2,7 @@ import tkinter as tk
from tkinter import ttk
from typing import TYPE_CHECKING
from core.gui.coreclient import CoreServer
from core.gui.appconfig import CoreServer
from core.gui.dialogs.dialog import Dialog
from core.gui.themes import FRAME_PAD, PADX, PADY
from core.gui.widgets import ListboxScroll
@ -20,7 +20,6 @@ class ServersDialog(Dialog):
super().__init__(app, "CORE Servers")
self.name = tk.StringVar(value=DEFAULT_NAME)
self.address = tk.StringVar(value=DEFAULT_ADDRESS)
self.port = tk.IntVar(value=DEFAULT_PORT)
self.servers = None
self.selected_index = None
self.selected = None
@ -54,31 +53,17 @@ class ServersDialog(Dialog):
frame.grid(pady=PADY, sticky="ew")
frame.columnconfigure(1, weight=1)
frame.columnconfigure(3, weight=1)
frame.columnconfigure(5, weight=1)
label = ttk.Label(frame, text="Name")
label.grid(row=0, column=0, sticky="w", padx=PADX, pady=PADY)
label.grid(row=0, column=0, sticky="w", padx=PADX)
entry = ttk.Entry(frame, textvariable=self.name)
entry.grid(row=0, column=1, sticky="ew")
label = ttk.Label(frame, text="Address")
label.grid(row=0, column=2, sticky="w", padx=PADX, pady=PADY)
label.grid(row=0, column=2, sticky="w", padx=PADX)
entry = ttk.Entry(frame, textvariable=self.address)
entry.grid(row=0, column=3, sticky="ew")
label = ttk.Label(frame, text="Port")
label.grid(row=0, column=4, sticky="w", padx=PADX, pady=PADY)
entry = ttk.Entry(
frame,
textvariable=self.port,
validate="key",
validatecommand=(self.app.validation.positive_int, "%P"),
)
entry.bind(
"<FocusOut>", lambda event: self.app.validation.focus_out(event, "50051")
)
entry.grid(row=0, column=5, sticky="ew")
def draw_servers_buttons(self):
frame = ttk.Frame(self.top)
frame.grid(pady=PADY, sticky="ew")
@ -113,13 +98,9 @@ class ServersDialog(Dialog):
button.grid(row=0, column=1, sticky="ew")
def click_save_configuration(self):
servers = []
for name in sorted(self.app.core.servers):
server = self.app.core.servers[name]
servers.append(
{"name": server.name, "address": server.address, "port": server.port}
)
self.app.guiconfig["servers"] = servers
self.app.guiconfig.servers.clear()
for server in self.app.core.servers.values():
self.app.guiconfig.servers.append(server)
self.app.save_config()
self.destroy()
@ -127,8 +108,7 @@ class ServersDialog(Dialog):
name = self.name.get()
if name not in self.app.core.servers:
address = self.address.get()
port = self.port.get()
server = CoreServer(name, address, port)
server = CoreServer(name, address)
self.app.core.servers[name] = server
self.servers.insert(tk.END, name)
@ -140,7 +120,6 @@ class ServersDialog(Dialog):
server = self.app.core.servers.pop(previous_name)
server.name = name
server.address = self.address.get()
server.port = self.port.get()
self.app.core.servers[name] = server
self.servers.delete(self.selected_index)
self.servers.insert(self.selected_index, name)
@ -154,7 +133,6 @@ class ServersDialog(Dialog):
self.selected_index = None
self.name.set(DEFAULT_NAME)
self.address.set(DEFAULT_ADDRESS)
self.port.set(DEFAULT_PORT)
self.servers.selection_clear(0, tk.END)
self.save_button.config(state=tk.DISABLED)
self.delete_button.config(state=tk.DISABLED)
@ -167,7 +145,6 @@ class ServersDialog(Dialog):
server = self.app.core.servers[self.selected]
self.name.set(server.name)
self.address.set(server.address)
self.port.set(server.port)
self.save_button.config(state=tk.NORMAL)
self.delete_button.config(state=tk.NORMAL)
else:

View file

@ -70,7 +70,7 @@ class SessionsDialog(Dialog):
selectmode=tk.BROWSE,
)
style = ttk.Style()
heading_size = int(self.app.guiconfig["scale"] * 10)
heading_size = int(self.app.guiconfig.scale * 10)
style.configure("Treeview.Heading", font=(None, heading_size, "bold"))
self.tree.grid(sticky="nsew")
self.tree.column("id", stretch=tk.YES, anchor="center")