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 83c5a6a commit 6f12aa5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 46 deletions.
37 changes: 4 additions & 33 deletions qtconsole/console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@

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

from .ansi_code_processor import QtAnsiCodeProcessor
from .completion_widget import CompletionWidget
from .completion_html import CompletionHtml
from .completion_plain import CompletionPlain
from .kill_ring import QtKillRing
from .util import columnize
from .util import columnize, ShortcutManager


def is_letter_or_number(char):
Expand All @@ -45,37 +45,6 @@ def is_whitespace(char):
# Classes
#-----------------------------------------------------------------------------

class ShortcutManager(HasTraits):
"""Default shortcuts definition and changes event handler."""

# Define traits for shortcuts
shortcut_print = Unicode('Ctrl+P').tag(config=True)
shortcut_select_all = Unicode('Ctrl+A').tag(config=True)
shortcut_cut = Unicode().tag(config=True)
shortcut_copy = Unicode().tag(config=True)
shortcut_paste = Unicode().tag(config=True)
shortcut_save = Unicode().tag(config=True)

@default('shortcut_save')
def _default_shortcut_save(self):
return QtGui.QKeySequence(QtGui.QKeySequence.Save).toString()

@default('shortcut_cut')
def _default_shortcut_cut(self):
return QtGui.QKeySequence(QtGui.QKeySequence.Cut).toString()

@default('shortcut_copy')
def _default_shortcut_copy(self):
return QtGui.QKeySequence(QtGui.QKeySequence.Copy).toString()

@default('shortcut_paste')
def _default_shortcut_paste(self):
return QtGui.QKeySequence(QtGui.QKeySequence.Paste).toString()

@observe('shortcut_print', 'shortcut_select_all','shortcut_cut','shortcut_copy','shortcut_paste','shortcut_save')
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 @@ -431,6 +400,8 @@ def update_shortcuts(self, change):
self.copy_action.setShortcut(change['new'])
elif change['name'] == 'shortcut_paste':
self.paste_action.setShortcut(change['new'])
elif change['name'] == 'shortcut_save':
self.export_action.setShortcut(change['new'])

#---------------------------------------------------------------------------
# Drag and drop support
Expand Down
13 changes: 1 addition & 12 deletions qtconsole/frontend_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,7 @@
from .call_tip_widget import CallTipWidget
from .history_console_widget import HistoryConsoleWidget
from .pygments_highlighter import PygmentsHighlighter
from .util import import_item


class ShortcutManager(HasTraits):
"""Default shortcuts definition and changes event handler."""

# Define traits for shortcuts
shortcut_copy_raw = Unicode('Ctrl+Shift+C').tag(config=True)

@observe('shortcut_copy_raw')
def _on_shortcut_changed(self, change):
print(f"Shortcut for {change['name']} changed to: {change['new']}")
from .util import import_item, ShortcutManager


class FrontendHighlighter(PygmentsHighlighter):
Expand Down
40 changes: 39 additions & 1 deletion qtconsole/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from qtpy import QtCore, QtGui

from traitlets import HasTraits, TraitType
from traitlets import default, HasTraits, TraitType, Unicode, observe

#-----------------------------------------------------------------------------
# Metaclasses
Expand Down Expand Up @@ -44,6 +44,44 @@ def __init__(mcls, name, bases, classdict):
# Classes
#-----------------------------------------------------------------------------

class ShortcutManager(HasTraits):
"""Default shortcuts definition and changes event handler."""

# Define traits for shortcuts
shortcut_print = Unicode('Ctrl+P').tag(config=True)
shortcut_select_all = Unicode('Ctrl+A').tag(config=True)
shortcut_cut = Unicode().tag(config=True)
shortcut_copy = Unicode().tag(config=True)
shortcut_paste = Unicode().tag(config=True)
shortcut_save = Unicode().tag(config=True)
shortcut_copy_raw = Unicode('Ctrl+Shift+C').tag(config=True)


@default('shortcut_save')
def _default_shortcut_save(self):
return QtGui.QKeySequence(QtGui.QKeySequence.Save).toString()

@default('shortcut_cut')
def _default_shortcut_cut(self):
return QtGui.QKeySequence(QtGui.QKeySequence.Cut).toString()

@default('shortcut_copy')
def _default_shortcut_copy(self):
return QtGui.QKeySequence(QtGui.QKeySequence.Copy).toString()

@default('shortcut_paste')
def _default_shortcut_paste(self):
return QtGui.QKeySequence(QtGui.QKeySequence.Paste).toString()

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

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


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

0 comments on commit 6f12aa5

Please sign in to comment.