2019-11-14 06:49:32 +00:00
|
|
|
import logging
|
2020-03-02 18:18:37 +00:00
|
|
|
import math
|
2019-11-11 23:35:48 +00:00
|
|
|
import tkinter as tk
|
|
|
|
from tkinter import ttk
|
2020-01-13 23:31:41 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2019-11-11 23:35:48 +00:00
|
|
|
|
2019-12-19 17:30:21 +00:00
|
|
|
from core.gui import appconfig
|
|
|
|
from core.gui.dialogs.dialog import Dialog
|
2020-02-12 16:35:14 +00:00
|
|
|
from core.gui.themes import FRAME_PAD, PADX, PADY, scale_fonts
|
2020-03-02 19:20:00 +00:00
|
|
|
from core.gui.validation import LARGEST_SCALE, SMALLEST_SCALE
|
2019-12-11 22:09:50 +00:00
|
|
|
|
2020-01-13 23:31:41 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from core.gui.app import Application
|
|
|
|
|
2020-03-02 19:02:54 +00:00
|
|
|
SCALE_INTERVAL = 0.01
|
|
|
|
|
2019-11-11 23:35:48 +00:00
|
|
|
|
|
|
|
class PreferencesDialog(Dialog):
|
2020-05-05 06:50:59 +01:00
|
|
|
def __init__(self, app: "Application"):
|
|
|
|
super().__init__(app, "Preferences")
|
2020-02-17 19:10:13 +00:00
|
|
|
self.gui_scale = tk.DoubleVar(value=self.app.app_scale)
|
2019-11-22 06:03:07 +00:00
|
|
|
preferences = self.app.guiconfig["preferences"]
|
2019-11-11 23:35:48 +00:00
|
|
|
self.editor = tk.StringVar(value=preferences["editor"])
|
2019-11-14 06:49:32 +00:00
|
|
|
self.theme = tk.StringVar(value=preferences["theme"])
|
2019-11-11 23:35:48 +00:00
|
|
|
self.terminal = tk.StringVar(value=preferences["terminal"])
|
|
|
|
self.gui3d = tk.StringVar(value=preferences["gui3d"])
|
|
|
|
self.draw()
|
|
|
|
|
|
|
|
def draw(self):
|
2019-11-13 18:45:43 +00:00
|
|
|
self.top.columnconfigure(0, weight=1)
|
2019-12-11 22:09:50 +00:00
|
|
|
self.top.rowconfigure(0, weight=1)
|
2019-11-14 06:49:32 +00:00
|
|
|
self.draw_preferences()
|
2019-11-11 23:35:48 +00:00
|
|
|
self.draw_buttons()
|
|
|
|
|
2019-11-14 06:49:32 +00:00
|
|
|
def draw_preferences(self):
|
2019-12-11 22:36:27 +00:00
|
|
|
frame = ttk.LabelFrame(self.top, text="Preferences", padding=FRAME_PAD)
|
|
|
|
frame.grid(sticky="nsew", pady=PADY)
|
2019-11-11 23:35:48 +00:00
|
|
|
frame.columnconfigure(1, weight=1)
|
|
|
|
|
2019-11-14 06:49:32 +00:00
|
|
|
label = ttk.Label(frame, text="Theme")
|
2019-12-11 22:36:27 +00:00
|
|
|
label.grid(row=0, column=0, pady=PADY, padx=PADX, sticky="w")
|
2019-11-14 06:49:32 +00:00
|
|
|
themes = self.app.style.theme_names()
|
2019-11-11 23:35:48 +00:00
|
|
|
combobox = ttk.Combobox(
|
2019-11-14 06:49:32 +00:00
|
|
|
frame, textvariable=self.theme, values=themes, state="readonly"
|
2019-11-11 23:35:48 +00:00
|
|
|
)
|
2019-11-14 06:49:32 +00:00
|
|
|
combobox.set(self.theme.get())
|
2019-11-11 23:35:48 +00:00
|
|
|
combobox.grid(row=0, column=1, sticky="ew")
|
2019-11-14 06:49:32 +00:00
|
|
|
combobox.bind("<<ComboboxSelected>>", self.theme_change)
|
2019-11-11 23:35:48 +00:00
|
|
|
|
2019-11-14 06:49:32 +00:00
|
|
|
label = ttk.Label(frame, text="Editor")
|
2019-12-11 22:36:27 +00:00
|
|
|
label.grid(row=1, column=0, pady=PADY, padx=PADX, sticky="w")
|
2019-11-14 06:49:32 +00:00
|
|
|
combobox = ttk.Combobox(
|
|
|
|
frame, textvariable=self.editor, values=appconfig.EDITORS, state="readonly"
|
|
|
|
)
|
|
|
|
combobox.grid(row=1, column=1, sticky="ew")
|
|
|
|
|
|
|
|
label = ttk.Label(frame, text="Terminal")
|
2019-12-11 22:36:27 +00:00
|
|
|
label.grid(row=2, column=0, pady=PADY, padx=PADX, sticky="w")
|
2020-03-04 21:30:01 +00:00
|
|
|
terminals = sorted(appconfig.TERMINALS.values())
|
2020-03-04 22:27:29 +00:00
|
|
|
combobox = ttk.Combobox(frame, textvariable=self.terminal, values=terminals)
|
2019-11-14 06:49:32 +00:00
|
|
|
combobox.grid(row=2, column=1, sticky="ew")
|
2019-11-11 23:35:48 +00:00
|
|
|
|
|
|
|
label = ttk.Label(frame, text="3D GUI")
|
2019-12-11 22:36:27 +00:00
|
|
|
label.grid(row=3, column=0, pady=PADY, padx=PADX, sticky="w")
|
2019-11-11 23:35:48 +00:00
|
|
|
entry = ttk.Entry(frame, textvariable=self.gui3d)
|
2019-11-14 06:49:32 +00:00
|
|
|
entry.grid(row=3, column=1, sticky="ew")
|
2019-11-11 23:35:48 +00:00
|
|
|
|
2020-02-10 23:20:07 +00:00
|
|
|
label = ttk.Label(frame, text="Scaling")
|
|
|
|
label.grid(row=4, column=0, pady=PADY, padx=PADX, sticky="w")
|
|
|
|
|
|
|
|
scale_frame = ttk.Frame(frame)
|
|
|
|
scale_frame.grid(row=4, column=1, sticky="ew")
|
|
|
|
scale_frame.columnconfigure(0, weight=1)
|
|
|
|
scale = ttk.Scale(
|
|
|
|
scale_frame,
|
2020-03-02 19:20:00 +00:00
|
|
|
from_=SMALLEST_SCALE,
|
|
|
|
to=LARGEST_SCALE,
|
2020-02-10 23:20:07 +00:00
|
|
|
value=1,
|
|
|
|
orient=tk.HORIZONTAL,
|
|
|
|
variable=self.gui_scale,
|
|
|
|
)
|
|
|
|
scale.grid(row=0, column=0, sticky="ew")
|
|
|
|
entry = ttk.Entry(
|
2020-03-02 17:56:57 +00:00
|
|
|
scale_frame,
|
|
|
|
textvariable=self.gui_scale,
|
|
|
|
width=4,
|
|
|
|
validate="key",
|
|
|
|
validatecommand=(self.app.validation.app_scale, "%P"),
|
2020-02-10 23:20:07 +00:00
|
|
|
)
|
|
|
|
entry.grid(row=0, column=1)
|
|
|
|
|
2020-03-02 19:02:54 +00:00
|
|
|
scrollbar = ttk.Scrollbar(scale_frame, command=self.adjust_scale)
|
|
|
|
scrollbar.grid(row=0, column=2)
|
|
|
|
|
2019-11-11 23:35:48 +00:00
|
|
|
def draw_buttons(self):
|
2019-11-13 18:45:43 +00:00
|
|
|
frame = ttk.Frame(self.top)
|
2019-11-11 23:35:48 +00:00
|
|
|
frame.grid(sticky="ew")
|
|
|
|
for i in range(2):
|
|
|
|
frame.columnconfigure(i, weight=1)
|
|
|
|
|
|
|
|
button = ttk.Button(frame, text="Save", command=self.click_save)
|
2019-12-11 22:36:27 +00:00
|
|
|
button.grid(row=0, column=0, sticky="ew", padx=PADX)
|
2019-11-11 23:35:48 +00:00
|
|
|
|
|
|
|
button = ttk.Button(frame, text="Cancel", command=self.destroy)
|
|
|
|
button.grid(row=0, column=1, sticky="ew")
|
|
|
|
|
2020-01-13 20:03:13 +00:00
|
|
|
def theme_change(self, event: tk.Event):
|
2019-11-14 06:49:32 +00:00
|
|
|
theme = self.theme.get()
|
|
|
|
logging.info("changing theme: %s", theme)
|
|
|
|
self.app.style.theme_use(theme)
|
|
|
|
|
2019-11-11 23:35:48 +00:00
|
|
|
def click_save(self):
|
2019-11-22 06:03:07 +00:00
|
|
|
preferences = self.app.guiconfig["preferences"]
|
2019-11-11 23:35:48 +00:00
|
|
|
preferences["terminal"] = self.terminal.get()
|
|
|
|
preferences["editor"] = self.editor.get()
|
|
|
|
preferences["gui3d"] = self.gui3d.get()
|
2019-11-14 06:49:32 +00:00
|
|
|
preferences["theme"] = self.theme.get()
|
2020-02-17 19:10:13 +00:00
|
|
|
self.gui_scale.set(round(self.gui_scale.get(), 2))
|
|
|
|
app_scale = self.gui_scale.get()
|
|
|
|
self.app.guiconfig["scale"] = app_scale
|
|
|
|
|
2019-11-11 23:35:48 +00:00
|
|
|
self.app.save_config()
|
2020-02-12 22:13:28 +00:00
|
|
|
self.scale_adjust()
|
2019-11-11 23:35:48 +00:00
|
|
|
self.destroy()
|
2020-02-10 23:20:07 +00:00
|
|
|
|
2020-02-12 22:13:28 +00:00
|
|
|
def scale_adjust(self):
|
2020-02-10 23:20:07 +00:00
|
|
|
app_scale = self.gui_scale.get()
|
2020-02-17 19:10:13 +00:00
|
|
|
self.app.app_scale = app_scale
|
2020-02-12 22:13:28 +00:00
|
|
|
self.app.master.tk.call("tk", "scaling", app_scale)
|
|
|
|
|
|
|
|
# scale fonts
|
2020-02-12 16:35:14 +00:00
|
|
|
scale_fonts(self.app.fonts_size, app_scale)
|
2020-03-02 18:18:37 +00:00
|
|
|
text_scale = app_scale if app_scale < 1 else math.sqrt(app_scale)
|
|
|
|
self.app.icon_text_font.config(size=int(12 * text_scale))
|
|
|
|
self.app.edge_font.config(size=int(8 * text_scale))
|
2020-02-12 22:13:28 +00:00
|
|
|
|
2020-02-17 23:14:52 +00:00
|
|
|
# scale application window
|
|
|
|
self.app.center()
|
2020-02-13 20:15:56 +00:00
|
|
|
|
2020-02-17 23:14:52 +00:00
|
|
|
# scale toolbar and canvas items
|
|
|
|
self.app.toolbar.scale()
|
2020-02-13 20:15:56 +00:00
|
|
|
self.app.canvas.scale_graph()
|
2020-03-02 19:02:54 +00:00
|
|
|
|
|
|
|
def adjust_scale(self, arg1: str, arg2: str, arg3: str):
|
|
|
|
scale_value = self.gui_scale.get()
|
|
|
|
if arg2 == "-1":
|
2020-03-02 19:20:00 +00:00
|
|
|
if scale_value <= LARGEST_SCALE - SCALE_INTERVAL:
|
|
|
|
self.gui_scale.set(round(scale_value + SCALE_INTERVAL, 2))
|
2020-03-02 19:02:54 +00:00
|
|
|
else:
|
2020-03-02 19:20:00 +00:00
|
|
|
self.gui_scale.set(round(LARGEST_SCALE, 2))
|
2020-03-02 19:02:54 +00:00
|
|
|
elif arg2 == "1":
|
2020-03-02 19:20:00 +00:00
|
|
|
if scale_value >= SMALLEST_SCALE + SCALE_INTERVAL:
|
|
|
|
self.gui_scale.set(round(scale_value - SCALE_INTERVAL, 2))
|
2020-03-02 19:02:54 +00:00
|
|
|
else:
|
2020-03-02 19:20:00 +00:00
|
|
|
self.gui_scale.set(round(SMALLEST_SCALE, 2))
|