1#############################################################################
2##
3## Copyright (C) 2019 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
40
41import sys
42
43from ..properties import Properties
44from ..uiparser import UIParser
45from . import qtproxies
46from .indenter import createCodeIndenter, getIndenter, write_code
47from .qobjectcreator import CompilerCreatorPolicy
48
49
50class UICompiler(UIParser):
51    def __init__(self):
52        UIParser.__init__(self, qtproxies.QtCore, qtproxies.QtGui,
53                qtproxies.QtWidgets, CompilerCreatorPolicy())
54
55    def reset(self):
56        qtproxies.i18n_strings = []
57        UIParser.reset(self)
58
59    def setContext(self, context):
60        qtproxies.i18n_context = context
61
62    def createToplevelWidget(self, classname, widgetname):
63        indenter = getIndenter()
64        indenter.level = 0
65
66        indenter.write("from PyQt5 import QtCore, QtGui, QtWidgets")
67        indenter.write("")
68
69        indenter.write("")
70        indenter.write("class Ui_%s(object):" % self.uiname)
71        indenter.indent()
72        indenter.write("def setupUi(self, %s):" % widgetname)
73        indenter.indent()
74        w = self.factory.createQObject(classname, widgetname, (),
75                                   is_attribute = False,
76                                   no_instantiation = True)
77        w.baseclass = classname
78        w.uiclass = "Ui_%s" % self.uiname
79        return w
80
81    def setDelayedProps(self):
82        write_code("")
83        write_code("self.retranslateUi(%s)" % self.toplevelWidget)
84        UIParser.setDelayedProps(self)
85
86    def finalize(self):
87        indenter = getIndenter()
88        indenter.level = 1
89        indenter.write("")
90        indenter.write("def retranslateUi(self, %s):" % self.toplevelWidget)
91
92        indenter.indent()
93
94        if qtproxies.i18n_strings:
95            indenter.write("_translate = QtCore.QCoreApplication.translate")
96            for s in qtproxies.i18n_strings:
97                indenter.write(s)
98        else:
99            indenter.write("pass")
100
101        indenter.dedent()
102        indenter.dedent()
103
104        # Keep a reference to the resource modules to import because the parser
105        # will reset() before returning.
106        self._resources = self.resources
107        self._resources.sort()
108
109    def compileUi(self, input_stream, output_stream, from_imports, resource_suffix, import_from):
110        createCodeIndenter(output_stream)
111        w = self.parse(input_stream, resource_suffix)
112
113        self.factory._cpolicy._writeOutImports()
114
115        for res in self._resources:
116            if from_imports:
117                write_code("from %s import %s" % (import_from, res))
118            else:
119                write_code("import %s" % res)
120
121        return {"widgetname": str(w),
122                "uiclass" : w.uiclass,
123                "baseclass" : w.baseclass}
124