Coverage for src / ezqt_app / services / application / resource_service.py: 22.22%
24 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-26 07:07 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-26 07:07 +0000
1# ///////////////////////////////////////////////////////////////
2# SERVICES.APPLICATION.RESOURCE_SERVICE - Qt resource loader
3# Project: ezqt_app
4# ///////////////////////////////////////////////////////////////
6"""Qt resource loading service — fonts via QFontDatabase."""
8from __future__ import annotations
10# ///////////////////////////////////////////////////////////////
11# IMPORTS
12# ///////////////////////////////////////////////////////////////
13# Third-party imports
14from PySide6.QtGui import QFontDatabase
16from ...utils.printer import get_printer
17from ...utils.runtime_paths import get_bin_path
19# Local imports
20from ..config.config_service import get_package_resource
23# ///////////////////////////////////////////////////////////////
24# CLASSES
25# ///////////////////////////////////////////////////////////////
26class ResourceService:
27 """Loads Qt resources (fonts) from the package and the application bin."""
29 @staticmethod
30 def load_fonts_resources(app: bool = False) -> None:
31 """Load ``.ttf`` font files into Qt's font database.
33 Parameters
34 ----------
35 app:
36 When ``False`` loads from the installed package resources,
37 then recurses with ``app=True`` to also load from ``bin/fonts/``.
38 When ``True`` loads from the project ``bin/fonts/`` directory.
39 """
40 if not app:
41 fonts = get_package_resource("resources/fonts")
42 source = "Package"
43 else:
44 fonts = get_bin_path() / "fonts"
45 source = "Application"
47 if not fonts.exists():
48 return
50 printer = get_printer()
51 for font in fonts.iterdir():
52 if font.suffix == ".ttf":
53 font_id = QFontDatabase.addApplicationFont(str(font))
54 if font_id == -1:
55 printer.error(
56 f"[System] Failed to load font from {source}: {font.stem}."
57 )
58 else:
59 printer.action(f"[System] Font loaded from {source}: {font.stem}.")
61 # Recurse to also load application-level fonts
62 if not app:
63 ResourceService.load_fonts_resources(app=True)