Exemple #1
0
    def add_command(self, name, func=None, node_type=None, node_class=None):
        """
        Re-implemented to add a command to the specified node type menu.

        Args:
            name (str): command name.
            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.

        Returns:
            NodeGraphQt.NodeGraphCommand: the appended command.
        """
        if not node_type and not node_class:
            raise NodeMenuError('Node type or Node class not specified!')
        if node_class:
            node_type = node_class.__name__

        node_menu = self.qmenu.get_menu(node_type)
        if not node_menu:
            node_menu = BaseMenu(node_type, self.qmenu)

            if node_class:
                node_menu.node_class = node_class
                node_menu.graph = self._graph

            self.qmenu.addMenu(node_menu)

        if not self.qmenu.isEnabled():
            self.qmenu.setDisabled(False)

        action = NodeAction(name, self._graph.viewer())
        action.graph = self._graph
        if LooseVersion(QtCore.qVersion()) >= LooseVersion('5.10'):
            action.setShortcutVisibleInContextMenu(True)
        if func:
            action.executed.connect(func)

        if node_class:
            node_menus = self.qmenu.get_menus(node_class)
            if node_menu in node_menus:
                node_menus.remove(node_menu)
            for menu in node_menus:
                menu.addAction(action)

        qaction = node_menu.addAction(action)
        return NodeGraphCommand(self._graph, qaction)
Exemple #2
0
    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)
Exemple #3
0
    def add_command(self, name, func=None, shortcut=None):
        """
        Adds a command to the menu.

        Args:
            name (str): command name.
            func (function): command function eg. "func(``graph``)".
            shortcut (str): shotcut key.

        Returns:
            NodeGraphQt.NodeGraphCommand: the appended command.
        """
        action = GraphAction(name, self._graph.viewer())
        action.graph = self._graph
        if LooseVersion(QtCore.qVersion()) >= LooseVersion('5.10'):
            action.setShortcutVisibleInContextMenu(True)
        if shortcut:
            action.setShortcut(shortcut)
        if func:
            action.executed.connect(func)
        qaction = self.qmenu.addAction(action)
        return NodeGraphCommand(self._graph, qaction)
    def setupMenuBar(self, graph: NodeGraph):
        rootMenu = graph.context_menu()

        fileMenu = rootMenu.add_menu('&File')
        editMenu = rootMenu.add_menu('&Edit')

        # create "File" menu.
        fileMenu.add_command('Open Graph...',
                             lambda: actions._open_session(graph),
                             QtGui.QKeySequence.Open)
        fileMenu.add_command('Export Graph As...',
                             lambda: actions._save_session_as(graph),
                             'Alt+Shift+s')
        fileMenu.add_command('Clear', lambda: actions._clear_session(graph))

        fileMenu.add_separator()

        fileMenu.add_command('Zoom In', lambda: actions._zoom_in(graph), '=')
        fileMenu.add_command('Zoom Out', lambda: actions._zoom_out(graph), '-')
        fileMenu.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)
        editMenu.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)
        editMenu.qmenu.addAction(redo_actn)

        editMenu.add_separator()
        editMenu.add_command('Clear Undo History',
                             lambda: actions._clear_undo(graph))
        editMenu.add_separator()

        editMenu.add_command('Copy', graph.copy_nodes, QtGui.QKeySequence.Copy)
        editMenu.add_command('Paste', graph.paste_nodes,
                             QtGui.QKeySequence.Paste)
        editMenu.add_command(
            'Delete', lambda: graph.delete_nodes(graph.selected_nodes()),
            QtGui.QKeySequence.Delete)

        editMenu.add_separator()

        editMenu.add_command('Select all', graph.select_all, 'Ctrl+A')
        editMenu.add_command('Deselect all', graph.clear_selection,
                             'Ctrl+Shift+A')
        editMenu.add_command(
            'Enable/Disable',
            lambda: graph.disable_nodes(graph.selected_nodes()), 'd')

        editMenu.add_command(
            'Duplicate', lambda: graph.duplicate_nodes(graph.selected_nodes()),
            'Alt+c')
        editMenu.add_command('Center Selection', graph.fit_to_selection, 'f')

        editMenu.add_separator()

        menuBar = QtWidgets.QMenuBar()
        sessionMenu = QtWidgets.QMenu("Session")

        menuBar.addMenu(fileMenu.qmenu)
        menuBar.addMenu(editMenu.qmenu)
        menuBar.addMenu(sessionMenu)

        self.saveAction = QtWidgets.QAction("Save")
        self.saveAction.setShortcut(QtGui.QKeySequence.Save)
        self.saveAction.triggered.connect(self.onSave)

        self.saveAsAction = QtWidgets.QAction("Save As...")
        self.saveAsAction.setShortcut("Ctrl+Shift+S")
        self.saveAsAction.triggered.connect(self.onSaveAs)

        self.loadAction = QtWidgets.QAction("Load")
        self.loadAction.triggered.connect(self.onLoad)

        self.runAction = QtWidgets.QAction("Run")
        self.runAction.triggered.connect(self.onRun)
        self.runAction.setShortcut(QtGui.QKeySequence("R"))

        self.openInCode = QtWidgets.QAction("Show Code In Visual Studio Code")
        self.openInCode.triggered.connect(self.onOpenInVisualStudioCode)
        self.openInCode.setShortcut(QtGui.QKeySequence("Q"))

        sessionMenu.addAction(self.saveAction)
        sessionMenu.addAction(self.saveAsAction)
        sessionMenu.addAction(self.loadAction)
        sessionMenu.addAction(self.runAction)
        sessionMenu.addAction(self.openInCode)
        self.sessionMenu = sessionMenu

        self.viewMenu = QtWidgets.QMenu("View")
        menuBar.addMenu(self.viewMenu)

        self.window.verticalLayout.insertWidget(0, menuBar)
Exemple #5
0
def setup_context_menu(graph):
    """
    populate the specified graph's context menu with essential menus commands.

    example code:

    .. code-block:: python
        :linenos:

        from NodeGraphQt import NodeGraph, setup_context_menu

        graph = NodeGraph()
        setup_context_menu(graph)

    result:

    .. image:: _images/menu_hotkeys.png
        :width: 300px

    Args:
        graph (NodeGraphQt.NodeGraph): node graph.
    """
    root_menu = graph.get_context_menu('graph')

    file_menu = root_menu.add_menu('&File')
    edit_menu = root_menu.add_menu('&Edit')

    # create "File" menu.
    file_menu.add_command('Open...', _open_session, QtGui.QKeySequence.Open)
    file_menu.add_command('Import...', _import_session,
                          QtGui.QKeySequence.Open)
    file_menu.add_command('Save...', _save_session, QtGui.QKeySequence.Save)
    file_menu.add_command('Save As...', _save_session_as, 'Ctrl+Shift+s')
    file_menu.add_command('New Session', _new_session)

    file_menu.add_separator()

    file_menu.add_command('Zoom In', _zoom_in, '=')
    file_menu.add_command('Zoom Out', _zoom_out, '-')
    file_menu.add_command('Reset Zoom', _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', _clear_undo)
    edit_menu.add_separator()

    edit_menu.add_command('Copy', _copy_nodes, QtGui.QKeySequence.Copy)
    edit_menu.add_command('Paste', _paste_nodes, QtGui.QKeySequence.Paste)
    edit_menu.add_command('Delete', _delete_items, QtGui.QKeySequence.Delete)

    edit_menu.add_separator()

    edit_menu.add_command('Select all', _select_all_nodes, 'Ctrl+A')
    edit_menu.add_command('Deselect all', _clear_node_selection,
                          'Ctrl+Shift+A')
    edit_menu.add_command('Enable/Disable', _disable_nodes, 'd')

    edit_menu.add_command('Duplicate', _duplicate_nodes, 'Alt+c')
    edit_menu.add_command('Center Selection', _fit_to_selection, 'f')

    edit_menu.add_separator()
Exemple #6
0
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()