-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #560 'Reorder qt support in kernel'
closes gh-560
- Loading branch information
Showing
3 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,82 @@ | ||
""" Import Qt in a manner suitable for an IPython kernel. | ||
This is the import used for the `gui=qt` or `pylab=qt` initialization. | ||
Import Priority: | ||
if matplotlib has been imported and doesn't support v2 (<= 1.0.1): | ||
use PyQt4 @v1 | ||
Next, ask ETS' QT_API env variable | ||
if QT_API not set: | ||
ask matplotlib via rcParams['backend.qt4'] | ||
if it said PyQt: | ||
use PyQt4 @v1 | ||
elif it said PySide: | ||
use PySide | ||
else: (matplotlib said nothing) | ||
# this is the default path - nobody told us anything | ||
try: | ||
PyQt @v1 | ||
except: | ||
fallback on PySide | ||
else: | ||
use PyQt @v2 or PySide, depending on QT_API | ||
because ETS doesn't work with PyQt @v1. | ||
""" | ||
|
||
import os | ||
import sys | ||
|
||
# Older versions of matplotlib do not support PyQt4 v2 APIs or PySide, so we | ||
# cannot go through the preferred mechanism. | ||
matplotlib = sys.modules.get('matplotlib') | ||
if matplotlib and matplotlib.__version__ <= '1.0.1': | ||
# 1.0.1 doesn't support pyside or v2, so stick with PyQt @v1, | ||
# and ignore everything else | ||
from PyQt4 import QtCore, QtGui | ||
else: | ||
from IPython.external.qt import QtCore, QtGui | ||
# ask QT_API ETS variable *first* | ||
QT_API = os.environ.get('QT_API', None) | ||
if QT_API is None: | ||
# QT_API not set, ask matplotlib if it was imported (e.g. `pylab=qt`) | ||
if matplotlib: | ||
mpqt = matplotlib.rcParams.get('backend.qt4', None) | ||
else: | ||
mpqt = None | ||
if mpqt is None: | ||
# matplotlib not imported or had nothing to say. | ||
try: | ||
# default to unconfigured PyQt4 | ||
from PyQt4 import QtCore, QtGui | ||
except ImportError: | ||
# fallback on PySide | ||
try: | ||
from PySide import QtCore, QtGui | ||
except ImportError: | ||
raise ImportError('Cannot import PySide or PyQt4') | ||
elif mpqt.lower() == 'pyqt4': | ||
# import PyQt4 unconfigured | ||
from PyQt4 import QtCore, QtGui | ||
elif mpqt.lower() == 'pyside': | ||
from PySide import QtCore, QtGui | ||
else: | ||
raise ImportError("unhandled value for backend.qt4 from matplotlib: %r"%mpqt) | ||
else: | ||
# QT_API specified, use PySide or PyQt+v2 API from external.qt | ||
# this means ETS is likely to be used, which requires v2 | ||
try: | ||
from IPython.external.qt import QtCore, QtGui | ||
except ValueError as e: | ||
if 'API' in str(e): | ||
# API mismatch, give more meaningful message | ||
raise ImportError(""" | ||
Assigning the ETS variable `QT_API=pyqt` implies PyQt's v2 API for | ||
QString and QVariant, but PyQt has already been imported | ||
with v1 APIs. You must unset QT_API to work with PyQt4 | ||
in its default mode. | ||
""") | ||
else: | ||
raise | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters