fluent_app.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import sys
  5. import os
  6. from PySide6.QtGui import QGuiApplication, QIcon
  7. from PySide6.QtQml import QQmlApplicationEngine
  8. from PySide6.QtCore import Qt, QObject, QTranslator, QLocale, Slot
  9. from pathlib import Path
  10. from module.gui.utils import get_work_path
  11. from module.gui.Bridge import bridge
  12. from module.logger import logger
  13. # import module.gui.qml_rcc
  14. import module.gui.res_rcc
  15. class FluentApp():
  16. app = None
  17. engine = None
  18. translator = None
  19. def __init__(self):
  20. super().__init__()
  21. # 适配高分辨率、声明、设置Logo、设置软件名字
  22. # QGuiApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.Round)
  23. QGuiApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
  24. QGuiApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
  25. QGuiApplication.setAttribute(Qt.AA_ShareOpenGLContexts)
  26. os.putenv("QT_QUICK_CONTROLS_STYLE", "Basic")
  27. FluentApp.app = QGuiApplication(sys.argv)
  28. QGuiApplication.setWindowIcon(QIcon(os.fspath(Path(__file__).resolve().parent / "res/icon.ico")))
  29. QGuiApplication.setApplicationName("oas")
  30. QGuiApplication.setOrganizationName("oas")
  31. FluentApp.engine = QQmlApplicationEngine()
  32. FluentApp.engine.addImportPath(os.fspath(Path(__file__).resolve().parent))
  33. FluentApp.translator = Translator(engine=FluentApp.engine, app=FluentApp.app)
  34. self.set_context_property(context=FluentApp.translator, name='translator')
  35. @classmethod
  36. def run(cls):
  37. FluentApp.engine.load(os.fspath(Path(get_work_path() / 'module' / 'gui' / 'qml' / 'app.qml')))
  38. if not FluentApp.engine.rootObjects():
  39. sys.exit(-1)
  40. sys.exit(FluentApp.app.exec())
  41. @classmethod
  42. def set_context_property(cls, context, name: str) -> None:
  43. """
  44. 设置上下文
  45. :param name:
  46. :param context:
  47. :return:
  48. """
  49. FluentApp.engine.rootContext().setContextProperty(name, context)
  50. class Translator(QObject):
  51. def __init__(self, engine, app) -> None:
  52. super(Translator, self).__init__()
  53. self._engine = engine
  54. self._app = app
  55. self.path_en_US = str((Path.cwd() / "module" / "config" / "i18n" / "en_US.qm").resolve())
  56. self.path_zh_CN = str((Path.cwd() / "module" / "config" / "i18n" / "zh_CN.qm").resolve())
  57. self.translator = QTranslator()
  58. @Slot(str)
  59. def set_language(self, language: str) -> None:
  60. """
  61. 设置语言
  62. :param language:
  63. :return:
  64. """
  65. if language == "简体中文":
  66. if not self.translator.load(self.path_zh_CN):
  67. logger.error("load language 简体中文 failed!")
  68. QGuiApplication.installTranslator(self.translator)
  69. self._engine.retranslate()
  70. return
  71. if language == "English":
  72. if not self.translator.load(self.path_en_US):
  73. logger.error("load language English failed!")
  74. QGuiApplication.installTranslator(self.translator)
  75. self._engine.retranslate()
  76. return