Exemplo n.º 1
0
 def get_plugin_actions(self):
     """Return a list of actions related to plugin"""
     quit_action = create_action(self, _("&Quit"),
                                 icon=ima.icon('exit'), 
                                 tip=_("Quit"),
                                 triggered=self.quit)
     self.register_shortcut(quit_action, "_", "Quit", "Ctrl+Q")
     run_action = create_action(self, _("&Run..."), None,
                         ima.icon('run_small'),
                         _("Run a Python script"),
                         triggered=self.run_script)
     environ_action = create_action(self,
                         _("Environment variables..."),
                         icon=ima.icon('environ'),
                         tip=_("Show and edit environment variables"
                                     " (for current session)"),
                         triggered=self.show_env)
     syspath_action = create_action(self,
                         _("Show sys.path contents..."),
                         icon=ima.icon('syspath'),
                         tip=_("Show (read-only) sys.path"),
                         triggered=self.show_syspath)
     buffer_action = create_action(self,
                         _("Buffer..."), None,
                         tip=_("Set maximum line count"),
                         triggered=self.change_max_line_count)
     exteditor_action = create_action(self,
                         _("External editor path..."), None, None,
                         _("Set external editor executable path"),
                         triggered=self.change_exteditor)
     wrap_action = create_action(self,
                         _("Wrap lines"),
                         toggled=self.toggle_wrap_mode)
     wrap_action.setChecked(self.get_option('wrap'))
     calltips_action = create_action(self, _("Display balloon tips"),
         toggled=self.toggle_calltips)
     calltips_action.setChecked(self.get_option('calltips'))
     codecompletion_action = create_action(self,
                                       _("Automatic code completion"),
                                       toggled=self.toggle_codecompletion)
     codecompletion_action.setChecked(self.get_option('codecompletion/auto'))
     codecompenter_action = create_action(self,
                                 _("Enter key selects completion"),
                                 toggled=self.toggle_codecompletion_enter)
     codecompenter_action.setChecked(self.get_option(
                                                 'codecompletion/enter_key'))
     
     option_menu = QMenu(_('Internal console settings'), self)
     option_menu.setIcon(ima.icon('tooloptions'))
     add_actions(option_menu, (buffer_action, wrap_action,
                               calltips_action, codecompletion_action,
                               codecompenter_action, exteditor_action))
                 
     plugin_actions = [None, run_action, environ_action, syspath_action,
                       option_menu, None, quit_action]
     
     # Add actions to context menu
     add_actions(self.shell.menu, plugin_actions)
     
     return plugin_actions
Exemplo n.º 2
0
 def get_plugin_actions(self):
     """Return a list of actions related to plugin"""
     quit_action = create_action(self, _("&Quit"),
                                 icon=ima.icon('exit'), 
                                 tip=_("Quit"),
                                 triggered=self.quit)
     self.register_shortcut(quit_action, "_", "Quit", "Ctrl+Q")
     run_action = create_action(self, _("&Run..."), None,
                         ima.icon('run_small'),
                         _("Run a Python script"),
                         triggered=self.run_script)
     environ_action = create_action(self,
                         _("Environment variables..."),
                         icon=ima.icon('environ'),
                         tip=_("Show and edit environment variables"
                                     " (for current session)"),
                         triggered=self.show_env)
     syspath_action = create_action(self,
                         _("Show sys.path contents..."),
                         icon=ima.icon('syspath'),
                         tip=_("Show (read-only) sys.path"),
                         triggered=self.show_syspath)
     buffer_action = create_action(self,
                         _("Buffer..."), None,
                         tip=_("Set maximum line count"),
                         triggered=self.change_max_line_count)
     exteditor_action = create_action(self,
                         _("External editor path..."), None, None,
                         _("Set external editor executable path"),
                         triggered=self.change_exteditor)
     wrap_action = create_action(self,
                         _("Wrap lines"),
                         toggled=self.toggle_wrap_mode)
     wrap_action.setChecked(self.get_option('wrap'))
     calltips_action = create_action(self, _("Display balloon tips"),
         toggled=self.toggle_calltips)
     calltips_action.setChecked(self.get_option('calltips'))
     codecompletion_action = create_action(self,
                                       _("Automatic code completion"),
                                       toggled=self.toggle_codecompletion)
     codecompletion_action.setChecked(self.get_option('codecompletion/auto'))
     codecompenter_action = create_action(self,
                                 _("Enter key selects completion"),
                                 toggled=self.toggle_codecompletion_enter)
     codecompenter_action.setChecked(self.get_option(
                                                 'codecompletion/enter_key'))
     
     option_menu = QMenu(_('Internal console settings'), self)
     option_menu.setIcon(ima.icon('tooloptions'))
     add_actions(option_menu, (buffer_action, wrap_action,
                               calltips_action, codecompletion_action,
                               codecompenter_action, exteditor_action))
                 
     plugin_actions = [None, run_action, environ_action, syspath_action,
                       option_menu, None, quit_action]
     
     # Add actions to context menu
     add_actions(self.shell.menu, plugin_actions)
     
     return plugin_actions
Exemplo n.º 3
0
    def add_toggle_view_action_to_menu(self, toggle_view_action: QAction,
                                       group: str, group_icon: QIcon) -> QAction:
        '''
        Adds a toggle view action to the the internal view menu. You can either
        manage the insertion of the toggle view actions in your application or
        you can add the actions to the internal view menu and then simply
        insert the menu object into your.

        Parameters
        ----------
        toggle_view_action : QAction
        group : str
        group_icon : QIcon

        Returns
        -------
        value : QAction
        '''
        order = self._mgr.menu_insertion_order
        alphabetically_sorted = (
            InsertionOrder.by_spelling == order
        )

        if not group:
            self._mgr.add_action_to_menu(toggle_view_action,
                                         self._mgr.view_menu,
                                         alphabetically_sorted)
            return toggle_view_action

        try:
            group_menu = self._mgr.view_menu_groups[group]
        except KeyError:
            group_menu = QMenu(group, self)
            group_menu.setIcon(group_icon)
            self._mgr.add_action_to_menu(
                group_menu.menuAction(), self._mgr.view_menu,
                alphabetically_sorted)
            self._mgr.view_menu_groups[group] = group_menu

        self._mgr.add_action_to_menu(toggle_view_action, group_menu,
                                     alphabetically_sorted)
        return group_menu.menuAction()
Exemplo n.º 4
0
    def get_plugin_actions(self):
        """Get plugin actions."""
        new_terminal_menu = QMenu(_("Create new terminal"), self)
        new_terminal_menu.setIcon(ima.icon('project_expanded'))
        new_terminal_cwd = create_action(self,
                                         _("Current workspace path"),
                                         icon=ima.icon('cmdprompt'),
                                         tip=_("Sets the pwd at "
                                               "the current workspace "
                                               "folder"),
                                         triggered=self.create_new_term)

        self.new_terminal_project = create_action(
            self,
            _("Current project folder"),
            icon=ima.icon('cmdprompt'),
            tip=_("Sets the pwd at "
                  "the current project "
                  "folder"),
            triggered=lambda: self.create_new_term(path=self.project_path))

        if self.project_path is None:
            self.new_terminal_project.setEnabled(False)

        new_terminal_file = create_action(
            self,
            _("Current opened file folder"),
            icon=ima.icon('cmdprompt'),
            tip=_("Sets the pwd at "
                  "the folder that contains "
                  "the current opened file"),
            triggered=lambda: self.create_new_term(path=self.current_file_path
                                                   ))
        add_actions(
            new_terminal_menu,
            (new_terminal_cwd, self.new_terminal_project, new_terminal_file))
        self.menu_actions = [None, new_terminal_menu, None]
        return self.menu_actions
Exemplo n.º 5
0
 def _set_bpm_menu(self, sec):
     cmd = ['sirius-hla-as-di-bpm.py', sec]
     menu = QMenu('BPMs', self)
     menu.setObjectName(sec.upper() + 'App')
     menu.setIcon(qta.icon('mdi.currency-sign'))
     action = menu.addAction('Monitor')
     action.setIcon(util.get_monitor_icon('mdi.currency-sign'))
     self.connect_newprocess(action, cmd + [
         '-w',
         'Monitor',
     ])
     typs = ('Single Pass', 'Multi Turn')
     acts = ('SPass', 'MTurn')
     for typ, act in zip(typs, acts):
         if sec in {'bo', 'si'}:
             menu2 = menu.addMenu(typ)
             menu2.setObjectName(sec.upper() + 'App')
             if act == 'SPass':
                 self._create_bpm_actions(sec, menu2, act, cmd)
             else:
                 for mode in ('Antennas', 'Positions'):
                     menu3 = menu2.addMenu(mode)
                     menu3.setObjectName(sec.upper() + 'App')
                     cmd2 = cmd + ['-m', mode]
                     self._create_bpm_actions(sec, menu3, act, cmd2)
         else:
             if act == 'SPass':
                 self._create_bpm_actions(sec, menu, act, cmd, typ)
             else:
                 menu2 = menu.addMenu(typ)
                 menu2.setObjectName(sec.upper() + 'App')
                 for mode in ('Antennas', 'Positions'):
                     cmd2 = cmd + ['-m', mode]
                     self._create_bpm_actions(sec, menu2, act, cmd2,
                                              mode)
     return menu
Exemplo n.º 6
0
class JupyterNotebook(BaseExtension):
    """Handles various things related to JupyterLab:
    
    - Parses source code into cells, which are used for code execution by the
      OpenSesameIDE extension.
    - Allows code to be exported to, and imported from, ipynb format.
    - Allows JupyterLab to launched.
    """

    preferences_ui = 'extensions.JupyterNotebook.preferences'

    def event_startup(self):
        self._widget = None

    def event_ide_menubar_initialized(self, menubar):
        self._menu = QMenu(_('Import and export'), self.main_window)
        self._menu.setIcon(self.theme.qicon('text-x-script'))
        self._menu.addAction(
            self.qaction(
                u'document-open',
                _('Import notebook'),
                self._import_ipynb,
            ))
        self._menu.addSeparator()
        self._menu.addAction(
            self.qaction(u'document-save', _('Export notebook'),
                         self._export_ipynb))
        self._menu.addAction(
            self.qaction(u'document-save', _('Export pdf'), self._export_pdf))
        self._menu.addAction(
            self.qaction(u'document-save', _('Export html'),
                         self._export_html))
        self._menu.addAction(
            self.qaction(u'document-save', _('Export docx'),
                         self._export_docx))
        menubar._menu_file.insertMenu(menubar._action_quit, self._menu)
        menubar._menu_file.insertSeparator(menubar._action_quit)

    def event_close(self):

        if self._widget is None:
            return
        self._widget.kill()

    def activate(self):

        self.tabwidget.add(self.widget(), self.icon(), self.label())

    def widget(self):
        """A simple widget for launching and killing jupyterlab."""

        if self._widget is None:
            self.set_busy()
            from jupyter_widget import LaunchJupyterLabWidget
            self._widget = LaunchJupyterLabWidget(self.main_window, self)
            self.set_busy(False)
        return self._widget

    def provide_jupyter_notebook_cells(self, code=u'', cell_types=None):
        """Dynamically loads a parser function for the language of the current
        editor, and uses this to parse the code into cells. The `cell_types`
        keyword allows specific kinds of cells to be returned.
        """

        language = self.extension_manager.provide('ide_current_language')
        try:
            m = importlib.import_module(
                'jupyter_notebook_cell_parsers.parse_{}'.format(language))
        except ModuleNotFoundError:
            oslogger.debug('no cell parser for language {}'.format(language))
            return
        return getattr(m, 'parse_{}'.format(language))(code, cell_types)

    def provide_open_file_extension_ipynb(self):
        """A provider for directly opening .ipynb files."""

        return self._import_ipynb, _('Import as script')

    def _import_ipynb(self, path=None):
        """Import an ipynb file from path and return it as plain-text code."""

        from jupyter_notebook_cell_parsers import parse_nbformat

        if not isinstance(path, basestring):
            path = QFileDialog.getOpenFileName(
                self.main_window,
                _(u'Open Jupyter/ IPython Notebook'),
                filter=u'Notebooks (*.ipynb)',
                directory=cfg.file_dialog_path)
        if isinstance(path, tuple):
            path = path[0]
        if not path:
            return
        cfg.file_dialog_path = os.path.dirname(path)
        try:
            language, code = parse_nbformat.notebook_to_code(
                path, self.extension_manager.provide('image_writer'))
        except Exception as e:
            self.extension_manager.fire(
                u'notify',
                message=_(
                    u'Failed to read notebook. See console for details.'))
            self.console.write(e)
            return
        if language.lower() == 'r':
            ext = '.R'
        elif language.lower() == 'python':
            ext = '.py'
        else:
            ext = None
        self.extension_manager.fire(u'ide_new_file', source=code, ext=ext)
        self.extension_manager.fire(u'image_annotations_detect', code=code)

    def _export(self, dialog_title, dialog_filter, ext):
        """Export the code to an .ipynb file."""

        from jupyter_notebook_cell_parsers import parse_nbformat

        language = self.extension_manager.provide('ide_current_language')
        path = QFileDialog.getSaveFileName(self.main_window,
                                           dialog_title,
                                           filter=dialog_filter,
                                           directory=cfg.file_dialog_path)
        if isinstance(path, tuple):
            path = path[0]
        if not path:
            return None, None
        cfg.file_dialog_path = os.path.dirname(path)
        if not path.lower().endswith(ext):
            path += ext
        if path.lower().endswith('.ipynb'):
            ipynb_path = path
        else:
            ipynb_path = os.path.splitext(path)[0] + '.ipynb'
        parse_nbformat.cells_to_notebook(
            self.provide_jupyter_notebook_cells(
                self.extension_manager.provide('ide_current_source')),
            ipynb_path, language)
        return path, ipynb_path

    def _run(self, cmd):

        self.main_window.set_busy(True)
        self.extension_manager.fire('jupyter_run_system_command', cmd=cmd)
        while self.extension_manager.provide('jupyter_kernel_running'):
            QApplication.processEvents()
            time.sleep(0.1)
        self.main_window.set_busy(False)

    def _to_html(self, ipynb_path):

        # Square brackets need to be escaped, because nbconverts interprets
        # them as glob patterns
        ipynb_path = re.sub(r'(?P<bracket>[\[\]])', '[\g<bracket>]',
                            ipynb_path)
        self._run('jupyter nbconvert "{}" --to html'.format(ipynb_path))
        return os.path.splitext(ipynb_path)[0] + '.html'

    def _export_ipynb(self):
        """Export the code to an .ipynb file."""

        self._export(dialog_title=_(u'Export to Jupyter/ IPython Notebook'),
                     dialog_filter=u'Notebooks (*.ipynb)',
                     ext='.ipynb')

    def _export_pdf(self):
        path, ipynb_path = self._export(dialog_title=_(u'Export to pdf'),
                                        dialog_filter=u'pdf (*.pdf)',
                                        ext='.pdf')
        if path is None:
            return
        html_path = self._to_html(ipynb_path)
        self._run('pandoc "{}" -o "{}"'.format(html_path, path))
        misc.open_url(path)

    def _export_html(self):
        path, ipynb_path = self._export(dialog_title=_(u'Export to HTML'),
                                        dialog_filter=u'html (*.html)',
                                        ext='.html')
        if path is None:
            return
        html_path = self._to_html(ipynb_path)
        misc.open_url(html_path)

    def _export_docx(self):
        path, ipynb_path = self._export(dialog_title=_(u'Export to .docx'),
                                        dialog_filter=u'docx (*.docx)',
                                        ext='.docx')
        if path is None:
            return
        html_path = self._to_html(ipynb_path)
        self._run('pandoc "{}" -o "{}"'.format(html_path, path))
        misc.open_url(path)
Exemplo n.º 7
0
class MenuBar(QMenuBar):
    def __init__(self, parent, ide):

        super(MenuBar, self).__init__(parent)
        self._ide = ide
        self._cfg_actions = {}
        # File menu
        self._action_new_file = self._action(_(u'&New'), u'document-new',
                                             u'Ctrl+N', ide.new_file)
        self._action_open_file = self._action(_(u'&Open…'), u'document-open',
                                              u'Ctrl+O',
                                              ide.quick_select_files)
        self._action_open_folder = self._action(_(u'Open &folder…'), u'folder',
                                                u'Ctrl+Shift+O',
                                                ide.quick_select_folders)
        self._action_close_all_folders = self._action(_(u'&Close all folders'),
                                                      u'folder', None,
                                                      ide.close_all_folders)
        self._action_save_file = self._action(_(u'&Save'), u'document-save',
                                              u'Ctrl+S', ide.save_file)
        self._action_save_file_as = self._action(_(u'Save &as…'),
                                                 u'document-save-as',
                                                 u'Ctrl+Shift+S',
                                                 ide.save_file_as)
        self._action_quit = self._action(_(u'&Quit'), u'window-close',
                                         u'Alt+F4', ide.main_window.close)
        self._menu_file = self.addMenu(_(u'&File'))
        self._menu_file.addAction(self._action_new_file)
        self._menu_file.addAction(self._action_open_file)
        self._menu_file.addAction(self._action_save_file)
        self._menu_file.addAction(self._action_save_file_as)
        self._menu_file.addAction(self._action_open_folder)
        self._menu_file.addSeparator()
        self._menu_file.addAction(self._action_close_all_folders)
        self._menu_file.addSeparator()
        self._menu_file.addAction(self._action_quit)
        # Edit menu
        self._menu_edit = self.addMenu(_(u'&Edit'))
        self._menu_edit.aboutToShow.connect(self._show_edit_menu)
        self._menu_edit.aboutToHide.connect(self._menu_edit.clear)
        # Tools menu
        self._action_preferences = self._action(_(u'&Preferences'),
                                                u'preferences-system', None,
                                                ide.tabwidget.open_preferences)
        self._action_plugins = self._action(_(u'P&lugins'),
                                            u'preferences-system', None,
                                            ide.open_plugin_manager)
        self._menu_tools = self.addMenu(_(u'&Tools'))
        self._menu_tools.addAction(self._action_preferences)
        self._menu_tools.addAction(self._action_plugins)
        self._menu_tools.addSeparator()
        self._action_jupyter_notebook = self._add_extension_action(
            'JupyterNotebook', menu=self._menu_tools)
        self._action_git_gui = self._add_extension_action(
            'GitGUI', menu=self._menu_tools)
        self._action_symbol_selector = self._add_extension_action(
            'SymbolSelector', menu=self._menu_tools, separate=True)
        self._action_find_in_files = self._add_extension_action(
            'FindInFiles', menu=self._menu_tools, separate=False)
        self._action_command_palette = self._add_extension_action(
            'CommandPalette', menu=self._menu_tools, separate=False)
        self._action_spellcheck = self._add_extension_action(
            'SpellCheck', menu=self._menu_tools, separate=False)
        self._action_word_count = self._add_extension_action(
            'WordCount', menu=self._menu_tools, separate=False)
        # Editor menu (submenu of view)
        self._menu_editor = QMenu(_('&Editor'), self)
        self._menu_editor.setIcon(
            self._ide.theme.qicon(u'accessories-text-editor'))
        self._action_toggle_line_wrap = self._cfg_action(
            self._menu_editor, _(u'Wrap lines'), u'pyqode_line_wrap')
        self._action_toggle_whitespaces = self._cfg_action(
            self._menu_editor, _(u'Show whitespace'),
            u'pyqode_show_whitespaces')
        self._action_toggle_line_numbers = self._cfg_action(
            self._menu_editor, _(u'Show line numbers'),
            u'pyqode_show_line_numbers')
        self._action_toggle_tab_bar = self._action(
            _(u'Show editor tabs'),
            None,
            None,
            self._toggle_show_tab_bar,
            checkable=True,
            checked=cfg.opensesame_ide_show_tab_bar)
        self._action_select_indentation_mode = self._action(
            _(u'Select indentation mode'), u'accessories-text-editor', None,
            self._select_indentation_mode)
        self._action_toggle_code_folding = self._cfg_action(
            self._menu_editor, _(u'Code folding'), u'pyqode_code_folding')
        self._action_toggle_right_margin = self._cfg_action(
            self._menu_editor, _(u'Show right margin'), u'pyqode_right_margin')
        self._action_toggle_fixed_width = self._cfg_action(
            self._menu_editor, _(u'Fixed editor width'), u'pyqode_fixed_width')
        self._action_toggle_code_completion = self._cfg_action(
            self._menu_editor, _(u'Code completion'),
            u'pyqode_code_completion')
        self._menu_editor.addAction(self._action_toggle_tab_bar)
        self._menu_editor.addAction(self._action_select_indentation_mode)
        # Tabs menu (submenu of view)
        self._menu_tabs = QMenu(_('&Tabs'), self)
        self._menu_tabs.setIcon(
            self._ide.theme.qicon(u'accessories-text-editor'))
        self._action_close_tab = self._action(
            _(u'&Close tab'), u'window-close',
            cfg.opensesame_ide_shortcut_close_tab, ide.close_tab)
        self._action_close_other_tabs = self._action(
            _(u'Close &other tabs'), u'window-close',
            cfg.opensesame_ide_shortcut_close_other_tabs, ide.close_other_tabs)
        self._action_close_all_tabs = self._action(
            _(u'Close &all tabs'), u'window-close',
            cfg.opensesame_ide_shortcut_close_all_tabs, ide.close_all_tabs)
        self._action_split_vertical = self._action(
            _(u'Split &vertical'), u'go-down',
            cfg.opensesame_ide_shortcut_split_vertical, ide.split_vertical)
        self._action_split_horizontal = self._action(
            _(u'Split &horizontal'), u'go-next',
            cfg.opensesame_ide_shortcut_split_horizontal, ide.split_horizontal)
        self._action_switch_splitter_previous = self._action(
            _(u'Switch to previous panel'), u'go-previous',
            cfg.opensesame_ide_shortcut_switch_previous_panel,
            ide.switch_splitter_previous)
        self._action_switch_splitter_next = self._action(
            _(u'Switch to next panel'), u'go-next',
            cfg.opensesame_ide_shortcut_switch_next_panel,
            ide.switch_splitter_next)
        self._menu_tabs.addAction(self._action_close_tab)
        self._menu_tabs.addAction(self._action_close_other_tabs)
        self._menu_tabs.addAction(self._action_close_all_tabs)
        self._menu_tabs.addSeparator()
        self._menu_tabs.addAction(self._action_split_vertical)
        self._menu_tabs.addAction(self._action_split_horizontal)
        self._menu_tabs.addAction(self._action_switch_splitter_previous)
        self._menu_tabs.addAction(self._action_switch_splitter_next)
        # View menu
        self._action_toggle_fullscreen = self._action(
            _(u'Toggle fullscreen'),
            u'view-fullscreen',
            cfg.opensesame_ide_shortcut_toggle_fullscreen,
            ide._toggle_fullscreen,
            checkable=True)
        self._action_toggle_folder_browsers = self._action(
            _(u'Toggle &folder browsers'),
            u'os-overview',
            cfg.opensesame_ide_shortcut_toggle_folder_browsers,
            ide.toggle_folder_browsers,
            checkable=True)
        self._action_locate_file_in_folder = self._action(
            _(u'&Locate active file'), u'folder',
            cfg.opensesame_ide_shortcut_locate_active_file,
            ide.locate_file_in_folder)
        self._menu_view = self.addMenu(_('&View'))
        self._menu_view.addMenu(self._menu_editor)
        self._menu_view.addMenu(self._menu_tabs)
        self._menu_view.addSeparator()
        self._menu_view.addAction(self._action_toggle_fullscreen)
        self._menu_view.addSeparator()
        self._menu_view.addAction(self._action_toggle_folder_browsers)
        self._menu_view.addAction(self._action_locate_file_in_folder)
        self._action_toggle_console = self._add_extension_action(
            'JupyterConsole', menu=self._menu_view, separate=True)
        self._action_toggle_workspace = self._add_extension_action(
            'WorkspaceExplorer',
            menu=self._menu_view,
        )
        # Run menu
        self._menu_run = self.addMenu(_('&Run'))
        self._action_run_current_file = self._action(
            _(u'&Run project or file'),
            u'os-run',
            cfg.opensesame_ide_shortcut_run_file,
            ide.run_current_file,
        )
        self._action_run_current_selection = self._action(
            _(u'Run &selection, cell, or current line'),
            u'os-run-quick',
            cfg.opensesame_ide_shortcut_run_selection,
            ide.run_current_selection,
        )
        self._action_run_from_current_position = self._action(
            _(u'Run &from current position'),
            u'os-run-quick',
            cfg.opensesame_ide_shortcut_run_from_current_position,
            ide.run_from_current_position,
        )
        self._action_run_up_to_current_position = self._action(
            _(u'Run &up to current position'),
            u'os-run-quick',
            cfg.opensesame_ide_shortcut_run_up_to_current_position,
            ide.run_up_to_current_position,
        )
        self._action_run_debug = self._action(
            _(u'Run file in &debugger'),
            u'os-run',
            cfg.opensesame_ide_shortcut_run_debug,
            ide.run_debug,
        )
        self._action_toggle_breakpoint = self._action(
            _(u'&Toggle breakpoint'),
            u'list-add',
            cfg.opensesame_ide_shortcut_toggle_breakpoint,
            ide.toggle_breakpoint,
        )
        self._action_clear_breakpoints = self._action(
            _(u'Clear &breakpoints'),
            u'list-remove',
            cfg.opensesame_ide_shortcut_clear_breakpoints,
            ide.clear_breakpoints,
        )
        self._action_run_interrupt = self._action(
            _(u'&Interrupt kernel'),
            u'os-kill',
            cfg.opensesame_ide_shortcut_run_interrupt,
            ide.run_interrupt,
        )
        self._action_run_restart = self._action(
            _(u'Restart &kernel'),
            u'view-refresh',
            None,
            ide.run_restart,
        )
        self._action_change_working_directory = self._action(
            _(u'Change &working directory to active file'),
            u'folder-open',
            cfg.opensesame_ide_shortcut_change_working_directory,
            ide.change_working_directory,
        )
        # Output menu (submenu of run)
        self._menu_output = QMenu(_('&Capture output'), self)
        self._menu_output.setIcon(self._ide.theme.qicon(u'os-debug'))
        self._action_no_capture = self._action(
            _(u'Don\'t capture output'),
            None,
            None,
            self._image_annotations_no_capture,
            checkable=True,
            checked=not cfg.image_annotations_enabled)
        self._action_capture_images = self._action(
            _(u'Capture images'),
            None,
            None,
            self._image_annotations_capture_images,
            checkable=True,
            checked=cfg.image_annotations_enabled and \
                not cfg.image_annotations_capture_output
        )
        self._action_capture_images_and_text = self._action(
            _(u'Capture images and text'),
            None,
            None,
            self._image_annotations_capture_images_and_text,
            checkable=True,
            checked=cfg.image_annotations_capture_output)
        self._action_clear_output = self._action(
            _(u'Clear output'), u'edit-clear', None, lambda: self._ide.
            extension_manager.fire('image_annotations_clear_output'))
        self._menu_output.addAction(self._action_no_capture)
        self._menu_output.addAction(self._action_capture_images)
        self._menu_output.addAction(self._action_capture_images_and_text)
        self._menu_output.addSeparator()
        self._menu_output.addAction(self._action_clear_output)
        # Logging menu (submeny of run)
        self._menu_logging = QMenu(_(u'&Logging level'), self)
        self._menu_logging.setIcon(self._ide.theme.qicon('text-x-script'))
        self._menu_logging.aboutToShow.connect(self._show_logging_menu)
        # Run menu
        self._menu_run.addAction(self._action_run_current_file)
        self._menu_run.addAction(self._action_run_current_selection)
        self._menu_run.addAction(self._action_run_from_current_position)
        self._menu_run.addAction(self._action_run_up_to_current_position)
        self._menu_run.addSeparator()
        self._menu_run.addMenu(self._menu_output)
        self._menu_run.addMenu(self._menu_logging)
        self._menu_run.addSeparator()
        self._menu_run.addAction(self._action_run_interrupt)
        self._menu_run.addAction(self._action_run_restart)
        self._menu_run.addSeparator()
        self._menu_run.addAction(self._action_change_working_directory)
        self._menu_run.addSeparator()
        self._menu_run.addAction(self._action_run_debug)
        self._menu_run.addAction(self._action_toggle_breakpoint)
        self._menu_run.addAction(self._action_clear_breakpoints)

    def _show_edit_menu(self):

        editor = self._ide._current_editor()
        self._menu_edit.clear()
        if editor is None:
            action = self._menu_edit.addAction(_(u'No active editor'))
            action.setEnabled(False)
            return
        for action in editor.get_context_menu().actions():
            self._menu_edit.addAction(action)

    def _show_logging_menu(self):

        logging_commands = self._ide.extension_manager.provide(
            'workspace_logging_commands')
        self._menu_logging.clear()
        if logging_commands is None:
            action = self._menu_logging.addAction(_(u'Kernel not supported'))
            action.setEnabled(False)
            return
        for level, command in logging_commands.items():

            def set_logging_level(command):
                def inner():
                    self._ide.extension_manager.fire('jupyter_run_code',
                                                     code=command)

                return inner

            action = self._menu_logging.addAction(
                self._ide.theme.qicon('text-x-script'), level,
                set_logging_level(command))

    def _add_extension_action(self, ext, menu, separate=False):

        if ext not in self._ide.extension_manager:
            return None
        if separate:
            menu.addSeparator()
        menu.addAction(self._ide.extension_manager[ext].action)
        return self._ide.extension_manager[ext].action

    def build_tool_bar(self):

        tool_bar = ToolBar(self.parent(), self._ide)
        tool_bar.addAction(self._action_new_file)
        tool_bar.addAction(self._action_open_file)
        tool_bar.addAction(self._action_save_file)
        tool_bar.addAction(self._action_open_folder)
        tool_bar.addSeparator()
        tool_bar.addAction(self._action_run_current_file)
        tool_bar.addAction(self._action_run_current_selection)
        tool_bar.addAction(self._action_run_interrupt)
        tool_bar.addAction(self._action_run_restart)
        tool_bar.addSeparator()
        tool_bar.addAction(self._action_toggle_folder_browsers)
        if self._action_toggle_console is not None:
            tool_bar.addAction(self._action_toggle_console)
        if self._action_toggle_workspace is not None:
            tool_bar.addAction(self._action_toggle_workspace)
        if self._action_find_in_files is not None:
            tool_bar.addSeparator()
            tool_bar.addAction(self._action_find_in_files)
        tool_bar.setWindowTitle(u'IDE toolbar')
        tool_bar.setObjectName(u'OpenSesameIDE_Toolbar')
        return tool_bar

    def setting_changed(self, setting, value):

        if setting in self._cfg_actions:
            self._cfg_actions[setting].setChecked(value)

    def _cfg_action(self,
                    menu,
                    title,
                    setting,
                    icon=None,
                    shortcut=None,
                    negate=False):

        action = QAction(title, self)
        if icon:
            action.setIcon(self._ide.theme.qicon(icon))
        if shortcut:
            action.setShortcut(shortcut)
            action.setToolTip(u'{} ({})'.format(title.replace(u'&', u''),
                                                shortcut))

        def change_setting(value):
            self._ide.extension_manager.fire('setting_changed',
                                             setting=setting,
                                             value=value)

        def change_negated_setting(value):
            change_setting(not value)

        action.triggered.connect(
            change_negated_setting if negate else change_setting)
        action.setCheckable(True)
        action.setChecked(cfg[setting])
        action.setPriority(QAction.HighPriority)
        menu.addAction(action)
        self._cfg_actions[setting] = action
        return action

    def _action(self,
                title,
                icon,
                shortcut,
                target,
                checkable=False,
                checked=False):

        action = QAction(title, self)
        if icon:
            action.setIcon(self._ide.theme.qicon(icon))
        if shortcut:
            action.setShortcut(shortcut)
            action.setToolTip(u'{} ({})'.format(title.replace(u'&', u''),
                                                shortcut))
        action.triggered.connect(target)
        if checkable:
            action.setCheckable(True)
            action.setChecked(checked)
        action.setPriority(QAction.HighPriority)
        return action

    def _toggle_show_tab_bar(self, show_tab_bar):

        self._ide.extension_manager.fire('ide_show_tab_bar',
                                         show_tab_bar=show_tab_bar)

    def _select_indentation_mode(self):

        self._ide.extension_manager.fire('pyqode_select_indentation_mode')

    def _image_annotations_no_capture(self):

        self._action_capture_images.setChecked(False)
        self._action_capture_images_and_text.setChecked(False)
        self._ide.extension_manager.fire('setting_changed',
                                         setting='image_annotations_enabled',
                                         value=False)

    def _image_annotations_capture_images(self):
        self._action_no_capture.setChecked(False)
        self._action_capture_images_and_text.setChecked(False)
        self._ide.extension_manager.fire('setting_changed',
                                         setting='image_annotations_enabled',
                                         value=True)

    def _image_annotations_capture_images_and_text(self):
        self._action_no_capture.setChecked(False)
        self._action_capture_images.setChecked(False)
        self._ide.extension_manager.fire(
            'setting_changed',
            setting='image_annotations_capture_output',
            value=True)
Exemplo n.º 8
0
        def _set_diagnostic_menu(self, sec):
            diag = LEVEL2M('DI', self)
            diag.setObjectName(sec.upper() + 'App')
            BPMs = self._set_bpm_menu(sec)
            act = QAction('BPMs', diag)
            act.setIcon(qta.icon('mdi.currency-sign'))
            act.setMenu(BPMs)
            diag.addAction(act)
            # diag.addMenu(BPMs)
            if sec in {'tb', 'ts'}:
                ICTs = QAction('ICTs', diag)
                self.connect_newprocess(ICTs,
                                        'sirius-hla-' + sec + '-di-icts.py')
                diag.addAction(ICTs)
            elif sec in {'bo', 'si'}:
                DCCT = QMenu('DCCTs', diag)
                DCCT.setObjectName(sec.upper() + 'App')
                DCCT.setIcon(qta.icon('mdi.current-dc'))
                for dev in get_dcct_list(sec.upper()):
                    act_dev = DCCT.addAction(dev)
                    self.connect_newprocess(act_dev,
                                            ['sirius-hla-as-di-dcct.py', dev])
                diag.addMenu(DCCT)
            if 'tb' in sec:
                Slits = QAction('Slits', diag)
                self.connect_newprocess(Slits, 'sirius-hla-tb-di-slits.py')
                diag.addAction(Slits)
            if sec in {'bo', 'si'}:
                Tune = QAction('Tune', diag)
                Tune.setIcon(qta.icon('mdi.pulse', scale_factor=1.3))
                self.connect_newprocess(Tune,
                                        'sirius-hla-' + sec + '-di-tune.py')
                diag.addAction(Tune)
                VLight = QAction('VLight', diag)
                self.connect_newprocess(VLight,
                                        'sirius-hla-' + sec + '-di-vlight.py')
                diag.addAction(VLight)
            if 'si' not in sec:
                Scrns = QMenu('Screens', diag)
                Scrns.setObjectName(sec.upper() + 'App')
                for dev in get_scrn_list(sec.upper()):
                    act_dev = Scrns.addAction(dev)
                    self.connect_newprocess(act_dev,
                                            ['sirius-hla-as-di-scrn.py', dev])
                diag.addMenu(Scrns)
            else:
                Scrap = QAction('Scrapers', diag)
                self.connect_newprocess(Scrap, 'sirius-hla-si-di-scraps.py')
                diag.addAction(Scrap)

                BbB = QMenu('BbB', diag)
                BbB.setObjectName(sec.upper() + 'App')

                AllBbB = BbB.addAction('All')
                self.connect_newprocess(
                    AllBbB, ['sirius-hla-si-di-bbb.py', '-dev', 'all'])

                for idc in ['Horizontal', 'Vertical', 'Longitudinal']:
                    dev_pref = 'SI-Glob:DI-BbBProc-' + idc[0]
                    act_dev = BbB.addAction(idc)
                    self.connect_newprocess(
                        act_dev, ['sirius-hla-si-di-bbb.py', '-dev', dev_pref])

                diag.addMenu(BbB)
            return diag