##// END OF EJS Templates
Reading a line is now correctly implemented in ConsoleWidget.
epatters -
Show More
@@ -141,6 +141,7 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
141 self._prompt = ''
141 self._prompt = ''
142 self._prompt_pos = 0
142 self._prompt_pos = 0
143 self._reading = False
143 self._reading = False
144 self._reading_callback = None
144
145
145 # Set a monospaced font.
146 # Set a monospaced font.
146 self.reset_font()
147 self.reset_font()
@@ -191,6 +192,11 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
191
192
192 self._context_menu.exec_(event.globalPos())
193 self._context_menu.exec_(event.globalPos())
193
194
195 def dragMoveEvent(self, event):
196 """ Reimplemented to disable dropping text.
197 """
198 event.ignore()
199
194 def keyPressEvent(self, event):
200 def keyPressEvent(self, event):
195 """ Reimplemented to create a console-like interface.
201 """ Reimplemented to create a console-like interface.
196 """
202 """
@@ -256,12 +262,15 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
256
262
257 else:
263 else:
258 if key in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter):
264 if key in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter):
259 intercepted = True
260 if self._reading:
265 if self._reading:
261 intercepted = False
266 self.appendPlainText('\n')
262 self._reading = False
267 self._reading = False
268 if self._reading_callback:
269 self._reading_callback()
270 self._reading_callback = None
263 elif not self._executing:
271 elif not self._executing:
264 self.execute(interactive=True)
272 self.execute(interactive=True)
273 intercepted = True
265
274
266 elif key == QtCore.Qt.Key_Up:
275 elif key == QtCore.Qt.Key_Up:
267 if self._reading or not self._up_pressed():
276 if self._reading or not self._up_pressed():
@@ -304,7 +313,8 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
304
313
305 # Line deletion (remove continuation prompt)
314 # Line deletion (remove continuation prompt)
306 len_prompt = len(self._continuation_prompt)
315 len_prompt = len(self._continuation_prompt)
307 if cursor.columnNumber() == len_prompt and \
316 if not self._reading and \
317 cursor.columnNumber() == len_prompt and \
308 position != self._prompt_pos:
318 position != self._prompt_pos:
309 cursor.setPosition(position - len_prompt,
319 cursor.setPosition(position - len_prompt,
310 QtGui.QTextCursor.KeepAnchor)
320 QtGui.QTextCursor.KeepAnchor)
@@ -678,18 +688,44 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
678 self.setReadOnly(True)
688 self.setReadOnly(True)
679 self._prompt_finished_hook()
689 self._prompt_finished_hook()
680
690
681 def _readline(self, prompt=''):
691 def _readline(self, prompt='', callback=None):
682 """ Read and return one line of input from the user. The trailing
692 """ Reads one line of input from the user.
683 newline is stripped.
693
694 Parameters
695 ----------
696 prompt : str, optional
697 The prompt to print before reading the line.
698
699 callback : callable, optional
700 A callback to execute with the read line. If not specified, input is
701 read *synchronously* and this method does not return until it has
702 been read.
703
704 Returns
705 -------
706 If a callback is specified, returns nothing. Otherwise, returns the
707 input string with the trailing newline stripped.
684 """
708 """
685 if not self.isVisible():
709 if self._reading:
686 raise RuntimeError('Cannot read a line if widget is not visible!')
710 raise RuntimeError('Cannot read a line. Widget is already reading.')
711
712 if not callback and not self.isVisible():
713 # If the user cannot see the widget, this function cannot return.
714 raise RuntimeError('Cannot synchronously read a line if the widget'
715 'is not visible!')
687
716
688 self._reading = True
717 self._reading = True
689 self._show_prompt(prompt)
718 self._show_prompt(prompt)
690 while self._reading:
719
691 QtCore.QCoreApplication.processEvents()
720 if callback is None:
692 return self.input_buffer.rstrip('\n\r')
721 self._reading_callback = None
722 while self._reading:
723 QtCore.QCoreApplication.processEvents()
724 return self.input_buffer.rstrip('\n')
725
726 else:
727 self._reading_callback = lambda: \
728 callback(self.input_buffer.rstrip('\n'))
693
729
694 def _set_position(self, position):
730 def _set_position(self, position):
695 """ Convenience method to set the position of the cursor.
731 """ Convenience method to set the position of the cursor.
@@ -710,10 +746,11 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
710 # Use QTextDocumentFragment intermediate object because it strips
746 # Use QTextDocumentFragment intermediate object because it strips
711 # out the Unicode line break characters that Qt insists on inserting.
747 # out the Unicode line break characters that Qt insists on inserting.
712 cursor = self._get_end_cursor()
748 cursor = self._get_end_cursor()
713 cursor.movePosition(QtGui.QTextCursor.Left,
749 if cursor.position() > 0:
714 QtGui.QTextCursor.KeepAnchor)
750 cursor.movePosition(QtGui.QTextCursor.Left,
715 if str(cursor.selection().toPlainText()) not in '\r\n':
751 QtGui.QTextCursor.KeepAnchor)
716 self.appendPlainText('\n')
752 if str(cursor.selection().toPlainText()) != '\n':
753 self.appendPlainText('\n')
717
754
718 if prompt is not None:
755 if prompt is not None:
719 self._prompt = prompt
756 self._prompt = prompt
@@ -293,7 +293,10 b' class FrontendWidget(HistoryConsoleWidget):'
293 self._call_tip()
293 self._call_tip()
294
294
295 def _handle_req(self):
295 def _handle_req(self):
296 print self._readline()
296 def callback(line):
297 print repr(line)
298 self._show_prompt()
299 self._readline(callback=callback)
297
300
298 def _handle_sub(self, omsg):
301 def _handle_sub(self, omsg):
299 if self._hidden:
302 if self._hidden:
@@ -345,5 +348,3 b' class FrontendWidget(HistoryConsoleWidget):'
345 doc = rep['content']['docstring']
348 doc = rep['content']['docstring']
346 if doc:
349 if doc:
347 self._call_tip_widget.show_docstring(doc)
350 self._call_tip_widget.show_docstring(doc)
348
349
General Comments 0
You need to be logged in to leave comments. Login now