diff --git a/IPython/frontend/qt/console/console_widget.py b/IPython/frontend/qt/console/console_widget.py index 52b5684..8acacfc 100644 --- a/IPython/frontend/qt/console/console_widget.py +++ b/IPython/frontend/qt/console/console_widget.py @@ -236,14 +236,14 @@ class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): 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) @@ -251,7 +251,73 @@ class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): 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="Ctrl++", + 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="Ctrl+-", + 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) + + self.undo_action = QtGui.QAction("Undo", + self, + shortcut="Ctrl+Z", + statusTip="Undo last action if possible", + triggered=self._control.undo) + self.addAction(self.undo_action) + + self.redo_action = QtGui.QAction("Redo", + self, + shortcut="Ctrl+Shift+Z", + statusTip="Redo last action if possible", + triggered=self._control.redo) + self.addAction(self.redo_action) + + self.reset_action = QtGui.QAction("Reset", + self, + statusTip="Clear all varible from workspace", + triggered=self.reset_magic) + self.addAction(self.reset_action) + + self.clear_action = QtGui.QAction("Clear", + self, + statusTip="Clear the console", + triggered=self.clear_magic) + self.addAction(self.clear_action) + + self.who_action = QtGui.QAction("Who", + self, + statusTip="List interactive variable", + triggered=self.who_magic) + self.addAction(self.who_action) + + self.whos_action = QtGui.QAction("Whos", + self, + statusTip="List interactive variable with detail", + triggered=self.whos_magic) + self.addAction(self.whos_action) + + self.who_ls_action = QtGui.QAction("Who ls", + self, + statusTip="Return a list of interactive variable", + triggered=self.who_ls_magic) + self.addAction(self.who_ls_action) + def eventFilter(self, obj, event): """ Reimplemented to ensure a console-like behavior in the underlying @@ -579,12 +645,14 @@ class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): font = property(_get_font, _set_font) - def paste(self, mode=QtGui.QClipboard.Clipboard): - """ Paste the contents of the clipboard into the input region. + def paste(self, mode=QtGui.QClipboard.Clipboard,text=None): + """ Paste the contents of the clipboard, or given text, + into the input region. Parameters: ----------- mode : QClipboard::Mode, optional [default QClipboard::Clipboard] + text : string, optional, overide mode if given. Controls which part of the system clipboard is used. This can be used to access the selection clipboard in X11 and the Find buffer @@ -597,9 +665,30 @@ class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): # Remove any trailing newline, which confuses the GUI and forces the # user to backspace. - text = QtGui.QApplication.clipboard().text(mode).rstrip() + if not text: + text = QtGui.QApplication.clipboard().text(mode).rstrip() self._insert_plain_text_into_buffer(cursor, dedent(text)) + def pasteMagic(self,text): + self._keyboard_quit() + self.paste(text=text) + self.execute() + + def who_magic(self): + self.pasteMagic("%who") + + def whos_magic(self): + self.pasteMagic("%whos") + + def who_ls_magic(self): + self.pasteMagic("%who_ls") + + def clear_magic(self): + self.pasteMagic("%clear") + + def reset_magic(self): + self.pasteMagic("%reset") + def print_(self, printer = None): """ Print the contents of the ConsoleWidget to the specified QPrinter. """ @@ -652,6 +741,12 @@ class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): 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. """ @@ -855,11 +950,11 @@ class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): 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 +1153,6 @@ class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): 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: diff --git a/IPython/frontend/qt/console/history_console_widget.py b/IPython/frontend/qt/console/history_console_widget.py index 6a54ae3..66e9c35 100644 --- a/IPython/frontend/qt/console/history_console_widget.py +++ b/IPython/frontend/qt/console/history_console_widget.py @@ -31,6 +31,18 @@ class HistoryConsoleWidget(ConsoleWidget): self._history_index = 0 self._history_prefix = '' + self.history_action = QtGui.QAction("History", + self, + statusTip="show command history", + triggered=self.history_magic) + self.addAction(self.history_action) + + self.save_action = QtGui.QAction("Export History ", + self, + statusTip="Export History as Python File", + triggered=self.save_magic) + self.addAction(self.save_action) + #--------------------------------------------------------------------------- # 'ConsoleWidget' public interface #--------------------------------------------------------------------------- @@ -204,6 +216,22 @@ class HistoryConsoleWidget(ConsoleWidget): """ return self._history[-n:] + def history_magic(self): + self.pasteMagic("%history") + + def save_magic(self): + file_name, ok = QtGui.QInputDialog.getText(self, + 'Enter A file Name', + 'Please enter a filename to wich export the history as python file:', + text='untilted.py') + if ok: + hist_range, ok = QtGui.QInputDialog.getText(self, + 'Please enter an interval of command to save', + 'Saving commands:', + text='1-500') + if ok: + self.pasteMagic("%save"+" "+file_name+" "+str(hist_range)) + #--------------------------------------------------------------------------- # 'HistoryConsoleWidget' protected interface #--------------------------------------------------------------------------- diff --git a/IPython/frontend/qt/console/qtconsoleapp.py b/IPython/frontend/qt/console/qtconsoleapp.py index 50c5af4..53807fb 100644 --- a/IPython/frontend/qt/console/qtconsoleapp.py +++ b/IPython/frontend/qt/console/qtconsoleapp.py @@ -23,7 +23,7 @@ import signal import sys # System library imports -from IPython.external.qt import QtGui +from IPython.external.qt import QtGui,QtCore from pygments.styles import get_all_styles # Local imports @@ -74,7 +74,7 @@ class MainWindow(QtGui.QMainWindow): #--------------------------------------------------------------------------- # 'object' interface #--------------------------------------------------------------------------- - + def __init__(self, app, frontend, existing=False, may_close=True, confirm_exit=True): """ Create a MainWindow for the specified FrontendWidget. @@ -98,6 +98,49 @@ class MainWindow(QtGui.QMainWindow): self._confirm_exit = confirm_exit self.setCentralWidget(frontend) + # MenuBar is always present on Mac Os, so let's populate + # it with possible action, don't do it on other platform + # as some user might not want the menu bar, or give them + # an option to remove it + if sys.platform == 'darwin': + #create menu in the order they should appear in the menu bar + self.fileMenu = self.menuBar().addMenu("File") + self.editMenu = self.menuBar().addMenu("Edit") + self.fontMenu = self.menuBar().addMenu("Font") + self.windowMenu = self.menuBar().addMenu("Window") + self.magicMenu = self.menuBar().addMenu("Magic") + + # please keep the Help menu in Mac Os even if empty. It will + # automatically contain a search field to search inside menus and + # please keep it spelled in English, as long as Qt Doesn't support + # a QAction.MenuRole like HelpMenuRole otherwise it will loose + # this search field fonctionnality + + self.helpMenu = self.menuBar().addMenu("Help") + + # sould wrap every line of the following block into a try/except, + # as we are not sure of instanciating a _frontend which support all + # theses actions, but there might be a better way + + self.fileMenu.addAction(self._frontend.print_action) + self.fileMenu.addAction(self._frontend.export_action) + self.fileMenu.addAction(self._frontend.select_all_action) + + self.editMenu.addAction(self._frontend.undo_action) + self.editMenu.addAction(self._frontend.redo_action) + + self.fontMenu.addAction(self._frontend.increase_font_size) + self.fontMenu.addAction(self._frontend.decrease_font_size) + self.fontMenu.addAction(self._frontend.reset_font_size) + + self.magicMenu.addAction(self._frontend.reset_action) + self.magicMenu.addAction(self._frontend.history_action) + self.magicMenu.addAction(self._frontend.save_action) + self.magicMenu.addAction(self._frontend.clear_action) + self.magicMenu.addAction(self._frontend.who_action) + self.magicMenu.addAction(self._frontend.who_ls_action) + self.magicMenu.addAction(self._frontend.whos_action) + #--------------------------------------------------------------------------- # QWidget interface #--------------------------------------------------------------------------- @@ -562,16 +605,76 @@ class IPythonQtConsoleApp(BaseIPythonApplication): self.init_window_shortcut() def init_window_shortcut(self): - fullScreenAction = QtGui.QAction('Toggle Full Screen', self.window) - fullScreenAction.setShortcut('Ctrl+Meta+Space') - fullScreenAction.triggered.connect(self.toggleFullScreen) - self.window.addAction(fullScreenAction) + self.fullScreenAct = QtGui.QAction("Full Screen", + self.window, + shortcut="Ctrl+Meta+Space", + statusTip="Toggle between Fullscreen and Normal Size", + triggered=self.toggleFullScreen) + + + # creating shortcut in menubar only for Mac OS as I don't + # know the shortcut or if the windows manager assign it in + # other platform. + if sys.platform == 'darwin': + self.minimizeAct = QtGui.QAction("Minimize", + self.window, + shortcut="Ctrl+m", + statusTip="Minimize the window/Restore Normal Size", + triggered=self.toggleMinimized) + self.maximizeAct = QtGui.QAction("Maximize", + self.window, + shortcut="Ctrl+Shift+M", + statusTip="Maximize the window/Restore Normal Size", + triggered=self.toggleMaximized) + + self.onlineHelpAct = QtGui.QAction("Open Online Help", + self.window, + triggered=self._open_online_help) + + self.windowMenu = self.window.windowMenu + self.windowMenu.addAction(self.minimizeAct) + self.windowMenu.addAction(self.maximizeAct) + self.windowMenu.addSeparator() + self.windowMenu.addAction(self.fullScreenAct) + + self.window.helpMenu.addAction(self.onlineHelpAct) + else: + # if we don't put it in a menu, we add it to the window so + # that it can still be triggerd by shortcut + self.window.addAction(self.fullScreenAct) + + def toggleMinimized(self): + if not self.window.isMinimized(): + self.window.showMinimized() + else: + self.window.showNormal() + + def _open_online_help(self): + QtGui.QDesktopServices.openUrl( + QtCore.QUrl("http://ipython.org/documentation.html", + QtCore.QUrl.TolerantMode) + ) + + def toggleMaximized(self): + if not self.window.isMaximized(): + self.window.showMaximized() + else: + self.window.showNormal() + + # Min/Max imizing while in full screen give a bug + # when going out of full screen, at least on OSX def toggleFullScreen(self): if not self.window.isFullScreen(): self.window.showFullScreen() + if sys.platform == 'darwin': + self.maximizeAct.setEnabled(False) + self.minimizeAct.setEnabled(False) else: self.window.showNormal() + if sys.platform == 'darwin': + self.maximizeAct.setEnabled(True) + self.minimizeAct.setEnabled(True) def start(self):