Skip to content

Commit

Permalink
Merge pull request #381 from jchanvfx/context_menu_shotcut_fix_#375
Browse files Browse the repository at this point in the history
Context menu shotcut fix #375
  • Loading branch information
jchanvfx committed Sep 13, 2023
2 parents 44fb644 + 09578c2 commit 4a645b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
35 changes: 22 additions & 13 deletions NodeGraphQt/base/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ def add_menu(self, name):
self._items.append(menu)
return menu

@staticmethod
def _set_shortcut(action, shortcut):
if isinstance(shortcut, str):
search = re.search(r'(?:\.|)QKeySequence\.(\w+)', shortcut)
if search:
shortcut = getattr(QtGui.QKeySequence, search.group(1))
elif all([i in ['Alt', 'Enter'] for i in shortcut.split('+')]):
shortcut = QtGui.QKeySequence(
QtCore.Qt.ALT + QtCore.Qt.Key_Return
)
elif all([i in ['Return', 'Enter'] for i in shortcut.split('+')]):
shortcut = QtCore.Qt.Key_Return
if shortcut:
action.setShortcut(shortcut)

def add_command(self, name, func=None, shortcut=None):
"""
Adds a command to the menu.
Expand All @@ -129,19 +144,8 @@ def add_command(self, name, func=None, shortcut=None):
if LooseVersion(QtCore.qVersion()) >= LooseVersion('5.10'):
action.setShortcutVisibleInContextMenu(True)

if isinstance(shortcut, str):
search = re.search(r'(?:\.|)QKeySequence\.(\w+)', shortcut)
if search:
shortcut = getattr(QtGui.QKeySequence, search.group(1))
elif all([i in ['Alt', 'Enter'] for i in shortcut.split('+')]):
shortcut = QtGui.QKeySequence(
QtCore.Qt.ALT + QtCore.Qt.Key_Return
)
elif all([i in ['Return', 'Enter'] for i in shortcut.split('+')]):
shortcut = QtCore.Qt.Key_Return

if shortcut:
action.setShortcut(shortcut)
self._set_shortcut(action, shortcut)
if func:
action.executed.connect(func)
self.qmenu.addAction(action)
Expand Down Expand Up @@ -178,7 +182,8 @@ class NodesMenu(NodeGraphMenu):
nodes_menu = node_graph.get_context_menu('nodes')
"""

def add_command(self, name, func=None, node_type=None, node_class=None):
def add_command(self, name, func=None, node_type=None, node_class=None,
shortcut=None):
"""
Re-implemented to add a command to the specified node type menu.
Expand All @@ -187,6 +192,7 @@ def add_command(self, name, func=None, node_type=None, node_class=None):
func (function): command function eg. "func(``graph``, ``node``)".
node_type (str): specified node type for the command.
node_class (class): specified node class for the command.
shortcut (str): shortcut key.
Returns:
NodeGraphQt.NodeGraphCommand: the appended command.
Expand Down Expand Up @@ -214,6 +220,9 @@ def add_command(self, name, func=None, node_type=None, node_class=None):
action.graph = self._graph
if LooseVersion(QtCore.qVersion()) >= LooseVersion('5.10'):
action.setShortcutVisibleInContextMenu(True)

if shortcut:
self._set_shortcut(action, shortcut)
if func:
action.executed.connect(func)

Expand Down
2 changes: 1 addition & 1 deletion NodeGraphQt/pkg_info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
__version__ = '0.6.23'
__version__ = '0.6.24'
__status__ = 'Work in Progress'
__license__ = 'MIT'

Expand Down

0 comments on commit 4a645b4

Please sign in to comment.