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 2, 2024
1 parent 39cd91b commit b8e75a3
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions qtconsole/console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

from traitlets.config.configurable import LoggingConfigurable
from traitlets import Bool, Enum, Integer, Unicode
from traitlets import HasTraits, Unicode, observe


from .ansi_code_processor import QtAnsiCodeProcessor
from .completion_widget import CompletionWidget
Expand All @@ -43,6 +45,17 @@ def is_whitespace(char):
#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
class ShortcutManager(HasTraits):
# Definimos traits para los shortcuts
shortcut_print = Unicode(QtGui.QKeySequence.Print).tag(config=True)
shortcut_select_all = Unicode(QtGui.QKeySequence.SelectAll).tag(config=True)
shortcut_cut = Unicode(QtGui.QKeySequence.Cut).tag(config=True)
shortcut_copy = Unicode(QtGui.QKeySequence.Copy).tag(config=True)
shortcut_paste = Unicode(QtGui.QKeySequence.Paste).tag(config=True)

@observe('shortcut_print', 'shortcut_select_all','shortcut_cut','shortcut_copy','shortcut_paste')
def _on_shortcut_changed(self, change):
print(f"Shortcut for {change['name']} changed to: {change['new']}")

class ConsoleWidget(MetaQObjectHasTraits('NewBase', (LoggingConfigurable, superQ(QtWidgets.QWidget)), {})):
""" An abstract base class for console-type widgets. This class has
Expand Down Expand Up @@ -223,6 +236,7 @@ def _font_family_default(self):
# 'QObject' interface
#---------------------------------------------------------------------------


def __init__(self, parent=None, **kw):
""" Create a ConsoleWidget.
Expand Down Expand Up @@ -319,19 +333,22 @@ 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(QtGui.QKeySequence.Print)
printkey = self.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.
printkey = "Ctrl+Shift+P"
action.setShortcut(printkey)
self.shortcut_manager.shortcut_print = "Ctrl+Shift+P"
action.setShortcut(self.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'])

action = QtWidgets.QAction('Save as HTML/XML', None)
action.setShortcut(QtGui.QKeySequence.Save)
Expand All @@ -342,16 +359,17 @@ def __init__(self, parent=None, **kw):

action = QtWidgets.QAction('Select All', None)
action.setEnabled(True)
selectall = QtGui.QKeySequence(QtGui.QKeySequence.SelectAll)
selectall = self.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.
selectall = "Ctrl+Shift+A"
action.setShortcut(selectall)
self.shortcut_manager.shortcut_select_all = "Ctrl+Shift+A"
action.setShortcut(self.shortcut_manager.shortcut_print)
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'])

self.increase_font_size = QtWidgets.QAction("Bigger Font",
self,
Expand Down Expand Up @@ -381,6 +399,18 @@ def __init__(self, parent=None, **kw):
# in self._control when that widget was created.
self.setAcceptDrops(True)

def update_shortcuts(self, change):
if change['name'] == 'shortcut_print':
self.open_action.setShortcut(change['new'])
elif change['name'] == 'shortcut_select_all':
self.save_action.setShortcut(change['new'])
elif change['name'] == 'shortcut_cut':
self.close_action.setShortcut(change['new'])
elif change['name'] == 'shortcut_copy':
self.new_action.setShortcut(change['new'])
elif change['name'] == 'shortcut_paste':
self.new_action.setShortcut(change['new'])

#---------------------------------------------------------------------------
# Drag and drop support
#---------------------------------------------------------------------------
Expand Down Expand Up @@ -1146,15 +1176,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(QtGui.QKeySequence.Cut)
self.cut_action.setShortcut(self.shortcut_manager.shortcut_cut)
self.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(QtGui.QKeySequence.Copy)
self.copy_action.setShortcut(self.shortcut_manager.shortcut_copy)
self.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(QtGui.QKeySequence.Paste)
self.paste_action.setShortcut(self.shortcut_manager.shortcut_paste)
self.shortcut_manager.observe(self.update_shortcuts, names=['shortcut_paste'])

anchor = self._control.anchorAt(pos)
if anchor:
Expand Down

0 comments on commit b8e75a3

Please sign in to comment.