##// END OF EJS Templates
Merge in qtconsole/qt.py API-loading logic....
Zachary Pincus -
Show More
@@ -1,83 +1,85 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 Qt4 has been imported anywhere else:
7 if Qt4 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 ETS' QT_API env variable
13 Next, ask ETS' QT_API env variable
14
14
15 if QT_API not set:
15 if QT_API not set:
16 ask matplotlib via rcParams['backend.qt4']
16 ask matplotlib via rcParams['backend.qt4']
17 if it said PyQt:
17 if it said PyQt:
18 use PyQt4 @v1
18 use PyQt4 @v1
19 elif it said PySide:
19 elif it said PySide:
20 use PySide
20 use PySide
21
21
22 else: (matplotlib said nothing)
22 else: (matplotlib said nothing)
23 # this is the default path - nobody told us anything
23 # this is the default path - nobody told us anything
24 try:
24 try:
25 PyQt @v1
25 PyQt @v1
26 except:
26 except:
27 fallback on PySide
27 fallback on PySide
28 else:
28 else:
29 use PyQt @v2 or PySide, depending on QT_API
29 use PyQt @v2 or PySide, depending on QT_API
30 because ETS doesn't work with PyQt @v1.
30 because ETS doesn't work with PyQt @v1.
31
31
32 """
32 """
33
33
34 import os
34 import os
35 import sys
35 import sys
36
36
37 from IPython.utils.version import check_version
37 from IPython.utils.version import check_version
38 from IPython.external.qt_loaders import (load_qt, QT_API_PYSIDE,
38 from IPython.external.qt_loaders import (load_qt, loaded_api, QT_API_PYSIDE,
39 QT_API_PYQT, QT_API_PYQT_DEFAULT,
39 QT_API_PYQT, QT_API_PYQT5,
40 loaded_api)
40 QT_API_PYQTv1, QT_API_PYQT_DEFAULT)
41
42 _qt_apis = (QT_API_PYSIDE, QT_API_PYQT, QT_API_PYQT5, QT_API_PYQTv1,
43 QT_API_PYQT_DEFAULT)
41
44
42 #Constraints placed on an imported matplotlib
45 #Constraints placed on an imported matplotlib
43 def matplotlib_options(mpl):
46 def matplotlib_options(mpl):
44 if mpl is None:
47 if mpl is None:
45 return
48 return
46 mpqt = mpl.rcParams.get('backend.qt4', None)
49 mpqt = mpl.rcParams.get('backend.qt4', None)
47 if mpqt is None:
50 if mpqt is None:
48 return None
51 return None
49 if mpqt.lower() == 'pyside':
52 if mpqt.lower() == 'pyside':
50 return [QT_API_PYSIDE]
53 return [QT_API_PYSIDE]
51 elif mpqt.lower() == 'pyqt4':
54 elif mpqt.lower() == 'pyqt4':
52 return [QT_API_PYQT_DEFAULT]
55 return [QT_API_PYQT_DEFAULT]
53 raise ImportError("unhandled value for backend.qt4 from matplotlib: %r" %
56 raise ImportError("unhandled value for backend.qt4 from matplotlib: %r" %
54 mpqt)
57 mpqt)
55
58
56 def get_options():
59 def get_options():
57 """Return a list of acceptable QT APIs, in decreasing order of
60 """Return a list of acceptable QT APIs, in decreasing order of
58 preference
61 preference
59 """
62 """
60 #already imported Qt somewhere. Use that
63 #already imported Qt somewhere. Use that
61 loaded = loaded_api()
64 loaded = loaded_api()
62 if loaded is not None:
65 if loaded is not None:
63 return [loaded]
66 return [loaded]
64
67
65 mpl = sys.modules.get('matplotlib', None)
68 mpl = sys.modules.get('matplotlib', None)
66
69
67 if mpl is not None and not check_version(mpl.__version__, '1.0.2'):
70 if mpl is not None and not check_version(mpl.__version__, '1.0.2'):
68 #1.0.1 only supports PyQt4 v1
71 #1.0.1 only supports PyQt4 v1
69 return [QT_API_PYQT_DEFAULT]
72 return [QT_API_PYQT_DEFAULT]
70
73
71 if os.environ.get('QT_API', None) is None:
74 qt_api = os.environ.get('QT_API', None)
75 if qt_api is None:
72 #no ETS variable. Ask mpl, then use either
76 #no ETS variable. Ask mpl, then use either
73 return matplotlib_options(mpl) or [QT_API_PYQT_DEFAULT, QT_API_PYSIDE]
77 return matplotlib_options(mpl) or [QT_API_PYQT_DEFAULT, QT_API_PYSIDE]
74
78 elif qt_api not in _qt_apis:
75 #ETS variable present. Will fallback to external.qt
79 raise RuntimeError("Invalid Qt API %r, valid values are: %r" %
76 return None
80 (qt_api, ' ,'.join(_qt_apis)))
81 else:
82 return [qt_api]
77
83
78 api_opts = get_options()
84 api_opts = get_options()
79 if api_opts is not None:
85 QtCore, QtGui, QtSvg, QT_API = load_qt(api_opts)
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
General Comments 0
You need to be logged in to leave comments. Login now