From abab9f97bcb230e1c1927dae21645aa1ed1449df Mon Sep 17 00:00:00 2001 From: Alarcon Ace Belen Date: Fri, 15 Apr 2022 22:21:25 +0800 Subject: [PATCH] Use TOML format for launch config --- code/MainWindow.py | 26 ++++---- code/utils/config.py | 146 +---------------------------------------- code/utils/config.toml | 119 +++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 159 deletions(-) create mode 100644 code/utils/config.toml diff --git a/code/MainWindow.py b/code/MainWindow.py index a876713..62ad386 100644 --- a/code/MainWindow.py +++ b/code/MainWindow.py @@ -190,29 +190,25 @@ def toggle_mouse_mode(self): self.canvas.toggleZoomPanMode() def toggle_stylesheet(self): - config = "./utils/config.py" + config = "./utils/config.toml" light_mode = "./assets/styles.qss" dark_mode = "./assets/styles-dark.qss" - light_text = f"stylesheet_path = '{light_mode}'\n" - dark_text = f"stylesheet_path = '{dark_mode}'\n" - with open(config, 'r') as fh: - lines = fh.readlines() - mode_text = "" - if lines[18] == light_text: - mode_text = dark_text - mode_ = dark_mode - elif lines[18] == dark_text: - mode_text = light_text - mode_ = light_mode - lines[18] = mode_text + + import toml + data = toml.load(config) + if data["STYLES_DEFAULT"] == light_mode: + data["STYLES_DEFAULT"] = dark_mode + elif data["STYLES_DEFAULT"] == dark_mode: + data["STYLES_DEFAULT"] = light_mode with open(config, 'w') as fh: - fh.writelines(lines) + toml.dump(data, fh) app = QApplication.instance() if app is None: raise RuntimeError("No Qt Application found.") - styles = mode_ + styles = data["STYLES_DEFAULT"] + cfg["STYLES_DEFAULT"] = data["STYLES_DEFAULT"] with open(styles, 'r') as fh: app.setStyleSheet(fh.read()) diff --git a/code/utils/config.py b/code/utils/config.py index 25dd2d5..283e73a 100644 --- a/code/utils/config.py +++ b/code/utils/config.py @@ -16,147 +16,5 @@ along with this program. If not, see . """ -stylesheet_path = './assets/styles.qss' -cfg = { - "IMAGE_EXTENSIONS": ["*.bmp", "*.gif", "*.jpg", "*.jpeg", "*.png", - "*.pbm", "*.pgm", "*.ppm", "*.webp", "*.xbm", "*.xpm"], - - "LANGUAGE": [" Japanese", " Korean", " Chinese SIM", " Chinese TRA ", " English"], - "ORIENTATION": [" Vertical", " Horizontal"], - "LANG_PATH": "./assets/languages/", - - "STYLES_PATH": "./assets/", - "STYLES_DEFAULT": stylesheet_path, - - "NAV_VIEW_RATIO": [3,11], - "NAV_ROOT": "./assets/images/", - - "NAV_FUNCS": { - "path_changed": "view_image_from_fdialog", - "nav_clicked": "view_image_from_explorer" - }, - - "LOGO": "./assets/images/icons/logo.ico", - "HOME_IMAGE": "./assets/images/home.png", - - "RBN_HEIGHT": 2.4, - - "TBAR_ISIZE_REL": 0.1, - "TBAR_ISIZE_MARGIN": 1.3, - - "TBAR_ICONS": "./assets/images/icons/", - "TBAR_ICONS_LIGHT": "./assets/images/icons/", - "TBAR_ICON_DEFAULT": "./assets/images/icons/default_icon.png", - - "TBAR_FUNCS": { - "FILE": { - "open_dir": { - "help_title": "Open manga directory", - "help_msg": "Open a directory containing images.", - "path": "open_dir.png", - "toggle": False, - "align": "AlignLeft", - "icon_h": 1.0, - "icon_w": 1.0 - }, - "open_manga": { - "help_title": "Open manga file", - "help_msg": "Supports the following formats: cbr, cbz, pdf.", - "path": "open_manga.png", - "toggle": False, - "align": "AlignLeft", - "icon_h": 1.0, - "icon_w": 1.0 - } - }, - "SETTINGS": { - "load_model": { - "help_title": "Switch detection model", - "help_msg": "Switch between MangaOCR and Tesseract models.", - "path": "load_model.png", - "toggle": True, - "align": "AlignLeft", - "icon_h": 1.0, - "icon_w": 1.0 - }, - "toggle_logging": { - "help_title": "Enable text logging", - "help_msg": "Save detected text to a text file located in the \ - current project directory.", - "path": "toggle_logging.png", - "toggle": True, - "align": "AlignLeft", - "icon_h": 1.0, - "icon_w": 1.0 - }, - "toggle_mouse_mode": { - "help_title": "Change mouse behavior", - "help_msg": "This will disable text detection. Turn this on \ - only if do not want to hold CTRL key to zoom and pan \ - on an image.", - "path": "toggle_mouse_mode.png", - "toggle": True, - "align": "AlignLeft", - "icon_h": 1.0, - "icon_w": 1.0 - }, - "toggle_stylesheet": { - "help_title": "Change theme", - "help_msg": "Switch between light and dark mode.", - "path": "toggle_stylesheet.png", - "toggle": False, - "align": "AlignLeft", - "icon_h": 1.0, - "icon_w": 1.0 - } - } - }, - - "MODE_FUNCS": { - "zoom_in": { - "help_title": "Zoom in", - "help_msg": "Hint: Double click the image to reset zoom.", - "path": "zoom_in.png", - "toggle": False, - "align": "AlignRight", - "icon_h": 0.45, - "icon_w": 0.45 - }, - "zoom_out": { - "help_title": "Zoom out", - "help_msg": "Hint: Double click the image to reset zoom.", - "path": "zoom_out.png", - "toggle": False, - "align": "AlignRight", - "icon_h": 0.45, - "icon_w": 0.45 - }, - "load_image_at_idx": { - "help_title": "", - "help_msg": "Jump to page", - "path": "load_image_at_idx.png", - "toggle": False, - "align": "AlignRight", - "icon_h": 0.45, - "icon_w": 1.3 - }, - "load_prev_image": { - "help_title": "", - "help_msg": "Show previous image", - "path": "load_prev_image.png", - "toggle": False, - "align": "AlignRight", - "icon_h": 0.45, - "icon_w": 0.6 - }, - "load_next_image": { - "help_title": "", - "help_msg": "Show next image", - "path": "load_next_image.png", - "toggle": False, - "align": "AlignRight", - "icon_h": 0.45, - "icon_w": 0.6 - } - } -} \ No newline at end of file +import toml +cfg = toml.load("./utils/config.toml") \ No newline at end of file diff --git a/code/utils/config.toml b/code/utils/config.toml new file mode 100644 index 0000000..d212191 --- /dev/null +++ b/code/utils/config.toml @@ -0,0 +1,119 @@ +IMAGE_EXTENSIONS = [ "*.bmp", "*.gif", "*.jpg", "*.jpeg", "*.png", "*.pbm", "*.pgm", "*.ppm", "*.webp", "*.xbm", "*.xpm",] +LANGUAGE = [ " Japanese", " Korean", " Chinese SIM", " Chinese TRA ", " English",] +ORIENTATION = [ " Vertical", " Horizontal",] +LANG_PATH = "./assets/languages/" +STYLES_PATH = "./assets/" +STYLES_DEFAULT = "./assets/styles.qss" +NAV_VIEW_RATIO = [ 3, 11,] +NAV_ROOT = "./assets/images/" +LOGO = "./assets/images/icons/logo.ico" +HOME_IMAGE = "./assets/images/home.png" +RBN_HEIGHT = 2.4 +TBAR_ISIZE_REL = 0.1 +TBAR_ISIZE_MARGIN = 1.3 +TBAR_ICONS = "./assets/images/icons/" +TBAR_ICONS_LIGHT = "./assets/images/icons/" +TBAR_ICON_DEFAULT = "./assets/images/icons/default_icon.png" + +[NAV_FUNCS] +path_changed = "view_image_from_fdialog" +nav_clicked = "view_image_from_explorer" + +[MODE_FUNCS.zoom_in] +help_title = "Zoom in" +help_msg = "Hint: Double click the image to reset zoom." +path = "zoom_in.png" +toggle = false +align = "AlignRight" +icon_h = 0.45 +icon_w = 0.45 + +[MODE_FUNCS.zoom_out] +help_title = "Zoom out" +help_msg = "Hint: Double click the image to reset zoom." +path = "zoom_out.png" +toggle = false +align = "AlignRight" +icon_h = 0.45 +icon_w = 0.45 + +[MODE_FUNCS.load_image_at_idx] +help_title = "" +help_msg = "Jump to page" +path = "load_image_at_idx.png" +toggle = false +align = "AlignRight" +icon_h = 0.45 +icon_w = 1.3 + +[MODE_FUNCS.load_prev_image] +help_title = "" +help_msg = "Show previous image" +path = "load_prev_image.png" +toggle = false +align = "AlignRight" +icon_h = 0.45 +icon_w = 0.6 + +[MODE_FUNCS.load_next_image] +help_title = "" +help_msg = "Show next image" +path = "load_next_image.png" +toggle = false +align = "AlignRight" +icon_h = 0.45 +icon_w = 0.6 + +[TBAR_FUNCS.FILE.open_dir] +help_title = "Open manga directory" +help_msg = "Open a directory containing images." +path = "open_dir.png" +toggle = false +align = "AlignLeft" +icon_h = 1.0 +icon_w = 1.0 + +[TBAR_FUNCS.FILE.open_manga] +help_title = "Open manga file" +help_msg = "Supports the following formats: cbr, cbz, pdf." +path = "open_manga.png" +toggle = false +align = "AlignLeft" +icon_h = 1.0 +icon_w = 1.0 + +[TBAR_FUNCS.SETTINGS.load_model] +help_title = "Switch detection model" +help_msg = "Switch between MangaOCR and Tesseract models." +path = "load_model.png" +toggle = true +align = "AlignLeft" +icon_h = 1.0 +icon_w = 1.0 + +[TBAR_FUNCS.SETTINGS.toggle_logging] +help_title = "Enable text logging" +help_msg = "Save detected text to a text file located in the current project directory." +path = "toggle_logging.png" +toggle = true +align = "AlignLeft" +icon_h = 1.0 +icon_w = 1.0 + +[TBAR_FUNCS.SETTINGS.toggle_mouse_mode] +help_title = "Change mouse behavior" +help_msg = "This will disable text detection. Turn this on only if do not want to hold CTRL key to zoom and pan on an image." +path = "toggle_mouse_mode.png" +toggle = true +align = "AlignLeft" +icon_h = 1.0 +icon_w = 1.0 + +[TBAR_FUNCS.SETTINGS.toggle_stylesheet] +help_title = "Change theme" +help_msg = "Switch between light and dark mode." +path = "toggle_stylesheet.png" +toggle = false +align = "AlignLeft" +icon_h = 1.0 +icon_w = 1.0