Newer
Older
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
####################################################################################################
def _getcpppointer(object):
if hasattr(Qt, '_shiboken2'):
return getattr(Qt, '_shiboken2').getCppPointer(object)[0]
elif hasattr(Qt, '_sip'):
return getattr(Qt, '_sip').unwrapinstance(object)
raise AttributeError("'module' has no attribute 'getCppPointer'")
####################################################################################################
def _wrapinstance(ptr, base=None):
'''Enable implicit cast of pointer to most suitable class
This behaviour is available in sip per default.
Based on http://nathanhorne.com/pyqtpyside-wrap-instance
Usage:
This mechanism kicks in under these circumstances.
1. Qt.py is using PySide 1 or 2.
2. A `base` argument is not provided.
See :func:`QtCompat.wrapInstance()`
Arguments:
ptr (int): Pointer to QObject in memory
base (QObject, optional): Base class to wrap with. Defaults to QObject,
which should handle anything.
'''
assert isinstance(ptr, int), "Argument 'ptr' must be of type <int>"
assert (base is None) or issubclass(base, Qt.QtCore.QObject), (
"Argument 'base' must be of type <QObject>")
if Qt.IsPyQt4 or Qt.IsPyQt5:
func = getattr(Qt, '_sip').wrapinstance
elif Qt.IsPySide2:
func = getattr(Qt, '_shiboken2').wrapInstance
elif Qt.IsPySide:
func = getattr(Qt, '_shiboken').wrapInstance
else:
raise AttributeError("'module' has no attribute 'wrapInstance'")
if base is None:
q_object = func(int(ptr), Qt.QtCore.QObject)
meta_object = q_object.metaObject()
class_name = meta_object.className()
super_class_name = meta_object.superClass().className()
if hasattr(Qt.QtWidgets, class_name):
base = getattr(Qt.QtWidgets, class_name)
elif hasattr(Qt.QtWidgets, super_class_name):
base = getattr(Qt.QtWidgets, super_class_name)
else:
base = Qt.QtCore.QObject
return func(int(ptr), base)
####################################################################################################
def _translate(context, sourceText, *args):
# In Qt4 bindings, translate can be passed 2 or 3 arguments
# In Qt5 bindings, translate can be passed 2 arguments
# The first argument is disambiguation[str]
# The last argument is n[int]
# The middle argument can be encoding[QtCore.QCoreApplication.Encoding]
if len(args) == 3:
disambiguation, encoding, n = args
elif len(args) == 2:
disambiguation, n = args
encoding = None
else:
raise TypeError(
'Expected 4 or 5 arguments, got {0}.'.format(len(args) + 2))
if hasattr(Qt.QtCore, 'QCoreApplication'):
app = getattr(Qt.QtCore, 'QCoreApplication')
else:
raise NotImplementedError(
'Missing QCoreApplication implementation for {binding}'.format(
binding=Qt.__binding__,
)
)
if Qt.__binding__ in ('PySide2', 'PyQt5'):
sanitized_args = [context, sourceText, disambiguation, n]
else:
sanitized_args = [
context,
sourceText,
disambiguation,
encoding or app.CodecForTr,
n
]
return app.translate(*sanitized_args)
####################################################################################################
####################################################################################################
def _none():
'''Internal option (used in installer)'''
Mock = type('Mock', (), {'__getattr__': lambda Qt, attr: None})
Qt.__binding__ = 'None'
Qt.__qt_version__ = '0.0.0'
Qt.__binding_version__ = '0.0.0'
Qt.QtCompat.loadUi = lambda uifile, baseinstance=None: None
Qt.QtCompat.setSectionResizeMode = lambda *args, **kwargs: None
for submodule in _common_members.keys():
setattr(Qt, submodule, Mock())
setattr(Qt, '_' + submodule, Mock())