Skip to content

Commit

Permalink
v0.13.0 - config feature
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomiejduda committed Feb 20, 2025
1 parent abb6a7b commit a24c4e9
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 9 deletions.
99 changes: 91 additions & 8 deletions src/GUI/gui_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sys
import time
import tkinter as tk
from configparser import ConfigParser
from idlelib.tooltip import Hovertip
from tkinter import filedialog, messagebox, ttk
from typing import List, Optional
Expand Down Expand Up @@ -51,6 +52,7 @@
TRANSLATION_MEMORY,
ZOOM_RESAMPLING_TYPES_NAMES,
ZOOM_TYPES_NAMES,
ConfigKeys,
TranslationEntry,
TranslationKeys,
get_compression_id,
Expand Down Expand Up @@ -92,14 +94,13 @@ def __init__(self, master: tk.Tk, in_version_num: str, in_main_directory: str):
self.ph_img = None
self.preview_final_pil_image = None
self.validate_spinbox_command = (master.register(self.validate_spinbox), '%P')
self.current_program_language = tk.StringVar(value="EN")
self.current_background_color = tk.StringVar(value="#595959")
self.pixel_x: int = 1
self.pixel_y: int = 1
self.pixel_offset: int = 0
self.pixel_value_str: str = ""
self.pixel_value_rgba: bytearray = bytearray(10)

# icon logic
try:
if platform.uname().system == "Linux":
self.master.iconphoto(False, tk.PhotoImage(file=self.icon_path))
Expand All @@ -108,6 +109,37 @@ def __init__(self, master: tk.Tk, in_version_num: str, in_main_directory: str):
except tk.TclError:
logger.info("Can't load the icon file from %s", self.icon_path)

# user config logic
self.user_config = ConfigParser()
self.user_config_file_name: str = "config.ini"
self.user_config.add_section("config")
self.user_config.set("config", ConfigKeys.SAVE_AS_DIRECTORY_PATH, "")
self.user_config.set("config", ConfigKeys.SAVE_RAW_DATA_DIRECTORY_PATH, "")
self.user_config.set("config", ConfigKeys.OPEN_FILE_DIRECTORY_PATH, "")
self.user_config.set("config", ConfigKeys.OPEN_PALETTE_DIRECTORY_PATH, "")
self.user_config.set("config", ConfigKeys.CURRENT_PROGRAM_LANGUAGE, "EN")
self.user_config.set("config", ConfigKeys.CURRENT_CANVAS_COLOR, "#595959")
if not os.path.exists(self.user_config_file_name):
with open(self.user_config_file_name, "w") as configfile:
self.user_config.write(configfile)

self.user_config.read(self.user_config_file_name)
try:
self.current_save_as_directory_path = self.user_config.get("config", ConfigKeys.SAVE_AS_DIRECTORY_PATH)
self.current_save_raw_data_directory_path = self.user_config.get("config", ConfigKeys.SAVE_RAW_DATA_DIRECTORY_PATH)
self.current_open_file_directory_path = self.user_config.get("config", ConfigKeys.OPEN_FILE_DIRECTORY_PATH)
self.current_open_palette_directory_path = self.user_config.get("config", ConfigKeys.OPEN_PALETTE_DIRECTORY_PATH)
self.current_program_language = tk.StringVar(value=self.user_config.get("config", ConfigKeys.CURRENT_PROGRAM_LANGUAGE))
self.current_background_color = tk.StringVar(value=self.user_config.get("config", ConfigKeys.CURRENT_CANVAS_COLOR))
except Exception as error:
logger.error(f"Error while loading user config: {error}")
self.current_save_as_directory_path = ""
self.current_save_raw_data_directory_path = ""
self.current_open_file_directory_path = ""
self.current_open_palette_directory_path = ""
self.current_program_language = tk.StringVar(value="EN")
self.current_background_color = tk.StringVar(value="#595959")

########################
# MAIN FRAME #
########################
Expand Down Expand Up @@ -743,6 +775,13 @@ def _zoom_by_shortcut(event):

master.config(menu=self.menubar)

######################################################################################################
# STARTUP LOGIC (options menu logic) #
######################################################################################################

# set language on startup logic
self.set_program_language()

######################################################################################################
# methods #
######################################################################################################
Expand Down Expand Up @@ -837,11 +876,21 @@ def set_program_language(self) -> None:
self.helpmenu.entryconfigure(0, label=self.get_translation_text(TranslationKeys.TRANSLATION_TEXT_HELPMENU_ABOUT))
self.menubar.entryconfigure(3, label=self.get_translation_text(TranslationKeys.TRANSLATION_TEXT_HELPMENU_HELP))

# save current language to config file
self.user_config.set("config", ConfigKeys.CURRENT_PROGRAM_LANGUAGE, self.current_program_language.get())
with open(self.user_config_file_name, "w") as configfile:
self.user_config.write(configfile)

def reload_image_callback(self, event):
self.gui_reload_image_on_gui_element_change()
self.parameters_box_disable_enable_logic()
self.master.focus()

# save current canvas color to config file
self.user_config.set("config", ConfigKeys.CURRENT_CANVAS_COLOR, self.current_background_color.get())
with open(self.user_config_file_name, "w") as configfile:
self.user_config.write(configfile)

def check_if_paletted_format_chosen(self, pixel_format: str) -> bool:
for format_name in PALETTE_FORMATS_NAMES:
if format_name in pixel_format:
Expand Down Expand Up @@ -1019,13 +1068,20 @@ def gui_reload_image_on_gui_element_change(self) -> bool:
logger.info("Image is not opened yet...")
return True

# File > Open
def open_image_file(self) -> bool:
try:
in_file = filedialog.askopenfile(
mode="rb"
)
in_file = filedialog.askopenfile(mode="rb", initialdir=self.current_open_file_directory_path)
if not in_file:
return False
try:
selected_directory = os.path.dirname(in_file.name)
self.current_open_file_directory_path = selected_directory # set directory path from history
self.user_config.set("config", ConfigKeys.OPEN_FILE_DIRECTORY_PATH, selected_directory) # save directory path to config file
with open(self.user_config_file_name, "w") as configfile:
self.user_config.write(configfile)
except Exception:
pass
in_file_path = in_file.name
in_file_name = in_file_path.split("/")[-1]
except Exception as error:
Expand Down Expand Up @@ -1056,11 +1112,17 @@ def open_image_file(self) -> bool:

def open_palette_file(self) -> bool:
try:
in_file = filedialog.askopenfile(
mode="rb"
)
in_file = filedialog.askopenfile(mode="rb", initialdir=self.current_open_palette_directory_path)
if not in_file:
return False
try:
selected_directory = os.path.dirname(in_file.name)
self.current_open_palette_directory_path = selected_directory # set directory path from history
self.user_config.set("config", ConfigKeys.OPEN_PALETTE_DIRECTORY_PATH, selected_directory) # save directory path to config file
with open(self.user_config_file_name, "w") as configfile:
self.user_config.write(configfile)
except Exception:
pass
in_file_path = in_file.name
except Exception as error:
logger.error("Failed to open file! Error: %s", error)
Expand All @@ -1076,6 +1138,7 @@ def open_palette_file(self) -> bool:
self.gui_reload_image_on_gui_element_change()
return True

# File > Save As
def export_image_file(self) -> bool:
if self.opened_image:
out_file = None
Expand All @@ -1084,10 +1147,20 @@ def export_image_file(self) -> bool:
mode="wb",
defaultextension="" if platform.uname().system == "Linux" else ".dds",
initialfile="exported_image",
initialdir=self.current_save_as_directory_path,
filetypes=((self.get_translation_text(TranslationKeys.TRANSLATION_TEXT_EXPORT_FILETYPES_DDS), "*.dds"),
(self.get_translation_text(TranslationKeys.TRANSLATION_TEXT_EXPORT_FILETYPES_PNG), "*.png"),
(self.get_translation_text(TranslationKeys.TRANSLATION_TEXT_EXPORT_FILETYPES_BMP), "*.bmp")),
)
try:
selected_directory = os.path.dirname(out_file.name)
self.current_save_as_directory_path = selected_directory # set directory path from history
self.user_config.set("config", ConfigKeys.SAVE_AS_DIRECTORY_PATH, selected_directory) # save directory path to config file
with open(self.user_config_file_name, "w") as configfile:
self.user_config.write(configfile)
except Exception:
pass

except Exception as error:
logger.error(f"Error: {error}")
messagebox.showwarning("Warning", self.get_translation_text(TranslationKeys.TRANSLATION_TEXT_POPUPS_FAILED_TO_SAVE_FILE))
Expand All @@ -1114,6 +1187,7 @@ def export_image_file(self) -> bool:

return True

# File > Save Raw Data
def export_raw_file(self) -> bool:
if self.opened_image:
out_file = None
Expand All @@ -1122,8 +1196,17 @@ def export_raw_file(self) -> bool:
mode="wb",
defaultextension=".bin",
initialfile="exported_raw_data",
initialdir=self.current_save_raw_data_directory_path,
filetypes=((self.get_translation_text(TranslationKeys.TRANSLATION_TEXT_EXPORT_FILETYPES_BINARY), "*.bin"), ),
)
try:
selected_directory = os.path.dirname(out_file.name)
self.current_save_raw_data_directory_path = selected_directory # set directory path from history
self.user_config.set("config", ConfigKeys.SAVE_RAW_DATA_DIRECTORY_PATH, selected_directory) # save directory path to config file
with open(self.user_config_file_name, "w") as configfile:
self.user_config.write(configfile)
except Exception:
pass
except Exception as error:
logger.error(f"Error: {error}")
messagebox.showwarning("Warning", self.get_translation_text(TranslationKeys.TRANSLATION_TEXT_POPUPS_FAILED_TO_SAVE_FILE))
Expand Down
9 changes: 9 additions & 0 deletions src/Image/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ def get_rotate_id(rotate_name: str) -> str:
DEFAULT_ROTATE_NAME: str = ROTATE_TYPES_NAMES[0]


class ConfigKeys(str, Enum):
SAVE_AS_DIRECTORY_PATH = "save_as_directory_path"
SAVE_RAW_DATA_DIRECTORY_PATH = "save_raw_data_directory_path"
OPEN_FILE_DIRECTORY_PATH = "open_file_directory_path"
OPEN_PALETTE_DIRECTORY_PATH = "open_palette_directory_path"
CURRENT_PROGRAM_LANGUAGE = "current_program_language"
CURRENT_CANVAS_COLOR = "current_canvas_color"


class TranslationKeys(str, Enum):
TRANSLATION_TEXT_IMAGE_PARAMETERS = "TRANSLATION_TEXT_IMAGE_PARAMETERS"
TRANSLATION_TEXT_IMAGE_WIDTH = "TRANSLATION_TEXT_IMAGE_WIDTH"
Expand Down
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from src.GUI.gui_main import ImageHeatGUI

VERSION_NUM: Final[str] = "v0.12.0"
VERSION_NUM: Final[str] = "v0.13.0"

logger = get_logger("main")

Expand Down

0 comments on commit a24c4e9

Please sign in to comment.