Show More
@@ -113,6 +113,20 b' class ConsoleWidget(QtGui.QWidget):' | |||||
113 | # 'ConsoleWidget' public interface |
|
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 | def clear(self, keep_input=False): |
|
130 | def clear(self, keep_input=False): | |
117 | """ Clear the console, then write a new prompt. If 'keep_input' is set, |
|
131 | """ Clear the console, then write a new prompt. If 'keep_input' is set, | |
118 | restores the old input buffer when the new prompt is written. |
|
132 | restores the old input buffer when the new prompt is written. | |
@@ -262,6 +276,7 b' class ConsoleWidget(QtGui.QWidget):' | |||||
262 | def paste(self): |
|
276 | def paste(self): | |
263 | """ Paste the contents of the clipboard into the input region. |
|
277 | """ Paste the contents of the clipboard into the input region. | |
264 | """ |
|
278 | """ | |
|
279 | if self.can_paste(): | |||
265 | self._keep_cursor_in_buffer() |
|
280 | self._keep_cursor_in_buffer() | |
266 | self._control.paste() |
|
281 | self._control.paste() | |
267 |
|
282 | |||
@@ -446,6 +461,7 b' class ConsoleWidget(QtGui.QWidget):' | |||||
446 | control = QtGui.QPlainTextEdit() |
|
461 | control = QtGui.QPlainTextEdit() | |
447 | elif kind == 'rich': |
|
462 | elif kind == 'rich': | |
448 | control = QtGui.QTextEdit() |
|
463 | control = QtGui.QTextEdit() | |
|
464 | control.setAcceptRichText(False) | |||
449 | else: |
|
465 | else: | |
450 | raise ValueError("Kind %s unknown." % repr(kind)) |
|
466 | raise ValueError("Kind %s unknown." % repr(kind)) | |
451 | layout.addWidget(control) |
|
467 | layout.addWidget(control) | |
@@ -895,7 +911,7 b' class ConsoleWidget(QtGui.QWidget):' | |||||
895 |
|
911 | |||
896 | paste_action = QtGui.QAction('Paste', menu) |
|
912 | paste_action = QtGui.QAction('Paste', menu) | |
897 | paste_action.triggered.connect(self.paste) |
|
913 | paste_action.triggered.connect(self.paste) | |
898 |
paste_action.setEnabled(self. |
|
914 | paste_action.setEnabled(self.can_paste()) | |
899 | paste_action.setShortcut(QtGui.QKeySequence.Paste) |
|
915 | paste_action.setShortcut(QtGui.QKeySequence.Paste) | |
900 | menu.addAction(paste_action) |
|
916 | menu.addAction(paste_action) | |
901 | menu.addSeparator() |
|
917 | menu.addSeparator() |
@@ -22,14 +22,12 b' class RichIPythonWidget(IPythonWidget):' | |||||
22 | super(RichIPythonWidget, self).__init__(kind='rich', parent=parent) |
|
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 | def _handle_execute_payload(self, payload): |
|
28 | def _handle_execute_payload(self, payload): | |
29 | """ Reimplemented to handle pylab plot payloads. |
|
29 | """ Reimplemented to handle pylab plot payloads. | |
30 | """ |
|
30 | """ | |
31 | super(RichIPythonWidget, self)._handle_execute_payload(payload) |
|
|||
32 |
|
||||
33 | plot_payload = payload.get('plot', None) |
|
31 | plot_payload = payload.get('plot', None) | |
34 | if plot_payload and plot_payload['format'] == 'svg': |
|
32 | if plot_payload and plot_payload['format'] == 'svg': | |
35 | try: |
|
33 | try: | |
@@ -41,3 +39,5 b' class RichIPythonWidget(IPythonWidget):' | |||||
41 | cursor.insertBlock() |
|
39 | cursor.insertBlock() | |
42 | cursor.insertImage(image) |
|
40 | cursor.insertImage(image) | |
43 | cursor.insertBlock() |
|
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 | def image_from_svg(string, size=None): |
|
31 | def image_from_svg(string, size=None): | |
32 |
""" Convert a |
|
32 | """ Convert a SVG document to a QImage. | |
33 |
|
33 | |||
34 | Parameters: |
|
34 | Parameters: | |
35 | ----------- |
|
35 | ----------- | |
36 | string : str |
|
36 | string : str | |
37 |
A Python string containing |
|
37 | A Python string containing a SVG document. | |
38 |
|
38 | |||
39 |
size : QSize |
|
39 | size : QSize, optional | |
40 |
The size of the image that is produced. If not specified, the SVG |
|
40 | The size of the image that is produced. If not specified, the SVG | |
41 | default size is used. |
|
41 | document's default size is used. | |
42 |
|
42 | |||
43 | Raises: |
|
43 | Raises: | |
44 | ------- |
|
44 | ------- | |
@@ -47,18 +47,18 b' def image_from_svg(string, size=None):' | |||||
47 |
|
47 | |||
48 | Returns: |
|
48 | Returns: | |
49 | -------- |
|
49 | -------- | |
50 |
A QImage |
|
50 | A QImage of format QImage.Format_ARGB32. | |
51 | """ |
|
51 | """ | |
52 | from PyQt4 import QtSvg |
|
52 | from PyQt4 import QtSvg | |
53 |
|
53 | |||
54 | bytes = QtCore.QByteArray(string) |
|
54 | bytes = QtCore.QByteArray.fromRawData(string) # shallow copy | |
55 | renderer = QtSvg.QSvgRenderer(bytes) |
|
55 | renderer = QtSvg.QSvgRenderer(bytes) | |
56 | if not renderer.isValid(): |
|
56 | if not renderer.isValid(): | |
57 | raise ValueError('Invalid SVG data.') |
|
57 | raise ValueError('Invalid SVG data.') | |
58 |
|
58 | |||
59 | if size is None: |
|
59 | if size is None: | |
60 | size = renderer.defaultSize() |
|
60 | size = renderer.defaultSize() | |
61 |
image = QtGui.QImage(size, QtGui.QImage.Format_ARGB32 |
|
61 | image = QtGui.QImage(size, QtGui.QImage.Format_ARGB32) | |
62 | painter = QtGui.QPainter(image) |
|
62 | painter = QtGui.QPainter(image) | |
63 | renderer.render(painter) |
|
63 | renderer.render(painter) | |
64 | return image |
|
64 | return image |
General Comments 0
You need to be logged in to leave comments.
Login now