spyder.api.widgets.mixins#
Spyder API Mixins.
Classes
Provide methods to create, add and get actions in a unified way. |
|
Mixin with additional functionality for the QMainWindow's used in Spyder. |
|
Provide methods to create, add and get menus. |
|
Provide methods to create, add and get toolbuttons. |
|
Provide methods to create, add and get toolbars. |
|
|
Basic functionality for all Spyder widgets and Qt items. |
Mixin to transform an SVG to a QPixmap that is scaled according to the factor set by users in Preferences. |
- class spyder.api.widgets.mixins.SpyderToolButtonMixin[source]#
Bases:
objectProvide methods to create, add and get toolbuttons.
- create_toolbutton(name, text=None, icon=None, tip=None, toggled=None, triggered=None, autoraise=True, text_beside_icon=False, section=None, option=None, register=True)[source]#
Create a Spyder toolbutton.
- get_toolbutton(name: str, context: str | None = None, plugin: str | None = None) QToolButton[source]#
Return toolbutton by name, plugin and context.
- Parameters:
- Returns:
toolbutton – The corresponding toolbutton stored under the given name, context and plugin.
- Return type:
QToolButton
- Raises:
KeyError – If either of name, context or plugin keys do not exist in the toolbutton registry.
- get_toolbuttons(context: str | None = None, plugin: str | None = None) Dict[str, QToolButton][source]#
Return all toolbuttons defined by a context on a given plugin.
- Parameters:
- Returns:
toolbuttons – A dictionary that maps string keys to their corresponding toolbuttons.
- Return type:
Dict[str, QToolButton]
- class spyder.api.widgets.mixins.SpyderToolbarMixin[source]#
Bases:
objectProvide methods to create, add and get toolbars.
- add_item_to_toolbar(action_or_widget, toolbar, section=None, before=None, before_section=None)[source]#
If you provide a before action, the action will be placed before this one, so the section option will be ignored, since the action will now be placed in the same section as the before action.
- create_toolbar(name: str, register: bool = True) SpyderToolbar[source]#
Create a Spyder toolbar.
- get_toolbar(name: str, context: str | None = None, plugin: str | None = None) QToolBar[source]#
Return toolbar by name, plugin and context.
- Parameters:
- Returns:
toolbar – The corresponding toolbar stored under the given name, context and plugin.
- Return type:
QToolBar
- Raises:
KeyError – If either of name, context or plugin keys do not exist in the toolbar registry.
- class spyder.api.widgets.mixins.SpyderMenuMixin[source]#
Bases:
objectProvide methods to create, add and get menus.
This mixin uses a custom menu object that allows for the creation of sections in a simple way.
Add a SpyderAction or a QWidget to the menu.
Create a menu for Spyder.
- Parameters:
menu_id (str) – Unique str identifier for the menu.
title (str or None) – Localized text string for the menu.
icon (QIcon or None) – Icon to use for the menu.
reposition (bool, optional (default True)) – Whether to vertically reposition the menu due to its padding.
register (bool) – Whether to register the menu in the global registry.
- Returns:
The created menu.
- Return type:
Return a menu by name, plugin and context.
- Parameters:
- Returns:
menu – The corresponding menu stored under the given name, context and plugin.
- Return type:
- Raises:
KeyError – If either of name, context or plugin keys do not exist in the menu registry.
Return all menus defined by a context on a given plugin.
- Parameters:
- Returns:
menus – A dictionary that maps string keys to their corresponding menus.
- Return type:
Dict[str, SpyderMenu]
- class spyder.api.widgets.mixins.SpyderActionMixin[source]#
Bases:
objectProvide methods to create, add and get actions in a unified way.
This mixin uses a custom action object.
- create_action(name, text, icon=None, icon_text='', tip=None, toggled=None, triggered=None, data=None, shortcut=None, shortcut_context=None, context=3, initial=None, register_shortcut=False, section=None, option=None, parent=None, register_action=True, overwrite=False, context_name=None, menurole=None)[source]#
- name: str
unique identifiable name for the action
- text: str
Localized text for the action
- icon: QIcon,
Icon for the action when applied to menu or toolbutton.
- icon_text: str
Icon for text in toolbars. If True, this will also disable the tooltip on this toolbutton if part of a toolbar.
- tip: str
Tooltip to define for action on menu or toolbar.
- toggled: Optional[Union[Callable, bool]]
If True, then the action modifies the configuration option on the section specified. Otherwise, it should be a callable to use when toggling this action. If None, then the action does not behave like a checkbox.
- triggered: callable
The callable to use when triggering this action.
- data: Any
Data to be set on the action.
- shortcut_context: str
Set the str context of the shortcut.
- context: Qt.ShortcutContext
Set the context for the shortcut.
- initial: object
Sets the initial state of a togglable action. This does not emit the toggled signal.
- section: Optional[str]
Name of the configuration section whose option is going to be modified. If None, and option is not None, then it defaults to the class CONF_SECTION attribute.
- option: ConfigurationKey
Name of the configuration option whose value is reflected and affected by the action.
- register_shortcut: bool, optional
If True, main window will expose the shortcut in Preferences. The default value is False.
- parent: QWidget (None)
Define the parent of the widget. Use self if not provided.
- register_action: bool, optional
If True, the action will be registered and searchable. The default value is True.
- overwrite: bool, optional
If True, in case of action overwriting no warning will be shown. The default value is False
- context_name: Optional[str]
Name of the context that holds the action in case of registration. The combination of name and context_name is unique so trying to register an action with the same name and context_name will cause a warning unless overwrite is set to True.
- menurole: QAction.MenuRole, optional
Menu role for the action (it only has effect on macOS).
Notes
There is no need to set shortcuts right now. We only create actions with this (and similar methods) and these are then exposed as possible shortcuts on plugin registration in the main window with the register_shortcut argument.
If icon_text is True, this will also disable the tooltip.
If a shortcut is found in the default config then it is assigned, otherwise it’s left blank for the user to define one for it.
- get_action(name: str, context: str | None = None, plugin: str | None = None) Any[source]#
Return an action by name, context and plugin.
- Parameters:
- Returns:
action – The corresponding action stored under the given name, context and plugin.
- Return type:
SpyderAction
- Raises:
KeyError – If either of name, context or plugin keys do not exist in the action registry.
- get_actions(context: str | None = None, plugin: str | None = None) dict[source]#
Return all actions defined by a context on a given plugin.
- Parameters:
- Returns:
actions – A dictionary that maps string keys to their corresponding actions.
- Return type:
Dict[str, SpyderAction]
Notes
Actions should be created once. Creating new actions on menu popup is highly discouraged.
Actions can be created directly on a PluginMainWidget or PluginMainContainer subclass. Child widgets can also create actions, but they need to subclass SpyderWidgetMixin.
PluginMainWidget or PluginMainContainer will collect any actions defined in subwidgets (if defined) and expose them in the get_actions method at the plugin level.
Any action created this way is now exposed as a possible shortcut automatically without manual shortcut registration. If an option is found in the config system then it is assigned, otherwise it’s left with an empty shortcut.
There is no need to override this method.
- class spyder.api.widgets.mixins.SpyderWidgetMixin(class_parent=None, parent=None)[source]#
Bases:
SpyderActionMixin,SpyderMenuMixin,SpyderToolbarMixin,SpyderToolButtonMixin,SpyderShortcutsMixinBasic functionality for all Spyder widgets and Qt items.
This provides a simple management of widget options, as well as Qt helpers for defining the actions a widget provides.
- class spyder.api.widgets.mixins.SpyderMainWindowMixin[source]#
Bases:
objectMixin with additional functionality for the QMainWindow’s used in Spyder.
- class spyder.api.widgets.mixins.SvgToScaledPixmap[source]#
Bases:
SpyderConfigurationAccessorMixin to transform an SVG to a QPixmap that is scaled according to the factor set by users in Preferences.