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
|
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2004-2017 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""Common wrapper over Python Qt bindings:
- `PyQt5 <http://pyqt.sourceforge.net/Docs/PyQt5/>`_,
- `PyQt4 <http://pyqt.sourceforge.net/Docs/PyQt4/>`_ or
- `PySide <http://www.pyside.org>`_.
If a Qt binding is already loaded, it will use it, otherwise the different
Qt bindings are tried in this order: PyQt4, PySide, PyQt5.
The name of the loaded Qt binding is stored in the BINDING variable.
For an alternative solution providing a structured namespace,
see `qtpy <https://pypi.python.org/pypi/QtPy/>`_ which
provides the namespace of PyQt5 over PyQt4 and PySide.
"""
__authors__ = ["V.A. Sole - ESRF Data Analysis"]
__license__ = "MIT"
__date__ = "17/01/2017"
import logging
import sys
import traceback
_logger = logging.getLogger(__name__)
BINDING = None
"""The name of the Qt binding in use: 'PyQt5', 'PyQt4' or 'PySide'."""
QtBinding = None # noqa
"""The Qt binding module in use: PyQt5, PyQt4 or PySide."""
HAS_SVG = False
"""True if Qt provides support for Scalable Vector Graphics (QtSVG)."""
HAS_OPENGL = False
"""True if Qt provides support for OpenGL (QtOpenGL)."""
# First check for an already loaded wrapper
if 'PyQt5.QtCore' in sys.modules:
BINDING = 'PyQt5'
elif 'PySide.QtCore' in sys.modules:
BINDING = 'PySide'
elif 'PyQt4.QtCore' in sys.modules:
BINDING = 'PyQt4'
else: # Then try Qt bindings
try:
import PyQt4 # noqa
except ImportError:
try:
import PyQt5 # noqa
except ImportError:
try:
import PySide # noqa
except ImportError:
raise ImportError(
'No Qt wrapper found. Install PyQt4, PyQt5 or PySide.')
else:
BINDING = 'PySide'
else:
BINDING = 'PyQt5'
else:
BINDING = 'PyQt4'
if BINDING == 'PyQt4':
_logger.debug('Using PyQt4 bindings')
if sys.version < "3.0.0":
try:
import sip
sip.setapi("QString", 2)
sip.setapi("QVariant", 2)
except:
_logger.warning("Cannot set sip API")
import PyQt4 as QtBinding # noqa
from PyQt4.QtCore import * # noqa
from PyQt4.QtGui import * # noqa
try:
from PyQt4.QtOpenGL import * # noqa
except ImportError:
_logger.info("PyQt4.QtOpenGL not available")
HAS_OPENGL = False
else:
HAS_OPENGL = True
try:
from PyQt4.QtSvg import * # noqa
except ImportError:
_logger.info("PyQt4.QtSvg not available")
HAS_SVG = False
else:
HAS_SVG = True
from PyQt4.uic import loadUi # noqa
Signal = pyqtSignal
Property = pyqtProperty
Slot = pyqtSlot
elif BINDING == 'PySide':
_logger.debug('Using PySide bindings')
import PySide as QtBinding # noqa
from PySide.QtCore import * # noqa
from PySide.QtGui import * # noqa
try:
from PySide.QtOpenGL import * # noqa
except ImportError:
_logger.info("PySide.QtOpenGL not available")
HAS_OPENGL = False
else:
HAS_OPENGL = True
try:
from PySide.QtSvg import * # noqa
except ImportError:
_logger.info("PySide.QtSvg not available")
HAS_SVG = False
else:
HAS_SVG = True
pyqtSignal = Signal
# Import loadUi wrapper for PySide
from ._pyside_dynamic import loadUi # noqa
# Import missing classes
if not hasattr(locals(), "QIdentityProxyModel"):
from ._pyside_missing import QIdentityProxyModel # noqa
elif BINDING == 'PyQt5':
_logger.debug('Using PyQt5 bindings')
import PyQt5 as QtBinding # noqa
from PyQt5.QtCore import * # noqa
from PyQt5.QtGui import * # noqa
from PyQt5.QtWidgets import * # noqa
from PyQt5.QtPrintSupport import * # noqa
try:
from PyQt5.QtOpenGL import * # noqa
except ImportError:
_logger.info("PySide.QtOpenGL not available")
HAS_OPENGL = False
else:
HAS_OPENGL = True
try:
from PyQt5.QtSvg import * # noqa
except ImportError:
_logger.info("PyQt5.QtSvg not available")
HAS_SVG = False
else:
HAS_SVG = True
from PyQt5.uic import loadUi # noqa
Signal = pyqtSignal
Property = pyqtProperty
Slot = pyqtSlot
else:
raise ImportError('No Qt wrapper found. Install PyQt4, PyQt5 or PySide')
# provide a exception handler but not implement it by default
def exceptionHandler(type_, value, trace):
"""
This exception handler prevents quitting to the command line when there is
an unhandled exception while processing a Qt signal.
The script/application willing to use it should implement code similar to:
.. code-block:: python
if __name__ == "__main__":
sys.excepthook = qt.exceptionHandler
"""
_logger.error("%s %s %s", type_, value, ''.join(traceback.format_tb(trace)))
msg = QMessageBox()
msg.setWindowTitle("Unhandled exception")
msg.setIcon(QMessageBox.Critical)
msg.setInformativeText("%s %s\nPlease report details" % (type_, value))
msg.setDetailedText(("%s " % value) + ''.join(traceback.format_tb(trace)))
msg.raise_()
msg.exec_()
|