Skip to content

Commit

Permalink
Changes for handling Shortcuts with traitlets in consoleWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbautista committed Sep 23, 2024
1 parent 6f12aa5 commit c21cc13
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
36 changes: 17 additions & 19 deletions qtconsole/console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from .completion_html import CompletionHtml
from .completion_plain import CompletionPlain
from .kill_ring import QtKillRing
from .util import columnize, ShortcutManager
from .util import columnize, shortcut_manager


def is_letter_or_number(char):
Expand Down Expand Up @@ -322,44 +322,42 @@ def __init__(self, parent=None, **kw):
# Set a monospaced font.
self.reset_font()

self.shortcut_manager = ShortcutManager()

# Configure actions.
action = QtWidgets.QAction('Print', None)
action.setEnabled(True)
printkey = QtGui.QKeySequence(self.shortcut_manager.shortcut_print)
printkey = QtGui.QKeySequence(shortcut_manager.shortcut_print)
if printkey.matches("Ctrl+P") 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.
self.shortcut_manager.shortcut_print = "Ctrl+Shift+P"
action.setShortcut(self.shortcut_manager.shortcut_print)
shortcut_manager.shortcut_print = "Ctrl+Shift+P"
action.setShortcut(shortcut_manager.shortcut_print)
action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut)
action.triggered.connect(self.print_)
self.addAction(action)
self.print_action = action
self.shortcut_manager.observe(self.update_shortcuts, names=['shortcut_print'])
shortcut_manager.observe(self.update_shortcuts, names=['shortcut_print'])

action = QtWidgets.QAction('Save as HTML/XML', None)
action.setShortcut(self.shortcut_manager.shortcut_save)
action.setShortcut(shortcut_manager.shortcut_save)
action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut)
action.triggered.connect(self.export_html)
self.addAction(action)
self.export_action = action
self.shortcut_manager.observe(self.update_shortcuts, names=['shortcut_save'])
shortcut_manager.observe(self.update_shortcuts, names=['shortcut_save'])

action = QtWidgets.QAction('Select All', None)
action.setEnabled(True)
selectall = QtGui.QKeySequence(self.shortcut_manager.shortcut_select_all)
selectall = QtGui.QKeySequence(shortcut_manager.shortcut_select_all)
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.
self.shortcut_manager.shortcut_select_all = "Ctrl+Shift+A"
action.setShortcut(self.shortcut_manager.shortcut_select_all)
shortcut_manager.shortcut_select_all = "Ctrl+Shift+A"
action.setShortcut(shortcut_manager.shortcut_select_all)
action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut)
action.triggered.connect(self.select_all_smart)
self.addAction(action)
self.select_all_action = action
self.shortcut_manager.observe(self.update_shortcuts, names=['shortcut_select_all'])
shortcut_manager.observe(self.update_shortcuts, names=['shortcut_select_all'])

self.increase_font_size = QtWidgets.QAction("Bigger Font",
self,
Expand Down Expand Up @@ -1168,18 +1166,18 @@ def _context_menu_make(self, pos):

self.cut_action = menu.addAction('Cut', self.cut)
self.cut_action.setEnabled(self.can_cut())
self.cut_action.setShortcut(self.shortcut_manager.shortcut_cut)
self.shortcut_manager.observe(self.update_shortcuts, names=['shortcut_cut'])
self.cut_action.setShortcut(shortcut_manager.shortcut_cut)
shortcut_manager.observe(self.update_shortcuts, names=['shortcut_cut'])

self.copy_action = menu.addAction('Copy', self.copy)
self.copy_action.setEnabled(self.can_copy())
self.copy_action.setShortcut(self.shortcut_manager.shortcut_copy)
self.shortcut_manager.observe(self.update_shortcuts, names=['shortcut_copy'])
self.copy_action.setShortcut(shortcut_manager.shortcut_copy)
shortcut_manager.observe(self.update_shortcuts, names=['shortcut_copy'])

self.paste_action = menu.addAction('Paste', self.paste)
self.paste_action.setEnabled(self.can_paste())
self.paste_action.setShortcut(self.shortcut_manager.shortcut_paste)
self.shortcut_manager.observe(self.update_shortcuts, names=['shortcut_paste'])
self.paste_action.setShortcut(shortcut_manager.shortcut_paste)
shortcut_manager.observe(self.update_shortcuts, names=['shortcut_paste'])

anchor = self._control.anchorAt(pos)
if anchor:
Expand Down
8 changes: 3 additions & 5 deletions qtconsole/frontend_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .call_tip_widget import CallTipWidget
from .history_console_widget import HistoryConsoleWidget
from .pygments_highlighter import PygmentsHighlighter
from .util import import_item, ShortcutManager
from .util import import_item, shortcut_manager


class FrontendHighlighter(PygmentsHighlighter):
Expand Down Expand Up @@ -190,18 +190,16 @@ def __init__(self, local_kernel=_local_kernel, *args, **kw):
self._call_tip_widget.setFont(self.font)
self.font_changed.connect(self._call_tip_widget.setFont)

self.shortcut_manager = ShortcutManager()

# Configure actions.
action = self._copy_raw_action
action.setEnabled(False)
action.setShortcut(self.shortcut_manager.shortcut_copy_raw)
action.setShortcut(shortcut_manager.shortcut_copy_raw)
action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut)
action.triggered.connect(self.copy_raw)
self.copy_available.connect(action.setEnabled)
self.addAction(action)
self.copy_raw_action = action
self.shortcut_manager.observe(self.update_shortcuts, names=['shortcut_copy_raw'])
shortcut_manager.observe(self.update_shortcuts, names=['shortcut_copy_raw'])

# Connect signal handlers.
document = self._control.document()
Expand Down
2 changes: 2 additions & 0 deletions qtconsole/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def _on_shortcut_changed(self, change):
def _on_shortcut_changed(self, change):
print(f"Shortcut for {change['name']} changed to: {change['new']}")

shortcut_manager = ShortcutManager()


def superQ(QClass):
""" Permits the use of super() in class hierarchies that contain Qt classes.
Expand Down

0 comments on commit c21cc13

Please sign in to comment.