def create_menu(self, name, text=None, icon=None): """ Create a menu. Parameters ---------- name: str Unique str identifier. text: str or None Localized text string. icon: QIcon or None Icon to use for the menu. Return: QMenu Return the created menu. """ from spyder.api.widgets.menus import SpyderMenu menu = SpyderMenu(parent=self, title=text) if icon is not None: menu.menuAction().setIconVisibleInMenu(True) menu.setIcon(icon) MENU_REGISTRY.register_reference(menu, name, self.PLUGIN_NAME, self.CONTEXT_NAME) return menu
def get_menu(self, name: str, context: Optional[str] = None, plugin: Optional[str] = None) -> SpyderMenu: """ Return a menu by name, plugin and context. Parameters ---------- name: str Name of the menu to retrieve. context: Optional[str] Widget or context identifier under which the menu was stored. If None, then `CONTEXT_NAME` is used instead plugin: Optional[str] Name of the plugin where the menu was defined. If None, then `PLUGIN_NAME` is used. Returns ------- menu: SpyderMenu The corresponding menu stored under the given `name`, `context` and `plugin`. Raises ------ KeyError If either of `name`, `context` or `plugin` keys do not exist in the toolbar registry. """ plugin = self.PLUGIN_NAME if plugin is None else plugin context = self.CONTEXT_NAME if context is None else context return MENU_REGISTRY.get_reference(name, plugin, context)
def create_menu(self, menu_id, title='', icon=None): """ Override SpyderMenuMixin method to use a different menu class. Parameters ---------- menu_id: str Unique toolbar string identifier. title: str Toolbar localized title. icon: QIcon or None Icon to use for the menu. Returns ------- MainWidgetMenu The main widget menu. """ menus = getattr(self, '_menus', None) if menus is None: self._menus = OrderedDict() if menu_id in self._menus: raise SpyderAPIError( 'Menu name "{}" already in use!'.format(menu_id) ) menu = MainWidgetMenu(parent=self, title=title) menu.ID = menu_id MENU_REGISTRY.register_reference( menu, menu.ID, self.PLUGIN_NAME, self.CONTEXT_NAME) if icon is not None: menu.menuAction().setIconVisibleInMenu(True) menu.setIcon(icon) self._menus[menu_id] = menu return menu
def get_menus(self, context: Optional[str] = None, plugin: Optional[str] = None) -> Dict[str, SpyderMenu]: """ Return all menus defined by a context on a given plugin. Parameters ---------- context: Optional[str] Widget or context identifier under which the menus were stored. If None, then `CONTEXT_NAME` is used instead plugin: Optional[str] Name of the plugin where the menus were defined. If None, then `PLUGIN_NAME` is used. Returns ------- menus: Dict[str, SpyderMenu] A dictionary that maps string keys to their corresponding menus. """ plugin = self.PLUGIN_NAME if plugin is None else plugin context = self.CONTEXT_NAME if context is None else context return MENU_REGISTRY.get_references(plugin, context)