spyder.api.widgets.mixins#
Mixin classes for the Spyder widget API.
Classes
Provide methods to create, add and get actions in a unified way. |
|
Mixin with additional functionality for Spyder's |
|
Provide methods to create, add and get menus. |
|
Provide methods to create, add and get toolbar buttons. |
|
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. |
- class spyder.api.widgets.mixins.SpyderToolButtonMixin[source]#
Bases:
objectProvide methods to create, add and get toolbar buttons.
- create_toolbutton(
- name: str,
- text: str | None = None,
- icon: QIcon | str | None = None,
- tip: str | None = None,
- toggled: Callable[[Any], None] | bool | None = None,
- triggered: Callable[[], None] | None = None,
- autoraise: bool = True,
- text_beside_icon: bool = False,
- section: str | None = None,
- option: spyder.config.types.ConfigurationKey | None = None,
- register: bool = True,
Create a new Spyder toolbar button.
- Parameters:
name (str) – Unique identifier for the toolbutton.
text (str | None, optional) – Localized text for the toolbutton, displayed in the interface. If
None, the default, no text is shown.icon (QIcon | str | None, optional) – Icon name or object to use in the toolbutton. If
None, the default, no icon is shown.tip (str | None, optional) – Tooltip text for the toolbutton. If
None(the default), no tooltip is shown.toggled (Callable[[Any], None] | bool | None, optional) – If
None(default) then the button doesn’t act like a checkbox. IfTrue, then the button modifies the configurationoptionon thesectionspecified (or the defaultCONF_SECTIONifsectionis not set). Otherwise, must be a callable, called when toggling this button. One oftoggledandtriggeredmust not beNone.triggered (Callable[[], None] | None, optional) – If a callable, will be called when triggering this button. Otherwise, if
None(the default), this will not be a triggered button andtoggledmust be non-Noneinstead.autoraise (bool, optional) – If
True(the default), the button will only draw a 3D frame when hovered over. IfFalse, will draw the button frame all the time.text_beside_icon (bool, optional) – If
True, the button text will be displayed beside the icon. IfFalse(the default), will only show the icon.section (str | None, optional) – Name of the configuration section whose
optionis going to be modified. IfNone(the default) andoptionis notNone, then it defaults to the class’CONF_SECTIONattribute.option (spyder.config.types.ConfigurationKey | None, optional) – Name of the configuration option whose value is reflected and affected by the button. If
None(the default), no option is associated with this button, e.g. for buttons that aretriggeredrather thantoggled.register_action (bool, optional) – If
True(default) the action will be registered and searchable. IfFalse, the action will be created but not registered.
- Returns:
The toolbar button object that was created.
- Return type:
- get_toolbutton( ) QToolButton[source]#
Retrieve a toolbar button by name, context and plugin.
- Parameters:
name (str) – Identifier of the toolbutton to retrieve.
context (str | None, optional) – Context identifier under which the toolbutton was stored. If
None, the default, then theCONTEXT_NAMEattribute is used instead.plugin (str | None, optional) – Identifier of the plugin in which the toolbutton was defined. If
None, the default, then thePLUGIN_NAMEattribute is used instead.
- Returns:
The corresponding toolbutton widget stored under the given
name,contextandplugin.- Return type:
- Raises:
KeyError – If the combination of
name,contextandpluginkeys does not exist in the toolbutton registry.
- get_toolbuttons( ) dict[str, QToolButton][source]#
Return all toolbar buttons defined by a context on a given plugin.
- Parameters:
context (str | None, optional) – Context identifier under which the toolbuttons were stored. If
None, the default, then theCONTEXT_NAMEattribute is used instead.plugin (str | None, optional) – Identifier of the plugin in which the toolbuttons were defined. If
None, the default, then thePLUGIN_NAMEattribute is used instead.
- Returns:
A dictionary that maps identifier name keys to their corresponding toolbutton objects.
- Return type:
- class spyder.api.widgets.mixins.SpyderToolbarMixin[source]#
Bases:
objectProvide methods to create, add and get toolbars.
- add_item_to_toolbar(
- action_or_widget: spyder.utils.qthelpers.SpyderAction | QWidget,
- toolbar: SpyderToolbar,
- section: str | None = None,
- before: str | None = None,
- before_section: str | None = None,
Add the given action or widget to this toolbar.
- Parameters:
action_or_widget (spyder.utils.qthelpers.SpyderAction | QWidget) – The action or widget to add to the toolbar.
toolbar (SpyderToolbar) – The toolbar object to add
action_or_widgetto.section (str | None, optional) – The section id in which to insert the
action_or_widget, orNone(default) for no section.before (str | None, optional) – Make the
action_or_widgetappear before the action with the identifierbefore. IfNone(default), add it to the end. Ifbeforeis notNone,before_sectionwill be ignored.before_section (str | None, optional) – Make the
sectionappear prior tobefore_section. IfNone(the default), add the section to the end. If you provide abeforeaction, the new 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 thebeforeaction.
- Return type:
None
- create_toolbar( ) SpyderToolbar[source]#
Create a Spyder toolbar.
- Parameters:
- Returns:
The created toolbar object.
- Return type:
- get_toolbar( ) SpyderToolbar[source]#
Return toolbar by name, context and plugin.
- Parameters:
name (str) – Identifier of the toolbar to retrieve.
context (str | None, optional) – Context identifier under which the toolbar was stored. If
None, the default, then theCONTEXT_NAMEattribute is used instead.plugin (str | None, optional) – Identifier of the plugin in which the toolbar was defined. If
None, the default, then thePLUGIN_NAMEattribute is used instead.
- Returns:
The corresponding toolbar widget stored under the given
name,contextandplugin.- Return type:
- Raises:
KeyError – If the combination of
name,contextandpluginkeys does not exist in the toolbar registry.
- get_toolbars( ) dict[str, SpyderToolbar][source]#
Return all toolbars defined by a context on a given plugin.
- Parameters:
context (str | None, optional) – Context identifier under which the toolbars were stored. If
None, the default, then theCONTEXT_NAMEattribute is used instead.plugin (str | None, optional) – Identifier of the plugin in which the toolbars were defined. If
None, the default, then thePLUGIN_NAMEattribute is used instead.
- Returns:
A dictionary that maps identifier name keys to their corresponding toolbar objects.
- Return type:
- 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.
- action_or_menu: spyder.utils.qthelpers.SpyderAction | SpyderMenu,
- menu: SpyderMenu,
- section: str | None = None,
- before: str | None = None,
- before_section: str | None = None,
Add the given action or widget to the menu.
- Parameters:
action_or_menu (spyder.utils.qthelpers.SpyderAction | SpyderMenu) – The action or submenu to add to the menu.
menu (SpyderMenu) – The menu object to add
action_or_menuto.section (str | None, optional) – The section id in which to insert the
action_or_menu, orNone(default) for no section.before (str | None, optional) – Make the
action_or_menuappear before the action with the identifierbefore. IfNone(default), add it to the end. Ifbeforeis notNone,before_sectionwill be ignored.before_section (str | None, optional) – Make the
sectionappear prior tobefore_section. IfNone(the default), add the section to the end.
- Return type:
None
- Raises:
SpyderAPIError – If
menuis not an instance ofSpyderMenu.
- menu_id: str,
- title: str | None = None,
- icon: QIcon | None = None,
- reposition: bool = True,
- register: bool = True,
Create a menu for Spyder.
- Parameters:
menu_id (str) – Unique string identifier name for the menu to create.
title (str | None, optional) – Localized title for the menu.
icon (QIcon | None, optional) – Icon object to use for the menu.
reposition (bool, optional) – If
True(the default), vertically reposition the menu per its padding. IfFalse, don’t reposition.register (bool, optional) – If
True(default), register the menu in the global registry. IfFalse, don’t register it.
- Returns:
The created menu object.
- Return type:
-
) SpyderMenu[source]#
Return a menu by name, context and plugin.
- Parameters:
name (str) – Identifier of the menu to retrieve.
context (str | None, optional) – Context identifier under which the menu was stored. If
None, the default, then theCONTEXT_NAMEattribute is used instead.plugin (str | None, optional) – Identifier of the plugin in which the menu was defined. If
None, the default, then thePLUGIN_NAMEattribute is used instead.
- Returns:
The corresponding menu widget stored under the given
name,contextandplugin.- Return type:
- Raises:
KeyError – If the combination of
name,contextandpluginkeys does not exist in the menu registry.
-
) dict[str, SpyderMenu][source]#
Return all menus defined by a context on a given plugin.
- Parameters:
context (str | None, optional) – Context identifier under which the menus were stored. If
None, the default, then theCONTEXT_NAMEattribute is used instead.plugin (str | None, optional) – Identifier of the plugin in which the menus were defined. If
None, the default, then thePLUGIN_NAMEattribute is used instead.
- Returns:
A dictionary that maps identifier name keys to their corresponding menu objects.
- Return type:
- 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: str,
- text: str,
- icon: QIcon | str | None = None,
- icon_text: str = '',
- tip: str | None = None,
- toggled: Callable[[Any], None] | bool | None = None,
- triggered: Callable[[], None] | None = None,
- data: Any | None = None,
- shortcut: str | None = None,
- shortcut_context: str | None = None,
- context: ShortcutContext = 3,
- initial: bool | None = None,
- register_shortcut: bool = False,
- section: str | None = None,
- option: spyder.config.types.ConfigurationKey | None = None,
- parent: QWidget | None = None,
- register_action: bool = True,
- overwrite: bool = False,
- context_name: str | None = None,
- menurole: MenuRole | None = None,
Create a new Spyder-specialized
QAction.Note
There is no need to set a shortcut when creating an action, unless it’s a fixed (non-customizable) one. Otherwise, set the
register_shortcutargument toTrue. If the shortcut is found in theshortcutsconfig section of your plugin, then it’ll be assigned; if not, it’ll be left blank for the user to define it in Spyder Preferences.- Parameters:
name (str) – Unique identifier for the action.
text (str) – Localized text for the action, displayed in the interface.
icon (QIcon | str | None, optional) – Icon name or object for the action when used in menus/toolbuttons. If
None, the default, no icon is shown.icon_text (str, optional) – Descriptive text for the action’s
icon. If a non-empty string is passed, this will also disable the tooltip on this toolbutton if part of a toolbar. If the empty string ("") (the default), no icon text is set and the tooltip will not be disabled.tip (str | None, optional) – Tooltip text for the action when used in menus or toolbars. If
None(the default), no tooltip is shown.toggled (Callable[[Any], None] | bool | None, optional) – If
None(default) then the action doesn’t act like a checkbox. IfTrue, then the action modifies the configurationoptionon thesectionspecified (or the defaultCONF_SECTIONifsectionis not set). Otherwise, must be a callable, called when toggling this action. One oftoggledandtriggeredmust not beNone.triggered (Callable[[], None] | None, optional) – If a callable, will be called when triggering this action. Otherwise, if
None(the default), this will not be a triggered action andtoggledmust be non-Noneinstead.data (Any | None, optional) – Arbitrary user data to be set on the action. If
None, the default, no custom data is set.shortcut (str | None, optional) – A fixed (not configurable) keyboard shortcut to use for the action, in cases where it is not practical for the user to configure the shortcut. If
None(the default), no fixed shortcut is set. For a normal configurable shortcut, you instead need to setregister_shortcuttoTrueand list the shortcut as one of the plugin’s configuration options. Seespyder.api.shortcutsfor more details.shortcut_context (str | None, optional) – The context name of the fixed
shortcut.None(the default) for no context or no shortcut. Use"_"for global shortcuts ( i.e. that can be used anywhere in the application).context (ShortcutContext, optional) – Set the context object for the fixed shortcut. By default,
Qt.WidgetWithChildrenShortcut.initial (bool | None, optional) – Set the initial state of a togglable action. This does not emit the toggled signal. If
None, the default, no initial state.register_shortcut (bool, optional) – If
True, the main window will expose the shortcut in the Spyder Preferences. IfFalse, the default, the shortcut will not be registered.section (str | None, optional) – Name of the configuration section whose
optionis going to be modified. IfNone(the default) andoptionis notNone, then it defaults to the class’CONF_SECTIONattribute.option (spyder.config.types.ConfigurationKey | None, optional) – Name of the configuration option whose value is reflected and affected by the action. If
None(the default), no option is associated with this action, e.g. for actions that aretriggeredrather thantoggled.parent (QWidget | None, optional) – The parent of this widget. If
None, the default, uses this instance object itself as the parent.register_action (bool, optional) – If
True(default) the action will be registered and searchable. IfFalse, the action will be created but not registered.overwrite (bool, optional) – If
False(the default) and this action overwrites another with the samenameandcontext_name, raise a warning. Set toTrueto disable this warning if intentionally overwriting another action.context_name (str | None, optional) – Name of the context that holds the action when registered. The combination of
nameandcontext_namemust be unique, and registering an action with the samename& context_name` will raise a warning by default unlessoverwriteisTrue.menurole (MenuRole | None, optional) – Menu role for the action. Only has an effect on macOS.
- Returns:
The Spyder action object that was created (a specialized
QAction).- Return type:
spyder.utils.qthelpers.SpyderAction
- Raises:
SpyderAPIError – If both
triggeredandtoggledareNone, as at least one must be provided; or if bothinitialisTrueandtriggeredis notNone, as initial values can only apply totoggledactions.
- get_action( ) spyder.utils.qthelpers.SpyderAction[source]#
Return an action by name, context and plugin.
- Parameters:
name (str) – Identifier of the action to retrieve.
context (str | None, optional) – Context identifier under which the action was stored. If
None, the default, then theCONTEXT_NAMEattribute is used instead.plugin (str | None, optional) – Identifier of the plugin in which the action was defined. If
None, the default, then thePLUGIN_NAMEattribute is used instead.
- Returns:
The corresponding action widget stored under the given
name,contextandplugin.- Return type:
spyder.utils.qthelpers.SpyderAction
- Raises:
KeyError – If the combination of
name,contextandpluginkeys does not exist in the action registry.
- get_actions( ) dict[str, spyder.utils.qthelpers.SpyderAction][source]#
Return all actions defined by a context on a given plugin.
- Parameters:
context (str | None, optional) – Context identifier under which the actions were stored. If
None, the default, then theCONTEXT_NAMEattribute is used instead.plugin (str | None, optional) – Identifier of the plugin in which the actions were defined. If
None, the default, then thePLUGIN_NAMEattribute is used instead.
- Returns:
A dictionary that maps identifier name keys to their corresponding action objects.
- Return type:
Notes
Actions should only be created once. Creating new actions on menu popup is highly discouraged.
Actions can be created directly on a
PluginMainWidgetorPluginMainContainersubclass. Child widgets can also create actions, but they need to subclass this class orSpyderWidgetMixin.PluginMainWidgetorPluginMainContainerwill collect any actions defined in subwidgets (if defined) and expose them in theget_actionsmethod at the plugin level.There is no need to override this method.
- update_actions() None[source]#
Update the state of exposed actions.
Exposed actions are actions created by the
create_action()method.- Return type:
None
- Raises:
NotImplementedError – If this method is not implemented by the plugin, as is required.
- class spyder.api.widgets.mixins.SpyderWidgetMixin( )[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.
- PLUGIN_NAME: str | None = None#
Plugin name in the action, toolbar, toolbutton & menu registries.
Usually the same as
SpyderPluginV2.NAME, but may be different fromCONTEXT_NAME.
- CONTEXT_NAME: str | None = None#
The name under which to store actions, toolbars, toolbuttons and menus.
This optional attribute defines the context name under which actions, toolbars, toolbuttons and menus should be registered in the Spyder global registry.
If those elements belong to the global scope of the plugin, then this attribute should have a
Nonevalue, which will use the plugin’s name as the context scope
- update_style() None[source]#
Modify the interface styling used by the plugin.
This must be reimplemented by plugins that need to adjust their style.
Changing from the dark to the light interface theme might require specific styles or stylesheets to be applied. When the theme is changed by the user through our Preferences, this method will be called for all plugins.
- Return type:
None
- class spyder.api.widgets.mixins.SpyderMainWindowMixin[source]#
Bases:
objectMixin with additional functionality for Spyder’s
QMainWindows.
- class spyder.api.widgets.mixins.SvgToScaledPixmap[source]#
Bases:
SpyderConfigurationAccessorMixin to transform an SVG to a QPixmap.
The resulting
QPixmapis scaled according to the scale factor set by users in Spyder’s Preferences.- svg_to_scaled_pixmap( ) QPixmap[source]#
Transform an SVG to a QPixmap.
The resulting
QPixmapis scaled according to the scale factor set by users in Spyder’s Preferences.- Parameters:
svg_file (str) – Name of or path to the SVG file to convert.
rescale (float | None, optional) – Rescale pixmap according to a factor between 0 and 1. If
None(default), will use the default scale factor.in_package (bool, optional) – If
True(the default), get the SVG from theimagesdirectory in the installed Spyder package. IfFalse, retrieve it from the specified file on disk.
- Returns:
The converted rasterized image.
- Return type: