Show More
@@ -1,67 +1,81 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 | pyside_found = False | |||
26 | try: |
|
27 | try: | |
27 | import PySide |
|
28 | import PySide | |
28 | if PySide.__version__ < '1.0.3': |
|
29 | if PySide.__version__ < '1.0.3': | |
29 | # old PySide, fallback on PyQt |
|
30 | # old PySide, fallback on PyQt | |
30 | raise ImportError |
|
31 | raise ImportError | |
|
32 | # we can't import an incomplete pyside and pyqt4 | |||
|
33 | # this will cause a crash in sip (#1431) | |||
|
34 | # check for complete presence before importing | |||
|
35 | import imp | |||
|
36 | imp.find_module("QtCore", PySide.__path__) | |||
|
37 | imp.find_module("QtGui", PySide.__path__) | |||
|
38 | imp.find_module("QtSvg", PySide.__path__) | |||
|
39 | pyside_found = True | |||
31 | from PySide import QtCore, QtGui, QtSvg |
|
40 | from PySide import QtCore, QtGui, QtSvg | |
32 | QT_API = QT_API_PYSIDE |
|
41 | QT_API = QT_API_PYSIDE | |
33 | except ImportError: |
|
42 | except ImportError: | |
34 | try: |
|
43 | try: | |
35 | prepare_pyqt4() |
|
44 | prepare_pyqt4() | |
36 | import PyQt4 |
|
45 | import PyQt4 | |
37 | from PyQt4 import QtCore, QtGui, QtSvg |
|
46 | from PyQt4 import QtCore, QtGui, QtSvg | |
|
47 | if pyside_found: | |||
|
48 | print "WARNING: PySide installation incomplete and PyQt4 " \ | |||
|
49 | "present.\nThis will likely crash, please install " \ | |||
|
50 | "PySide completely, remove PySide or PyQt4 or set " \ | |||
|
51 | "the QT_API environment variable to pyqt or pyside" | |||
38 | if QtCore.PYQT_VERSION_STR < '4.7': |
|
52 | if QtCore.PYQT_VERSION_STR < '4.7': | |
39 | # PyQt 4.6 has issues with null strings returning as None |
|
53 | # PyQt 4.6 has issues with null strings returning as None | |
40 | raise ImportError |
|
54 | raise ImportError | |
41 | QT_API = QT_API_PYQT |
|
55 | QT_API = QT_API_PYQT | |
42 | except ImportError: |
|
56 | except ImportError: | |
43 | raise ImportError('Cannot import PySide >= 1.0.3 or PyQt4 >= 4.7') |
|
57 | raise ImportError('Cannot import PySide >= 1.0.3 or PyQt4 >= 4.7') | |
44 |
|
58 | |||
45 | elif QT_API == QT_API_PYQT: |
|
59 | elif QT_API == QT_API_PYQT: | |
46 | # Note: This must be called *before* PyQt4 is imported. |
|
60 | # Note: This must be called *before* PyQt4 is imported. | |
47 | prepare_pyqt4() |
|
61 | prepare_pyqt4() | |
48 |
|
62 | |||
49 | # Now peform the imports. |
|
63 | # Now peform the imports. | |
50 | if QT_API == QT_API_PYQT: |
|
64 | if QT_API == QT_API_PYQT: | |
51 | from PyQt4 import QtCore, QtGui, QtSvg |
|
65 | from PyQt4 import QtCore, QtGui, QtSvg | |
52 | if QtCore.PYQT_VERSION_STR < '4.7': |
|
66 | if QtCore.PYQT_VERSION_STR < '4.7': | |
53 | raise ImportError("IPython requires PyQt4 >= 4.7, found %s"%QtCore.PYQT_VERSION_STR) |
|
67 | raise ImportError("IPython requires PyQt4 >= 4.7, found %s"%QtCore.PYQT_VERSION_STR) | |
54 |
|
68 | |||
55 | # Alias PyQt-specific functions for PySide compatibility. |
|
69 | # Alias PyQt-specific functions for PySide compatibility. | |
56 | QtCore.Signal = QtCore.pyqtSignal |
|
70 | QtCore.Signal = QtCore.pyqtSignal | |
57 | QtCore.Slot = QtCore.pyqtSlot |
|
71 | QtCore.Slot = QtCore.pyqtSlot | |
58 |
|
72 | |||
59 | elif QT_API == QT_API_PYSIDE: |
|
73 | elif QT_API == QT_API_PYSIDE: | |
60 | import PySide |
|
74 | import PySide | |
61 | if PySide.__version__ < '1.0.3': |
|
75 | if PySide.__version__ < '1.0.3': | |
62 | raise ImportError("IPython requires PySide >= 1.0.3, found %s"%PySide.__version__) |
|
76 | raise ImportError("IPython requires PySide >= 1.0.3, found %s"%PySide.__version__) | |
63 | from PySide import QtCore, QtGui, QtSvg |
|
77 | from PySide import QtCore, QtGui, QtSvg | |
64 |
|
78 | |||
65 | else: |
|
79 | else: | |
66 | raise RuntimeError('Invalid Qt API %r, valid values are: %r or %r' % |
|
80 | raise RuntimeError('Invalid Qt API %r, valid values are: %r or %r' % | |
67 | (QT_API, QT_API_PYQT, QT_API_PYSIDE)) |
|
81 | (QT_API, QT_API_PYQT, QT_API_PYSIDE)) |
General Comments 0
You need to be logged in to leave comments.
Login now