From 881374a4642b59ed82ae8730b13a2fd858a4be69 Mon Sep 17 00:00:00 2001 From: jsbautista <42411448+jsbautista@users.noreply.github.com> Date: Sat, 14 Sep 2024 11:40:53 -0500 Subject: [PATCH] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Althviz Moré <16781833+dalthviz@users.noreply.github.com> --- qtconsole/console_widget.py | 42 +++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/qtconsole/console_widget.py b/qtconsole/console_widget.py index a14e43aa..70dbe60b 100644 --- a/qtconsole/console_widget.py +++ b/qtconsole/console_widget.py @@ -21,8 +21,7 @@ from traitlets.config.configurable import LoggingConfigurable from traitlets import Bool, Enum, Integer, Unicode -from traitlets import HasTraits, Unicode, observe - +from traitlets import default, HasTraits, Unicode, observe from .ansi_code_processor import QtAnsiCodeProcessor from .completion_widget import CompletionWidget @@ -45,17 +44,34 @@ def is_whitespace(char): #----------------------------------------------------------------------------- # Classes #----------------------------------------------------------------------------- + class ShortcutManager(HasTraits): - # Define traits for shortcuts - shortcut_print = Unicode('Ctrl+P').tag(config=True) - shortcut_select_all = Unicode('Ctrl+A').tag(config=True) - shortcut_cut = Unicode(QtGui.QKeySequence(QtGui.QKeySequence.Cut).toString()).tag(config=True) - shortcut_copy = Unicode(QtGui.QKeySequence(QtGui.QKeySequence.Copy).toString()).tag(config=True) - shortcut_paste = Unicode(QtGui.QKeySequence(QtGui.QKeySequence.Paste).toString()).tag(config=True) + """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) + + @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') + def _on_shortcut_changed(self, change): + print(f"Shortcut for {change['name']} changed to: {change['new']}") + - @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 @@ -338,7 +354,7 @@ def __init__(self, parent=None, **kw): # Configure actions. action = QtWidgets.QAction('Print', None) action.setEnabled(True) - printkey = self.shortcut_manager.shortcut_print + printkey = QtGui.QKeySequence(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. @@ -359,7 +375,7 @@ def __init__(self, parent=None, **kw): action = QtWidgets.QAction('Select All', None) action.setEnabled(True) - selectall = self.shortcut_manager.shortcut_select_all + selectall = QtGui.QKeySequence(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.