File: qtproxies.py

package info (click to toggle)
python-qt4 4.0.1-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 18,632 kB
  • ctags: 2,639
  • sloc: python: 29,409; sh: 5,646; cpp: 3,168; xml: 149; makefile: 109
file content (289 lines) | stat: -rw-r--r-- 9,843 bytes parent folder | download
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# Hic sunt leones!
import re
import sys

from indenter import write_code


i18n_strings = []
i18n_context = ""

def i18n_print(string):
    i18n_strings.append(string)
    
def moduleMember(module, name):
    if module == "":
        return name
    else:
        return "%s.%s" % (module, name)

def obj_to_argument(obj):
    if isinstance(obj, str):
        return "\"%s\"" % (obj,)
    else:
        return str(obj)
    
def i18n_func(name):
    def _printer(self, *args):
        i18n_print("%s.%s(%s)" % (self, name, ",".join(map(obj_to_argument, args))))
    return _printer

class Literal(object):
    """Literal(string) -> new literal

    string will not be quoted when put into an argument list"""
    def __init__(self, string):
        self.string = string

    def __str__(self):
        return self.string

class i18n_string(object):
    """i18n_string(string)

    string will be UTF-8-encoded, escaped, quoted and translated when included
    into a function call argument list."""
    _esc_regex = re.compile(r"(\"|\'|\\)")
    def __init__(self, string):
        if isinstance(string, unicode):
            self.string = string.encode("UTF-8")
        else:
            self.string = string

    def escape(self, text):
        x = self._esc_regex.sub(r"\\\1", text)
        return re.sub(r"\n", r'\\n"\n"', x)

    def __str__(self):
        return "QtGui.QApplication.translate(\"%s\", \"%s\", None, QtGui.QApplication.UnicodeUTF8)" % (i18n_context, self.escape(self.string))


# classes with this flag will occur in retranslateUi completely
# (construction, function calls, all calls to other functions with this
#  class as an argument)
I18N_ONLY = 1
# classes with this flag will be handled as literal values. If functions are
# called on these classes, the literal value changes.
# Example:
# the code
# >>> QSize(9,10).expandedTo(...)
# will print just that code.
AS_ARGUMENT = 2

# ATTENTION: currently, classes can either be literal or normal. If a class
# should need both kinds of behaviour, the code has to be changed.

class ProxyClassMember(object):
    def __init__(self, proxy, function_name, flags):
        self.proxy = proxy
        self.function_name = function_name
        self.flags = flags
        
    def __str__(self):
        return "%s.%s" % (self.proxy, self.function_name)
    
    def __call__(self, *args):
        func_call = "%s.%s(%s)" % (self.proxy,
                                   self.function_name,
                                   ",".join(map(obj_to_argument, args)))
        if self.flags & AS_ARGUMENT:
            self.proxy._name = func_call
            return self.proxy
        else:
            needs_translation = self.flags & I18N_ONLY
            for arg in args:
                if isinstance(arg, i18n_string) or getattr(arg, "flags", 0) & I18N_ONLY:
                    needs_translation = True
            if needs_translation:
                i18n_print(func_call)
            else:
                write_code(func_call)


class ProxyType(type):
    def __getattribute__(cls, name):
        try:
            attr = type.__getattribute__(cls, name)
            if isinstance(attr, ProxyType):
                type.__setattr__(attr, "module",
                                 moduleMember(type.__getattribute__(cls, "module"),
                                              type.__getattribute__(cls, "__name__")))
            return attr
        except AttributeError:
            return type(name, (LiteralProxyClass,),
                        {"module": moduleMember(type.__getattribute__(cls, "module"),
                                                type.__getattribute__(cls, "__name__"))})

    def __str__(cls):
        return moduleMember(type.__getattribute__(cls, "module"),
                            type.__getattribute__(cls, "__name__"))


class ProxyClass(object):
    __metaclass__ = ProxyType
    flags = 0
    module = ""
    def __init__(self, objectname, is_attribute,
                 args = (), noInstantiation = False):
        if is_attribute:
            self._name = "self.%s" % (objectname, )
        else:
            self._name = objectname
        if not noInstantiation:
            funcall = "\n%s = %s(%s)" % \
                      (self._name,
                       moduleMember(self.module, self.__class__.__name__),
                       ",".join(map(str, args)))
            if self.flags & I18N_ONLY:
                i18n_print(funcall)
            else:
                write_code(funcall)
    
    def __str__(self):
        return self._name

    def __getattribute__(self, attribute):
        try:
            return object.__getattribute__(self, attribute)
        except AttributeError:
            return ProxyClassMember(self, attribute, self.flags)

class I18NProxyClass(ProxyClass):
    flags = I18N_ONLY
    
class LiteralProxyClass(ProxyClass):
    """LiteralObject(*args) -> new literal class

    a literal class can be used as argument in a function call

    >>> class Foo(LiteralProxyClass): pass
    >>> str(Foo(1,2,3)) == "Foo(1,2,3)"
    """
    flags = AS_ARGUMENT
    def __init__(self, *args):
        self._name = "%s(%s)" % \
                     (moduleMember(self.module, self.__class__.__name__),
                      ",".join(map(obj_to_argument, args)))

class ProxyNamespace(ProxyClass):
    pass

# These are all the Qt classes used by pyuic4 in their namespaces. 
# If a class is missing, the compiler will fail, normally with an
# AttributeError.
# For adding new classes:
#     - utility classes used as literal values do not need to be listed
#       because they are created on the fly as subclasses of LiteralProxyClass
#     - classes which are *not* QWidgets inherit from ProxyClass and they
#       have to be listed explicitly in the correct namespace. These classes
#       are created via a the ProxyQObjectCreator
#     - new QWidget-derived classes have to inherit from qtproxies.QWidget
#       If the widget does not need any special methods, it can be listed
#       in _qwidgets

class QtCore(ProxyNamespace):
    class Qt(ProxyNamespace):
        pass

    ## connectSlotsByName and connect have to be handled as class methods,
    ## otherwise they would be created as LiteralProxyClasses and never be
    ## printed
    class QMetaObject(ProxyClass):
        def connectSlotsByName(cls, *args):
            ProxyClassMember(cls, "connectSlotsByName", 0)(*args)
        connectSlotsByName = classmethod(connectSlotsByName)


    class QObject(ProxyClass):
        def metaObject(self):
            class _FakeMetaObject(object):
                def className(*args):
                    return self.__class__.__name__
            return _FakeMetaObject()

        def objectName(self):
            return self._name.split(".")[-1]

        def connect(cls, *args):
            ProxyClassMember(cls, "connect", 0)(*args)
        connect = classmethod(connect)

_qwidgets = (
    "QCheckBox", "QComboBox", "QDateEdit", "QDateTimeEdit", "QDial", "QDialog",
    "QDockWidget", "QDoubleSpinBox", "QFrame", "QGroupBox", "QLabel",
    "QLCDNumber", "QLineEdit", "QListView", "QListWidget", "QMainWindow",
    "QMenu", "QMenuBar", "QProgressBar", "QPushButton", "QRadioButton", 
    "QScrollBar", "QSlider", "QSpinBox", "QSplitter", "QStackedWidget", 
    "QStatusBar", "QTableView", "QTableWidget", "QTabWidget", "QTextBrowser",
    "QTextEdit", "QTimeEdit", "QToolBar", "QToolBox", "QToolButton", "QTreeView",
    "QTreeWidget", "QAbstractItemView",)

class QtGui(ProxyNamespace):
    class QListWidgetItem(I18NProxyClass):
        pass

    class QTreeWidgetItem(I18NProxyClass):
        pass

    class QTableWidgetItem(I18NProxyClass):
        pass

    class QPalette(ProxyClass): pass
    class QFont(ProxyClass): pass
    class QSpacerItem(ProxyClass): pass
    class QSizePolicy(ProxyClass): pass
    ## QActions inherit from QObject for the metaobject stuff
    ## and the hierarchy has to be correct since we have a
    ## isinstance(x, QtGui.QLayout) call in the ui parser
    class QAction(QtCore.QObject): pass
    class QLayout(QtCore.QObject): pass
    class QGridLayout(QLayout): pass
    class QHBoxLayout(QLayout): pass
    class QVBoxLayout(QLayout): pass

    class QWidget(QtCore.QObject):
        def font(self):
            return Literal("%s.font()" % (self,))

        def minimumSizeHint(self):
            return Literal("%s.minimumSizeHint()" % (self,))

        def sizePolicy(self):
            sp = LiteralProxyClass()
            sp._name = "%s.sizePolicy()" % (self,)
            return sp

    class QListWidget(QWidget):
        clear = i18n_func("clear")
    
    class QTableWidget(QWidget):
        clear = i18n_func("clear")
        setRowCount = i18n_func("setRowCount")
        setColumnCount = i18n_func("setColumnCount")
        
    class QTreeWidget(QWidget):
        clear = i18n_func("clear")

        def headerItem(self):
            return QtGui.QWidget("%s.headerItem()" % (self,), False, (), noInstantiation = True)

    class QMenu(QWidget):
        def menuAction(self):
            return Literal("%s.menuAction()" % (self,))

    class QTabWidget(QWidget):
        def addTab(self, *args):
            write_code("%s.addTab(%s, \"\")" % (self._name, args[0]))
            i18n_print("%s.setTabText(%s.indexOf(%s), %s)" % \
                       (self._name, self._name, args[0], args[1]))

    class QToolBox(QWidget):
        def addItem(self, *args):
            write_code("%s.addItem(%s, \"\")" % (self._name, args[0]))
            i18n_print("%s.setItemText(%s.indexOf(%s), %s)" % \
                       (self._name, self._name, args[0], args[1]))

    # add all remaining classes
    for _class in _qwidgets:
        if not locals().has_key(_class):
            locals()[_class] = type(_class, (QWidget, ), {})