8.5.4.1. pymodaq_gui.managers.action_manager module

class pymodaq_gui.managers.action_manager.ActionManager(toolbar=None, menu=None)[source]

Bases: object

MixIn Class to be used by all UserInterface to manage their QActions and the action they are connected to

Parameters:
  • toolbar (QToolBar) – The toolbar to use as default

  • menu (QMenu) – The menu to use as default

Attributes:
actions
actions_names
menu

Get the default menu

menus

Get all menus

menus_names

Get all menu names

toolbar

Get the default toolbar

toolbars

Get all toolbars

toolbars_names

Get all toolbar names

Methods

add_action([short_name, name, icon_name, ...])

Create a new action and add it to toolbar and menu

add_menu(short_name[, title, parent_menu, ...])

Create and add a menu to a parent menu

add_toolbar(short_name[, title, parent, ...])

Create and add a toolbar

add_widget(short_name, klass, *args[, tip, ...])

Create and add a widget to a toolbar

affect_to(action_name, obj)

Affect action to an object either a toolbar or a menu

connect_action(name[, slot, connect, ...])

Connect (or disconnect) the action referenced by name to the given slot

get_action(name)

Getter of a given action

get_menu(name)

Getter of a given menu

get_toolbar(name)

Getter of a given toolbar

has_action(action_name)

Check if an action has been defined :type action_name: str :param action_name: The action name as defined in setup_actions :type action_name: str

has_menu(menu_name)

Check if a menu has been defined

has_toolbar(toolbar_name)

Check if a toolbar has been defined

reference_menu(short_name, menu)

Add an existing toolbar to the list of managed toolbars

reference_toolbar(short_name, toolbar)

Add an existing toolbar to the list of managed toolbars

set_action_text(action_name, text)

Convenience method to set the displayed text on an action

set_menu(menu)

Set the default menu

set_toolbar(toolbar)

Set the default toolbar

setup_actions()

Method where to create actions to be subclassed.

is_action_checked

is_action_enabled

is_action_visible

set_action_checked

set_action_enabled

set_action_visible

add_action(short_name='', name='', icon_name='', tip='', checkable=False, checked=False, toolbar=None, menu=None, visible=True, shortcut=None, auto_toolbar=True, auto_menu=False, enabled=True, icon_checked=None, icon_color=None, icon_checked_color=None, flip_h=False, flip_v=False, before=None, action=None)[source]

Create a new action and add it to toolbar and menu

Parameters:
  • short_name (str) – the name as referenced in the dict self.actions

  • name (str) – Displayed name if should be displayed in

  • icon_name (Union[str, Path, QIcon]) – str/Path: the png file name/path to produce the icon QtGui.QIcon: the instance of a QIcon element ThemeIcon enum: the value of QtGui.QIcon.ThemeIcon (requires Qt>=6.7)

  • tip (str) – a tooltip to be displayed when hovering above the action

  • checkable (bool) – set the checkable state of the action

  • checked (bool) – set the current state of the action

  • toolbar (Union[str, QToolBar, None]) –

    a toolbar where action should be added. Can be: - None: adds to the default menu (self._toolbar) - str: toolbar name as registered via add_toolbar() - QToolbar: direct QToolbar instance

    Actions can also be added later see affect_to

  • menu (Union[str, QMenu, None]) – Where to add the action. Can be: - None: adds to the default menu (self._menu) - str: menu name as registered via add_menu() - QMenu: direct QMenu instance Actions can also be added later see affect_to

  • visible (bool) – display or not the action in the toolbar/menu

  • shortcut (Union[str, Key, QKeySequence]) – Using this shortcut will trigger the action

  • auto_toolbar (bool) – if True add this action to the defined toolbar

  • auto_menu (bool) – if True add this action to the defined menu

  • enabled (bool) – set the enabled state of this action

  • icon_checked (Union[str, Path, QIcon]) – str/Path: the png file name/path to produce the icon QtGui.QIcon: the instance of a QIcon element ThemeIcon enum: the value of QtGui.QIcon.ThemeIcon (requires Qt>=6.7) Optional, if set, will be the icon when the action is checked

  • icon_color (Union[QColor, bytes, str]) – color to be applied (if possible) to the unchecked icon

  • icon_checked_color (Union[QColor, bytes, str]) – color to be applied to the checked icon (if any)

  • flip_h (bool) – mirror the icon horizontally (left ↔ right)

  • flip_v (bool) – mirror the icon vertically (top ↔ bottom)

  • before (Union[str, QAction, WidgetActionProxy, None]) – if set, the action is inserted before this action in the toolbar/menu; accepts a short_name str, a QAction instance, or a WidgetActionProxy

  • action (QAction | QWidgetAction)

See also

affect_to, pymodaq.resources.QtDesigner_Ressources.icon_library, pymodaq.utils.managers.action_manager.add_action

add_menu(short_name, title='', parent_menu=None, icon_name='', auto_menu=True, menu=None, before=None)[source]

Create and add a menu to a parent menu

Parameters:
  • short_name (str) – the name as referenced in the dict self._menus

  • title (str) – Displayed title of the menu. When menu is also provided and title is non-empty, the title overrides the instance’s own title.

  • parent_menu (QMenuBar | QMenu | str) – the parent menu or menubar where this menu should be added. If None, uses the default menu

  • icon_name (Union[str, Path, QIcon]) – str/Path: the png file name/path to produce the icon QtGui.QIcon: the instance of a QIcon element ThemeIcon enum: the value of QtGui.QIcon.ThemeIcon (requires Qt>=6.7)

  • auto_menu (bool) – if True add this menu to the defined parent menu

  • menu (QMenu) – an existing QMenu instance to register instead of creating a new one.

  • before (Union[QMenu, str, None]) – if specified, the new menu will be inserted before this menu (by instance or short_name); if None the menu is appended at the end

Returns:

The created (or provided) menu

Return type:

QMenu

See also

add_action, get_menu

add_toolbar(short_name, title='', parent=None, toolbar=None, area=<ToolBarArea.TopToolBarArea: 4>, add_break=True, before=None)[source]

Create and add a toolbar

Parameters:
  • short_name (str) – the name as referenced in the dict self._toolbars

  • title (str) – Displayed title of the toolbar

  • parent (QWidget) – parent widget for the toolbar (typically a QMainWindow)

  • toolbar (QToolBar) – A given toolbar, if None, it is created

  • area (ToolBarArea, optional) – the area where this toolbar should be added, valid only for a QMainWindow parent

  • add_break (bool, optional) – If True, a toolbar break is added in the given area before adding the toolbar, valid only for a QMainWindow parent

  • before (Union[str, QToolBar, None]) – if specified, the new toolbar will be inserted before this toolbar (by instance or short_name)

Returns:

The created toolbar

Return type:

QToolBar

add_widget(short_name, klass, *args, tip='', toolbar=None, visible=True, signal_str=None, slot=None, enabled=True, auto_toolbar=True, **kwargs)[source]

Create and add a widget to a toolbar

Parameters:
  • short_name (str) – the name as referenced in the dict self.actions

  • klass (Union[str, QWidget, object]) – should be a custom widget class or the name of a standard widget of QWidgets

  • args (list) – variable arguments passed as is to the widget constructor

  • tip (str) – a tooltip to be displayed when hovering above the widget

  • toolbar (Union[str, QToolBar]) – a toolbar where the widget should be added.

  • visible (bool) – display or not the action in the toolbar/menu

  • signal_str (str) – an attribute of type Signal of the widget

  • slot (Callable) – a callable connected to the signal

  • enabled (bool) – enable state of the widget

  • auto_toolbar (bool) – if True add this action to the defined toolbar

  • kwargs (dict) – variable named arguments passed as is to the widget constructor

Return type:

Union[WidgetActionProxy, QWidget]

affect_to(action_name, obj)[source]

Affect action to an object either a toolbar or a menu

Parameters:
connect_action(name, slot=None, connect=True, signal_name='')[source]

Connect (or disconnect) the action referenced by name to the given slot

Parameters:
  • name (str) – key of the action as referenced in the self._actions dict

  • slot (method) – a method/function

  • connect (bool) – if True connect the trigger signal of the action to the defined slot else disconnect it

  • signal_name (str) – try to use it as a signal (for widgets added…) otherwise use the triggered signal

get_action(name)[source]

Getter of a given action

Parameters:

name (str) – The action name as defined in setup_actions

Return type:

Union[QAction, WidgetActionProxy]

get_menu(name)[source]

Getter of a given menu

Parameters:

name (str) – The menu name as defined when calling add_menu

Return type:

QMenu

get_toolbar(name)[source]

Getter of a given toolbar

Parameters:

name (str) – The toolbar name as defined when calling add_toolbar

Return type:

QToolBar

has_action(action_name)[source]

Check if an action has been defined :type action_name: str :param action_name: The action name as defined in setup_actions :type action_name: str

Returns:

bool

Return type:

bool

has_menu(menu_name)[source]

Check if a menu has been defined

Parameters:

menu_name (str) – The menu name as defined when calling add_menu

Returns:

bool

Return type:

bool

has_toolbar(toolbar_name)[source]

Check if a toolbar has been defined

Parameters:

toolbar_name (str) – The toolbar name as defined when calling add_toolbar

Returns:

bool

Return type:

bool

reference_menu(short_name, menu)[source]

Add an existing toolbar to the list of managed toolbars

reference_toolbar(short_name, toolbar)[source]

Add an existing toolbar to the list of managed toolbars

set_action_text(action_name, text)[source]

Convenience method to set the displayed text on an action

Parameters:
  • action_name (str) – The action name as defined in setup_actions

  • text (str) – The text to display

set_menu(menu)[source]

Set the default menu

Parameters:

menu (QMenu) – The menu to set as default

set_toolbar(toolbar)[source]

Set the default toolbar

Parameters:

toolbar (Union[QToolBar, str]) – The toolbar to set as default

setup_actions()[source]

Method where to create actions to be subclassed. Mandatory

Examples

>>> self.add_action('Quit', 'close2', "Quit program")
>>> self.add_action('Grab', 'camera', "Grab from camera", checkable=True)
>>> self.add_action('Load', 'Open', "Load target file (.h5, .png, .jpg) or data from camera", checkable=False)
>>> self.add_action('Save', 'SaveAs', "Save current data", checkable=False)
property actions: list[QAction]
property actions_names: list[str]
is_action_checked

Dispatch methods based on type signature

See also

Dispatcher

is_action_enabled

Dispatch methods based on type signature

See also

Dispatcher

is_action_visible

Dispatch methods based on type signature

See also

Dispatcher

property menu: QMenu

Get the default menu

property menus: list[QMenu]

Get all menus

property menus_names: list[str]

Get all menu names

set_action_checked

Dispatch methods based on type signature

See also

Dispatcher

set_action_enabled

Dispatch methods based on type signature

See also

Dispatcher

set_action_visible

Dispatch methods based on type signature

See also

Dispatcher

property toolbar: QToolBar

Get the default toolbar

property toolbars: list[QToolBar]

Get all toolbars

property toolbars_names: list[str]

Get all toolbar names

class pymodaq_gui.managers.action_manager.QAction(icon_unchecked, name='', icon_checked=None, icon_color=None, icon_checked_color=None, flip_h=False, flip_v=False)[source]

Bases: QAction

QAction subclass to mimic signals as pushbuttons. Done to be sure of backcompatibility when I moved from pushbuttons to QAction

Attributes:
clicked

Methods

click

connect_to

set_icon

click()[source]
connect_to(slot)[source]
set_icon(icon_name=None, icon_color=None)[source]
property clicked
class pymodaq_gui.managers.action_manager.WidgetActionProxy(widget, action)[source]

Bases: QWidget

Wrapper class of a Widget and its associated toolbar Action. All methods call are forwarded to the wrapped Widget. Even its class name is copied.

Only the setVisible method is different, as the Action need to be hidden. (monkey-patching setVisible on the widget wasn’t compatible with PySide6)

Attributes:
action
widget

Methods

isVisible()

setVisible(visible)

isVisible()[source]
Return type:

bool

setVisible(visible)[source]
property action: QAction
property widget: QWidget
pymodaq_gui.managers.action_manager.addaction(name='', icon_name='', tip='', checkable=False, checked=False, slot=None, toolbar=None, menu=None, visible=True, shortcut=None, enabled=True, icon_checked=None, icon_color=None, icon_checked_color=None, flip_h=False, flip_v=False, before=None, action=None)[source]

Create a new action and add it eventually to a toolbar and a menu

Parameters:
  • name (str) – Displayed name if should be displayed (for instance in menus)

  • icon_name (Union[str, Path, QIcon]) – str/Path: the png file name/path to produce the icon QtGui.QIcon: the instance of a QIcon element ThemeIcon enum: the value of QtGui.QIcon.ThemeIcon (requires Qt>=6.7)

  • tip (str) – a tooltip to be displayed when hovering above the action

  • checkable (bool) – set the checkable state of the action

  • checked (bool) – set the current state of the action

  • slot (Callable) – Method or function that will be called when the action is triggered

  • toolbar (QToolBar) – a toolbar where action should be added.

  • menu (QMenu) – a menu where action should be added.

  • visible (bool) – display or not the action in the toolbar/menu

  • shortcut (Union[str, Key, QKeySequence]) – Using this shortcut will trigger the action

  • enabled (bool) – set the enabled state

  • icon_checked (Union[str, Path, QIcon]) – str/Path: the png file name/path to produce the icon QtGui.QIcon: the instance of a QIcon element ThemeIcon enum: the value of QtGui.QIcon.ThemeIcon (requires Qt>=6.7) Optional, if set, will be the icon when the action is checked (checkable will be set to True)

  • icon_color (Union[QColor, str]) – color to be applied (if possible) to the unchecked icon

  • icon_checked_color (Union[QColor, str]) – color to be applied to the checked icon (if any)

  • flip_h (bool) – mirror the icon horizontally (left ↔ right)

  • flip_v (bool) – mirror the icon vertically (top ↔ bottom)

  • before (QAction) – if set, the action is inserted before this action in the toolbar/menu; if None the action is appended at the end

  • action (QAction | QWidgetAction)

pymodaq_gui.managers.action_manager.addwidget(klass, *args, tip='', toolbar=None, visible=True, signal_str=None, slot=None, setters=None, enabled=True, **kwargs)[source]

Create and eventually add a widget to a toolbar

Parameters:
  • klass (Union[str, QWidget, object]) – should be a custom widget class or the name of a standard widget of QWidgets

  • args (list) – variable arguments passed as is to the widget constructor

  • tip (str) – a tooltip to be displayed when hovering above the widget

  • toolbar (QToolBar) – a toolbar where the widget should be added.

  • visible (bool) – display or not the action in the toolbar/menu

  • signal_str (str) – an attribute of type Signal of the widget

  • slot (Callable) – a callable connected to the signal

  • enabled (bool) – enable state of the widget

  • kwargs (dict) – variable named arguments used as is in the widget constructor

  • setters (dict) – method/value pair of the widget (for instance setMaximumWidth)

Return type:

Union[WidgetActionProxy, QWidget]