Example #1
0
def compileUi(uifile, pyfile, execute=False, indent=4, from_imports=False):
    """compileUi(uifile, pyfile, execute=False, indent=4, from_imports=False)

    Creates a Python module from a Qt Designer .ui file.

    uifile is a file name or file-like object containing the .ui file.
    pyfile is the file-like object to which the Python code will be written to.
    execute is optionally set to generate extra Python code that allows the
    code to be run as a standalone application.  The default is False.
    indent is the optional indentation width using spaces.  If it is 0 then a
    tab is used.  The default is 4.
    from_imports is optionally set to generate import statements that are
    relative to '.'.
    """

    from time import ctime
    import PySide2

    try:
        uifname = uifile.name
    except AttributeError:
        uifname = uifile

    indenter.indentwidth = indent

    global PySideToolsVersion
    pyfile.write(_header %
                 (uifname, uifname, ctime(), __version__, PySide2.__version__))

    ui_comp = compiler.UICompiler(PySide2.__all__)
    winfo = ui_comp.compileUi(uifile, pyfile, from_imports)

    if execute:
        indenter.write_code(_display_code % winfo)
Example #2
0
    def _writeImportCode(self):
        imports = {}
        for widget in self._usedWidgets:
            _, module = self._widgets[widget]
            imports.setdefault(module, []).append(widget)

        for module, classes in imports.items():
            write_code("from %s import %s" % (module, ", ".join(classes)))
Example #3
0
    def _writeImportCode(self):
        imports = {}
        for widget in self._usedWidgets:
            _, module = self._widgets[widget]
            imports.setdefault(module, []).append(widget)

        for module, classes in imports.items():
            write_code("from %s import %s" % (module, ", ".join(classes)))
Example #4
0
    def _writeImportCode(self):
        imports = {}
        for widget in self._usedWidgets:
            _, module = self._widgets[widget]
            imports.setdefault(module, []).append(widget)

        for module, classes in imports.items():
            parts = module.split(".")
            if (len(parts) == 2 and not parts[0].startswith("PySide2")
                    and parts[0] in pyside2_modules):
                module = "PySide2.{}".format(parts[0])
            write_code("from %s import %s" % (module, ", ".join(classes)))
Example #5
0
 def __call__(self, *args):
     func_call = "%s.%s(%s)" % (self.proxy, self.function_name, ", ".join(map(as_string, args)))
     if self.flags & AS_ARGUMENT:
         self.proxy._uic_name = func_call
         return self.proxy
     else:
         needs_translation = False
         for arg in args:
             if isinstance(arg, i18n_string):
                 needs_translation = True
         if needs_translation:
             i18n_print(func_call)
         else:
             write_code(func_call)
Example #6
0
    def __init__(self, objectname, is_attribute, args=(), noInstantiation=False):
        if objectname:
            if is_attribute:
                objectname = "self." + objectname

            self._uic_name = objectname
        else:
            self._uic_name = "Unnamed"

        if not noInstantiation:
            funcall = "%s(%s)" % (moduleMember(self.module, self.__class__.__name__), ", ".join(map(str, args)))

            if objectname:
                funcall = "%s = %s" % (objectname, funcall)

            write_code(funcall)
Example #7
0
 def __call__(self, *args):
     func_call = "%s.%s(%s)" % (self.proxy,
                                self.function_name,
                                ", ".join(map(as_string, args)))
     if self.flags & AS_ARGUMENT:
         self.proxy._uic_name = func_call
         return self.proxy
     else:
         needs_translation = False
         for arg in args:
             if isinstance(arg, i18n_string):
                 needs_translation = True
         if needs_translation:
             i18n_print(func_call)
         else:
             write_code(func_call)
Example #8
0
    def __init__(self, objectname, is_attribute, args=(), noInstantiation=False):
        if objectname:
            if is_attribute:
                objectname = "self." + objectname

            self._uic_name = objectname
        else:
            self._uic_name = "Unnamed"

        if not noInstantiation:
            funcall = "%s(%s)" % \
                    (moduleMember(self.module, self.__class__.__name__),
                    ", ".join(map(str, args)))

            if objectname:
                funcall = "%s = %s" % (objectname, funcall)

            write_code(funcall)
Example #9
0
 def setDelayedProps(self):
     write_code("")
     write_code("self.retranslateUi(%s)" % self.toplevelWidget)
     UIParser.setDelayedProps(self)
Example #10
0
 def _writeImportCode(self):
     if self._used:
         if self._package is None:
             write_code("import %s" % self._module)
         else:
             write_code("from %s import %s" % (self._package, self._module))
Example #11
0
def write_import(module_name, from_imports):
    if from_imports:
        write_code("from . import %s" % module_name)
    else:
        write_code("import %s" % module_name)
Example #12
0
 def _writeImportCode(self):
     if self._used:
         if self._package is None:
             write_code("import %s" % self._module)
         else:
             write_code("from %s import %s" % (self._package, self._module))
Example #13
0
 def setDelayedProps(self):
     write_code("")
     write_code("self.retranslateUi(%s)" % self.toplevelWidget)
     UIParser.setDelayedProps(self)
Example #14
0
def write_import(module_name, from_imports):
    if from_imports:
        write_code("from . import %s" % module_name)
    else:
        write_code("import %s" % module_name)