qobjectcreator.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #############################################################################
  2. ##
  3. ## Copyright (C) 2023 Riverbank Computing Limited.
  4. ## Copyright (C) 2006 Thorsten Marek.
  5. ## All right reserved.
  6. ##
  7. ## This file is part of PyQt.
  8. ##
  9. ## You may use this file under the terms of the GPL v2 or the revised BSD
  10. ## license as follows:
  11. ##
  12. ## "Redistribution and use in source and binary forms, with or without
  13. ## modification, are permitted provided that the following conditions are
  14. ## met:
  15. ## * Redistributions of source code must retain the above copyright
  16. ## notice, this list of conditions and the following disclaimer.
  17. ## * Redistributions in binary form must reproduce the above copyright
  18. ## notice, this list of conditions and the following disclaimer in
  19. ## the documentation and/or other materials provided with the
  20. ## distribution.
  21. ## * Neither the name of the Riverbank Computing Limited nor the names
  22. ## of its contributors may be used to endorse or promote products
  23. ## derived from this software without specific prior written
  24. ## permission.
  25. ##
  26. ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. ## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. ## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. ## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. ## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  31. ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  32. ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  33. ## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  34. ## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. ## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  37. ##
  38. #############################################################################
  39. import sys
  40. from PyQt6 import QtGui, QtWidgets
  41. class _QtWrapper(object):
  42. @classmethod
  43. def search(cls, name):
  44. return getattr(cls.module, name, None)
  45. class _QtGuiWrapper(_QtWrapper):
  46. module = QtGui
  47. class _QtWidgetsWrapper(_QtWrapper):
  48. module = QtWidgets
  49. class _ModuleWrapper(object):
  50. def __init__(self, moduleName, classes):
  51. self._moduleName = moduleName
  52. self._module = None
  53. self._classes = classes
  54. def search(self, cls):
  55. if cls in self._classes:
  56. if self._module is None:
  57. self._module = __import__(self._moduleName, {}, {}, self._classes)
  58. # Remove any C++ scope.
  59. cls = cls.split('.')[-1]
  60. return getattr(self._module, cls)
  61. return None
  62. class _CustomWidgetLoader(object):
  63. def __init__(self, package):
  64. # should it stay this way?
  65. if '.' not in sys.path:
  66. sys.path.append('.')
  67. self._widgets = {}
  68. self._modules = {}
  69. self._package = package
  70. def addCustomWidget(self, widgetClass, baseClass, module):
  71. assert widgetClass not in self._widgets
  72. self._widgets[widgetClass] = module
  73. def search(self, cls):
  74. module_name = self._widgets.get(cls)
  75. if module_name is None:
  76. return None
  77. module = self._modules.get(module_name)
  78. if module is None:
  79. if module_name.startswith('.'):
  80. if self._package == '':
  81. raise ImportError(
  82. "relative import of %s without base package specified" % module_name)
  83. if self._package.startswith('.'):
  84. raise ImportError(
  85. "base package %s is relative" % self._package)
  86. mname = self._package + module_name
  87. else:
  88. mname = module_name
  89. try:
  90. module = __import__(mname, {}, {}, (cls,))
  91. except ValueError:
  92. # Raise a more helpful exception.
  93. raise ImportError("unable to import module %s" % mname)
  94. self._modules[module_name] = module
  95. return getattr(module, cls)
  96. class LoaderCreatorPolicy(object):
  97. def __init__(self, package):
  98. self._package = package
  99. def createQtGuiWidgetsWrappers(self):
  100. return [_QtGuiWrapper, _QtWidgetsWrapper]
  101. def createModuleWrapper(self, moduleName, classes):
  102. return _ModuleWrapper(moduleName, classes)
  103. def createCustomWidgetLoader(self):
  104. return _CustomWidgetLoader(self._package)
  105. def instantiate(self, ctor, object_name, ctor_args, ctor_kwargs,
  106. is_attribute, no_instantiation):
  107. if ctor_args is None:
  108. ctor_args = ()
  109. if ctor_kwargs is None:
  110. ctor_kwargs = {}
  111. return ctor(*ctor_args, **ctor_kwargs)
  112. def invoke(self, rname, method, args):
  113. return method(*args)
  114. def getSlot(self, object, slotname):
  115. # Rename slots that correspond to Python keyword arguments.
  116. if slotname == 'raise':
  117. slotname += '_'
  118. return getattr(object, slotname)
  119. def asString(self, s):
  120. return s