##// END OF EJS Templates
Fixed PySide incompatibility with QVariant....
epatters -
Show More
@@ -11,10 +11,12 b" QT_API_PYSIDE = 'pyside'"
11 QT_API = os.environ.get('QT_API', QT_API_PYQT)
11 QT_API = os.environ.get('QT_API', QT_API_PYQT)
12
12
13 if QT_API == QT_API_PYQT:
13 if QT_API == QT_API_PYQT:
14 # For PySide compatibility, use the new string API that automatically
14 # For PySide compatibility, use the new-style string API that automatically
15 # converts QStrings to Unicode Python strings.
15 # converts QStrings to Unicode Python strings. Also, automatically unpack
16 # QVariants to their underlying objects.
16 import sip
17 import sip
17 sip.setapi('QString', 2)
18 sip.setapi('QString', 2)
19 sip.setapi('QVariant', 2)
18
20
19 from PyQt4 import QtCore, QtGui, QtSvg
21 from PyQt4 import QtCore, QtGui, QtSvg
20
22
@@ -19,7 +19,6 b' class RichIPythonWidget(IPythonWidget):'
19
19
20 # RichIPythonWidget protected class variables.
20 # RichIPythonWidget protected class variables.
21 _payload_source_plot = 'IPython.zmq.pylab.backend_payload.add_plot_payload'
21 _payload_source_plot = 'IPython.zmq.pylab.backend_payload.add_plot_payload'
22 _svg_text_format_property = 1
23
22
24 #---------------------------------------------------------------------------
23 #---------------------------------------------------------------------------
25 # 'object' interface
24 # 'object' interface
@@ -34,9 +33,8 b' class RichIPythonWidget(IPythonWidget):'
34 # Configure the ConsoleWidget HTML exporter for our formats.
33 # Configure the ConsoleWidget HTML exporter for our formats.
35 self._html_exporter.image_tag = self._get_image_tag
34 self._html_exporter.image_tag = self._get_image_tag
36
35
37 # Dictionary for resolving Qt names to images when generating XHTML
36 # Dictionary for resolving document resource names to SVG data.
38 # output
37 self._name_to_svg_map = {}
39 self._name_to_svg = {}
40
38
41 #---------------------------------------------------------------------------
39 #---------------------------------------------------------------------------
42 # 'ConsoleWidget' protected interface
40 # 'ConsoleWidget' protected interface
@@ -54,8 +52,8 b' class RichIPythonWidget(IPythonWidget):'
54 menu.addAction('Save Image As...', lambda: self._save_image(name))
52 menu.addAction('Save Image As...', lambda: self._save_image(name))
55 menu.addSeparator()
53 menu.addSeparator()
56
54
57 svg = format.stringProperty(self._svg_text_format_property)
55 svg = self._name_to_svg_map.get(name, None)
58 if svg:
56 if svg is not None:
59 menu.addSeparator()
57 menu.addSeparator()
60 menu.addAction('Copy SVG', lambda: svg_to_clipboard(svg))
58 menu.addAction('Copy SVG', lambda: svg_to_clipboard(svg))
61 menu.addAction('Save SVG As...',
59 menu.addAction('Save SVG As...',
@@ -118,26 +116,6 b' class RichIPythonWidget(IPythonWidget):'
118 return super(RichIPythonWidget, self)._handle_display_data(msg)
116 return super(RichIPythonWidget, self)._handle_display_data(msg)
119
117
120 #---------------------------------------------------------------------------
118 #---------------------------------------------------------------------------
121 # 'FrontendWidget' protected interface
122 #---------------------------------------------------------------------------
123
124 def _process_execute_payload(self, item):
125 """ Reimplemented to handle matplotlib plot payloads.
126 """
127 # TODO: remove this as all plot data is coming back through the
128 # display_data message type.
129 if item['source'] == self._payload_source_plot:
130 if item['format'] == 'svg':
131 svg = item['data']
132 self._append_svg(svg)
133 return True
134 else:
135 # Add other plot formats here!
136 return False
137 else:
138 return super(RichIPythonWidget, self)._process_execute_payload(item)
139
140 #---------------------------------------------------------------------------
141 # 'RichIPythonWidget' protected interface
119 # 'RichIPythonWidget' protected interface
142 #---------------------------------------------------------------------------
120 #---------------------------------------------------------------------------
143
121
@@ -150,8 +128,7 b' class RichIPythonWidget(IPythonWidget):'
150 self._append_plain_text('Received invalid plot data.')
128 self._append_plain_text('Received invalid plot data.')
151 else:
129 else:
152 format = self._add_image(image)
130 format = self._add_image(image)
153 self._name_to_svg[str(format.name())] = svg
131 self._name_to_svg_map[format.name()] = svg
154 format.setProperty(self._svg_text_format_property, svg)
155 cursor = self._get_end_cursor()
132 cursor = self._get_end_cursor()
156 cursor.insertBlock()
133 cursor.insertBlock()
157 cursor.insertImage(format)
134 cursor.insertImage(format)
@@ -194,9 +171,9 b' class RichIPythonWidget(IPythonWidget):'
194 """ Returns the QImage stored as the ImageResource with 'name'.
171 """ Returns the QImage stored as the ImageResource with 'name'.
195 """
172 """
196 document = self._control.document()
173 document = self._control.document()
197 variant = document.resource(QtGui.QTextDocument.ImageResource,
174 image = document.resource(QtGui.QTextDocument.ImageResource,
198 QtCore.QUrl(name))
175 QtCore.QUrl(name))
199 return variant.toPyObject()
176 return image
200
177
201 def _get_image_tag(self, match, path = None, format = "png"):
178 def _get_image_tag(self, match, path = None, format = "png"):
202 """ Return (X)HTML mark-up for the image-tag given by match.
179 """ Return (X)HTML mark-up for the image-tag given by match.
@@ -242,7 +219,7 b' class RichIPythonWidget(IPythonWidget):'
242
219
243 elif format == "svg":
220 elif format == "svg":
244 try:
221 try:
245 svg = str(self._name_to_svg[match.group("name")])
222 svg = str(self._name_to_svg_map[match.group("name")])
246 except KeyError:
223 except KeyError:
247 return "<b>Couldn't find image %s</b>" % match.group("name")
224 return "<b>Couldn't find image %s</b>" % match.group("name")
248
225
General Comments 0
You need to be logged in to leave comments. Login now