Skip to content

ActionMixin

Sub classes

Name Children Inherits
Action
prettyqt.gui.action
Abstraction for user commands that can be added to different GUI components.
  • PopupMenuAction
  • FullScreenAction
WidgetAction
prettyqt.widgets.widgetaction

    Base classes

    Name Children Inherits
    ObjectMixin
    prettyqt.core.object
    object
    builtins
    The base class of the class hierarchy.
    • type
    • async_generator
    • bytearray_iterator
    • bytearray
    • bytes_iterator
    • bytes
    • builtin_function_or_method
    • callable_iterator
    • PyCapsule
    • cell
    • classmethod_descriptor
    • ...

      â‹” Inheritance diagram

      graph TD
        2227052313["gui.ActionMixin"]
        3227531497["core.ObjectMixin"]
        2751680898["builtins.object"]
        3227531497 --> 2227052313
        2751680898 --> 3227531497
        2751680898 --> 2227052313

      🛈 DocStrings

      Bases: ObjectMixin, _ActionBase

      Source code in prettyqt/gui/action.py
      class ActionMixin(core.ObjectMixin, _ActionBase):
          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"
              if self.isSeparator():
                  return "separator"
              if hasattr(self, "defaultWidget"):
                  return "widget"
              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:
                      qsize = datatypes.to_size(size)
                      w, h = qsize.width(), qsize.height()
                      tooltip = f'<img src={path!r} width="{w}" height="{h}">'
              if rich_text:
                  tooltip = html.escape(tooltip).replace("\n", "<br/>")
                  tooltip = f"<html>{tooltip}</html>"
              else:
                  tooltip = tooltip.replace("\n", "<br/>")
              super().setToolTip(tooltip)
      
          setToolTip = set_tooltip  # noqa: N815
      
          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  # noqa: N815
      
          def set_shortcut(self, shortcut: gui.QKeySequence | str | None):
              if shortcut is None:
                  shortcut = ""
              if isinstance(shortcut, str):
                  if shortcut in gui.keysequence.STANDARD_KEYS:
                      shortcut = gui.KeySequence(shortcut)
                  else:
                      shortcut = gui.KeySequence(
                          shortcut, gui.KeySequence.SequenceFormat.PortableText
                      )
              super().setShortcut(shortcut)
      
          setShortcut = set_shortcut  # noqa: N815
      
          def setText(self, text: str | None):
              super().setText(text or "")
      
          set_text = setText
      
          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 not shortcut.isEmpty()
                  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  # noqa: N815
      
          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  # noqa: N815
      
          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  # noqa: N815
      
          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))
      

      :fa-brands-github: Show source on GitHub