3
0

utils.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. # -------------------------------------------------------------------------
  9. #
  10. from __future__ import unicode_literals
  11. # from builtins import str
  12. # built in's
  13. import os
  14. import site
  15. import uuid
  16. from pathlib import Path
  17. import logging as _logging
  18. import xml.etree.ElementTree as xml # Qt .ui files are xml
  19. from io import StringIO # for handling unicode strings
  20. # -------------------------------------------------------------------------
  21. # global scope
  22. _MODULENAME = 'azpy.shared.ui.utils'
  23. _LOGGER = _logging.getLogger(_MODULENAME)
  24. _LOGGER.debug('Initializing: {0}.'.format({_MODULENAME}))
  25. _MODULE_PATH = Path(__file__)
  26. _LOGGER.debug(f'_MODULE_PATH: {_MODULE_PATH.as_posix()}')
  27. _UI_FILE = Path(_MODULE_PATH.parent, 'resources', 'example.ui')
  28. # -------------------------------------------------------------------------
  29. # -------------------------------------------------------------------------
  30. # O3DE imports
  31. # propogates settings from __init__ so they aren't initialized over and over
  32. from DccScriptingInterface.azpy.shared.ui import settings_core
  33. # now we can import lumberyards PySide2
  34. import PySide2
  35. from PySide2 import QtGui, QtWidgets, QtGui, QtUiTools
  36. from PySide2.QtWidgets import QApplication, QSizePolicy
  37. from PySide2.QtUiTools import QUiLoader
  38. # azpy
  39. #import azpy.shared.ui.settings as qt_settings
  40. #import azpy.shared.ui.help_menu as help_menu
  41. # -------------------------------------------------------------------------
  42. class UiLoader(QUiLoader):
  43. def __init__(self, base_instance):
  44. super(UiLoader, self).__init__(base_instance)
  45. self._base_instance = base_instance
  46. def createWidget(self, classname, parent=None, name=""):
  47. widget = super(UiLoader, self).createWidget(
  48. classname, parent, name)
  49. if parent is None:
  50. return self._base_instance
  51. else:
  52. setattr(self._base_instance, name, widget)
  53. return widget
  54. class UiWidget(QtWidgets.QWidget):
  55. def __init__(self, ui_file=_UI_FILE, parent=None):
  56. super().__init__(parent)
  57. loader = UiLoader(parent)
  58. file = QFile(ui_file)
  59. file.open(QFile.ReadOnly)
  60. loader.load(file, self)
  61. file.close()
  62. ###########################################################################
  63. # Main Code Block, runs this script as main (testing)
  64. # -------------------------------------------------------------------------
  65. if __name__ == '__main__':
  66. """Run this file as main"""
  67. import sys
  68. _LOGGER = azpy.initialize_logger('{0}-TEST'.format(_TOOL_TAG), log_to_file=True)
  69. _LOGGER.debug('Something invoked :: {0}.'.format({_MODULENAME}))
  70. _LOGGER.info("{0} :: if __name__ == '__main__':".format(_TOOL_TAG))
  71. _LOGGER.info("Starting App:{0} TEST ...".format(_TOOL_TAG))
  72. _FORM_CLASS, _BASE_CLASS = from_ui_generate_form_and_base_class(_UI_FILE)
  73. _LOGGER.info(_FORM_CLASS)
  74. _LOGGER.info(_BASE_CLASS)
  75. from azpy.shared.ui.templates import TemplateMainWindow
  76. _MAIN_WINDOW = TemplateMainWindow(logger=_LOGGER)
  77. _MAIN_WINDOW.show()
  78. del _LOGGER
  79. sys.exit(_MAIN_WINDOW.qapp.exec_())
  80. # -------------------------------------------------------------------------