Show More
@@ -1,83 +1,91 b'' | |||
|
1 | 1 | """ Import Qt in a manner suitable for an IPython kernel. |
|
2 | 2 | |
|
3 | 3 | This is the import used for the `gui=qt` or `matplotlib=qt` initialization. |
|
4 | 4 | |
|
5 | 5 | Import Priority: |
|
6 | 6 | |
|
7 |
if Qt |
|
|
7 | if Qt has been imported anywhere else: | |
|
8 | 8 | use that |
|
9 | 9 | |
|
10 | 10 | if matplotlib has been imported and doesn't support v2 (<= 1.0.1): |
|
11 | 11 | use PyQt4 @v1 |
|
12 | 12 | |
|
13 |
Next, ask |
|
|
13 | Next, ask QT_API env variable | |
|
14 | 14 | |
|
15 | 15 | if QT_API not set: |
|
16 | 16 | ask matplotlib via rcParams['backend.qt4'] |
|
17 | 17 | if it said PyQt: |
|
18 | 18 | use PyQt4 @v1 |
|
19 | 19 | elif it said PySide: |
|
20 | 20 | use PySide |
|
21 | 21 | |
|
22 | 22 | else: (matplotlib said nothing) |
|
23 | 23 | # this is the default path - nobody told us anything |
|
24 | 24 | try: |
|
25 | 25 | PyQt @v1 |
|
26 | 26 | except: |
|
27 | 27 | fallback on PySide |
|
28 | 28 | else: |
|
29 | use PyQt @v2 or PySide, depending on QT_API | |
|
30 | because ETS doesn't work with PyQt @v1. | |
|
29 | use what QT_API says | |
|
31 | 30 | |
|
32 | 31 | """ |
|
32 | # NOTE: This is no longer an external, third-party module, and should be | |
|
33 | # considered part of IPython. For compatibility however, it is being kept in | |
|
34 | # IPython/external. | |
|
33 | 35 | |
|
34 | 36 | import os |
|
35 | 37 | import sys |
|
36 | 38 | |
|
37 | 39 | from IPython.utils.version import check_version |
|
38 | from IPython.external.qt_loaders import (load_qt, QT_API_PYSIDE, | |
|
39 |
QT_API_PYQT, QT_API_PYQT |
|
|
40 |
|
|
|
40 | from IPython.external.qt_loaders import (load_qt, loaded_api, QT_API_PYSIDE, | |
|
41 | QT_API_PYQT, QT_API_PYQT5, | |
|
42 | QT_API_PYQTv1, QT_API_PYQT_DEFAULT) | |
|
43 | ||
|
44 | _qt_apis = (QT_API_PYSIDE, QT_API_PYQT, QT_API_PYQT5, QT_API_PYQTv1, | |
|
45 | QT_API_PYQT_DEFAULT) | |
|
41 | 46 | |
|
42 | 47 | #Constraints placed on an imported matplotlib |
|
48 | # TODO: Make sure this logic is still in sync with matplotlib's requirements. | |
|
49 | # In particular, matplotlib can also now support a qt5 backend, and so this will | |
|
50 | # break if matplotlib is imported and running happily with qt5, because | |
|
51 | # it only queries for the preferred qt4 option. | |
|
43 | 52 | def matplotlib_options(mpl): |
|
44 | 53 | if mpl is None: |
|
45 | 54 | return |
|
46 | 55 | mpqt = mpl.rcParams.get('backend.qt4', None) |
|
47 | 56 | if mpqt is None: |
|
48 | 57 | return None |
|
49 | 58 | if mpqt.lower() == 'pyside': |
|
50 | 59 | return [QT_API_PYSIDE] |
|
51 | 60 | elif mpqt.lower() == 'pyqt4': |
|
52 | 61 | return [QT_API_PYQT_DEFAULT] |
|
53 | 62 | raise ImportError("unhandled value for backend.qt4 from matplotlib: %r" % |
|
54 | 63 | mpqt) |
|
55 | 64 | |
|
56 | 65 | def get_options(): |
|
57 | 66 | """Return a list of acceptable QT APIs, in decreasing order of |
|
58 | 67 | preference |
|
59 | 68 | """ |
|
60 | 69 | #already imported Qt somewhere. Use that |
|
61 | 70 | loaded = loaded_api() |
|
62 | 71 | if loaded is not None: |
|
63 | 72 | return [loaded] |
|
64 | 73 | |
|
65 | 74 | mpl = sys.modules.get('matplotlib', None) |
|
66 | 75 | |
|
67 | 76 | if mpl is not None and not check_version(mpl.__version__, '1.0.2'): |
|
68 | 77 | #1.0.1 only supports PyQt4 v1 |
|
69 | 78 | return [QT_API_PYQT_DEFAULT] |
|
70 | 79 | |
|
71 |
|
|
|
80 | qt_api = os.environ.get('QT_API', None) | |
|
81 | if qt_api is None: | |
|
72 | 82 | #no ETS variable. Ask mpl, then use either |
|
73 | 83 | return matplotlib_options(mpl) or [QT_API_PYQT_DEFAULT, QT_API_PYSIDE] |
|
74 | ||
|
75 | #ETS variable present. Will fallback to external.qt | |
|
76 | return None | |
|
84 | elif qt_api not in _qt_apis: | |
|
85 | raise RuntimeError("Invalid Qt API %r, valid values are: %r" % | |
|
86 | (qt_api, ', '.join(_qt_apis))) | |
|
87 | else: | |
|
88 | return [qt_api] | |
|
77 | 89 | |
|
78 | 90 | api_opts = get_options() |
|
79 | if api_opts is not None: | |
|
80 | QtCore, QtGui, QtSvg, QT_API = load_qt(api_opts) | |
|
81 | ||
|
82 | else: # use ETS variable | |
|
83 | from IPython.external.qt import QtCore, QtGui, QtSvg, QT_API | |
|
91 | QtCore, QtGui, QtSvg, QT_API = load_qt(api_opts) |
General Comments 0
You need to be logged in to leave comments.
Login now