Qtconsole menu #887

Merged
merged 52 commits into from Oct 20, 2011
Commits
+1,761 −173
Split
@@ -236,22 +236,50 @@ def __init__(self, parent=None, **kw):
action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut)
action.triggered.connect(self.print_)
self.addAction(action)
- self._print_action = action
+ self.print_action = action
action = QtGui.QAction('Save as HTML/XML', None)
action.setShortcut(QtGui.QKeySequence.Save)
action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut)
action.triggered.connect(self.export_html)
self.addAction(action)
- self._export_action = action
+ self.export_action = action
action = QtGui.QAction('Select All', None)
action.setEnabled(True)
- action.setShortcut(QtGui.QKeySequence.SelectAll)
+ selectall = QtGui.QKeySequence(QtGui.QKeySequence.SelectAll)
+ if selectall.matches("Ctrl+A") and sys.platform != 'darwin':
+ # Only override the default if there is a collision.
+ # Qt ctrl = cmd on OSX, so the match gets a false positive on OSX.
+ selectall = "Ctrl+Shift+A"
+ action.setShortcut(selectall)
action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut)
action.triggered.connect(self.select_all)
self.addAction(action)
- self._select_all_action = action
+ self.select_all_action = action
+
+ self.increase_font_size = QtGui.QAction("Bigger Font",
+ self,
+ shortcut=QtGui.QKeySequence.ZoomIn,
+ statusTip="Increase the font size by one point",
+ triggered=self._increase_font_size)
+ self.addAction(self.increase_font_size)
+
+ self.decrease_font_size = QtGui.QAction("Smaller Font",
+ self,
+ shortcut=QtGui.QKeySequence.ZoomOut,
+ statusTip="Decrease the font size by one point",
+ triggered=self._decrease_font_size)
+ self.addAction(self.decrease_font_size)
+
+ self.reset_font_size = QtGui.QAction("Normal Font",
+ self,
+ shortcut="Ctrl+0",
+ statusTip="Restore the Normal font size",
+ triggered=self.reset_font)
+ self.addAction(self.reset_font_size)
+
+
def eventFilter(self, obj, event):
""" Reimplemented to ensure a console-like behavior in the underlying
@@ -652,6 +680,12 @@ def change_font_size(self, delta):
font.setPointSize(size)
self._set_font(font)
+ def _increase_font_size(self):
+ self.change_font_size(1)
+
+ def _decrease_font_size(self):
+ self.change_font_size(-1)
+
def select_all(self):
""" Selects all the text in the buffer.
"""
@@ -842,24 +876,24 @@ def _context_menu_make(self, pos):
"""
menu = QtGui.QMenu(self)
- cut_action = menu.addAction('Cut', self.cut)
- cut_action.setEnabled(self.can_cut())
- cut_action.setShortcut(QtGui.QKeySequence.Cut)
+ self.cut_action = menu.addAction('Cut', self.cut)
+ self.cut_action.setEnabled(self.can_cut())
+ self.cut_action.setShortcut(QtGui.QKeySequence.Cut)
- copy_action = menu.addAction('Copy', self.copy)
- copy_action.setEnabled(self.can_copy())
- copy_action.setShortcut(QtGui.QKeySequence.Copy)
+ self.copy_action = menu.addAction('Copy', self.copy)
+ self.copy_action.setEnabled(self.can_copy())
+ self.copy_action.setShortcut(QtGui.QKeySequence.Copy)
- paste_action = menu.addAction('Paste', self.paste)
- paste_action.setEnabled(self.can_paste())
- paste_action.setShortcut(QtGui.QKeySequence.Paste)
+ self.paste_action = menu.addAction('Paste', self.paste)
+ self.paste_action.setEnabled(self.can_paste())
+ self.paste_action.setShortcut(QtGui.QKeySequence.Paste)
menu.addSeparator()
- menu.addAction(self._select_all_action)
+ menu.addAction(self.select_all_action)
menu.addSeparator()
- menu.addAction(self._export_action)
- menu.addAction(self._print_action)
+ menu.addAction(self.export_action)
+ menu.addAction(self.print_action)
return menu
@@ -1058,18 +1092,6 @@ def _event_filter_console_keypress(self, event):
self._kill_ring.kill_cursor(cursor)
intercepted = True
- elif key in (QtCore.Qt.Key_Plus, QtCore.Qt.Key_Equal):
- self.change_font_size(1)
- intercepted = True
-
- elif key == QtCore.Qt.Key_Minus:
- self.change_font_size(-1)
- intercepted = True
-
- elif key == QtCore.Qt.Key_0:
- self.reset_font()
- intercepted = True
-
#------ Alt modifier ---------------------------------------------------
elif alt_down:
@@ -1136,7 +1158,10 @@ def _event_filter_console_keypress(self, event):
elif key == QtCore.Qt.Key_Tab:
if not self._reading:
- intercepted = not self._tab_pressed()
+ if self._tab_pressed():
+ # real tab-key, insert four spaces
+ cursor.insertText(' '*4)
+ intercepted = True
elif key == QtCore.Qt.Key_Left:
@@ -102,7 +102,7 @@ class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
executed = QtCore.Signal(object)
# Emitted when an exit request has been received from the kernel.
- exit_requested = QtCore.Signal()
+ exit_requested = QtCore.Signal(object)
# Protected class variables.
_CallTipRequest = namedtuple('_CallTipRequest', ['id', 'pos'])
@@ -241,19 +241,26 @@ def _context_menu_make(self, pos):
break
return menu
+ def request_interrupt_kernel(self):
+ if self._executing:
+ self.interrupt_kernel()
+
+ def request_restart_kernel(self):
+ message = 'Are you sure you want to restart the kernel?'
+ self.restart_kernel(message, now=False)
+
def _event_filter_console_keypress(self, event):
""" Reimplemented for execution interruption and smart backspace.
"""
key = event.key()
if self._control_key_down(event.modifiers(), include_command=False):
if key == QtCore.Qt.Key_C and self._executing:
- self.interrupt_kernel()
+ self.request_interrupt_kernel()
return True
elif key == QtCore.Qt.Key_Period:
- message = 'Are you sure you want to restart the kernel?'
- self.restart_kernel(message, now=False)
+ self.request_restart_kernel()
return True
elif not event.modifiers() & QtCore.Qt.AltModifier:
@@ -324,6 +331,8 @@ def _handle_execute_reply(self, msg):
self._show_interpreter_prompt_for_reply(msg)
self.executed.emit(msg)
+ else:
+ super(FrontendWidget, self)._handle_execute_reply(msg)
def _handle_input_request(self, msg):
""" Handle requests for raw_input.
@@ -405,7 +414,7 @@ def _handle_shutdown_reply(self, msg):
if not self._hidden and not self._is_from_this_session(msg):
if self._local_kernel:
if not msg['content']['restart']:
- sys.exit(0)
+ self.exit_requested.emit(self)
else:
# we just got notified of a restart!
time.sleep(0.25) # wait 1/4 sec to reset
@@ -420,7 +429,7 @@ def _handle_shutdown_reply(self, msg):
"Close the Console?",
QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
- sys.exit(0)
+ self.exit_requested.emit(self)
else:
reply = QtGui.QMessageBox.question(self, title,
"Kernel has been reset. Clear the Console?",
@@ -581,7 +590,7 @@ def _process_execute_error(self, msg):
if content['ename']=='SystemExit':
keepkernel = content['evalue']=='-k' or content['evalue']=='True'
self._keep_kernel_on_exit = keepkernel
- self.exit_requested.emit()
+ self.exit_requested.emit(self)
else:
traceback = ''.join(content['traceback'])
self._append_plain_text(traceback)
@@ -204,6 +204,47 @@ def history_tail(self, n=10):
"""
return self._history[-n:]
+ def _request_update_session_history_length(self):
+ msg_id = self.kernel_manager.shell_channel.execute('',
+ silent=True,
+ user_expressions={
+ 'hlen':'len(get_ipython().history_manager.input_hist_raw)',
+ }
+ )
+ self._request_info['execute'] = self._ExecutionRequest(msg_id, 'save_magic')
+
+ def _handle_execute_reply(self, msg):
+ """ Handles replies for code execution, here only session history length
+ """
+ info = self._request_info.get('execute')
+ if info and info.id == msg['parent_header']['msg_id'] and \
+ info.kind == 'save_magic' and not self._hidden:
+ content = msg['content']
+ status = content['status']
+ if status == 'ok':
+ self._max_session_history=(int(content['user_expressions']['hlen']))
+
+ def save_magic(self):
+ # update the session history length
+ self._request_update_session_history_length()
+
+ file_name,extFilter = QtGui.QFileDialog.getSaveFileName(self,
+ "Enter A filename",
+ filter='Python File (*.py);; All files (*.*)'
+ )
+
+ # let's the user search/type for a file name, while the history length
+ # is fetched
+
+ if file_name:
+ hist_range, ok = QtGui.QInputDialog.getText(self,
+ 'Please enter an interval of command to save',
+ 'Saving commands:',
+ text=str('1-'+str(self._max_session_history))
+ )
+ if ok:
+ self.execute("%save"+" "+file_name+" "+str(hist_range))
+
#---------------------------------------------------------------------------
# 'HistoryConsoleWidget' protected interface
#---------------------------------------------------------------------------
@@ -503,7 +503,7 @@ def _handle_payload_edit(self, item):
def _handle_payload_exit(self, item):
self._keep_kernel_on_exit = item['keepkernel']
- self.exit_requested.emit()
+ self.exit_requested.emit(self)
def _handle_payload_next_input(self, item):
self.input_buffer = dedent(item['text'].rstrip())
Oops, something went wrong.