Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5f287e1

Browse files
committed
qt4_compat.py: updated to match ipython 0.11 RC 1.
It is based on IPython/external/qt.py. The previous version was based on an earlier version of that file, but fails with the present ipython: http://www.mail-archive.com/[email protected]/msg08332.html
1 parent 1ae1b22 commit 5f287e1

1 file changed

Lines changed: 45 additions & 36 deletions

File tree

lib/matplotlib/backends/qt4_compat.py

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,69 @@
22
"""
33

44
import os
5+
import warnings
56

67
# Available APIs.
78
QT_API_PYQT = 'pyqt'
89
QT_API_PYSIDE = 'pyside'
910

10-
# Select PyQt4 or PySide using an environment variable, in the same way IPython does.
11-
# IPython is using PyQt as default (for now) so we will too.
12-
QT_API = os.environ.get('QT_API', QT_API_PYQT)
11+
def prepare_pyqt4():
12+
# For PySide compatibility, use the new-style string API that automatically
13+
# converts QStrings to Unicode Python strings. Also, automatically unpack
14+
# QVariants to their underlying objects.
15+
import sip
16+
sip.setapi('QString', 2)
17+
sip.setapi('QVariant', 2)
1318

14-
if QT_API == QT_API_PYQT:
19+
# Select Qt binding, using the QT_API environment variable if available.
20+
QT_API = os.environ.get('QT_API')
21+
if QT_API is None:
1522
try:
16-
from PyQt4 import QtCore, QtGui
23+
import PySide
24+
if PySide.__version_info__ < (1,0,3):
25+
warnings.warn("PySide found with version < 1.0.3; trying PyQt4")
26+
raise ImportError
27+
QT_API = QT_API_PYSIDE
1728
except ImportError:
18-
raise ImportError("Qt4 backend requires that PyQt4 is installed.")
29+
try:
30+
prepare_pyqt4()
31+
import PyQt4
32+
QT_API = QT_API_PYQT
33+
except ImportError:
34+
raise ImportError('Cannot import PySide or PyQt4')
35+
36+
elif QT_API == QT_API_PYQT:
37+
# Note: This must be called *before* PyQt4 is imported.
38+
prepare_pyqt4()
39+
40+
# Now perform the imports.
41+
if QT_API == QT_API_PYQT:
42+
from PyQt4 import QtCore, QtGui, QtSvg
43+
1944
# Alias PyQt-specific functions for PySide compatibility.
45+
QtCore.Signal = QtCore.pyqtSignal
2046
try:
2147
QtCore.Slot = QtCore.pyqtSlot
2248
except AttributeError:
23-
QtCore.Slot = pyqtSignature # Not a perfect match but
49+
QtCore.Slot = pyqtSignature # Not a perfect match but
2450
# works in simple cases
2551
QtCore.Property = QtCore.pyqtProperty
2652
__version__ = QtCore.PYQT_VERSION_STR
27-
import sip
28-
try :
29-
if sip.getapi("QString") > 1 :
30-
# Use new getSaveFileNameAndFilter()
31-
_getSaveFileName = lambda self, msg, start, filters, \
32-
selectedFilter : \
33-
QtGui.QFileDialog.getSaveFileNameAndFilter( \
34-
self, msg, start, filters, selectedFilter)[0]
35-
else :
36-
# Use old getSaveFileName()
37-
_getSaveFileName = QtGui.QFileDialog.getSaveFileName
38-
except (AttributeError, KeyError) :
39-
# call to getapi() can fail in older versions of sip
40-
# Use the old getSaveFileName()
41-
_getSaveFileName = QtGui.QFileDialog.getSaveFileName
53+
54+
# Use new getSaveFileNameAndFilter()
55+
_getSaveFileName = lambda self, msg, start, filters, \
56+
selectedFilter : \
57+
QtGui.QFileDialog.getSaveFileNameAndFilter( \
58+
self, msg, start, filters, selectedFilter)[0]
59+
60+
4261

4362
elif QT_API == QT_API_PYSIDE:
44-
try:
45-
from PySide import QtCore, QtGui, __version__, __version_info__
46-
except ImportError:
47-
raise ImportError("Qt4 backend requires that PySide is installed.")
63+
from PySide import QtCore, QtGui, __version__, __version_info__
4864
if __version_info__ < (1,0,3):
49-
raise ImportError("Matplotlib backend_qt4 and backend_qt4agg require PySide >=1.0.3")
50-
51-
# Alias PySide-specific function for PyQt compatibilty
52-
QtCore.pyqtProperty = QtCore.Property
53-
QtCore.pyqtSignature = QtCore.Slot # Not a perfect match but
54-
# works in simple cases
65+
raise ImportError(
66+
"Matplotlib backend_qt4 and backend_qt4agg require PySide >=1.0.3")
5567

56-
_getSaveFileName = lambda self, msg, start, filters, selectedFilter : \
57-
QtGui.QFileDialog.getSaveFileName(self, \
58-
msg, start, filters, selectedFilter)[0]
5968
else:
6069
raise RuntimeError('Invalid Qt API %r, valid values are: %r or %r' %
61-
(QT_API, QT_API_PYQT, QT_API_PYSIDE))
70+
(QT_API, QT_API_PYQT, QT_API_PYSIDE))

0 commit comments

Comments
 (0)