Show More
@@ -1,54 +1,66 b'' | |||||
1 | """ A Qt API selector that can be used to switch between PyQt and PySide. |
|
1 | """ A Qt API selector that can be used to switch between PyQt and PySide. | |
2 |
|
2 | |||
3 | This uses the ETS 4.0 selection pattern of: |
|
3 | This uses the ETS 4.0 selection pattern of: | |
4 | PySide first, PyQt with API v2. second. |
|
4 | PySide first, PyQt with API v2. second. | |
5 |
|
5 | |||
6 | Do not use this if you need PyQt with the old QString/QVariant API. |
|
6 | Do not use this if you need PyQt with the old QString/QVariant API. | |
7 | """ |
|
7 | """ | |
8 |
|
8 | |||
9 | import os |
|
9 | import os | |
10 |
|
10 | |||
11 | # Available APIs. |
|
11 | # Available APIs. | |
12 | QT_API_PYQT = 'pyqt' |
|
12 | QT_API_PYQT = 'pyqt' | |
13 | QT_API_PYSIDE = 'pyside' |
|
13 | QT_API_PYSIDE = 'pyside' | |
14 |
|
14 | |||
15 | def prepare_pyqt4(): |
|
15 | def prepare_pyqt4(): | |
16 | # For PySide compatibility, use the new-style string API that automatically |
|
16 | # For PySide compatibility, use the new-style string API that automatically | |
17 | # converts QStrings to Unicode Python strings. Also, automatically unpack |
|
17 | # converts QStrings to Unicode Python strings. Also, automatically unpack | |
18 | # QVariants to their underlying objects. |
|
18 | # QVariants to their underlying objects. | |
19 | import sip |
|
19 | import sip | |
20 | sip.setapi('QString', 2) |
|
20 | sip.setapi('QString', 2) | |
21 | sip.setapi('QVariant', 2) |
|
21 | sip.setapi('QVariant', 2) | |
22 |
|
22 | |||
23 | # Select Qt binding, using the QT_API environment variable if available. |
|
23 | # Select Qt binding, using the QT_API environment variable if available. | |
24 | QT_API = os.environ.get('QT_API') |
|
24 | QT_API = os.environ.get('QT_API') | |
25 | if QT_API is None: |
|
25 | if QT_API is None: | |
26 | try: |
|
26 | try: | |
27 | import PySide |
|
27 | import PySide | |
|
28 | if PySide.__version__ < '1.0.3': | |||
|
29 | # old PySide, fallback on PyQt | |||
|
30 | raise ImportError | |||
28 | QT_API = QT_API_PYSIDE |
|
31 | QT_API = QT_API_PYSIDE | |
29 | except ImportError: |
|
32 | except ImportError: | |
30 | try: |
|
33 | try: | |
31 | prepare_pyqt4() |
|
34 | prepare_pyqt4() | |
32 | import PyQt4 |
|
35 | import PyQt4 | |
|
36 | from PyQt4 import QtCore | |||
|
37 | if QtCore.PYQT_VERSION_STR < '4.7': | |||
|
38 | # PyQt 4.6 has issues with null strings returning as None | |||
|
39 | raise ImportError | |||
33 | QT_API = QT_API_PYQT |
|
40 | QT_API = QT_API_PYQT | |
34 | except ImportError: |
|
41 | except ImportError: | |
35 | raise ImportError('Cannot import PySide or PyQt4') |
|
42 | raise ImportError('Cannot import PySide >= 1.0.3 or PyQt4 >= 4.7') | |
36 |
|
43 | |||
37 | elif QT_API == QT_API_PYQT: |
|
44 | elif QT_API == QT_API_PYQT: | |
38 | # Note: This must be called *before* PyQt4 is imported. |
|
45 | # Note: This must be called *before* PyQt4 is imported. | |
39 | prepare_pyqt4() |
|
46 | prepare_pyqt4() | |
40 |
|
47 | |||
41 | # Now peform the imports. |
|
48 | # Now peform the imports. | |
42 | if QT_API == QT_API_PYQT: |
|
49 | if QT_API == QT_API_PYQT: | |
43 | from PyQt4 import QtCore, QtGui, QtSvg |
|
50 | from PyQt4 import QtCore, QtGui, QtSvg | |
|
51 | if QtCore.PYQT_VERSION_STR < '4.7': | |||
|
52 | raise ImportError("IPython requires PyQt4 >= 4.7, found %s"%QtCore.PYQT_VERSION_STR) | |||
44 |
|
53 | |||
45 | # Alias PyQt-specific functions for PySide compatibility. |
|
54 | # Alias PyQt-specific functions for PySide compatibility. | |
46 | QtCore.Signal = QtCore.pyqtSignal |
|
55 | QtCore.Signal = QtCore.pyqtSignal | |
47 | QtCore.Slot = QtCore.pyqtSlot |
|
56 | QtCore.Slot = QtCore.pyqtSlot | |
48 |
|
57 | |||
49 | elif QT_API == QT_API_PYSIDE: |
|
58 | elif QT_API == QT_API_PYSIDE: | |
|
59 | import PySide | |||
|
60 | if PySide.__version__ < '1.0.3': | |||
|
61 | raise ImportError("IPython requires PySide >= 1.0.3, found %s"%PySide.__version__) | |||
50 | from PySide import QtCore, QtGui, QtSvg |
|
62 | from PySide import QtCore, QtGui, QtSvg | |
51 |
|
63 | |||
52 | else: |
|
64 | else: | |
53 | raise RuntimeError('Invalid Qt API %r, valid values are: %r or %r' % |
|
65 | raise RuntimeError('Invalid Qt API %r, valid values are: %r or %r' % | |
54 | (QT_API, QT_API_PYQT, QT_API_PYSIDE)) |
|
66 | (QT_API, QT_API_PYQT, QT_API_PYSIDE)) |
General Comments 0
You need to be logged in to leave comments.
Login now