8.6.4.2. pymodaq_gui.managers.action_manager module
- class pymodaq_gui.managers.action_manager.ActionManager(toolbar=None, menu=None)[source]
Bases:
objectMixIn Class to be used by all UserInterface to manage their QActions and the action they are connected to
- Parameters:
- Attributes:
actionsGet all the actions (and widgets added to a toolbar) managed here
actions_namesGet all the action names
menuGet the default menu
menusGet all menus
menus_namesGet all menu names
toolbarGet the default toolbar
toolbarsGet all toolbars
toolbars_namesGet all toolbar names
Methods
add_action([short_name, name, icon_name, ...])Create a new action and add it to toolbar and menu
add_action_group(short_name[, name, ...])Add several related actions to a toolbar under a single button, to avoid overloading the toolbar with one slot per action.
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
apply_qt_theme(name)Apply a qt_themes theme to the whole application and persist it as the default
apply_toolbar_style(name)Persist the toolbar button style and apply it to all toolbars
connect_action(name[, slot, connect, ...])Connect (or disconnect) the action referenced by name to the given slot
Schedule the deletion of all the QActions referenced in this manager, and of its toolbars and menus that have no parent
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 :param action_name: The action name as defined in setup_actions :type action_name:
strhas_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 menu to the list of managed menus
reference_toolbar(short_name, toolbar)Add an existing toolbar to the list of managed toolbars
Rebuild the icons of all managed actions and menus with the current theme colours.
remove_action([toolbar, action])Remove an action from a toolbar and forget it
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
Method where to create actions to be subclassed.
setup_theme_menu(parent_menu)Add a 'Theme' submenu choosing the qt_themes theme of the whole application
setup_toolbar_style_menu(parent_menu)Add a 'Toolbar style' submenu choosing how the toolbar buttons of all windows are drawn
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, fill=None, rotate=0, follow_theme=True)[source]
Create a new action and add it to toolbar and menu
- Parameters:
short_name (
str) – the name as referenced in the dict self.actionsname (
str) – Displayed name if should be displayed inicon_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 actioncheckable (
bool) – set the checkable state of the actionchecked (
bool) – set the current state of the actiontoolbar (
Union[str,QToolBar,None]) –a toolbar where action should be added. Can be:
None: adds to the default toolbar (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/menushortcut (
Union[str,Key,QKeySequence,None]) – Using this shortcut will trigger the actionauto_toolbar (
bool) – if True add this action to the defined toolbarauto_menu (
bool) – if True add this action to the defined menuenabled (
bool) – set the enabled state of this actionicon_checked (
Union[str,Path,QIcon,None]) – 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 checkedicon_color (
Union[QColor,bytes,str,None]) – color to be applied (if possible) to the unchecked icon. Give theme colours by name (ThemeColor.GREEN or ‘green’) so they follow theme changesicon_checked_color (
Union[QColor,bytes,str,None]) – color to be applied to the checked icon (if any), same as icon_colorflip_h (
bool) – mirror the icon horizontally (left ↔ right)flip_v (
bool) – mirror the icon vertically (top ↔ bottom)rotate (
int, optional) – rotate the icon by the given value in degreebefore (
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 WidgetActionProxyaction (
Optional[QAction]) – An existing action to register instead of creating a new onefill (
Optional[bool]) – Fill or not the icon, if None left to the user configurationrotate – Rotate the icon by this value in degree
follow_theme (
bool) – True (default): colours given as theme colours (ThemeColor, their names, or values resolved from the theme) follow theme changes. False: the colours are resolved once, here, and kept as they are
- Returns:
The created action, also referenced as short_name
- Return type:
See also
- add_action_group(short_name, name='', icon_name='', tip='', checkable=False, checked=False, icon_checked=None, icon_color=None, icon_checked_color=None, toolbar=None, secondary_actions=(), visible=True, enabled=True, follow_theme=True)[source]
Add several related actions to a toolbar under a single button, to avoid overloading the toolbar with one slot per action.
The button shows and triggers a primary action directly (a regular action, created and registered exactly like
add_action(), soconnect_action(),is_action_checked(),set_action_checked()… all keep working on itsshort_nameunchanged). A small dropdown arrow next to it reveals the secondary actions, each created the same way asadd_action()would from its spec dict, but added only to the dropdown, never to a toolbar directly.If there are no secondary actions, the button behaves like a plain toolbar action (no dropdown arrow).
- Parameters:
:param : :type icon_checked_color:
Union[QColor,bytes,str,None] :param icon_checked_color: forwarded toadd_action()for the primary action :type follow_theme:bool:param follow_theme: forwarded toadd_action()for the primary action :param toolbar: the toolbar the button is added to (defaults to the default toolbar) :type toolbar:Union[str,QToolBar,None] :param secondary_actions: one spec per dropdown entry, using the same keys asadd_action()(at least
short_name); order is preserved in the dropdown menu- Parameters:
- Returns:
the created button, already added to the toolbar
- Return type:
See also
Create and add a menu to a parent menu
- Parameters:
short_name (
str) – the name as referenced in the dict self._menustitle (
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|None) – the parent menu or menubar where this menu should be added. If None, uses the default menuicon_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 menumenu (
Optional[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:
See also
- 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._toolbarstitle (
str) – Displayed title of the toolbarparent (
Optional[QWidget]) – parent widget for the toolbar (typically a QMainWindow)toolbar (
Optional[QToolBar]) – A given toolbar, if None, it is createdarea (
ToolBarArea) – the area where this toolbar should be added, valid only for a QMainWindow parentadd_break (
bool) – If True, a toolbar break is added in the given area before adding the toolbar, valid only for a QMainWindow parentbefore (
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:
See also
- add_widget(short_name, klass, *args, tip='', toolbar=None, visible=True, signal_str=None, slot=None, enabled=True, auto_toolbar=True, before=None, **kwargs)[source]
Create and add a widget to a toolbar
- Parameters:
short_name (
str) – the name as referenced in the dict self.actionsklass (
Union[str,QWidget,Callable[...,QWidget]]) – should be a custom widget class or the name of a standard widget of QWidgetsargs (
Any) – variable arguments passed as is to the widget constructortip (
str) – a tooltip to be displayed when hovering above the widgettoolbar (
Union[str,QToolBar,None]) – a toolbar where the widget should be added.visible (
bool) – display or not the action in the toolbar/menusignal_str (
Optional[str]) – an attribute of type Signal of the widgetslot (
Optional[Callable[...,Any]]) – a callable connected to the signalenabled (
bool) – enable state of the widgetauto_toolbar (
bool) – if True add this action to the defined toolbarbefore (
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 WidgetActionProxykwargs (
Any) – variable named arguments passed as is to the widget constructor
- Return type:
- apply_qt_theme(name)[source]
Apply a qt_themes theme to the whole application and persist it as the default
- Return type:
- apply_toolbar_style(name)[source]
Persist the toolbar button style and apply it to all toolbars
- Return type:
- connect_action(name, slot=None, connect=True, signal_name='')[source]
Connect (or disconnect) the action referenced by name to the given slot
- Parameters:
- Return type:
- delete_actions()[source]
Schedule the deletion of all the QActions referenced in this manager, and of its toolbars and menus that have no parent
A Qt object keeps the slots connected to its signals (often lambdas holding the manager) alive as long as the C++ object exists. Unparented actions, toolbars or menus therefore keep the whole UI that created them alive after it is closed, out of reach of Python’s garbage collector. Call this when the UI is closed for good.
Added in version 5.3.9.
- Return type:
- get_action(name)[source]
Getter of a given action
- Parameters:
name (
str) – The action name as defined in setup_actions- Return type:
Getter of a given menu
- has_action(action_name)[source]
Check if an action has been defined :param action_name: The action name as defined in setup_actions :type action_name:
str- Returns:
bool
- Return type:
Check if a menu has been defined
Add an existing menu to the list of managed menus
- Return type:
- reference_toolbar(short_name, toolbar)[source]
Add an existing toolbar to the list of managed toolbars
- Return type:
- refresh_icons()[source]
Rebuild the icons of all managed actions and menus with the current theme colours.
Icons bake their colours when created. This is done automatically when
pymodaq_gui.qt_utils.apply_theme()changes the theme; call it if the palette was changed by other means. Only icons given by name are rebuilt, seeQAction.refresh_icon().- Return type:
- set_action_text(action_name, text)[source]
Convenience method to set the displayed text on an action
- 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)
See also
- Return type:
Add a ‘Theme’ submenu choosing the qt_themes theme of the whole application
- Return type:
Add a ‘Toolbar style’ submenu choosing how the toolbar buttons of all windows are drawn
- Return type:
- is_action_checked[source]
MethodDispatcher that does not store the instance it is accessed from
multipledispatch’s MethodDispatcher, shared by all instances, keeps the last one in an attribute: the last UI that called each method stayed alive after it was closed.
- is_action_enabled[source]
MethodDispatcher that does not store the instance it is accessed from
multipledispatch’s MethodDispatcher, shared by all instances, keeps the last one in an attribute: the last UI that called each method stayed alive after it was closed.
- is_action_visible[source]
MethodDispatcher that does not store the instance it is accessed from
multipledispatch’s MethodDispatcher, shared by all instances, keeps the last one in an attribute: the last UI that called each method stayed alive after it was closed.
Get the default menu
Get all menus
Get all menu names
- set_action_checked[source]
MethodDispatcher that does not store the instance it is accessed from
multipledispatch’s MethodDispatcher, shared by all instances, keeps the last one in an attribute: the last UI that called each method stayed alive after it was closed.
- set_action_enabled[source]
MethodDispatcher that does not store the instance it is accessed from
multipledispatch’s MethodDispatcher, shared by all instances, keeps the last one in an attribute: the last UI that called each method stayed alive after it was closed.
- class pymodaq_gui.managers.action_manager.MenuIconRefresher(icon_name, parent)[source]
Bases:
QObjectRebuilds a menu icon on theme change, lives as long as the menu it is parented to
Methods
refresh
- staticMetaObject = PySide6.QtCore.QMetaObject("MenuIconRefresher" inherits "QObject": Methods: #4 type=Slot, signature=refresh(PyObject), parameters=PyObject )
- 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, rotate=0, fill=None, follow_theme=True)[source]
Bases:
QActionQAction subclass to mimic signals as pushbuttons. Done to be sure of backcompatibility when I moved from pushbuttons to QAction
- Attributes:
- clicked
Methods
connect_to(slot)Connect the triggered signal to a slot
refresh_icon([_theme])Rebuild the icon(s) with the current theme colours.
set_icon([icon_name, icon_color])Set the icon of the action, by default the one of the current checked state
click
- refresh_icon(_theme=None)[source]
Rebuild the icon(s) with the current theme colours. Called automatically when
pymodaq_gui.qt_utils.apply_theme()changes the theme.No-op for actions created without icon or with an icon instance (QIcon, MaterialIcon) rather than a name, and for actions whose icon was then replaced with
set_icon(icon): their owner manages that icon (e.g. a state colour), rebuilding the original one would undo it.
- set_icon(icon_name=None, icon_color=None)[source]
Set the icon of the action, by default the one of the current checked state
- Return type:
- staticMetaObject = PySide6.QtCore.QMetaObject("QAction" inherits "QAction": Methods: #20 type=Slot, signature=refresh_icon(PyObject), parameters=PyObject #21 type=Slot, signature=refresh_icon() )
- class pymodaq_gui.managers.action_manager.ThemeChecks(actions, themes, parent)[source]
Bases:
QObjectKeeps the checkable theme actions in sync, lives as long as the menu it is parented to
Methods
sync
- staticMetaObject = PySide6.QtCore.QMetaObject("ThemeChecks" inherits "QObject": Methods: #4 type=Slot, signature=sync(PyObject), parameters=PyObject )
- class pymodaq_gui.managers.action_manager.ToolbarStyleChecks(actions, parent)[source]
Bases:
QObjectKeeps the checkable toolbar style actions in sync, lives as long as the menu it is parented to
Methods
sync
- staticMetaObject = PySide6.QtCore.QMetaObject("ToolbarStyleChecks" inherits "QObject": Methods: #4 type=Slot, signature=sync(PyObject), parameters=PyObject )
- class pymodaq_gui.managers.action_manager.ToolbarStyleSignaller[source]
Bases:
QObjectMethods
style_changed
- staticMetaObject = PySide6.QtCore.QMetaObject("ToolbarStyleSignaller" inherits "QObject": Methods: #4 type=Signal, signature=style_changed(PyObject), parameters=PyObject )
- style_changed
- class pymodaq_gui.managers.action_manager.WidgetActionProxy(widget, action)[source]
Bases:
QWidgetWrapper 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
- staticMetaObject = PySide6.QtCore.QMetaObject("WidgetActionProxy" inherits "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, fill=None, rotate=0, follow_theme=True)[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,None]) – 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 actioncheckable (
bool) – set the checkable state of the actionchecked (
bool) – set the current state of the actionslot (
Optional[Callable[...,Any]]) – Method or function that will be called when the action is triggeredtoolbar (
Optional[QToolBar]) – a toolbar where action should be added.menu (
Optional[QMenu]) – a menu where action should be added.visible (
bool) – display or not the action in the toolbar/menushortcut (
Union[str,Key,QKeySequence,None]) – Using this shortcut will trigger the actionenabled (
bool) – set the enabled stateicon_checked (
Union[str,Path,QIcon,None]) – 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,bytes,str,None]) – color to be applied (if possible) to the unchecked icon. Give theme colours by name (ThemeColor.GREEN or ‘green’) so they follow theme changesicon_checked_color (
Union[QColor,bytes,str,None]) – color to be applied to the checked icon (if any), same as icon_colorflip_h (
bool) – mirror the icon horizontally (left ↔ right)flip_v (
bool) – mirror the icon vertically (top ↔ bottom)rotate (
int, optional) – rotate the icon by the given value in degreesbefore (
Optional[QAction]) – if set, the action is inserted before this action in the toolbar/menu; if None the action is appended at the endaction (
Optional[QAction]) – An existing action to configure instead of creating a new onefill (
Optional[bool]) – Fill or not the icon, if None left to the user configurationrotate – Rotate the icon by this value in degree
follow_theme (
bool) – True (default): colours given as theme colours (ThemeColor, their names, or values resolved from the theme) follow theme changes. False: the colours are resolved once, here, and kept as they are
- Returns:
The created (or given) action
- Return type:
- pymodaq_gui.managers.action_manager.addwidget(klass, *args, tip='', toolbar=None, visible=True, signal_str=None, slot=None, setters=None, enabled=True, before=None, **kwargs)[source]
Create and eventually add a widget to a toolbar
- Parameters:
klass (
Union[str,QWidget,Callable[...,QWidget]]) – should be a custom widget class or the name of a standard widget of QWidgetsargs (
Any) – variable arguments passed as is to the widget constructortip (
str) – a tooltip to be displayed when hovering above the widgettoolbar (
Optional[QToolBar]) – a toolbar where the widget should be added.visible (
bool) – display or not the action in the toolbar/menusignal_str (
Optional[str]) – an attribute of type Signal of the widgetslot (
Optional[Callable[...,Any]]) – a callable connected to the signalenabled (
bool) – enable state of the widgetbefore (
Optional[QAction]) – if not None, insert this widget before this actionkwargs (
Any) – variable named arguments used as is in the widget constructorsetters (
Optional[dict]) – method/value pair of the widget (for instance setMaximumWidth)
- Returns:
The widget (wrapped in a WidgetActionProxy if added to a toolbar), None if it could not be created
- Return type: