##// END OF EJS Templates
* Implemented "smart paste" for ConsoleWidget. Continuation prompts are now inserted automatically on paste....
epatters -
Show More
@@ -1,5 +1,6 b''
1 1 # Standard library imports
2 2 import sys
3 from textwrap import dedent
3 4
4 5 # System library imports
5 6 from PyQt4 import QtCore, QtGui
@@ -239,24 +240,6 b' class ConsoleWidget(QtGui.QWidget):'
239 240
240 241 input_buffer = property(_get_input_buffer, _set_input_buffer)
241 242
242 def _get_input_buffer_cursor_line(self):
243 """ The text in the line of the input buffer in which the user's cursor
244 rests. Returns a string if there is such a line; otherwise, None.
245 """
246 if self._executing:
247 return None
248 cursor = self._control.textCursor()
249 if cursor.position() >= self._prompt_pos:
250 text = self._get_block_plain_text(cursor.block())
251 if cursor.blockNumber() == self._get_prompt_cursor().blockNumber():
252 return text[len(self._prompt):]
253 else:
254 return text[len(self._continuation_prompt):]
255 else:
256 return None
257
258 input_buffer_cursor_line = property(_get_input_buffer_cursor_line)
259
260 243 def _get_font(self):
261 244 """ The base font being used by the ConsoleWidget.
262 245 """
@@ -276,9 +259,13 b' class ConsoleWidget(QtGui.QWidget):'
276 259 def paste(self):
277 260 """ Paste the contents of the clipboard into the input region.
278 261 """
279 if self.can_paste():
280 self._keep_cursor_in_buffer()
281 self._control.paste()
262 if self._control.textInteractionFlags() & QtCore.Qt.TextEditable:
263 try:
264 text = str(QtGui.QApplication.clipboard().text())
265 except UnicodeEncodeError:
266 pass
267 else:
268 self._insert_into_buffer(dedent(text))
282 269
283 270 def print_(self, printer):
284 271 """ Print the contents of the ConsoleWidget to the specified QPrinter.
@@ -707,6 +694,22 b' class ConsoleWidget(QtGui.QWidget):'
707 694 cursor.movePosition(QtGui.QTextCursor.End)
708 695 return cursor
709 696
697 def _get_input_buffer_cursor_line(self):
698 """ The text in the line of the input buffer in which the user's cursor
699 rests. Returns a string if there is such a line; otherwise, None.
700 """
701 if self._executing:
702 return None
703 cursor = self._control.textCursor()
704 if cursor.position() >= self._prompt_pos:
705 text = self._get_block_plain_text(cursor.block())
706 if cursor.blockNumber() == self._get_prompt_cursor().blockNumber():
707 return text[len(self._prompt):]
708 else:
709 return text[len(self._continuation_prompt):]
710 else:
711 return None
712
710 713 def _get_prompt_cursor(self):
711 714 """ Convenience method that returns a cursor for the prompt position.
712 715 """
@@ -763,20 +766,35 b' class ConsoleWidget(QtGui.QWidget):'
763 766 """
764 767 cursor.insertHtml(html)
765 768
766 # After inserting HTML, the text document "remembers" the current
767 # formatting, which means that subsequent calls adding plain text
768 # will result in similar formatting, a behavior that we do not want. To
769 # prevent this, we make sure that the last character has no formatting.
769 # After inserting HTML, the text document "remembers" it's in "html
770 # mode", which means that subsequent calls adding plain text will result
771 # in unwanted formatting, lost tab characters, etc. The following code
772 # hacks around this behavior, which I consider to be a bug in Qt.
770 773 cursor.movePosition(QtGui.QTextCursor.Left,
771 774 QtGui.QTextCursor.KeepAnchor)
772 if cursor.selection().toPlainText().trimmed().isEmpty():
773 # If the last character is whitespace, it doesn't matter how it's
774 # formatted, so just clear the formatting.
775 cursor.setCharFormat(QtGui.QTextCharFormat())
776 else:
777 # Otherwise, add an unformatted space.
778 cursor.movePosition(QtGui.QTextCursor.Right)
779 cursor.insertText(' ', QtGui.QTextCharFormat())
775 if cursor.selection().toPlainText() == ' ':
776 cursor.removeSelectedText()
777 cursor.movePosition(QtGui.QTextCursor.Right)
778 cursor.insertText(' ', QtGui.QTextCharFormat())
779
780 def _insert_into_buffer(self, text):
781 """ Inserts text into the input buffer at the current cursor position,
782 ensuring that continuation prompts are inserted as necessary.
783 """
784 lines = str(text).splitlines(True)
785 if lines:
786 self._keep_cursor_in_buffer()
787 cursor = self._control.textCursor()
788 cursor.beginEditBlock()
789 cursor.insertText(lines[0])
790 for line in lines[1:]:
791 if self._continuation_prompt_html is None:
792 cursor.insertText(self._continuation_prompt)
793 else:
794 self._insert_html(cursor, self._continuation_prompt_html)
795 cursor.insertText(line)
796 cursor.endEditBlock()
797 self._control.setTextCursor(cursor)
780 798
781 799 def _in_buffer(self, position):
782 800 """ Returns whether the given position is inside the editing region.
@@ -1,4 +1,4 b''
1 """ A demo of Qt console-style IPython frontend.
1 """ A demo of the Qt console-style IPython frontend.
2 2 """
3 3
4 4 # Systemm library imports
@@ -245,7 +245,7 b' class FrontendWidget(HistoryConsoleWidget):'
245 245 # Send the completion request to the kernel
246 246 text = '.'.join(context)
247 247 self._complete_id = self.kernel_manager.xreq_channel.complete(
248 text, self.input_buffer_cursor_line, self.input_buffer)
248 text, self._get_input_buffer_cursor_line(), self.input_buffer)
249 249 self._complete_pos = self._get_cursor().position()
250 250 return True
251 251
General Comments 0
You need to be logged in to leave comments. Login now