##// END OF EJS Templates
respect image size metadata in qtconsole...
MinRK -
Show More
@@ -868,7 +868,7 b' class ConsoleWidget(LoggingConfigurable, QtGui.QWidget):'
868 # 'ConsoleWidget' protected interface
868 # 'ConsoleWidget' protected interface
869 #--------------------------------------------------------------------------
869 #--------------------------------------------------------------------------
870
870
871 def _append_custom(self, insert, input, before_prompt=False):
871 def _append_custom(self, insert, input, before_prompt=False, *args, **kwargs):
872 """ A low-level method for appending content to the end of the buffer.
872 """ A low-level method for appending content to the end of the buffer.
873
873
874 If 'before_prompt' is enabled, the content will be inserted before the
874 If 'before_prompt' is enabled, the content will be inserted before the
@@ -883,7 +883,7 b' class ConsoleWidget(LoggingConfigurable, QtGui.QWidget):'
883 start_pos = cursor.position()
883 start_pos = cursor.position()
884
884
885 # Perform the insertion.
885 # Perform the insertion.
886 result = insert(cursor, input)
886 result = insert(cursor, input, *args, **kwargs)
887
887
888 # Adjust the prompt position if we have inserted before it. This is safe
888 # Adjust the prompt position if we have inserted before it. This is safe
889 # because buffer truncation is disabled when not executing.
889 # because buffer truncation is disabled when not executing.
@@ -117,17 +117,20 b' class RichIPythonWidget(IPythonWidget):'
117 content = msg['content']
117 content = msg['content']
118 prompt_number = content.get('execution_count', 0)
118 prompt_number = content.get('execution_count', 0)
119 data = content['data']
119 data = content['data']
120 metadata = msg['content']['metadata']
120 if 'image/svg+xml' in data:
121 if 'image/svg+xml' in data:
121 self._pre_image_append(msg, prompt_number)
122 self._pre_image_append(msg, prompt_number)
122 self._append_svg(data['image/svg+xml'], True)
123 self._append_svg(data['image/svg+xml'], True)
123 self._append_html(self.output_sep2, True)
124 self._append_html(self.output_sep2, True)
124 elif 'image/png' in data:
125 elif 'image/png' in data:
125 self._pre_image_append(msg, prompt_number)
126 self._pre_image_append(msg, prompt_number)
126 self._append_png(decodestring(data['image/png'].encode('ascii')), True)
127 png = decodestring(data['image/png'].encode('ascii'))
128 self._append_png(png, True, metadata=metadata.get('image/png', None))
127 self._append_html(self.output_sep2, True)
129 self._append_html(self.output_sep2, True)
128 elif 'image/jpeg' in data and self._jpg_supported:
130 elif 'image/jpeg' in data and self._jpg_supported:
129 self._pre_image_append(msg, prompt_number)
131 self._pre_image_append(msg, prompt_number)
130 self._append_jpg(decodestring(data['image/jpeg'].encode('ascii')), True)
132 jpg = decodestring(data['image/jpeg'].encode('ascii'))
133 self._append_jpg(jpg, True, metadata=metadata.get('image/jpeg', None))
131 self._append_html(self.output_sep2, True)
134 self._append_html(self.output_sep2, True)
132 else:
135 else:
133 # Default back to the plain text representation.
136 # Default back to the plain text representation.
@@ -151,11 +154,11 b' class RichIPythonWidget(IPythonWidget):'
151 # PNG data is base64 encoded as it passes over the network
154 # PNG data is base64 encoded as it passes over the network
152 # in a JSON structure so we decode it.
155 # in a JSON structure so we decode it.
153 png = decodestring(data['image/png'].encode('ascii'))
156 png = decodestring(data['image/png'].encode('ascii'))
154 self._append_png(png, True)
157 self._append_png(png, True, metadata=metadata.get('image/png', None))
155 elif 'image/jpeg' in data and self._jpg_supported:
158 elif 'image/jpeg' in data and self._jpg_supported:
156 self.log.debug("display: %s", msg.get('content', ''))
159 self.log.debug("display: %s", msg.get('content', ''))
157 jpg = decodestring(data['image/jpeg'].encode('ascii'))
160 jpg = decodestring(data['image/jpeg'].encode('ascii'))
158 self._append_jpg(jpg, True)
161 self._append_jpg(jpg, True, metadata=metadata.get('image/jpeg', None))
159 else:
162 else:
160 # Default back to the plain text representation.
163 # Default back to the plain text representation.
161 return super(RichIPythonWidget, self)._handle_display_data(msg)
164 return super(RichIPythonWidget, self)._handle_display_data(msg)
@@ -164,14 +167,14 b' class RichIPythonWidget(IPythonWidget):'
164 # 'RichIPythonWidget' protected interface
167 # 'RichIPythonWidget' protected interface
165 #---------------------------------------------------------------------------
168 #---------------------------------------------------------------------------
166
169
167 def _append_jpg(self, jpg, before_prompt=False):
170 def _append_jpg(self, jpg, before_prompt=False, metadata=None):
168 """ Append raw JPG data to the widget."""
171 """ Append raw JPG data to the widget."""
169 self._append_custom(self._insert_jpg, jpg, before_prompt)
172 self._append_custom(self._insert_jpg, jpg, before_prompt, metadata=metadata)
170
173
171 def _append_png(self, png, before_prompt=False):
174 def _append_png(self, png, before_prompt=False, metadata=None):
172 """ Append raw PNG data to the widget.
175 """ Append raw PNG data to the widget.
173 """
176 """
174 self._append_custom(self._insert_png, png, before_prompt)
177 self._append_custom(self._insert_png, png, before_prompt, metadata=metadata)
175
178
176 def _append_svg(self, svg, before_prompt=False):
179 def _append_svg(self, svg, before_prompt=False):
177 """ Append raw SVG data to the widget.
180 """ Append raw SVG data to the widget.
@@ -276,20 +279,31 b' class RichIPythonWidget(IPythonWidget):'
276 else:
279 else:
277 return '<b>Unrecognized image format</b>'
280 return '<b>Unrecognized image format</b>'
278
281
279 def _insert_jpg(self, cursor, jpg):
282 def _insert_jpg(self, cursor, jpg, metadata=None):
280 """ Insert raw PNG data into the widget."""
283 """ Insert raw PNG data into the widget."""
281 self._insert_img(cursor, jpg, 'jpg')
284 self._insert_img(cursor, jpg, 'jpg', metadata=metadata)
282
285
283 def _insert_png(self, cursor, png):
286 def _insert_png(self, cursor, png, metadata=None):
284 """ Insert raw PNG data into the widget.
287 """ Insert raw PNG data into the widget.
285 """
288 """
286 self._insert_img(cursor, png, 'png')
289 self._insert_img(cursor, png, 'png', metadata=metadata)
287
290
288 def _insert_img(self, cursor, img, fmt):
291 def _insert_img(self, cursor, img, fmt, metadata=None):
289 """ insert a raw image, jpg or png """
292 """ insert a raw image, jpg or png """
293 if metadata:
294 width = metadata.get('width', None)
295 height = metadata.get('height', None)
296 else:
297 width = height = None
290 try:
298 try:
291 image = QtGui.QImage()
299 image = QtGui.QImage()
292 image.loadFromData(img, fmt.upper())
300 image.loadFromData(img, fmt.upper())
301 if width and height:
302 image = image.scaled(width, height)
303 elif width and not height:
304 image = image.scaledToWidth(width)
305 elif height and not width:
306 image = image.scaledToHeight(height)
293 except ValueError:
307 except ValueError:
294 self._insert_plain_text(cursor, 'Received invalid %s data.'%fmt)
308 self._insert_plain_text(cursor, 'Received invalid %s data.'%fmt)
295 else:
309 else:
General Comments 0
You need to be logged in to leave comments. Login now