Skip to content

EnumFlagWidget

Qt Base Class: QToolButton

Signature: QToolButton(self, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None

Base classes

Name Children Inherits
ToolButton
prettyqt.widgets.toolbutton

⋔ Inheritance diagram

graph TD
  1473367113584["custom_widgets.EnumFlagWidget"]
  1473296219776["widgets.ToolButton"]
  1473293689216["widgets.AbstractButtonMixin"]
  1473293688240["widgets.WidgetMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473245548480["gui.PaintDeviceMixin"]
  1473365429552["QtWidgets.QToolButton"]
  1473365432480["QtWidgets.QAbstractButton"]
  1473290849680["QtWidgets.QWidget"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473300082368["QtGui.QPaintDevice"]
  1473296219776 --> 1473367113584
  1473293689216 --> 1473296219776
  1473293688240 --> 1473293689216
  1473299815024 --> 1473293688240
  140713234304496 --> 1473299815024
  1473245548480 --> 1473293688240
  140713234304496 --> 1473245548480
  1473365429552 --> 1473296219776
  1473365432480 --> 1473365429552
  1473290849680 --> 1473365432480
  1473288842240 --> 1473290849680
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208
  1473300082368 --> 1473290849680
  1473291690208 --> 1473300082368

🛈 DocStrings

Bases: ToolButton

Source code in prettyqt\custom_widgets\editors\enumflagwidget.py
class EnumFlagWidget(widgets.ToolButton):
    value_changed = core.Signal(enum.Flag)

    def __init__(self, *args, object_name: str = "enum_flag_widget", **kwargs):
        self._enum_class = None
        self._action_map = {}
        self.button_menu = widgets.Menu(triggered=self._on_menu_click)
        super().__init__(*args, object_name=object_name, **kwargs)
        self.setMenu(self.button_menu)
        self.set_popup_mode("instant")

    def _on_menu_click(self):
        values = []
        value = self.get_value()
        for i in self._enum_class.__members__.values():
            if i.value == 0:
                continue
            # self._action_map[i].setChecked(value & i == i)
            if value & i == i:
                values.append(i)
        text = " | ".join(i.name for i in values) if values else self._enum_class(0).name
        self.set_text(text)
        self.value_changed.emit(value)

    def clear(self):
        self._action_map = {}
        self.button_menu.clear()

    def _set_enum_class(self, enum: enum.EnumMeta | None):
        """Set enum class from which members value should be selected."""
        if enum == self._enum_class:
            return None
        self._enum_class = enum
        self.clear()
        for i in self._enum_class.__members__.values():
            if i.value == 0:
                continue
            action = gui.Action(text=i.name.replace("_", " "), checkable=True)
            action.setData(i)
            self._action_map[i] = action
            self.button_menu.add(action)

    def get_enum_class(self) -> enum.EnumMeta | None:
        """Return current Enum class."""
        return self._enum_class

    def get_value(self) -> enum.Flag:
        """Current value as Enum member."""
        if self._enum_class is None:
            return None
        flag = self._enum_class(0)
        for k, v in self._action_map.items():
            if v.isChecked():
                flag |= k
        return flag

    def set_value(self, value: enum.Flag) -> None:
        """Set value with Enum."""
        if not isinstance(value, enum.Flag):
            value = self._enum_class(value)
        self._set_enum_class(value.__class__)
        if not isinstance(value, self._enum_class):
            raise TypeError(
                "setValue(self, Enum): argument 1 has unexpected type "
                f"{type(value).__name__!r}"
            )
        # this filter shouldnt be needed, see https://bugreports.qt.io/browse/PYSIDE-2369
        values = []
        for i in self._enum_class.__members__.values():
            if i.value == 0:
                continue
            if value & i == i:
                self._action_map[i].setChecked(True)
                values.append(i)
        text = " | ".join(i.name for i in values) if values else self._enum_class(0).name
        self.set_text(text)

    value = core.Property(
        enum.Flag,
        get_value,
        set_value,
        user=True,
        doc="Currently chosen value",
    )

get_enum_class() -> enum.EnumMeta | None

Return current Enum class.

Source code in prettyqt\custom_widgets\editors\enumflagwidget.py
def get_enum_class(self) -> enum.EnumMeta | None:
    """Return current Enum class."""
    return self._enum_class

get_value() -> enum.Flag

Current value as Enum member.

Source code in prettyqt\custom_widgets\editors\enumflagwidget.py
def get_value(self) -> enum.Flag:
    """Current value as Enum member."""
    if self._enum_class is None:
        return None
    flag = self._enum_class(0)
    for k, v in self._action_map.items():
        if v.isChecked():
            flag |= k
    return flag

set_value(value: enum.Flag) -> None

Set value with Enum.

Source code in prettyqt\custom_widgets\editors\enumflagwidget.py
def set_value(self, value: enum.Flag) -> None:
    """Set value with Enum."""
    if not isinstance(value, enum.Flag):
        value = self._enum_class(value)
    self._set_enum_class(value.__class__)
    if not isinstance(value, self._enum_class):
        raise TypeError(
            "setValue(self, Enum): argument 1 has unexpected type "
            f"{type(value).__name__!r}"
        )
    # this filter shouldnt be needed, see https://bugreports.qt.io/browse/PYSIDE-2369
    values = []
    for i in self._enum_class.__members__.values():
        if i.value == 0:
            continue
        if value & i == i:
            self._action_map[i].setChecked(True)
            values.append(i)
    text = " | ".join(i.name for i in values) if values else self._enum_class(0).name
    self.set_text(text)

⌗ Property table

Qt Property Type Doc
objectName QString
modal bool
windowModality Qt::WindowModality
enabled bool
geometry QRect
frameGeometry QRect
normalGeometry QRect
x int
y int
pos QPoint
frameSize QSize
size QSize
width int
height int
rect QRect
childrenRect QRect
childrenRegion QRegion
sizePolicy QSizePolicy
minimumSize QSize
maximumSize QSize
minimumWidth int
minimumHeight int
maximumWidth int
maximumHeight int
sizeIncrement QSize
baseSize QSize
palette QPalette
font QFont
cursor QCursor
mouseTracking bool
tabletTracking bool
isActiveWindow bool
focusPolicy Qt::FocusPolicy
focus bool
contextMenuPolicy Qt::ContextMenuPolicy
updatesEnabled bool
visible bool
minimized bool
maximized bool
fullScreen bool
sizeHint QSize
minimumSizeHint QSize
acceptDrops bool
windowTitle QString
windowIcon QIcon
windowIconText QString
windowOpacity double
windowModified bool
toolTip QString
toolTipDuration int
statusTip QString
whatsThis QString
accessibleName QString
accessibleDescription QString
layoutDirection Qt::LayoutDirection
autoFillBackground bool
styleSheet QString
locale QLocale
windowFilePath QString
inputMethodHints QFlags
text QString
icon QIcon
iconSize QSize
shortcut QKeySequence
checkable bool
checked bool
autoRepeat bool
autoExclusive bool
autoRepeatDelay int
autoRepeatInterval int
down bool
popupMode QToolButton::ToolButtonPopupMode
toolButtonStyle Qt::ToolButtonStyle
autoRaise bool
arrowType Qt::ArrowType
value PySide::PyObjectWrapper Currently chosen value