Skip to content

check_box

checkbox

AYCheckBox

Bases: StyleMixin, QCheckBox

AYON styled checkbox widget.

Overrides Qt's stylesheet painting with AYONStyle custom rendering.

Parameters:

Name Type Description Default
*args

Positional arguments passed to QCheckBox.

()
**kwargs

Keyword arguments passed to QCheckBox.

{}
Source code in client/ayon_ui_qt/components/check_box.py
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class AYCheckBox(StyleMixin, QCheckBox):
    """AYON styled checkbox widget.

    Overrides Qt's stylesheet painting with AYONStyle custom rendering.

    Args:
        *args: Positional arguments passed to QCheckBox.
        **kwargs: Keyword arguments passed to QCheckBox.
    """

    Variants = QCheckBoxVariants

    def __init__(
        self,
        *args,
        variant: Variants = Variants.Default,
        **kwargs,
    ):
        super().__init__(*args, **kwargs)
        self._variant_str = variant.value
        self._style_dict = None
        self.setStyle(get_ayon_style())

        if variant == AYCheckBox.Variants.Button:
            self.setFixedSize(self.sizeHint())

    @property
    def style_dict(self):
        if self._style_dict is None:
            self._style_dict = get_ayon_style().model.get_style(
                "QCheckBox", variant=self._variant_str
            )
            self._style_dict.set_context(self)
        return self._style_dict

    def initStyleOption(self, option: QStyleOptionButton) -> None:
        """Initialize the style option with the default implementation, then
        override any properties needed for our custom painting.

        Args:
            option: The style option to initialize.
        """
        super().initStyleOption(option)
        option.fontMetrics = self.fontMetrics()

    def paintEvent(self, arg__1: QPaintEvent) -> None:
        """Render the checkbox using the AYON custom style.

        Args:
            arg__1: The paint event delivered by Qt.
        """
        p = QPainter(self)
        p.setFont(self.font())

        option = QStyleOptionButton()
        self.initStyleOption(option)
        _style = get_ayon_style()

        _expanding = self.sizePolicy().horizontalPolicy() in (
            QSizePolicy.Policy.Expanding,
            QSizePolicy.Policy.MinimumExpanding,
        )
        if _expanding:
            ind_w = _style.pixelMetric(
                QStyle.PixelMetric.PM_IndicatorWidth, option, self
            )
            ind_h = _style.pixelMetric(
                QStyle.PixelMetric.PM_IndicatorHeight, option, self
            )
            spacing = _style.pixelMetric(
                QStyle.PixelMetric.PM_CheckBoxLabelSpacing, option, self
            )
            cy = self.height() // 2

            ind_opt = QStyleOptionButton(option)
            ind_opt.rect = QRect(0, cy - ind_h // 2, ind_w, ind_h)
            _style.drawPrimitive(
                QStyle.PrimitiveElement.PE_IndicatorCheckBox, ind_opt, p, self
            )

            label_opt = QStyleOptionButton(option)
            label_opt.rect = QRect(
                ind_w + spacing,
                0,
                self.width() - ind_w - spacing,
                self.height(),
            )
            _style.drawControl(
                QStyle.ControlElement.CE_CheckBoxLabel, label_opt, p, self
            )
        else:
            _style.drawControl(
                QStyle.ControlElement.CE_CheckBox, option, p, self
            )

    def sizeHint(self) -> QSize:
        size = super().sizeHint()

        if self._variant_str == AYCheckBox.Variants.Button.value:
            h_pad, v_pad = self.style_dict.get("padding", [6, 6])
            size.setWidth(size.width() + h_pad * 2)
            size.setHeight(size.height() + v_pad * 2)
        else:
            # Recalculate width using the custom style's actual metrics
            option = QStyleOptionButton()
            self.initStyleOption(option)
            _style = get_ayon_style()
            ind_w = _style.pixelMetric(
                QStyle.PixelMetric.PM_IndicatorWidth, option, self
            )
            spacing = _style.pixelMetric(
                QStyle.PixelMetric.PM_CheckBoxLabelSpacing, option, self
            )
            fm = self.fontMetrics()
            text_w = fm.horizontalAdvance(self.text())
            size.setWidth(ind_w + spacing + text_w)

        return size

initStyleOption(option)

Initialize the style option with the default implementation, then override any properties needed for our custom painting.

Parameters:

Name Type Description Default
option QStyleOptionButton

The style option to initialize.

required
Source code in client/ayon_ui_qt/components/check_box.py
49
50
51
52
53
54
55
56
57
def initStyleOption(self, option: QStyleOptionButton) -> None:
    """Initialize the style option with the default implementation, then
    override any properties needed for our custom painting.

    Args:
        option: The style option to initialize.
    """
    super().initStyleOption(option)
    option.fontMetrics = self.fontMetrics()

paintEvent(arg__1)

Render the checkbox using the AYON custom style.

Parameters:

Name Type Description Default
arg__1 QPaintEvent

The paint event delivered by Qt.

required
Source code in client/ayon_ui_qt/components/check_box.py
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def paintEvent(self, arg__1: QPaintEvent) -> None:
    """Render the checkbox using the AYON custom style.

    Args:
        arg__1: The paint event delivered by Qt.
    """
    p = QPainter(self)
    p.setFont(self.font())

    option = QStyleOptionButton()
    self.initStyleOption(option)
    _style = get_ayon_style()

    _expanding = self.sizePolicy().horizontalPolicy() in (
        QSizePolicy.Policy.Expanding,
        QSizePolicy.Policy.MinimumExpanding,
    )
    if _expanding:
        ind_w = _style.pixelMetric(
            QStyle.PixelMetric.PM_IndicatorWidth, option, self
        )
        ind_h = _style.pixelMetric(
            QStyle.PixelMetric.PM_IndicatorHeight, option, self
        )
        spacing = _style.pixelMetric(
            QStyle.PixelMetric.PM_CheckBoxLabelSpacing, option, self
        )
        cy = self.height() // 2

        ind_opt = QStyleOptionButton(option)
        ind_opt.rect = QRect(0, cy - ind_h // 2, ind_w, ind_h)
        _style.drawPrimitive(
            QStyle.PrimitiveElement.PE_IndicatorCheckBox, ind_opt, p, self
        )

        label_opt = QStyleOptionButton(option)
        label_opt.rect = QRect(
            ind_w + spacing,
            0,
            self.width() - ind_w - spacing,
            self.height(),
        )
        _style.drawControl(
            QStyle.ControlElement.CE_CheckBoxLabel, label_opt, p, self
        )
    else:
        _style.drawControl(
            QStyle.ControlElement.CE_CheckBox, option, p, self
        )