Skip to content

ActionMixin

Base classes

Name Children Inherits
ObjectMixin
prettyqt.core.object

Subclasses

Class Module Description
Action prettyqt.gui.action
WidgetAction prettyqt.widgets.widgetaction

⋔ Inheritance diagram

graph TD
  1473245659744["gui.ActionMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473299815024 --> 1473245659744
  140713234304496 --> 1473299815024

🛈 DocStrings

Bases: ObjectMixin

Source code in prettyqt\gui\action.py
class ActionMixin(core.ObjectMixin):
    def __init__(
        self,
        *args,
        text: str | None = None,
        icon: datatypes.IconType = None,
        callback: Callable | None = None,
        **kwargs,
    ):
        super().__init__(*args, **kwargs)
        if callback is not None:
            self.triggered.connect(callback)
        self._usage_count = 0
        if text:
            self.setText(text)
        if icon:
            self.set_icon(icon)
        self.triggered.connect(self._increase_usage_counter)

    def __repr__(self) -> str:
        return get_repr(self, self.text())

    def _get_map(self):
        maps = super()._get_map()
        maps |= {
            "priority": PRIORITIES,
            "shortcutContext": constants.SHORTCUT_CONTEXT,
            "menuRole": MENU_ROLE,
        }
        return maps

    def _increase_usage_counter(self):
        self._usage_count += 1

    def get_usage_count(self) -> int:
        return self._usage_count

    def get_type(self) -> Literal["menu", "separator", "widget", "regular"]:
        if self.menu() is not None:
            return "menu"
        elif self.isSeparator():
            return "separator"
        elif hasattr(self, "defaultWidget"):
            return "widget"
        else:
            return "regular"

    def set_disabled(self):
        self.setEnabled(False)

    def set_enabled(self, enabled: bool = True):
        self.setEnabled(enabled)

    def set_tooltip(
        self,
        tooltip: str | datatypes.PathType,
        size: datatypes.SizeType | None = None,
        rich_text: bool = False,
    ):
        if isinstance(tooltip, os.PathLike):
            path = os.fspath(tooltip)
            if size is None:
                tooltip = f"<img src={path!r}>"
            else:
                if isinstance(size, core.QSize):
                    size = (size.width(), size.height())
                tooltip = f'<img src={path!r} width="{size[0]}" height="{size[1]}">'
        tooltip = tooltip.replace("\n", "<br/>")
        if rich_text:
            tooltip = f"<html>{html.escape(tooltip)}</html>"
        super().setToolTip(tooltip)

    setToolTip = set_tooltip

    def set_icon(self, icon: datatypes.IconType):
        """Set the icon for the action.

        Args:
            icon: icon to use
        """
        icon = iconprovider.get_icon(icon)
        super().setIcon(icon)

    setIcon = set_icon

    def set_shortcut(self, shortcut: None | gui.QKeySequence | str):
        if shortcut is None:
            shortcut = ""
        if isinstance(shortcut, str):
            shortcut = gui.KeySequence(
                shortcut, gui.KeySequence.SequenceFormat.PortableText
            )
        super().setShortcut(shortcut)

    setShortcut = set_shortcut

    def setText(self, text: str | None):
        super().setText(text or "")

    def get_icon(self) -> gui.Icon | None:
        icon = self.icon()
        return None if icon.isNull() else gui.Icon(icon)

    def get_shortcut(self) -> gui.KeySequence | None:
        shortcut = self.shortcut()
        return (
            gui.KeySequence(
                shortcut.toString(), gui.KeySequence.SequenceFormat.PortableText
            )
            if shortcut
            else None
        )

    def get_font(self) -> gui.Font:
        return gui.Font(self.font())

    def set_menu(self, menu):
        self.setMenu(menu)

    def set_priority(self, priority: PriorityStr | gui.QAction.Priority):
        """Set priority of the action.

        Args:
            priority: priority for the action
        """
        super().setPriority(PRIORITIES.get_enum_value(priority))

    setPriority = set_priority

    def get_priority(self) -> PriorityStr:
        """Return current priority.

        Returns:
            priority
        """
        return PRIORITIES.inverse[self.priority()]

    def set_shortcut_context(
        self, context: constants.ShortcutContextStr | constants.ShortcutContext
    ):
        """Set shortcut context.

        Args:
            context: shortcut context
        """
        super().setShortcutContext(constants.SHORTCUT_CONTEXT.get_enum_value(context))

    setShortcutContext = set_shortcut_context

    def get_shortcut_context(self) -> constants.ShortcutContextStr:
        """Return shortcut context.

        Returns:
            shortcut context
        """
        return constants.SHORTCUT_CONTEXT.inverse[super().shortcutContext()]

    def set_menu_role(self, role: MenuRoleStr | gui.QAction.MenuRole):
        """Set menu role.

        Args:
            role: menu role
        """
        super().setMenuRole(MENU_ROLE.get_enum_value(role))

    setMenuRole = set_menu_role

    def get_menu_role(self) -> MenuRoleStr:
        """Return menu role.

        Returns:
            menu role
        """
        return MENU_ROLE.inverse[super().menuRole()]

    def show_shortcut_in_contextmenu(self, state: bool = True):
        self.setShortcutVisibleInContextMenu(state)

    usage_count = core.Property(
        int,
        get_usage_count,
        doc="How often the action was triggered",
    )

get_menu_role() -> MenuRoleStr

Return menu role.

Source code in prettyqt\gui\action.py
def get_menu_role(self) -> MenuRoleStr:
    """Return menu role.

    Returns:
        menu role
    """
    return MENU_ROLE.inverse[super().menuRole()]

get_priority() -> PriorityStr

Return current priority.

Source code in prettyqt\gui\action.py
def get_priority(self) -> PriorityStr:
    """Return current priority.

    Returns:
        priority
    """
    return PRIORITIES.inverse[self.priority()]

get_shortcut_context() -> constants.ShortcutContextStr

Return shortcut context.

Source code in prettyqt\gui\action.py
def get_shortcut_context(self) -> constants.ShortcutContextStr:
    """Return shortcut context.

    Returns:
        shortcut context
    """
    return constants.SHORTCUT_CONTEXT.inverse[super().shortcutContext()]

set_icon(icon: datatypes.IconType)

Set the icon for the action.

Parameters:

Name Type Description Default
icon IconType

icon to use

required
Source code in prettyqt\gui\action.py
def set_icon(self, icon: datatypes.IconType):
    """Set the icon for the action.

    Args:
        icon: icon to use
    """
    icon = iconprovider.get_icon(icon)
    super().setIcon(icon)

set_menu_role(role: MenuRoleStr | gui.QAction.MenuRole)

Set menu role.

Parameters:

Name Type Description Default
role MenuRoleStr | MenuRole

menu role

required
Source code in prettyqt\gui\action.py
def set_menu_role(self, role: MenuRoleStr | gui.QAction.MenuRole):
    """Set menu role.

    Args:
        role: menu role
    """
    super().setMenuRole(MENU_ROLE.get_enum_value(role))

set_priority(priority: PriorityStr | gui.QAction.Priority)

Set priority of the action.

Parameters:

Name Type Description Default
priority PriorityStr | Priority

priority for the action

required
Source code in prettyqt\gui\action.py
def set_priority(self, priority: PriorityStr | gui.QAction.Priority):
    """Set priority of the action.

    Args:
        priority: priority for the action
    """
    super().setPriority(PRIORITIES.get_enum_value(priority))

set_shortcut_context(context: constants.ShortcutContextStr | constants.ShortcutContext)

Set shortcut context.

Parameters:

Name Type Description Default
context ShortcutContextStr | ShortcutContext

shortcut context

required
Source code in prettyqt\gui\action.py
def set_shortcut_context(
    self, context: constants.ShortcutContextStr | constants.ShortcutContext
):
    """Set shortcut context.

    Args:
        context: shortcut context
    """
    super().setShortcutContext(constants.SHORTCUT_CONTEXT.get_enum_value(context))