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

1# /////////////////////////////////////////////////////////////// 

2# SERVICES.APPLICATION.RESOURCE_SERVICE - Qt resource loader 

3# Project: ezqt_app 

4# /////////////////////////////////////////////////////////////// 

5 

6"""Qt resource loading service — fonts via QFontDatabase.""" 

7 

8from __future__ import annotations 

9 

10# /////////////////////////////////////////////////////////////// 

11# IMPORTS 

12# /////////////////////////////////////////////////////////////// 

13# Third-party imports 

14from PySide6.QtGui import QFontDatabase 

15 

16from ...utils.printer import get_printer 

17from ...utils.runtime_paths import get_bin_path 

18 

19# Local imports 

20from ..config.config_service import get_package_resource 

21 

22 

23# /////////////////////////////////////////////////////////////// 

24# CLASSES 

25# /////////////////////////////////////////////////////////////// 

26class ResourceService: 

27 """Loads Qt resources (fonts) from the package and the application bin.""" 

28 

29 @staticmethod 

30 def load_fonts_resources(app: bool = False) -> None: 

31 """Load ``.ttf`` font files into Qt's font database. 

32 

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" 

46 

47 if not fonts.exists(): 

48 return 

49 

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}.") 

60 

61 # Recurse to also load application-level fonts 

62 if not app: 

63 ResourceService.load_fonts_resources(app=True)