From 272e128de36aeb3d0ddfbf0c10f27ef6c6d3f3f6 2011-07-02 01:31:00 From: epatters Date: 2011-07-02 01:31:00 Subject: [PATCH] Support v2 PyQt4 APIs and PySide in kernel's GUI support. Closes gh-8. --- diff --git a/IPython/external/qt_for_kernel.py b/IPython/external/qt_for_kernel.py new file mode 100644 index 0000000..573359e --- /dev/null +++ b/IPython/external/qt_for_kernel.py @@ -0,0 +1,12 @@ +""" Import Qt in a manner suitable for an IPython kernel. +""" + +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': + from PyQt4 import QtCore, QtGui +else: + from IPython.external.qt import QtCore, QtGui diff --git a/IPython/lib/guisupport.py b/IPython/lib/guisupport.py index e16645b..1b9519b 100644 --- a/IPython/lib/guisupport.py +++ b/IPython/lib/guisupport.py @@ -109,7 +109,7 @@ def start_event_loop_wx(app=None): def get_app_qt4(*args, **kwargs): """Create a new qt4 app or return an existing one.""" - from PyQt4 import QtGui + from IPython.external.qt_for_kernel import QtGui app = QtGui.QApplication.instance() if app is None: if not args: diff --git a/IPython/lib/inputhook.py b/IPython/lib/inputhook.py index bb619cb..edd633f 100755 --- a/IPython/lib/inputhook.py +++ b/IPython/lib/inputhook.py @@ -181,7 +181,8 @@ class InputHookManager(object): But, we first check to see if an application has already been created. If so, we simply return that instance. """ - from PyQt4 import QtCore + from IPython.external.qt_for_kernel import QtCore, QtGui + # PyQt4 has had this since 4.3.1. In version 4.2, PyOS_InputHook # was set when QtCore was imported, but if it ever got removed, # you couldn't reset it. For earlier versions we can @@ -190,8 +191,8 @@ class InputHookManager(object): QtCore.pyqtRestoreInputHook() except AttributeError: pass + self._current_gui = GUI_QT4 - from PyQt4 import QtGui app = QtCore.QCoreApplication.instance() if app is None: app = QtGui.QApplication([" "]) diff --git a/IPython/zmq/ipkernel.py b/IPython/zmq/ipkernel.py index d628f44..082ec3b 100755 --- a/IPython/zmq/ipkernel.py +++ b/IPython/zmq/ipkernel.py @@ -465,7 +465,7 @@ class QtKernel(Kernel): def start(self): """Start a kernel with QtPy4 event loop integration.""" - from PyQt4 import QtCore + from IPython.external.qt_for_kernel import QtCore from IPython.lib.guisupport import get_app_qt4, start_event_loop_qt4 self.app = get_app_qt4([" "])