##// END OF EJS Templates
* Fixed bug where ConsoleWidget accepted non-ASCII characters on paste (and also rich text in 'rich' mode)...
epatters -
Show More
@@ -113,6 +113,20 b' class ConsoleWidget(QtGui.QWidget):'
113 113 # 'ConsoleWidget' public interface
114 114 #---------------------------------------------------------------------------
115 115
116 def can_paste(self):
117 """ Returns whether text can be pasted from the clipboard.
118 """
119 # Accept only text that can be ASCII encoded.
120 if self._control.textInteractionFlags() & QtCore.Qt.TextEditable:
121 text = QtGui.QApplication.clipboard().text()
122 if not text.isEmpty():
123 try:
124 str(text)
125 return True
126 except UnicodeEncodeError:
127 pass
128 return False
129
116 130 def clear(self, keep_input=False):
117 131 """ Clear the console, then write a new prompt. If 'keep_input' is set,
118 132 restores the old input buffer when the new prompt is written.
@@ -262,8 +276,9 b' class ConsoleWidget(QtGui.QWidget):'
262 276 def paste(self):
263 277 """ Paste the contents of the clipboard into the input region.
264 278 """
265 self._keep_cursor_in_buffer()
266 self._control.paste()
279 if self.can_paste():
280 self._keep_cursor_in_buffer()
281 self._control.paste()
267 282
268 283 def print_(self, printer):
269 284 """ Print the contents of the ConsoleWidget to the specified QPrinter.
@@ -446,6 +461,7 b' class ConsoleWidget(QtGui.QWidget):'
446 461 control = QtGui.QPlainTextEdit()
447 462 elif kind == 'rich':
448 463 control = QtGui.QTextEdit()
464 control.setAcceptRichText(False)
449 465 else:
450 466 raise ValueError("Kind %s unknown." % repr(kind))
451 467 layout.addWidget(control)
@@ -895,7 +911,7 b' class ConsoleWidget(QtGui.QWidget):'
895 911
896 912 paste_action = QtGui.QAction('Paste', menu)
897 913 paste_action.triggered.connect(self.paste)
898 paste_action.setEnabled(self._control.canPaste())
914 paste_action.setEnabled(self.can_paste())
899 915 paste_action.setShortcut(QtGui.QKeySequence.Paste)
900 916 menu.addAction(paste_action)
901 917 menu.addSeparator()
@@ -22,14 +22,12 b' class RichIPythonWidget(IPythonWidget):'
22 22 super(RichIPythonWidget, self).__init__(kind='rich', parent=parent)
23 23
24 24 #---------------------------------------------------------------------------
25 # 'FrontendWidget' interface
25 # 'FrontendWidget' protected interface
26 26 #---------------------------------------------------------------------------
27 27
28 28 def _handle_execute_payload(self, payload):
29 29 """ Reimplemented to handle pylab plot payloads.
30 30 """
31 super(RichIPythonWidget, self)._handle_execute_payload(payload)
32
33 31 plot_payload = payload.get('plot', None)
34 32 if plot_payload and plot_payload['format'] == 'svg':
35 33 try:
@@ -41,3 +39,5 b' class RichIPythonWidget(IPythonWidget):'
41 39 cursor.insertBlock()
42 40 cursor.insertImage(image)
43 41 cursor.insertBlock()
42 else:
43 super(RichIPythonWidget, self)._handle_execute_payload(payload)
@@ -29,16 +29,16 b' class MetaQObjectHasTraits(MetaQObject, MetaHasTraits):'
29 29 #-----------------------------------------------------------------------------
30 30
31 31 def image_from_svg(string, size=None):
32 """ Convert a string containing SVG data into a QImage.
32 """ Convert a SVG document to a QImage.
33 33
34 34 Parameters:
35 35 -----------
36 36 string : str
37 A Python string containing the SVG data.
37 A Python string containing a SVG document.
38 38
39 size : QSize or None [default None]
40 The size of the image that is produced. If not specified, the SVG data's
41 default size is used.
39 size : QSize, optional
40 The size of the image that is produced. If not specified, the SVG
41 document's default size is used.
42 42
43 43 Raises:
44 44 -------
@@ -47,18 +47,18 b' def image_from_svg(string, size=None):'
47 47
48 48 Returns:
49 49 --------
50 A QImage with format QImage.Format_ARGB32_Premultiplied.
50 A QImage of format QImage.Format_ARGB32.
51 51 """
52 52 from PyQt4 import QtSvg
53 53
54 bytes = QtCore.QByteArray(string)
54 bytes = QtCore.QByteArray.fromRawData(string) # shallow copy
55 55 renderer = QtSvg.QSvgRenderer(bytes)
56 56 if not renderer.isValid():
57 57 raise ValueError('Invalid SVG data.')
58 58
59 59 if size is None:
60 60 size = renderer.defaultSize()
61 image = QtGui.QImage(size, QtGui.QImage.Format_ARGB32_Premultiplied)
61 image = QtGui.QImage(size, QtGui.QImage.Format_ARGB32)
62 62 painter = QtGui.QPainter(image)
63 63 renderer.render(painter)
64 64 return image
General Comments 0
You need to be logged in to leave comments. Login now