compiler.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. import sys
  36. from ..properties import Properties
  37. from ..uiparser import UIParser
  38. from . import qtproxies
  39. from .indenter import createCodeIndenter, getIndenter, write_code
  40. from .qobjectcreator import CompilerCreatorPolicy
  41. class UICompiler(UIParser):
  42. def __init__(self):
  43. UIParser.__init__(self, qtproxies.QtCore, qtproxies.QtGui,
  44. qtproxies.QtWidgets, CompilerCreatorPolicy())
  45. def reset(self):
  46. qtproxies.i18n_strings = []
  47. UIParser.reset(self)
  48. def setContext(self, context):
  49. qtproxies.i18n_context = context
  50. def createToplevelWidget(self, classname, widgetname):
  51. indenter = getIndenter()
  52. indenter.level = 0
  53. indenter.write("from PyQt6 import QtCore, QtGui, QtWidgets")
  54. indenter.write("")
  55. indenter.write("")
  56. indenter.write("class Ui_%s(object):" % self.uiname)
  57. indenter.indent()
  58. indenter.write("def setupUi(self, %s):" % widgetname)
  59. indenter.indent()
  60. w = self.factory.createQtObject(classname, widgetname,
  61. is_attribute=False, no_instantiation=True)
  62. w.baseclass = classname
  63. w.uiclass = "Ui_%s" % self.uiname
  64. return w
  65. def setDelayedProps(self):
  66. write_code("")
  67. write_code("self.retranslateUi(%s)" % self.toplevelWidget)
  68. UIParser.setDelayedProps(self)
  69. def finalize(self):
  70. indenter = getIndenter()
  71. indenter.level = 1
  72. indenter.write("")
  73. indenter.write("def retranslateUi(self, %s):" % self.toplevelWidget)
  74. indenter.indent()
  75. if qtproxies.i18n_strings:
  76. indenter.write("_translate = QtCore.QCoreApplication.translate")
  77. for s in qtproxies.i18n_strings:
  78. indenter.write(s)
  79. else:
  80. indenter.write("pass")
  81. indenter.dedent()
  82. indenter.dedent()
  83. def compileUi(self, input_stream, output_stream):
  84. createCodeIndenter(output_stream)
  85. w = self.parse(input_stream)
  86. self.factory._cpolicy._writeOutImports()
  87. return {"widgetname": str(w),
  88. "uiclass" : w.uiclass,
  89. "baseclass" : w.baseclass}