diff --git a/cpdctrl_gui/init.py b/cpdctrl_gui/init.py index f993d66..428e639 100644 --- a/cpdctrl_gui/init.py +++ b/cpdctrl_gui/init.py @@ -17,7 +17,7 @@ def run() -> int: int: The exit status code. """ app: QApplication = QApplication(sys.argv) - icon_path = resources.get_resource_path("cpdctrl_gui.resources", "icon.png") + icon_path = resources.get_resource_path("icons/icon.png") app.setWindowIcon(QIcon(icon_path)) AppConfig.initialize() diff --git a/cpdctrl_gui/resources/__init__.py b/cpdctrl_gui/resources/__init__.py index ee5d36a..202d550 100644 --- a/cpdctrl_gui/resources/__init__.py +++ b/cpdctrl_gui/resources/__init__.py @@ -1,8 +1,22 @@ from importlib.resources import files, as_file from os import path -def get_resource_path(module: str, name: str) -> str: +def get_resource_path_from(module: str, rel_path: str) -> str: with as_file(files(module)) as file: - p = path.join(file, name) + p = path.join(file, rel_path) return p -# print(get_resource_path("resources", "about.md")) + +def get_resource_path(rel_path: str) -> str: + """ + Convenience function to get the path to a resource file from this packages resource directory + Parameters + ---------- + rel_path + Path of the file relative to cpdctrl_gui/resources + + Returns + ------- + str + Absolute path to the file + """ + return get_resource_path_from("cpdctrl_gui.resources", rel_path) diff --git a/cpdctrl_gui/resources/logo.png b/cpdctrl_gui/resources/icons/logo.png similarity index 100% rename from cpdctrl_gui/resources/logo.png rename to cpdctrl_gui/resources/icons/logo.png diff --git a/cpdctrl_gui/resources/logo.svg b/cpdctrl_gui/resources/icons/logo.svg similarity index 100% rename from cpdctrl_gui/resources/logo.svg rename to cpdctrl_gui/resources/icons/logo.svg diff --git a/cpdctrl_gui/ui/main_window.py b/cpdctrl_gui/ui/main_window.py index 13b443a..81e97f4 100644 --- a/cpdctrl_gui/ui/main_window.py +++ b/cpdctrl_gui/ui/main_window.py @@ -6,7 +6,7 @@ from PyQt6.QtWidgets import QToolBox from PyQt6.QtGui import QIcon, QPixmap from PyQt6.QtWidgets import QDialog, QDialogButtonBox -from .. import resources +from ..resources import get_resource_path from .widgets.menubar import MenuBar from .widgets.toolbar import ToolBar from .widgets.metadata_input import MetadataInput @@ -102,12 +102,12 @@ class MainWindow(QMainWindow): style=Qt.ToolButtonStyle.ToolButtonTextUnderIcon, icon_size=(24, 24)) # Top Toolbar Buttons - self.topbar.add_button("connect_vmdev", "CPD Devices", QIcon.fromTheme(QIcon.ThemeIcon.Printer), self.vmdev_connect_from_dialog) - self.topbar.add_button("connect_leddev", "LED Devices", QIcon.fromTheme(QIcon.ThemeIcon.Scanner), self.leddev_connect_from_dialog) + self.topbar.add_button("connect_vmdev", "CPD Devices", get_resource_path("icons/volt_device.svg"), self.vmdev_connect_from_dialog) + self.topbar.add_button("connect_leddev", "LED Devices", get_resource_path("icons/led_device.svg"), self.leddev_connect_from_dialog) self.topbar.add_button("meas_start", "Start", QIcon.fromTheme(QIcon.ThemeIcon.MediaPlaybackStart), self.measure_start) self.topbar.add_button("meas_stop", "Stop", QIcon.fromTheme(QIcon.ThemeIcon.MediaPlaybackStop), self.measure_stop) self.topbar.add_button("meas_save", "Save", QIcon.fromTheme(QIcon.ThemeIcon.DocumentSaveAs), self.measurement_save) - self.topbar.add_button("app_about", "About", ":/icons/cpdctrl-gui-logo", self.app_open_about) + self.topbar.add_button("app_about", "About", get_resource_path("icons/icon.svg"), self.app_open_about) self.topbar.add_separator() self.topbar.add_button("app_exit", "Exit", QIcon.fromTheme(QIcon.ThemeIcon.ApplicationExit), self.app_exit) self.addToolBar(Qt.ToolBarArea.TopToolBarArea, self.topbar) @@ -310,7 +310,7 @@ class MainWindow(QMainWindow): buttons.accepted.connect(dialog.accept) dialog.setLayout(QVBoxLayout()) # show the logo via a pixmap in a label - img_path = resources.get_resource_path("cpdctrl_gui.resources", "logo.svg") + img_path = get_resource_path("icons/logo.svg") pixmap = QPixmap(img_path) pixmap = pixmap.scaled(128, 128, Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation) label = QLabel() diff --git a/cpdctrl_gui/ui/widgets/about.py b/cpdctrl_gui/ui/widgets/about.py index 981dafb..c1b3926 100644 --- a/cpdctrl_gui/ui/widgets/about.py +++ b/cpdctrl_gui/ui/widgets/about.py @@ -1,13 +1,13 @@ from PyQt6.QtWidgets import QTextBrowser -from cpdctrl_gui import resources +from ...resources import get_resource_path class MarkdownView(QTextBrowser): def __init__(self, path): super().__init__() self.setReadOnly(True) - self.filepath = resources.get_resource_path("cpdctrl_gui.resources", path) + self.filepath = get_resource_path(path) with open(self.filepath, "r") as file: content = file.read() self.setMarkdown(content)