def add_command(self, name, func=None, shortcut=None): """ Adds a command to the menu. Args: name (str): command name. func (function): command function. shortcut (str): function shotcut key. Returns: NodeGraphQt.MenuCommand: the appended command. """ action = QtWidgets.QAction(name, self.__viewer) if LooseVersion(QtCore.qVersion()) >= LooseVersion('5.10'): action.setShortcutVisibleInContextMenu(True) if shortcut: action.setShortcut(shortcut) if func: action.triggered.connect(func) qaction = self.qmenu.addAction(action) return MenuCommand(self.__viewer, qaction)
def setup_context_menu(graph): """ Sets up the node graphs context menu with some basic menus and commands. Args: graph (NodeGraphQt.NodeGraph): node graph. """ root_menu = graph.context_menu() file_menu = root_menu.add_menu('&File') edit_menu = root_menu.add_menu('&Edit') # create "File" menu. file_menu.add_command('Open...', lambda: _open_session(graph), QtGui.QKeySequence.Open) file_menu.add_command('Save...', lambda: _save_session(graph), QtGui.QKeySequence.Save) file_menu.add_command('Save As...', lambda: _save_session_as(graph), 'Ctrl+Shift+s') file_menu.add_command('Clear', lambda: _clear_session(graph)) file_menu.add_separator() file_menu.add_command('Zoom In', lambda: _zoom_in(graph), '=') file_menu.add_command('Zoom Out', lambda: _zoom_out(graph), '-') file_menu.add_command('Reset Zoom', graph.reset_zoom, 'h') # create "Edit" menu. undo_actn = graph.undo_stack().createUndoAction(graph.viewer(), '&Undo') if LooseVersion(QtCore.qVersion()) >= LooseVersion('5.10'): undo_actn.setShortcutVisibleInContextMenu(True) undo_actn.setShortcuts(QtGui.QKeySequence.Undo) edit_menu.qmenu.addAction(undo_actn) redo_actn = graph.undo_stack().createRedoAction(graph.viewer(), '&Redo') if LooseVersion(QtCore.qVersion()) >= LooseVersion('5.10'): redo_actn.setShortcutVisibleInContextMenu(True) redo_actn.setShortcuts(QtGui.QKeySequence.Redo) edit_menu.qmenu.addAction(redo_actn) edit_menu.add_separator() edit_menu.add_command('Clear Undo History', lambda: _clear_undo(graph)) edit_menu.add_separator() edit_menu.add_command('Copy', graph.copy_nodes, QtGui.QKeySequence.Copy) edit_menu.add_command('Paste', graph.paste_nodes, QtGui.QKeySequence.Paste) edit_menu.add_command('Delete', lambda: graph.delete_nodes(graph.selected_nodes()), QtGui.QKeySequence.Delete) edit_menu.add_separator() edit_menu.add_command('Select all', graph.select_all, 'Ctrl+A') edit_menu.add_command('Deselect all', graph.clear_selection, 'Ctrl+Shift+A') edit_menu.add_command('Enable/Disable', lambda: graph.disable_nodes(graph.selected_nodes()), 'd') edit_menu.add_command( 'Duplicate', lambda: graph.duplicate_nodes(graph.selected_nodes()), 'Alt+c') edit_menu.add_command('Center Selection', graph.fit_to_selection, 'f') edit_menu.add_separator()