Show More
@@ -11,10 +11,12 QT_API_PYSIDE = 'pyside' | |||
|
11 | 11 | QT_API = os.environ.get('QT_API', QT_API_PYQT) |
|
12 | 12 | |
|
13 | 13 | if QT_API == QT_API_PYQT: |
|
14 | # For PySide compatibility, use the new string API that automatically | |
|
15 | # converts QStrings to Unicode Python strings. | |
|
14 | # For PySide compatibility, use the new-style string API that automatically | |
|
15 | # converts QStrings to Unicode Python strings. Also, automatically unpack | |
|
16 | # QVariants to their underlying objects. | |
|
16 | 17 | import sip |
|
17 | 18 | sip.setapi('QString', 2) |
|
19 | sip.setapi('QVariant', 2) | |
|
18 | 20 | |
|
19 | 21 | from PyQt4 import QtCore, QtGui, QtSvg |
|
20 | 22 |
@@ -19,7 +19,6 class RichIPythonWidget(IPythonWidget): | |||
|
19 | 19 | |
|
20 | 20 | # RichIPythonWidget protected class variables. |
|
21 | 21 | _payload_source_plot = 'IPython.zmq.pylab.backend_payload.add_plot_payload' |
|
22 | _svg_text_format_property = 1 | |
|
23 | 22 | |
|
24 | 23 | #--------------------------------------------------------------------------- |
|
25 | 24 | # 'object' interface |
@@ -34,9 +33,8 class RichIPythonWidget(IPythonWidget): | |||
|
34 | 33 | # Configure the ConsoleWidget HTML exporter for our formats. |
|
35 | 34 | self._html_exporter.image_tag = self._get_image_tag |
|
36 | 35 | |
|
37 |
# Dictionary for resolving |
|
|
38 | # output | |
|
39 | self._name_to_svg = {} | |
|
36 | # Dictionary for resolving document resource names to SVG data. | |
|
37 | self._name_to_svg_map = {} | |
|
40 | 38 | |
|
41 | 39 | #--------------------------------------------------------------------------- |
|
42 | 40 | # 'ConsoleWidget' protected interface |
@@ -54,8 +52,8 class RichIPythonWidget(IPythonWidget): | |||
|
54 | 52 | menu.addAction('Save Image As...', lambda: self._save_image(name)) |
|
55 | 53 | menu.addSeparator() |
|
56 | 54 | |
|
57 | svg = format.stringProperty(self._svg_text_format_property) | |
|
58 | if svg: | |
|
55 | svg = self._name_to_svg_map.get(name, None) | |
|
56 | if svg is not None: | |
|
59 | 57 | menu.addSeparator() |
|
60 | 58 | menu.addAction('Copy SVG', lambda: svg_to_clipboard(svg)) |
|
61 | 59 | menu.addAction('Save SVG As...', |
@@ -118,26 +116,6 class RichIPythonWidget(IPythonWidget): | |||
|
118 | 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 | 119 | # 'RichIPythonWidget' protected interface |
|
142 | 120 | #--------------------------------------------------------------------------- |
|
143 | 121 | |
@@ -150,8 +128,7 class RichIPythonWidget(IPythonWidget): | |||
|
150 | 128 | self._append_plain_text('Received invalid plot data.') |
|
151 | 129 | else: |
|
152 | 130 | format = self._add_image(image) |
|
153 |
self._name_to_svg[ |
|
|
154 | format.setProperty(self._svg_text_format_property, svg) | |
|
131 | self._name_to_svg_map[format.name()] = svg | |
|
155 | 132 | cursor = self._get_end_cursor() |
|
156 | 133 | cursor.insertBlock() |
|
157 | 134 | cursor.insertImage(format) |
@@ -194,9 +171,9 class RichIPythonWidget(IPythonWidget): | |||
|
194 | 171 | """ Returns the QImage stored as the ImageResource with 'name'. |
|
195 | 172 | """ |
|
196 | 173 | document = self._control.document() |
|
197 |
|
|
|
198 |
|
|
|
199 |
return |
|
|
174 | image = document.resource(QtGui.QTextDocument.ImageResource, | |
|
175 | QtCore.QUrl(name)) | |
|
176 | return image | |
|
200 | 177 | |
|
201 | 178 | def _get_image_tag(self, match, path = None, format = "png"): |
|
202 | 179 | """ Return (X)HTML mark-up for the image-tag given by match. |
@@ -242,7 +219,7 class RichIPythonWidget(IPythonWidget): | |||
|
242 | 219 | |
|
243 | 220 | elif format == "svg": |
|
244 | 221 | try: |
|
245 | svg = str(self._name_to_svg[match.group("name")]) | |
|
222 | svg = str(self._name_to_svg_map[match.group("name")]) | |
|
246 | 223 | except KeyError: |
|
247 | 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