diff --git a/NodeGraphQt/base/menu.py b/NodeGraphQt/base/menu.py index 035568a4..73991074 100644 --- a/NodeGraphQt/base/menu.py +++ b/NodeGraphQt/base/menu.py @@ -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. @@ -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) @@ -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. @@ -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. @@ -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) diff --git a/NodeGraphQt/pkg_info.py b/NodeGraphQt/pkg_info.py index 3a9332ec..436bc3c4 100644 --- a/NodeGraphQt/pkg_info.py +++ b/NodeGraphQt/pkg_info.py @@ -1,6 +1,6 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -__version__ = '0.6.23' +__version__ = '0.6.24' __status__ = 'Work in Progress' __license__ = 'MIT'