added a root property for all the container with the main window. (self.master.master ... -> self.root). Fixed an issue with mystuff profile not being deleted properly

This commit is contained in:
Faraphel 2022-08-02 18:01:25 +02:00
parent 5dcb0d4343
commit 4ebd01e924
2 changed files with 50 additions and 30 deletions

View file

@ -40,6 +40,7 @@ class InstallerState(enum.Enum):
class Window(tkinter.Tk): class Window(tkinter.Tk):
def __init__(self, options: Option): def __init__(self, options: Option):
super().__init__() super().__init__()
self.root = self
self.options: Option = options self.options: Option = options
@ -140,6 +141,7 @@ class Window(tkinter.Tk):
class Menu(tkinter.Menu): class Menu(tkinter.Menu):
def __init__(self, master): def __init__(self, master):
super().__init__(master) super().__init__(master)
self.root = master.root
self.language = self.Language(self) self.language = self.Language(self)
self.advanced = self.Advanced(self) self.advanced = self.Advanced(self)
@ -149,12 +151,14 @@ class Menu(tkinter.Menu):
class Language(tkinter.Menu): class Language(tkinter.Menu):
def __init__(self, master: tkinter.Menu): def __init__(self, master: tkinter.Menu):
super().__init__(master, tearoff=False) super().__init__(master, tearoff=False)
self.root = master.root
master.add_cascade(label=_("LANGUAGE_SELECTION"), menu=self) master.add_cascade(label=_("LANGUAGE_SELECTION"), menu=self)
self.variable = tkinter.StringVar(value=self.master.master.options["language"]) self.variable = tkinter.StringVar(value=self.root.options["language"])
def callback(file: Path): def callback(file: Path):
def func(): self.master.master.options["language"] = file.stem def func(): self.root.options["language"] = file.stem
return func return func
for file in Path("./assets/language/").iterdir(): for file in Path("./assets/language/").iterdir():
@ -170,6 +174,7 @@ class Menu(tkinter.Menu):
class Advanced(tkinter.Menu): class Advanced(tkinter.Menu):
def __init__(self, master: tkinter.Menu): def __init__(self, master: tkinter.Menu):
super().__init__(master, tearoff=False) super().__init__(master, tearoff=False)
self.root = master.root
master.add_cascade(label=_("ADVANCED_CONFIGURATION"), menu=self) master.add_cascade(label=_("ADVANCED_CONFIGURATION"), menu=self)
self.add_command(label=_("OPEN_MYSTUFF_WINDOW"), command= mystuff.Window) self.add_command(label=_("OPEN_MYSTUFF_WINDOW"), command= mystuff.Window)
@ -179,12 +184,14 @@ class Menu(tkinter.Menu):
class ThreadsUsed(tkinter.Menu): class ThreadsUsed(tkinter.Menu):
def __init__(self, master: tkinter.Menu): def __init__(self, master: tkinter.Menu):
super().__init__(master, tearoff=False) super().__init__(master, tearoff=False)
self.root = master.root
master.add_cascade(label=_("THREADS_USED"), menu=self) master.add_cascade(label=_("THREADS_USED"), menu=self)
self.variable = tkinter.IntVar(value=master.master.master.options["threads"]) self.variable = tkinter.IntVar(value=self.root.options["threads"])
def callback(threads_amount: int): def callback(threads_amount: int):
def func(): self.master.master.master.options["threads"] = threads_amount def func(): self.root.options["threads"] = threads_amount
return func return func
for i in [1, 2, 4, 8, 12, 16]: for i in [1, 2, 4, 8, 12, 16]:
@ -199,6 +206,7 @@ class Menu(tkinter.Menu):
class Help(tkinter.Menu): class Help(tkinter.Menu):
def __init__(self, master: tkinter.Menu): def __init__(self, master: tkinter.Menu):
super().__init__(master, tearoff=False) super().__init__(master, tearoff=False)
self.root = master.root
master.add_cascade(label="Help", menu=self) master.add_cascade(label="Help", menu=self)
self.menu_id = self.master.index(tkinter.END) self.menu_id = self.master.index(tkinter.END)
@ -234,6 +242,8 @@ class Menu(tkinter.Menu):
class SourceGame(ttk.LabelFrame): class SourceGame(ttk.LabelFrame):
def __init__(self, master: tkinter.Tk): def __init__(self, master: tkinter.Tk):
super().__init__(master, text="Original Game File") super().__init__(master, text="Original Game File")
self.root = master.root
self.columnconfigure(1, weight=1) self.columnconfigure(1, weight=1)
self.entry = ttk.Entry(self, width=55) self.entry = ttk.Entry(self, width=55)
@ -268,7 +278,7 @@ class SourceGame(ttk.LabelFrame):
self.entry.insert(0, str(path.absolute())) self.entry.insert(0, str(path.absolute()))
if path.suffix == ".dol": path = path.parent.parent if path.suffix == ".dol": path = path.parent.parent
self.master.destination_game.set_path(path.parent) self.root.destination_game.set_path(path.parent)
def get_path(self) -> Path: def get_path(self) -> Path:
""" """
@ -295,6 +305,8 @@ class SourceGame(ttk.LabelFrame):
class DestinationGame(ttk.LabelFrame): class DestinationGame(ttk.LabelFrame):
def __init__(self, master: tkinter.Tk): def __init__(self, master: tkinter.Tk):
super().__init__(master, text="Game Directory Destination") super().__init__(master, text="Game Directory Destination")
self.root = master.root
self.columnconfigure(1, weight=1) self.columnconfigure(1, weight=1)
self.entry = ttk.Entry(self) self.entry = ttk.Entry(self)
@ -361,21 +373,22 @@ class DestinationGame(ttk.LabelFrame):
class ButtonInstall(ttk.Button): class ButtonInstall(ttk.Button):
def __init__(self, master: tkinter.Tk): def __init__(self, master: tkinter.Tk):
super().__init__(master, text="Install", command=self.install) super().__init__(master, text="Install", command=self.install)
self.root = master.root
@threaded @threaded
@better_gui_error @better_gui_error
def install(self): def install(self):
try: try:
self.master.set_state(InstallerState.INSTALLING) self.root.set_state(InstallerState.INSTALLING)
# check if the user entered a source path # check if the user entered a source path
source_path = self.master.get_source_path() source_path = self.root.get_source_path()
if str(source_path) == ".": if str(source_path) == ".":
messagebox.showerror(_("ERROR"), _("ERROR_INVALID_SOURCE_GAME")) messagebox.showerror(_("ERROR"), _("ERROR_INVALID_SOURCE_GAME"))
return return
# check if the user entered a destination path # check if the user entered a destination path
destination_path = self.master.get_destination_path() destination_path = self.root.get_destination_path()
if str(destination_path) == ".": if str(destination_path) == ".":
messagebox.showerror(_("ERROR"), _("ERROR_INVALID_DESTINATION_GAME")) messagebox.showerror(_("ERROR"), _("ERROR_INVALID_DESTINATION_GAME"))
return return
@ -391,20 +404,20 @@ class ButtonInstall(ttk.Button):
return return
game = Game(source_path) game = Game(source_path)
mod_config = self.master.get_mod_config() mod_config = self.root.get_mod_config()
output_type = self.master.get_output_type() output_type = self.root.get_output_type()
self.master.progress_function( self.root.progress_function(
game.install_mod( game.install_mod(
dest=destination_path, dest=destination_path,
mod_config=mod_config, mod_config=mod_config,
output_type=output_type, output_type=output_type,
options=self.master.options options=self.root.options
) )
) )
message_texts = mod_config.messages.get("installation_completed", {}).get("text", {}) message_texts = mod_config.messages.get("installation_completed", {}).get("text", {})
message = message_texts.get(self.master.options["language"]) message = message_texts.get(self.root.options["language"])
if message is None: message = message_texts.get("*") if message is None: message = message_texts.get("*")
if message is None: message = _('NO_MESSAGE_FROM_AUTHOR') if message is None: message = _('NO_MESSAGE_FROM_AUTHOR')
message = mod_config.safe_eval(message, multiple=True) message = mod_config.safe_eval(message, multiple=True)
@ -415,7 +428,7 @@ class ButtonInstall(ttk.Button):
) )
finally: finally:
self.master.set_state(InstallerState.IDLE) self.root.set_state(InstallerState.IDLE)
def set_state(self, state: InstallerState) -> None: def set_state(self, state: InstallerState) -> None:
""" """
@ -432,6 +445,7 @@ class ButtonInstall(ttk.Button):
class ProgressBar(ttk.LabelFrame): class ProgressBar(ttk.LabelFrame):
def __init__(self, master: tkinter.Tk): def __init__(self, master: tkinter.Tk):
super().__init__(master) super().__init__(master)
self.root = master.root
# make the element fill the whole frame # make the element fill the whole frame
self.columnconfigure(1, weight=1) self.columnconfigure(1, weight=1)
@ -503,6 +517,7 @@ class ProgressBar(ttk.LabelFrame):
class SelectPack(ttk.Frame): class SelectPack(ttk.Frame):
def __init__(self, master: tkinter.Tk): def __init__(self, master: tkinter.Tk):
super().__init__(master) super().__init__(master)
self.root = master.root
self.combobox = ttk.Combobox(self) self.combobox = ttk.Combobox(self)
self.combobox.grid(row=1, column=1, sticky="NEWS") self.combobox.grid(row=1, column=1, sticky="NEWS")

View file

@ -13,12 +13,14 @@ class Window(tkinter.Toplevel):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.root = self.master.root
self.title(_("CONFIGURE_MYSTUFF_PATCH")) self.title(_("CONFIGURE_MYSTUFF_PATCH"))
self.resizable(False, False) self.resizable(False, False)
self.grab_set() # the others window will be disabled, keeping only this one activated self.grab_set() # the others window will be disabled, keeping only this one activated
self.disabled_text: str = _("<", "DISABLED", ">") self.disabled_text: str = _("<", "DISABLED", ">")
self.master.options["mystuff_pack_selected"] = self.master.options["mystuff_pack_selected"] self.root.options["mystuff_pack_selected"] = self.root.options["mystuff_pack_selected"]
self.frame_profile = ttk.Frame(self) self.frame_profile = ttk.Frame(self)
self.frame_profile.grid(row=1, column=1, sticky="NEWS") self.frame_profile.grid(row=1, column=1, sticky="NEWS")
@ -79,11 +81,11 @@ class Window(tkinter.Toplevel):
Refresh all the profile Refresh all the profile
""" """
combobox_values = [self.disabled_text, *self.master.options["mystuff_packs"]] combobox_values = [self.disabled_text, *self.root.options["mystuff_packs"]]
self.combobox_profile.configure(values=combobox_values) self.combobox_profile.configure(values=combobox_values)
self.combobox_profile.current(combobox_values.index( self.combobox_profile.current(combobox_values.index(
self.master.options["mystuff_pack_selected"] self.root.options["mystuff_pack_selected"]
if self.master.options["mystuff_pack_selected"] in self.master.options["mystuff_packs"] else if self.root.options["mystuff_pack_selected"] in self.root.options["mystuff_packs"] else
self.disabled_text self.disabled_text
)) ))
@ -93,16 +95,18 @@ class Window(tkinter.Toplevel):
""" """
profile_name = self.combobox_profile.get() if profile_name is None else profile_name profile_name = self.combobox_profile.get() if profile_name is None else profile_name
if not profile_name in self.root.options["mystuff_packs"]: profile_name = self.disabled_text
self.combobox_profile.set(profile_name) self.combobox_profile.set(profile_name)
self.master.options["mystuff_pack_selected"] = profile_name self.root.options["mystuff_pack_selected"] = profile_name
self.listbox_mystuff_paths.delete(0, tkinter.END) self.listbox_mystuff_paths.delete(0, tkinter.END)
is_disabled = (profile_name == self.disabled_text) is_disabled: bool = (profile_name == self.disabled_text)
for children in self.frame_mystuff_paths_action.children.values(): for children in self.frame_mystuff_paths_action.children.values():
children.configure(state=tkinter.DISABLED if is_disabled else tkinter.NORMAL) children.configure(state=tkinter.DISABLED if is_disabled else tkinter.NORMAL)
if is_disabled: return if is_disabled: return
profile_data = self.master.options["mystuff_packs"][profile_name] profile_data = self.root.options["mystuff_packs"][profile_name]
for path in profile_data["paths"]: for path in profile_data["paths"]:
self.listbox_mystuff_paths.insert(tkinter.END, path) self.listbox_mystuff_paths.insert(tkinter.END, path)
@ -111,8 +115,9 @@ class Window(tkinter.Toplevel):
""" """
Save the new profile Save the new profile
""" """
profile_name: str = self.combobox_profile.get() profile_name: str = self.combobox_profile.get()
if profile_name in self.master.options["mystuff_packs"]: if profile_name in self.root.options["mystuff_packs"]:
messagebox.showerror(_("ERROR"), _("MYSTUFF_PROFILE_ALREADY_EXIST")) messagebox.showerror(_("ERROR"), _("MYSTUFF_PROFILE_ALREADY_EXIST"))
return return
@ -121,7 +126,7 @@ class Window(tkinter.Toplevel):
messagebox.showerror(_("ERROR"), _("MYSTUFF_PROFILE_FORBIDDEN_NAME")) messagebox.showerror(_("ERROR"), _("MYSTUFF_PROFILE_FORBIDDEN_NAME"))
return return
self.master.options["mystuff_packs"][profile_name] = {"paths": []} self.root.options["mystuff_packs"][profile_name] = {"paths": []}
self.refresh_profiles() self.refresh_profiles()
self.select_profile(profile_name=profile_name) self.select_profile(profile_name=profile_name)
@ -129,9 +134,9 @@ class Window(tkinter.Toplevel):
""" """
Delete the currently selected profile Delete the currently selected profile
""" """
profile_name: str = self.combobox_profile.get()
self.master.options["mystuff_packs"][self.master.options["mystuff_pack_selected"]].pop(profile_name)
self.root.options["mystuff_packs"].pop(self.root.options["mystuff_pack_selected"])
self.refresh_profiles()
self.select_profile() self.select_profile()
def add_mystuff_path(self) -> None: def add_mystuff_path(self) -> None:
@ -142,7 +147,7 @@ class Window(tkinter.Toplevel):
if (mystuff_path := filedialog.askdirectory(title=_("SELECT_MYSTUFF"), mustexist=True)) is None: return if (mystuff_path := filedialog.askdirectory(title=_("SELECT_MYSTUFF"), mustexist=True)) is None: return
mystuff_path = Path(mystuff_path) mystuff_path = Path(mystuff_path)
self.master.options["mystuff_packs"][self.master.options["mystuff_pack_selected"]]["paths"].append( self.root.options["mystuff_packs"][self.root.options["mystuff_pack_selected"]]["paths"].append(
str(mystuff_path.resolve()) str(mystuff_path.resolve())
) )
@ -151,12 +156,12 @@ class Window(tkinter.Toplevel):
def remove_mystuff_path(self) -> None: def remove_mystuff_path(self) -> None:
""" """
Remove the selected MyStuff path from the profile Remove the selected MyStuff path from the profile
:return:
""" """
selections = self.listbox_mystuff_paths.curselection() selections = self.listbox_mystuff_paths.curselection()
if not selections: return if not selections: return
for selection in selections: for selection in selections:
self.master.options["mystuff_packs"][self.master.options["mystuff_pack_selected"]]["paths"].pop(selection) self.root.options["mystuff_packs"][self.root.options["mystuff_pack_selected"]]["paths"].pop(selection)
self.select_profile() self.select_profile()