Show More
@@ -1,136 +1,137 b'' | |||||
1 | """ Import Qt in a manner suitable for an IPython kernel. |
|
1 | """ Import Qt in a manner suitable for an IPython kernel. | |
2 |
|
2 | |||
3 | This is the import used for the `gui=qt` or `matplotlib=qt` initialization. |
|
3 | This is the import used for the `gui=qt` or `matplotlib=qt` initialization. | |
4 |
|
4 | |||
5 | Import Priority: |
|
5 | Import Priority: | |
6 |
|
6 | |||
7 | if Qt has been imported anywhere else: |
|
7 | if Qt has been imported anywhere else: | |
8 | use that |
|
8 | use that | |
9 |
|
9 | |||
10 | if matplotlib has been imported and doesn't support v2 (<= 1.0.1): |
|
10 | if matplotlib has been imported and doesn't support v2 (<= 1.0.1): | |
11 | use PyQt4 @v1 |
|
11 | use PyQt4 @v1 | |
12 |
|
12 | |||
13 | Next, ask QT_API env variable |
|
13 | Next, ask QT_API env variable | |
14 |
|
14 | |||
15 | if QT_API not set: |
|
15 | if QT_API not set: | |
16 | ask matplotlib what it's using. If Qt4Agg or Qt5Agg, then use the |
|
16 | ask matplotlib what it's using. If Qt4Agg or Qt5Agg, then use the | |
17 | version matplotlib is configured with |
|
17 | version matplotlib is configured with | |
18 |
|
18 | |||
19 | else: (matplotlib said nothing) |
|
19 | else: (matplotlib said nothing) | |
20 | # this is the default path - nobody told us anything |
|
20 | # this is the default path - nobody told us anything | |
21 | try in this order: |
|
21 | try in this order: | |
22 | PyQt default version, PySide, PyQt5 |
|
22 | PyQt default version, PySide, PyQt5 | |
23 | else: |
|
23 | else: | |
24 | use what QT_API says |
|
24 | use what QT_API says | |
25 |
|
25 | |||
26 | Note that %gui's implementation will always set a `QT_API`, see |
|
26 | Note that %gui's implementation will always set a `QT_API`, see | |
27 | `IPython.terminal.pt_inputhooks.get_inputhook_name_and_func` |
|
27 | `IPython.terminal.pt_inputhooks.get_inputhook_name_and_func` | |
28 |
|
28 | |||
29 | """ |
|
29 | """ | |
30 | # NOTE: This is no longer an external, third-party module, and should be |
|
30 | # NOTE: This is no longer an external, third-party module, and should be | |
31 | # considered part of IPython. For compatibility however, it is being kept in |
|
31 | # considered part of IPython. For compatibility however, it is being kept in | |
32 | # IPython/external. |
|
32 | # IPython/external. | |
33 |
|
33 | |||
34 | import os |
|
34 | import os | |
35 | import sys |
|
35 | import sys | |
36 |
|
36 | |||
37 | from IPython.external.qt_loaders import ( |
|
37 | from IPython.external.qt_loaders import ( | |
38 | load_qt, |
|
38 | load_qt, | |
39 | loaded_api, |
|
39 | loaded_api, | |
40 | enum_factory, |
|
40 | enum_factory, | |
41 | # QT6 |
|
41 | # QT6 | |
42 | QT_API_PYQT6, |
|
42 | QT_API_PYQT6, | |
43 | QT_API_PYSIDE6, |
|
43 | QT_API_PYSIDE6, | |
44 | # QT5 |
|
44 | # QT5 | |
45 | QT_API_PYQT5, |
|
45 | QT_API_PYQT5, | |
46 | QT_API_PYSIDE2, |
|
46 | QT_API_PYSIDE2, | |
47 | # QT4 |
|
47 | # QT4 | |
48 | QT_API_PYQTv1, |
|
48 | QT_API_PYQTv1, | |
49 | QT_API_PYQT, |
|
49 | QT_API_PYQT, | |
50 | QT_API_PYSIDE, |
|
50 | QT_API_PYSIDE, | |
51 | # default |
|
51 | # default | |
52 | QT_API_PYQT_DEFAULT, |
|
52 | QT_API_PYQT_DEFAULT, | |
53 | ) |
|
53 | ) | |
54 |
|
54 | |||
55 | _qt_apis = ( |
|
55 | _qt_apis = ( | |
56 | # QT6 |
|
56 | # QT6 | |
57 | QT_API_PYQT6, |
|
57 | QT_API_PYQT6, | |
58 | QT_API_PYSIDE6, |
|
58 | QT_API_PYSIDE6, | |
59 | # QT5 |
|
59 | # QT5 | |
60 | QT_API_PYQT5, |
|
60 | QT_API_PYQT5, | |
61 | QT_API_PYSIDE2, |
|
61 | QT_API_PYSIDE2, | |
62 | # QT4 |
|
62 | # QT4 | |
63 | QT_API_PYQTv1, |
|
63 | QT_API_PYQTv1, | |
64 | QT_API_PYQT, |
|
64 | QT_API_PYQT, | |
65 | QT_API_PYSIDE, |
|
65 | QT_API_PYSIDE, | |
66 | # default |
|
66 | # default | |
67 | QT_API_PYQT_DEFAULT, |
|
67 | QT_API_PYQT_DEFAULT, | |
68 | ) |
|
68 | ) | |
69 |
|
69 | |||
70 |
|
70 | |||
71 | def matplotlib_options(mpl): |
|
71 | def matplotlib_options(mpl): | |
72 | """Constraints placed on an imported matplotlib.""" |
|
72 | """Constraints placed on an imported matplotlib.""" | |
73 | if mpl is None: |
|
73 | if mpl is None: | |
74 | return |
|
74 | return | |
75 | backend = mpl.rcParams.get('backend', None) |
|
75 | backend = mpl.rcParams.get('backend', None) | |
76 | if backend == 'Qt4Agg': |
|
76 | if backend == 'Qt4Agg': | |
77 | mpqt = mpl.rcParams.get('backend.qt4', None) |
|
77 | mpqt = mpl.rcParams.get('backend.qt4', None) | |
78 | if mpqt is None: |
|
78 | if mpqt is None: | |
79 | return None |
|
79 | return None | |
80 | if mpqt.lower() == 'pyside': |
|
80 | if mpqt.lower() == 'pyside': | |
81 | return [QT_API_PYSIDE] |
|
81 | return [QT_API_PYSIDE] | |
82 | elif mpqt.lower() == 'pyqt4': |
|
82 | elif mpqt.lower() == 'pyqt4': | |
83 | return [QT_API_PYQT_DEFAULT] |
|
83 | return [QT_API_PYQT_DEFAULT] | |
84 | elif mpqt.lower() == 'pyqt4v2': |
|
84 | elif mpqt.lower() == 'pyqt4v2': | |
85 | return [QT_API_PYQT] |
|
85 | return [QT_API_PYQT] | |
86 | raise ImportError("unhandled value for backend.qt4 from matplotlib: %r" % |
|
86 | raise ImportError("unhandled value for backend.qt4 from matplotlib: %r" % | |
87 | mpqt) |
|
87 | mpqt) | |
88 | elif backend == 'Qt5Agg': |
|
88 | elif backend == 'Qt5Agg': | |
89 | mpqt = mpl.rcParams.get('backend.qt5', None) |
|
89 | mpqt = mpl.rcParams.get('backend.qt5', None) | |
90 | if mpqt is None: |
|
90 | if mpqt is None: | |
91 | return None |
|
91 | return None | |
92 | if mpqt.lower() == 'pyqt5': |
|
92 | if mpqt.lower() == 'pyqt5': | |
93 | return [QT_API_PYQT5] |
|
93 | return [QT_API_PYQT5] | |
94 | raise ImportError("unhandled value for backend.qt5 from matplotlib: %r" % |
|
94 | raise ImportError("unhandled value for backend.qt5 from matplotlib: %r" % | |
95 | mpqt) |
|
95 | mpqt) | |
96 |
|
96 | |||
97 | def get_options(): |
|
97 | def get_options(): | |
98 | print(f'`get_options` called with {os.environ.get("QT_API", None)=}') |
|
98 | print(f'`get_options` called with {os.environ.get("QT_API", None)=}') | |
99 | """Return a list of acceptable QT APIs, in decreasing order of preference.""" |
|
99 | """Return a list of acceptable QT APIs, in decreasing order of preference.""" | |
100 | #already imported Qt somewhere. Use that |
|
100 | #already imported Qt somewhere. Use that | |
101 | loaded = loaded_api() |
|
101 | loaded = loaded_api() | |
102 | if loaded is not None: |
|
102 | if loaded is not None: | |
|
103 | print(f'`QtCore` already imported: {loaded=}') | |||
103 | return [loaded] |
|
104 | return [loaded] | |
104 |
|
105 | |||
105 | mpl = sys.modules.get('matplotlib', None) |
|
106 | mpl = sys.modules.get('matplotlib', None) | |
106 | print(f'{mpl=}') # will be None of matplotlib has not yet been imported |
|
107 | print(f'{mpl=}') # will be None of matplotlib has not yet been imported | |
107 |
|
108 | |||
108 | if mpl is not None and tuple(mpl.__version__.split(".")) < ("1", "0", "2"): |
|
109 | if mpl is not None and tuple(mpl.__version__.split(".")) < ("1", "0", "2"): | |
109 | # 1.0.1 only supports PyQt4 v1 |
|
110 | # 1.0.1 only supports PyQt4 v1 | |
110 | return [QT_API_PYQT_DEFAULT] |
|
111 | return [QT_API_PYQT_DEFAULT] | |
111 |
|
112 | |||
112 | qt_api = os.environ.get('QT_API', None) |
|
113 | qt_api = os.environ.get('QT_API', None) | |
113 | if qt_api is None: |
|
114 | if qt_api is None: | |
114 | #no ETS variable. Ask mpl, then use default fallback path |
|
115 | #no ETS variable. Ask mpl, then use default fallback path | |
115 | return matplotlib_options(mpl) or [ |
|
116 | return matplotlib_options(mpl) or [ | |
116 | QT_API_PYQT_DEFAULT, |
|
117 | QT_API_PYQT_DEFAULT, | |
117 | QT_API_PYQT6, |
|
118 | QT_API_PYQT6, | |
118 | QT_API_PYSIDE6, |
|
119 | QT_API_PYSIDE6, | |
119 | QT_API_PYQT5, |
|
120 | QT_API_PYQT5, | |
120 | QT_API_PYSIDE2, |
|
121 | QT_API_PYSIDE2, | |
121 | QT_API_PYQT, |
|
122 | QT_API_PYQT, | |
122 | QT_API_PYSIDE, |
|
123 | QT_API_PYSIDE, | |
123 | ] |
|
124 | ] | |
124 | elif qt_api not in _qt_apis: |
|
125 | elif qt_api not in _qt_apis: | |
125 | raise RuntimeError("Invalid Qt API %r, valid values are: %r" % |
|
126 | raise RuntimeError("Invalid Qt API %r, valid values are: %r" % | |
126 | (qt_api, ', '.join(_qt_apis))) |
|
127 | (qt_api, ', '.join(_qt_apis))) | |
127 | else: |
|
128 | else: | |
128 | print(f'{qt_api=}') |
|
129 | print(f'{qt_api=}') | |
129 | return [qt_api] |
|
130 | return [qt_api] | |
130 |
|
131 | |||
131 |
|
132 | |||
132 | api_opts = get_options() |
|
133 | api_opts = get_options() | |
133 | print(f'Importing `IPython.terminal.pt_inputhooks.qt` with {api_opts=}') |
|
134 | print(f'Importing `IPython.terminal.pt_inputhooks.qt` with {api_opts=}') | |
134 | QtCore, QtGui, QtSvg, QT_API = load_qt(api_opts) |
|
135 | QtCore, QtGui, QtSvg, QT_API = load_qt(api_opts) | |
135 | print(f'Loaded Qt with {QT_API=}') |
|
136 | print(f'Loaded Qt with {QT_API=}') | |
136 | enum_helper = enum_factory(QT_API, QtCore) |
|
137 | enum_helper = enum_factory(QT_API, QtCore) |
General Comments 0
You need to be logged in to leave comments.
Login now