Show More
@@ -1,82 +1,87 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 `pylab=qt` initialization. |
|
3 | This is the import used for the `gui=qt` or `pylab=qt` initialization. | |
4 |
|
4 | |||
5 | Import Priority: |
|
5 | Import Priority: | |
6 |
|
6 | |||
7 | if matplotlib has been imported and doesn't support v2 (<= 1.0.1): |
|
7 | if matplotlib has been imported and doesn't support v2 (<= 1.0.1): | |
8 | use PyQt4 @v1 |
|
8 | use PyQt4 @v1 | |
9 |
|
9 | |||
10 | Next, ask ETS' QT_API env variable |
|
10 | Next, ask ETS' QT_API env variable | |
11 |
|
11 | |||
12 | if QT_API not set: |
|
12 | if QT_API not set: | |
13 | ask matplotlib via rcParams['backend.qt4'] |
|
13 | ask matplotlib via rcParams['backend.qt4'] | |
14 | if it said PyQt: |
|
14 | if it said PyQt: | |
15 | use PyQt4 @v1 |
|
15 | use PyQt4 @v1 | |
16 | elif it said PySide: |
|
16 | elif it said PySide: | |
17 | use PySide |
|
17 | use PySide | |
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: |
|
21 | try: | |
22 | PyQt @v1 |
|
22 | PyQt @v1 | |
23 | except: |
|
23 | except: | |
24 | fallback on PySide |
|
24 | fallback on PySide | |
25 | else: |
|
25 | else: | |
26 | use PyQt @v2 or PySide, depending on QT_API |
|
26 | use PyQt @v2 or PySide, depending on QT_API | |
27 | because ETS doesn't work with PyQt @v1. |
|
27 | because ETS doesn't work with PyQt @v1. | |
28 |
|
28 | |||
29 | """ |
|
29 | """ | |
30 |
|
30 | |||
31 | import os |
|
31 | import os | |
32 | import sys |
|
32 | import sys | |
33 |
|
33 | |||
|
34 | from IPython.utils.warn import warn | |||
|
35 | ||||
34 | matplotlib = sys.modules.get('matplotlib') |
|
36 | matplotlib = sys.modules.get('matplotlib') | |
35 | if matplotlib and matplotlib.__version__ <= '1.0.1': |
|
37 | if matplotlib and matplotlib.__version__ <= '1.0.1': | |
36 | # 1.0.1 doesn't support pyside or v2, so stick with PyQt @v1, |
|
38 | # 1.0.1 doesn't support pyside or v2, so stick with PyQt @v1, | |
37 | # and ignore everything else |
|
39 | # and ignore everything else | |
38 | from PyQt4 import QtCore, QtGui |
|
40 | from PyQt4 import QtCore, QtGui | |
39 | else: |
|
41 | else: | |
40 | # ask QT_API ETS variable *first* |
|
42 | # ask QT_API ETS variable *first* | |
41 | QT_API = os.environ.get('QT_API', None) |
|
43 | QT_API = os.environ.get('QT_API', None) | |
42 | if QT_API is None: |
|
44 | if QT_API is None: | |
43 | # QT_API not set, ask matplotlib if it was imported (e.g. `pylab=qt`) |
|
45 | # QT_API not set, ask matplotlib if it was imported (e.g. `pylab=qt`) | |
44 | if matplotlib: |
|
46 | if matplotlib: | |
45 | mpqt = matplotlib.rcParams.get('backend.qt4', None) |
|
47 | mpqt = matplotlib.rcParams.get('backend.qt4', None) | |
46 | else: |
|
48 | else: | |
47 | mpqt = None |
|
49 | mpqt = None | |
48 | if mpqt is None: |
|
50 | if mpqt is None: | |
49 | # matplotlib not imported or had nothing to say. |
|
51 | # matplotlib not imported or had nothing to say. | |
50 | try: |
|
52 | try: | |
51 | # default to unconfigured PyQt4 |
|
53 | # default to unconfigured PyQt4 | |
52 | from PyQt4 import QtCore, QtGui |
|
54 | from PyQt4 import QtCore, QtGui | |
53 | except ImportError: |
|
55 | except ImportError: | |
54 | # fallback on PySide |
|
56 | # fallback on PySide | |
55 | try: |
|
57 | try: | |
56 | from PySide import QtCore, QtGui |
|
58 | from PySide import QtCore, QtGui | |
57 | except ImportError: |
|
59 | except ImportError: | |
58 | raise ImportError('Cannot import PySide or PyQt4') |
|
60 | raise ImportError('Cannot import PySide or PyQt4') | |
59 | elif mpqt.lower() == 'pyqt4': |
|
61 | elif mpqt.lower() == 'pyqt4': | |
60 | # import PyQt4 unconfigured |
|
62 | # import PyQt4 unconfigured | |
61 | from PyQt4 import QtCore, QtGui |
|
63 | from PyQt4 import QtCore, QtGui | |
62 | elif mpqt.lower() == 'pyside': |
|
64 | elif mpqt.lower() == 'pyside': | |
63 | from PySide import QtCore, QtGui |
|
65 | from PySide import QtCore, QtGui | |
64 | else: |
|
66 | else: | |
65 | raise ImportError("unhandled value for backend.qt4 from matplotlib: %r"%mpqt) |
|
67 | raise ImportError("unhandled value for backend.qt4 from matplotlib: %r"%mpqt) | |
66 | else: |
|
68 | else: | |
67 | # QT_API specified, use PySide or PyQt+v2 API from external.qt |
|
69 | # QT_API specified, use PySide or PyQt+v2 API from external.qt | |
68 | # this means ETS is likely to be used, which requires v2 |
|
70 | # this means ETS is likely to be used, which requires v2 | |
69 | try: |
|
71 | try: | |
70 | from IPython.external.qt import QtCore, QtGui |
|
72 | from IPython.external.qt import QtCore, QtGui | |
71 | except ValueError as e: |
|
73 | except ValueError as e: | |
72 | if 'API' in str(e): |
|
74 | if 'API' in str(e): | |
73 | # API mismatch, give more meaningful message |
|
75 | # PyQt4 already imported, and APIv2 couldn't be set | |
74 | raise ImportError(""" |
|
76 | # Give more meaningful message, and warn instead of raising | |
|
77 | warn(""" | |||
75 | Assigning the ETS variable `QT_API=pyqt` implies PyQt's v2 API for |
|
78 | Assigning the ETS variable `QT_API=pyqt` implies PyQt's v2 API for | |
76 | QString and QVariant, but PyQt has already been imported |
|
79 | QString and QVariant, but PyQt has already been imported | |
77 |
with v1 APIs. You |
|
80 | with v1 APIs. You should unset QT_API to work with PyQt4 | |
78 | in its default mode. |
|
81 | in its default mode. | |
79 | """) |
|
82 | """) | |
|
83 | # allow it to still work | |||
|
84 | from PyQt4 import QtCore, QtGui | |||
80 | else: |
|
85 | else: | |
81 | raise |
|
86 | raise | |
82 |
|
87 |
General Comments 0
You need to be logged in to leave comments.
Login now