Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] PR: Changes for handling Shortcuts with traitlets in consoleWidget #621

Open
wants to merge 53 commits into
base: main
Choose a base branch
from
Open
Changes from 7 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
b8e75a3
Changes for handling Shortcuts with traitlets in consoleWidget
jsbautista Sep 2, 2024
e200cae
Changes for handling Shortcuts with traitlets in consoleWidget
jsbautista Sep 2, 2024
92549cb
Changes for handling Shortcuts with traitlets in consoleWidget
jsbautista Sep 2, 2024
fce7517
Changes for handling Shortcuts with traitlets in consoleWidget
jsbautista Sep 2, 2024
52ea153
Changes for handling Shortcuts with traitlets in consoleWidget
jsbautista Sep 9, 2024
cfe85aa
Changes for handling Shortcuts with traitlets in consoleWidget
jsbautista Sep 9, 2024
43524ac
Changes for handling Shortcuts with traitlets in consoleWidget
jsbautista Sep 9, 2024
881374a
Apply suggestions from code review
jsbautista Sep 14, 2024
a989ba7
add shortcut save console Widget
jsbautista Sep 16, 2024
5d8cb80
shortcuts frontend
jsbautista Sep 16, 2024
f999c97
add shortcut save console Widget
jsbautista Sep 16, 2024
213eedc
shortcuts frontend
jsbautista Sep 16, 2024
83c5a6a
Fix observe
jsbautista Sep 16, 2024
6f12aa5
Changes for handling Shortcuts with traitlets in consoleWidget
jsbautista Sep 23, 2024
c21cc13
Changes for handling Shortcuts with traitlets in consoleWidget
jsbautista Sep 23, 2024
6410fdc
Changes for handling Shortcuts with traitlets in consoleWidget
jsbautista Sep 29, 2024
6f70a04
shortcuts frontend
jsbautista Sep 29, 2024
5d3c768
Clean code
jsbautista Sep 29, 2024
4152b0f
Clean code
jsbautista Sep 29, 2024
38bfe95
shortcuts for mainWindow
jsbautista Sep 30, 2024
3e28103
shortcuts for mainWindow
jsbautista Sep 30, 2024
1591daf
shortcuts for mainWindow
jsbautista Sep 30, 2024
28465a1
shortcuts for mainWindow
jsbautista Oct 1, 2024
ffaece1
shortcuts for mainWindow
jsbautista Oct 2, 2024
3f74b2e
shortcuts console widget
jsbautista Oct 2, 2024
d1cd581
Delete update shortcut
jsbautista Oct 8, 2024
e027971
Apply suggestions
jsbautista Oct 17, 2024
97fcb75
Merge branch 'jupyter:main' into shortcutTraitlets
jsbautista Oct 17, 2024
6b0bef1
Apply suggestions
jsbautista Oct 17, 2024
891f3a9
Merge branch 'shortcutTraitlets' of https://github.com/jsbautista/qtc…
jsbautista Oct 17, 2024
1b8afd2
add test shortcuts
jsbautista Oct 21, 2024
709ccc6
add test shortcuts
jsbautista Oct 28, 2024
f0ad735
Clean Code
jsbautista Nov 5, 2024
c117dfb
Merge branch 'jupyter:main' into shortcutTraitlets
jsbautista Nov 5, 2024
fb73b90
Clean Code
jsbautista Nov 5, 2024
535130a
Merge branch 'shortcutTraitlets' of https://github.com/jsbautista/qtc…
jsbautista Nov 5, 2024
449c6c7
Clean Code
jsbautista Nov 5, 2024
4a28546
Add linux shortcut
jsbautista Nov 5, 2024
d87bc52
Add linux shortcuts
jsbautista Nov 5, 2024
8e1672e
Add linux shortcuts
jsbautista Nov 5, 2024
b1da48e
Add linux shortcuts
jsbautista Nov 5, 2024
6c0b2f6
Add linux shortcuts
jsbautista Nov 5, 2024
b8f348c
Add linux shortcuts
jsbautista Nov 5, 2024
1c68f9e
Add linux shortcuts
jsbautista Nov 5, 2024
3ce3707
Add linux shortcuts
jsbautista Nov 5, 2024
48ab8e8
Add linux shortcuts
jsbautista Nov 5, 2024
de5a9b7
Add linux shortcuts
jsbautista Nov 5, 2024
18fe59f
Add linux shortcuts
jsbautista Nov 5, 2024
e9fc85e
fix matches mainwindow
jsbautista Nov 5, 2024
0d90363
fix matches mainwindow
jsbautista Nov 5, 2024
4e14958
add test shortcuts
jsbautista Nov 12, 2024
10a4a4e
add test shortcuts
jsbautista Nov 12, 2024
f72bdf4
Fix test shortcuts
jsbautista Nov 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

jsbautista marked this conversation as resolved.
Show resolved Hide resolved

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):
# 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)

@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']}")
jsbautista marked this conversation as resolved.
Show resolved Hide resolved

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
jsbautista marked this conversation as resolved.
Show resolved Hide resolved
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
jsbautista marked this conversation as resolved.
Show resolved Hide resolved
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_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'])

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.print_action.setShortcut(change['new'])
elif change['name'] == 'shortcut_select_all':
self.select_all_action.setShortcut(change['new'])
elif change['name'] == 'shortcut_cut':
self.cut_action.setShortcut(change['new'])
elif change['name'] == 'shortcut_copy':
self.copy_action.setShortcut(change['new'])
elif change['name'] == 'shortcut_paste':
self.paste_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
Loading