1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
import sys
from PyQt4.uic.properties import Properties
from PyQt4.uic.uiparser import UIParser
from PyQt4.uic.Compiler import qtproxies
from PyQt4.uic.Compiler.indenter import createCodeIndenter, getIndenter, \
write_code
from PyQt4.uic.Compiler.qobjectcreator import CompilerCreatorPolicy
class UICompiler(UIParser):
def __init__(self):
UIParser.__init__(self, qtproxies.QtCore, qtproxies.QtGui,
CompilerCreatorPolicy())
def reset(self):
qtproxies.i18n_strings = []
UIParser.reset(self)
def setContext(self, context):
qtproxies.i18n_context = context
def createToplevelWidget(self, classname, widgetname):
indenter = getIndenter()
indenter.level = 0
indenter.write("from PyQt4 import QtCore, QtGui")
indenter.write("")
indenter.write("class Ui_%s(object):" % self.uiname)
indenter.indent()
indenter.write("def setupUi(self, %s):" % widgetname)
indenter.indent()
w = self.factory.createQObject(classname, widgetname, (),
is_attribute = False,
no_instantiation = True)
w.baseclass = classname
w.uiclass = "Ui_%s" % self.uiname
return w
def setDelayedProps(self):
write_code("")
write_code("self.retranslateUi(%s)" % self.toplevelWidget)
UIParser.setDelayedProps(self)
def finalize(self):
indenter = getIndenter()
indenter.level = 1
indenter.write("")
indenter.write("def retranslateUi(self, %s):" % self.toplevelWidget)
indenter.indent()
if qtproxies.i18n_strings:
for s in qtproxies.i18n_strings:
indenter.write(s)
else:
indenter.write("pass")
indenter.dedent()
indenter.dedent()
# Make a copy of the resource modules to import because the parser will
# reset() before returning.
self._resources = self.resources
def compileUi(self, input_stream, output_stream):
createCodeIndenter(output_stream)
w = self.parse(input_stream)
indenter = getIndenter()
indenter.write("")
self.factory._cpolicy._writeOutImports()
for res in self._resources:
indenter.write("import %s" % res)
return {"widgetname": str(w),
"uiclass" : w.uiclass,
"baseclass" : w.baseclass}
|