loader.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright (c) 2023 Riverbank Computing Limited.
  2. # Copyright (c) 2006 Thorsten Marek.
  3. # All right reserved.
  4. #
  5. # This file is part of PyQt.
  6. #
  7. # You may use this file under the terms of the GPL v3 or the revised BSD
  8. # license as follows:
  9. #
  10. # "Redistribution and use in source and binary forms, with or without
  11. # modification, are permitted provided that the following conditions are
  12. # met:
  13. # * Redistributions of source code must retain the above copyright
  14. # notice, this list of conditions and the following disclaimer.
  15. # * Redistributions in binary form must reproduce the above copyright
  16. # notice, this list of conditions and the following disclaimer in
  17. # the documentation and/or other materials provided with the
  18. # distribution.
  19. # * Neither the name of the Riverbank Computing Limited nor the names
  20. # of its contributors may be used to endorse or promote products
  21. # derived from this software without specific prior written
  22. # permission.
  23. #
  24. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  35. from PyQt6 import QtCore, QtGui, QtWidgets
  36. from ..uiparser import UIParser
  37. from .qobjectcreator import LoaderCreatorPolicy
  38. class DynamicUILoader(UIParser):
  39. def __init__(self, package):
  40. UIParser.__init__(self, QtCore, QtGui, QtWidgets,
  41. LoaderCreatorPolicy(package))
  42. def createToplevelWidget(self, classname, widgetname):
  43. if self.toplevelInst is None:
  44. return self.factory.createQtObject(classname, widgetname)
  45. if not isinstance(self.toplevelInst, self.factory.findQObjectType(classname)):
  46. raise TypeError(
  47. ("Wrong base class of toplevel widget",
  48. (type(self.toplevelInst), classname)))
  49. return self.toplevelInst
  50. def loadUi(self, filename, toplevelInst):
  51. self.toplevelInst = toplevelInst
  52. return self.parse(filename)