initial add with a common dialog class and leveraging it for a session options dialog
This commit is contained in:
parent
e19e8c12f7
commit
891e9aef9a
6 changed files with 117 additions and 5 deletions
52
coretk/coretk/configutils.py
Normal file
52
coretk/coretk/configutils.py
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
import enum
|
||||||
|
import logging
|
||||||
|
import tkinter as tk
|
||||||
|
from tkinter import ttk
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigType(enum.Enum):
|
||||||
|
STRING = 10
|
||||||
|
BOOL = 11
|
||||||
|
|
||||||
|
|
||||||
|
def create_config(master, config, pad_x=2, pad_y=2):
|
||||||
|
master.columnconfigure(0, weight=1)
|
||||||
|
master.columnconfigure(1, weight=3)
|
||||||
|
values = {}
|
||||||
|
for index, key in enumerate(sorted(config)):
|
||||||
|
option = config[key]
|
||||||
|
label = tk.Label(master, text=option.label)
|
||||||
|
label.grid(row=index, pady=pad_y, padx=pad_x, sticky="ew")
|
||||||
|
value = tk.StringVar()
|
||||||
|
config_type = ConfigType(option.type)
|
||||||
|
if config_type == ConfigType.BOOL:
|
||||||
|
select = tuple(option.select)
|
||||||
|
combobox = ttk.Combobox(master, textvariable=value, values=select)
|
||||||
|
combobox.grid(row=index, column=1, sticky="ew", pady=pad_y)
|
||||||
|
if option.value == "1":
|
||||||
|
value.set("On")
|
||||||
|
else:
|
||||||
|
value.set("Off")
|
||||||
|
elif config_type == ConfigType.STRING:
|
||||||
|
entry = tk.Entry(master, textvariable=value)
|
||||||
|
entry.grid(row=index, column=1, sticky="ew", pady=pad_y)
|
||||||
|
else:
|
||||||
|
logging.error("unhandled config option type: %s", config_type)
|
||||||
|
values[key] = value
|
||||||
|
return values
|
||||||
|
|
||||||
|
|
||||||
|
def parse_config(options, values):
|
||||||
|
config = {}
|
||||||
|
for key in options:
|
||||||
|
option = options[key]
|
||||||
|
value = values[key]
|
||||||
|
config_type = ConfigType(option.type)
|
||||||
|
config_value = value.get()
|
||||||
|
if config_type == ConfigType.BOOL:
|
||||||
|
if config_value == "On":
|
||||||
|
config_value = "1"
|
||||||
|
else:
|
||||||
|
config_value = "0"
|
||||||
|
config[key] = config_value
|
||||||
|
return config
|
|
@ -620,7 +620,7 @@ class CoreMenubar(object):
|
||||||
underline=0,
|
underline=0,
|
||||||
)
|
)
|
||||||
session_menu.add_command(
|
session_menu.add_command(
|
||||||
label="Options...", command=action.session_options, underline=0
|
label="Options...", command=self.menu_action.session_options, underline=0
|
||||||
)
|
)
|
||||||
|
|
||||||
self.menubar.add_cascade(label="Session", menu=session_menu, underline=0)
|
self.menubar.add_cascade(label="Session", menu=session_menu, underline=0)
|
||||||
|
|
0
coretk/coretk/dialogs/__init__.py
Normal file
0
coretk/coretk/dialogs/__init__.py
Normal file
23
coretk/coretk/dialogs/dialog.py
Normal file
23
coretk/coretk/dialogs/dialog.py
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
import tkinter as tk
|
||||||
|
|
||||||
|
from coretk.images import ImageEnum, Images
|
||||||
|
|
||||||
|
|
||||||
|
class Dialog(tk.Toplevel):
|
||||||
|
def __init__(self, master, title, modal=False):
|
||||||
|
super().__init__(master, padx=5, pady=5)
|
||||||
|
image = Images.get(ImageEnum.CORE.value)
|
||||||
|
self.tk.call("wm", "iconphoto", self._w, image)
|
||||||
|
self.title(title)
|
||||||
|
self.protocol("WM_DELETE_WINDOW", self.destroy)
|
||||||
|
self.withdraw()
|
||||||
|
self.modal = modal
|
||||||
|
|
||||||
|
def show(self):
|
||||||
|
self.transient(self.master)
|
||||||
|
self.focus_force()
|
||||||
|
if self.modal:
|
||||||
|
self.grab_set()
|
||||||
|
self.update()
|
||||||
|
self.deiconify()
|
||||||
|
self.wait_window()
|
35
coretk/coretk/dialogs/sessionoptions.py
Normal file
35
coretk/coretk/dialogs/sessionoptions.py
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
import logging
|
||||||
|
import tkinter as tk
|
||||||
|
|
||||||
|
from coretk import configutils
|
||||||
|
from coretk.dialogs.dialog import Dialog
|
||||||
|
|
||||||
|
PAD_X = 2
|
||||||
|
PAD_Y = 2
|
||||||
|
|
||||||
|
|
||||||
|
class SessionOptionsDialog(Dialog):
|
||||||
|
def __init__(self, master):
|
||||||
|
super().__init__(master, "Session Options", modal=True)
|
||||||
|
self.options = None
|
||||||
|
self.values = None
|
||||||
|
self.save_button = tk.Button(self, text="Save", command=self.save)
|
||||||
|
self.cancel_button = tk.Button(self, text="Cancel", command=self.destroy)
|
||||||
|
self.draw()
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
session_id = self.master.core_grpc.session_id
|
||||||
|
response = self.master.core_grpc.core.get_session_options(session_id)
|
||||||
|
logging.info("session options: %s", response)
|
||||||
|
self.options = response.config
|
||||||
|
self.values = configutils.create_config(self, self.options, PAD_X, PAD_Y)
|
||||||
|
row = len(response.config)
|
||||||
|
self.save_button.grid(row=row, pady=PAD_Y, padx=PAD_X, sticky="ew")
|
||||||
|
self.cancel_button.grid(row=row, column=1, pady=PAD_Y, padx=PAD_X, sticky="ew")
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
config = configutils.parse_config(self.options, self.values)
|
||||||
|
session_id = self.master.core_grpc.session_id
|
||||||
|
response = self.master.core_grpc.core.set_session_options(session_id, config)
|
||||||
|
logging.info("saved session config: %s", response)
|
||||||
|
self.destroy()
|
|
@ -7,6 +7,7 @@ import webbrowser
|
||||||
from tkinter import filedialog, messagebox
|
from tkinter import filedialog, messagebox
|
||||||
|
|
||||||
from core.api.grpc import core_pb2
|
from core.api.grpc import core_pb2
|
||||||
|
from coretk.dialogs.sessionoptions import SessionOptionsDialog
|
||||||
from coretk.setwallpaper import CanvasWallpaper
|
from coretk.setwallpaper import CanvasWallpaper
|
||||||
from coretk.sizeandscale import SizeAndScale
|
from coretk.sizeandscale import SizeAndScale
|
||||||
|
|
||||||
|
@ -314,10 +315,6 @@ def session_emulation_servers():
|
||||||
logging.debug("Click session emulation servers")
|
logging.debug("Click session emulation servers")
|
||||||
|
|
||||||
|
|
||||||
def session_options():
|
|
||||||
logging.debug("Click session options")
|
|
||||||
|
|
||||||
|
|
||||||
def help_about():
|
def help_about():
|
||||||
logging.debug("Click help About")
|
logging.debug("Click help About")
|
||||||
|
|
||||||
|
@ -433,3 +430,8 @@ class MenuAction:
|
||||||
|
|
||||||
def help_core_documentation(self):
|
def help_core_documentation(self):
|
||||||
webbrowser.open_new("http://coreemu.github.io/core/")
|
webbrowser.open_new("http://coreemu.github.io/core/")
|
||||||
|
|
||||||
|
def session_options(self):
|
||||||
|
logging.debug("Click session options")
|
||||||
|
dialog = SessionOptionsDialog(self.application)
|
||||||
|
dialog.show()
|
||||||
|
|
Loading…
Add table
Reference in a new issue