Skip to content

Commit 25c7643

Browse files
committed
address bug in issue #375
1 parent 44fb644 commit 25c7643

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

NodeGraphQt/base/menu.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ def add_menu(self, name):
112112
self._items.append(menu)
113113
return menu
114114

115+
@staticmethod
116+
def _set_shortcut(action, shortcut):
117+
if isinstance(shortcut, str):
118+
search = re.search(r'(?:\.|)QKeySequence\.(\w+)', shortcut)
119+
if search:
120+
shortcut = getattr(QtGui.QKeySequence, search.group(1))
121+
elif all([i in ['Alt', 'Enter'] for i in shortcut.split('+')]):
122+
shortcut = QtGui.QKeySequence(
123+
QtCore.Qt.ALT + QtCore.Qt.Key_Return
124+
)
125+
elif all([i in ['Return', 'Enter'] for i in shortcut.split('+')]):
126+
shortcut = QtCore.Qt.Key_Return
127+
if shortcut:
128+
action.setShortcut(shortcut)
129+
115130
def add_command(self, name, func=None, shortcut=None):
116131
"""
117132
Adds a command to the menu.
@@ -129,19 +144,8 @@ def add_command(self, name, func=None, shortcut=None):
129144
if LooseVersion(QtCore.qVersion()) >= LooseVersion('5.10'):
130145
action.setShortcutVisibleInContextMenu(True)
131146

132-
if isinstance(shortcut, str):
133-
search = re.search(r'(?:\.|)QKeySequence\.(\w+)', shortcut)
134-
if search:
135-
shortcut = getattr(QtGui.QKeySequence, search.group(1))
136-
elif all([i in ['Alt', 'Enter'] for i in shortcut.split('+')]):
137-
shortcut = QtGui.QKeySequence(
138-
QtCore.Qt.ALT + QtCore.Qt.Key_Return
139-
)
140-
elif all([i in ['Return', 'Enter'] for i in shortcut.split('+')]):
141-
shortcut = QtCore.Qt.Key_Return
142-
143147
if shortcut:
144-
action.setShortcut(shortcut)
148+
self._set_shortcut(action, shortcut)
145149
if func:
146150
action.executed.connect(func)
147151
self.qmenu.addAction(action)
@@ -178,7 +182,8 @@ class NodesMenu(NodeGraphMenu):
178182
nodes_menu = node_graph.get_context_menu('nodes')
179183
"""
180184

181-
def add_command(self, name, func=None, node_type=None, node_class=None):
185+
def add_command(self, name, func=None, node_type=None, node_class=None,
186+
shortcut=None):
182187
"""
183188
Re-implemented to add a command to the specified node type menu.
184189
@@ -187,6 +192,7 @@ def add_command(self, name, func=None, node_type=None, node_class=None):
187192
func (function): command function eg. "func(``graph``, ``node``)".
188193
node_type (str): specified node type for the command.
189194
node_class (class): specified node class for the command.
195+
shortcut (str): shortcut key.
190196
191197
Returns:
192198
NodeGraphQt.NodeGraphCommand: the appended command.
@@ -214,6 +220,9 @@ def add_command(self, name, func=None, node_type=None, node_class=None):
214220
action.graph = self._graph
215221
if LooseVersion(QtCore.qVersion()) >= LooseVersion('5.10'):
216222
action.setShortcutVisibleInContextMenu(True)
223+
224+
if shortcut:
225+
self._set_shortcut(action, shortcut)
217226
if func:
218227
action.executed.connect(func)
219228

0 commit comments

Comments
 (0)