Refactor resources

This commit is contained in:
CPD 2025-03-05 15:11:01 +01:00
parent 8d8a829832
commit 83fa6fabf5
6 changed files with 25 additions and 11 deletions

View File

@ -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()

View File

@ -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)

View File

Before

Width:  |  Height:  |  Size: 9.8 KiB

After

Width:  |  Height:  |  Size: 9.8 KiB

View File

Before

Width:  |  Height:  |  Size: 3.9 KiB

After

Width:  |  Height:  |  Size: 3.9 KiB

View File

@ -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()

View File

@ -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)