Skip to content

scrollbar

ScrollBarDrawer: custom painting for QScrollBar.

ScrollBarDrawer

Source code in client/ayon_ui_qt/drawers/scrollbar.py
 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
class ScrollBarDrawer:
    def __init__(self, style_inst: AYONStyle) -> None:
        self.style_inst = style_inst
        self.model = style_inst.model
        self._style = self.model.get_style("QScrollBar")
        self._cache = {}

    @property
    def _super(self):
        """Return proxy for calling QCommonStyle methods on style_inst."""
        from ..style import AYONStyle as _AYONStyle

        return super(_AYONStyle, self.style_inst)

    @property
    def base_class(self):
        return {"QScrollBar": QtWidgets.QScrollBar}

    def register_drawers(self):
        return {
            enum_to_str(
                QStyle.ControlElement,
                QStyle.ControlElement.CE_ScrollBarSlider,
                "QScrollBar",
            ): self.draw_scrollbar_slider,
            enum_to_str(
                QStyle.ControlElement,
                QStyle.ControlElement.CE_ScrollBarAddPage,
                "QScrollBar",
            ): self.draw_scrollbar_page,
            enum_to_str(
                QStyle.ControlElement,
                QStyle.ControlElement.CE_ScrollBarSubPage,
                "QScrollBar",
            ): self.draw_scrollbar_page,
        }

    def register_sizers(self):
        return {
            enum_to_str(
                QStyle.ComplexControl,
                QStyle.ComplexControl.CC_ScrollBar,
                "QScrollBar",
            ): self.get_size,
        }

    def register_metrics(self):
        return {
            enum_to_str(
                QStyle.PixelMetric,
                QStyle.PixelMetric.PM_ScrollBarExtent,
                "QScrollBar",
            ): self.get_metric,
            enum_to_str(
                QStyle.PixelMetric,
                QStyle.PixelMetric.PM_ScrollBarSliderMin,
                "QScrollBar",
            ): self.get_metric,
        }

    def get_size(
        self,
        cc: QStyle.ComplexControl,
        opt: QStyleOptionComplex,
        sc: QStyle.SubControl,
        w: QWidget | None = None,
    ) -> QRect | None:
        if not w:
            raise ValueError(
                "Widget required to calculate scrollbar sub-control rects"
            )

        if not isinstance(opt, (QStyleOptionSlider, QStyleOptionComplex)):
            raise ValueError(f"Unexpected option type: {type(opt)}")

        sup = self._super
        try:
            als = self._cache["add_line_size"]
        except KeyError:
            als = self._cache["add_line_size"] = sup.subControlRect(
                cc, opt, QStyle.SubControl.SC_ScrollBarAddLine, w
            ).size()
        try:
            sls = self._cache["sub_line_size"]
        except KeyError:
            sls = self._cache["sub_line_size"] = sup.subControlRect(
                cc, opt, QStyle.SubControl.SC_ScrollBarSubLine, w
            ).size()

        orientation = w.orientation()

        if sc in (
            QStyle.SubControl.SC_ScrollBarSlider,
            QStyle.SubControl.SC_ScrollBarGroove,
        ):
            rect = sup.subControlRect(cc, opt, sc, w)
            if orientation == Qt.Orientation.Vertical:
                rect.adjust(0, -sls.height(), 0, als.height())
            else:
                rect.adjust(-sls.width(), 0, als.width(), 0)
            return rect

        elif sc == QStyle.SubControl.SC_ScrollBarAddPage:
            rect = sup.subControlRect(cc, opt, sc, w)
            if orientation == Qt.Orientation.Vertical:
                rect.adjust(0, 0, 0, als.height())
            else:
                rect.adjust(0, 0, als.width(), 0)
            return rect

        elif sc == QStyle.SubControl.SC_ScrollBarSubPage:
            rect = sup.subControlRect(cc, opt, sc, w)
            if orientation == Qt.Orientation.Vertical:
                rect.adjust(0, -sls.height(), 0, 0)
            else:
                rect.adjust(-sls.width(), 0, 0, 0)
            return rect

        raise ValueError("Unexpected sub-control")

    def get_metric(
        self,
        metric: QStyle.PixelMetric,
        opt: QStyleOption | None = None,
        widget: QWidget | None = None,
    ) -> int:
        self._style.set_context(widget)
        if metric == QStyle.PixelMetric.PM_ScrollBarExtent:
            # Width of a vertical scroll bar and the height of a horizontal
            # scroll bar.
            return int(self._style["width"])
        elif metric == QStyle.PixelMetric.PM_ScrollBarSliderMin:
            # The minimum height of a vertical scroll bar's slider and the
            # minimum width of a horizontal scroll bar's slider.
            return int(self._style["min-length"])
        return 0

    def draw_scrollbar_slider(
        self,
        option: QStyleOptionComplex,
        painter: QPainter,
        widget: QWidget | None = None,
    ) -> None:
        """Draw the scrollbar slider/thumb."""
        style = self.model.get_style("QScrollBar")
        style.set_context(widget)
        painter.save()
        painter.setRenderHint(QPainter.RenderHint.Antialiasing)

        # Draw slider background
        painter.setBrush(QBrush(QColor(style.get("slider-color"))))
        pen = QPen(QColor(style.get("background-color")))
        pen.setWidth(style.get("border-width"))
        painter.setPen(pen)
        radius = style.get("border-radius")
        painter.drawRoundedRect(option.rect, radius, radius)

        painter.restore()

    def draw_scrollbar_page(
        self,
        option: QStyleOptionComplex,
        painter: QPainter,
        widget: QWidget | None = None,
    ) -> None:
        """Draw scrollbar page buttons."""
        style = self.model.get_style("QScrollBar")
        style.set_context(widget)
        painter.save()
        painter.setRenderHint(QPainter.RenderHint.Antialiasing)

        # Draw slider background
        painter.setBrush(QBrush(QColor(style.get("background-color"))))
        painter.setPen(Qt.PenStyle.NoPen)
        painter.drawRect(option.rect)

        painter.restore()

draw_scrollbar_page(option, painter, widget=None)

Draw scrollbar page buttons.

Source code in client/ayon_ui_qt/drawers/scrollbar.py
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
def draw_scrollbar_page(
    self,
    option: QStyleOptionComplex,
    painter: QPainter,
    widget: QWidget | None = None,
) -> None:
    """Draw scrollbar page buttons."""
    style = self.model.get_style("QScrollBar")
    style.set_context(widget)
    painter.save()
    painter.setRenderHint(QPainter.RenderHint.Antialiasing)

    # Draw slider background
    painter.setBrush(QBrush(QColor(style.get("background-color"))))
    painter.setPen(Qt.PenStyle.NoPen)
    painter.drawRect(option.rect)

    painter.restore()

draw_scrollbar_slider(option, painter, widget=None)

Draw the scrollbar slider/thumb.

Source code in client/ayon_ui_qt/drawers/scrollbar.py
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
def draw_scrollbar_slider(
    self,
    option: QStyleOptionComplex,
    painter: QPainter,
    widget: QWidget | None = None,
) -> None:
    """Draw the scrollbar slider/thumb."""
    style = self.model.get_style("QScrollBar")
    style.set_context(widget)
    painter.save()
    painter.setRenderHint(QPainter.RenderHint.Antialiasing)

    # Draw slider background
    painter.setBrush(QBrush(QColor(style.get("slider-color"))))
    pen = QPen(QColor(style.get("background-color")))
    pen.setWidth(style.get("border-width"))
    painter.setPen(pen)
    radius = style.get("border-radius")
    painter.drawRoundedRect(option.rect, radius, radius)

    painter.restore()