Skip to content

slider

AYSlider component module.

AYSlider

Bases: AYContainer

Composite slider widget: caption label + slider bar + value label.

Layout (VBox root)::

┌─────────────────────────────────┐
│ Caption label (optional)        │
│ [========○──────────────] 220   │
└─────────────────────────────────┘

The numeric value display on the right is a read-only AYLabel that updates live as the slider moves.

Parameters:

Name Type Description Default
parent QWidget | None

Parent widget.

None
label str

Caption text shown above the slider (hidden when empty).

''
value int

Initial integer value.

0
minimum int

Minimum integer value.

0
maximum int

Maximum integer value.

100
step int

Snapping step; values are always multiples of this.

1
variant Variants

Visual style variant.

Default
Signals

value_changed (int): Emitted whenever the slider value changes.

Source code in client/ayon_ui_qt/components/slider.py
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
class AYSlider(AYContainer):
    """Composite slider widget: caption label + slider bar + value label.

    Layout (VBox root)::

        ┌─────────────────────────────────┐
        │ Caption label (optional)        │
        │ [========○──────────────] 220   │
        └─────────────────────────────────┘

    The numeric value display on the right is a read-only ``AYLabel`` that
    updates live as the slider moves.

    Args:
        parent: Parent widget.
        label: Caption text shown above the slider (hidden when empty).
        value: Initial integer value.
        minimum: Minimum integer value.
        maximum: Maximum integer value.
        step: Snapping step; values are always multiples of this.
        variant: Visual style variant.

    Signals:
        value_changed (int): Emitted whenever the slider value changes.
    """

    Variants = AYSliderVariants
    value_changed = Signal(int)

    def __init__(
        self,
        parent: QWidget | None = None,
        label: str = "",
        value: int = 0,
        minimum: int = 0,
        maximum: int = 100,
        step: int = 1,
        variant: Variants = Variants.Default,
    ) -> None:
        super().__init__(
            parent,
            layout=AYContainer.Layout.VBox,
            variant=AYContainer.Variants(variant.value),
            layout_spacing=5,
            layout_margin=0,
        )

        self._variant_str = variant.value
        self._style_data = get_ayon_style().model.get_styles(
            "AYSlider",
            variant=self._variant_str,
        )
        self._step = max(1, step)

        # --- Caption label ---------------------------------------------------
        self._caption: AYLabel | None = None
        if label:
            self._caption = AYLabel(
                label,
                text_color=self._style_data["base"].get(
                    "label-color", "#ffffff"
                ),
                bold=self._style_data["base"].get("label-font-weight", 600)
                > 500,
            )
            self.add_widget(self._caption)

        # --- Inner HBox row --------------------------------------------------
        row_spacing = self._style_data["base"].get("row-spacing", 6)
        self._row = AYContainer(
            layout=AYContainer.Layout.HBox,
            variant=AYContainer.Variants(self._variant_str),
            layout_spacing=row_spacing,
            layout_margin=0,
        )

        # Slider bar
        self._bar = AYSliderBar(variant=AYSlider.Variants(self._variant_str))
        self._bar.setMinimum(minimum)
        self._bar.setMaximum(maximum)
        self._bar.setSingleStep(self._step)
        self._bar.setPageStep(self._step * 10)

        # Value label — initialise with the snapped initial value.
        snapped_initial = self._snap_unclamped(value, minimum, maximum)
        self._value_label = AYLabel(
            str(snapped_initial),
            variant=AYLabel.Variants.Default,
            text_color=self._style_data["base"].get("value-color", "#ffffff"),
        )
        self._value_label.setAlignment(Qt.AlignmentFlag.AlignRight)
        value_min_w = self._style_data["base"].get("value-min-width", 40)
        self._value_label.setMinimumWidth(value_min_w)

        self._row.add_widget(self._bar, stretch=1)
        self._row.add_widget(self._value_label, stretch=0)
        self.add_widget(self._row)

        # Connect before setValue so _on_bar_value_changed fires and keeps
        # the label in sync with the snapped value from the very first set.
        self._bar.valueChanged.connect(self._on_bar_value_changed)
        self._bar.setValue(snapped_initial)

    # -------------------------------------------------------------------------
    # Private helpers
    # -------------------------------------------------------------------------

    def _snap_unclamped(self, v: int, minimum: int, maximum: int) -> int:
        """Snap *v* to the nearest step and clamp to [minimum, maximum].

        This version accepts explicit range arguments so it can be called
        before ``self._bar`` has its range configured.

        Args:
            v: Raw integer value.
            minimum: Lower bound.
            maximum: Upper bound.

        Returns:
            Clamped and snapped integer value.
        """
        v = max(minimum, min(maximum, v))
        if self._step > 1:
            v = round(v / self._step) * self._step
            v = max(minimum, min(maximum, v))
        return v

    def _snap(self, v: int) -> int:
        """Snap *v* to the nearest multiple of ``_step`` within range.

        Args:
            v: Raw integer value.

        Returns:
            Clamped and snapped integer value.
        """
        return self._snap_unclamped(
            v, self._bar.minimum(), self._bar.maximum()
        )

    def _on_bar_value_changed(self, raw: int) -> None:
        """Snap the raw slider value, sync the label, emit the signal.

        Args:
            raw: The raw integer value emitted by ``QSlider.valueChanged``.
        """
        snapped = self._snap(raw)
        if snapped != raw:
            # Re-set without entering an infinite loop.
            self._bar.blockSignals(True)
            self._bar.setValue(snapped)
            self._bar.blockSignals(False)

        self._value_label.setText(str(snapped))
        self.value_changed.emit(snapped)

    # -------------------------------------------------------------------------
    # Public API
    # -------------------------------------------------------------------------

    def value(self) -> int:
        """Return the current slider value.

        Returns:
            Current snapped integer value.
        """
        return self._snap(self._bar.value())

    def setValue(self, v: int) -> None:
        """Set the slider to *v* (clamped and snapped).

        Args:
            v: Target integer value.
        """
        self._bar.setValue(self._snap(v))

    def setRange(self, minimum: int, maximum: int) -> None:
        """Update the slider range.

        Args:
            minimum: New minimum value.
            maximum: New maximum value.
        """
        self._bar.setMinimum(minimum)
        self._bar.setMaximum(maximum)

    def setStep(self, step: int) -> None:
        """Change the snapping step.

        Args:
            step: New step size (must be >= 1).
        """
        self._step = max(1, step)
        self._bar.setSingleStep(self._step)
        self._bar.setPageStep(self._step * 10)
        # Re-snap current value to the new step.
        self._bar.setValue(self._snap(self._bar.value()))

    def setLabel(self, text: str) -> None:
        """Update the caption label text.

        Args:
            text: New caption string.
        """
        if self._caption is not None:
            self._caption.setText(text)

setLabel(text)

Update the caption label text.

Parameters:

Name Type Description Default
text str

New caption string.

required
Source code in client/ayon_ui_qt/components/slider.py
359
360
361
362
363
364
365
366
def setLabel(self, text: str) -> None:
    """Update the caption label text.

    Args:
        text: New caption string.
    """
    if self._caption is not None:
        self._caption.setText(text)

setRange(minimum, maximum)

Update the slider range.

Parameters:

Name Type Description Default
minimum int

New minimum value.

required
maximum int

New maximum value.

required
Source code in client/ayon_ui_qt/components/slider.py
337
338
339
340
341
342
343
344
345
def setRange(self, minimum: int, maximum: int) -> None:
    """Update the slider range.

    Args:
        minimum: New minimum value.
        maximum: New maximum value.
    """
    self._bar.setMinimum(minimum)
    self._bar.setMaximum(maximum)

setStep(step)

Change the snapping step.

Parameters:

Name Type Description Default
step int

New step size (must be >= 1).

required
Source code in client/ayon_ui_qt/components/slider.py
347
348
349
350
351
352
353
354
355
356
357
def setStep(self, step: int) -> None:
    """Change the snapping step.

    Args:
        step: New step size (must be >= 1).
    """
    self._step = max(1, step)
    self._bar.setSingleStep(self._step)
    self._bar.setPageStep(self._step * 10)
    # Re-snap current value to the new step.
    self._bar.setValue(self._snap(self._bar.value()))

setValue(v)

Set the slider to v (clamped and snapped).

Parameters:

Name Type Description Default
v int

Target integer value.

required
Source code in client/ayon_ui_qt/components/slider.py
329
330
331
332
333
334
335
def setValue(self, v: int) -> None:
    """Set the slider to *v* (clamped and snapped).

    Args:
        v: Target integer value.
    """
    self._bar.setValue(self._snap(v))

value()

Return the current slider value.

Returns:

Type Description
int

Current snapped integer value.

Source code in client/ayon_ui_qt/components/slider.py
321
322
323
324
325
326
327
def value(self) -> int:
    """Return the current slider value.

    Returns:
        Current snapped integer value.
    """
    return self._snap(self._bar.value())

AYSliderBar

Bases: StyleMixin, QSlider

Custom-painted horizontal slider track and handle.

Reads all visual properties (track height, handle size, colors, etc.) from ayon_style.json via the AYSlider variant block. The native Qt style is still routed through AYONStyle (setStyle), but the visual painting is done entirely in paintEvent so there is no dependency on drawComplexControl(CC_Slider, ...).

Parameters:

Name Type Description Default
parent QWidget | None

Parent widget.

None
variant_str

Variant string key used to fetch the style dict.

required
Source code in client/ayon_ui_qt/components/slider.py
 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
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
class AYSliderBar(StyleMixin, QSlider):
    """Custom-painted horizontal slider track and handle.

    Reads all visual properties (track height, handle size, colors, etc.)
    from ``ayon_style.json`` via the ``AYSlider`` variant block.  The native
    Qt style is still routed through ``AYONStyle`` (``setStyle``), but the
    visual painting is done entirely in ``paintEvent`` so there is no
    dependency on ``drawComplexControl(CC_Slider, ...)``.

    Args:
        parent: Parent widget.
        variant_str: Variant string key used to fetch the style dict.
    """

    def __init__(
        self,
        parent: QWidget | None = None,
        variant: AYSliderVariants = AYSliderVariants.Default,
    ) -> None:
        self._variant_str = variant.value
        self._style_data = StyleDict()
        super().__init__(Qt.Orientation.Horizontal, parent)

        # Remove all native decoration — we paint everything ourselves.
        self.setAttribute(Qt.WidgetAttribute.WA_Hover, True)
        self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, False)

        _style = get_ayon_style()
        self.setStyle(_style)
        self._style_data = _style.model.get_styles(
            "AYSlider",
            variant=self._variant_str,
        )
        self._style_data.set_context(self)

    def _draw_slider(self, painter: QPainter, style: StyleDict) -> None:
        """Draw track, filled portion, and handle.

        Args:
            painter: QPainter instance (already configured).
            style: Style dictionary for the current state.
        """
        track_h = style.get("track-height", 4)
        track_radius = style.get("track-radius", 2)
        track_color = QColor(style.get("track-color", "#41474d"))
        filled_color = QColor(style.get("filled-color", "#8fceff"))
        handle_size = style.get("handle-size", 14)
        handle_color = QColor(style.get("handle-color", "#8fceff"))
        handle_border_color = QColor(
            style.get("handle-border-color", "#8fceff")
        )
        handle_border_width = style.get("handle-border-width", 0)

        w = self.width()
        h = self.height()

        # Horizontal padding so the handle centre can reach both ends.
        pad = 0
        track_y = (h - track_h) / 2.0
        track_rect = QRectF(pad, track_y, w - pad * 2, track_h)

        minimum = self.minimum()
        maximum = self.maximum()
        value = self.value()
        span = maximum - minimum
        ratio = (value - minimum) / span if span > 0 else 0.0

        filled_w = track_rect.width() * ratio
        filled_rect = QRectF(track_rect.x(), track_rect.y(), filled_w, track_h)

        # Empty track
        painter.setPen(Qt.PenStyle.NoPen)
        painter.setBrush(QBrush(track_color))
        painter.drawRoundedRect(track_rect, track_radius, track_radius)

        # Filled portion (clip to left half of track)
        if filled_w > 0:
            painter.setBrush(QBrush(filled_color))
            painter.drawRoundedRect(filled_rect, track_radius, track_radius)

        # Handle
        h_size = handle_size / 2.0
        t_width = track_rect.width() - handle_size
        handle_x = track_rect.x() + h_size + t_width * ratio
        handle_y = h / 2.0
        handle_r = handle_size / 2.0
        handle_rect = QRectF(
            handle_x - handle_r,
            handle_y - handle_r,
            handle_size,
            handle_size,
        )

        if handle_border_width > 0:
            pen = QPen(handle_border_color)
            pen.setWidthF(handle_border_width)
            painter.setPen(pen)
        else:
            painter.setPen(Qt.PenStyle.NoPen)

        painter.setBrush(QBrush(handle_color))
        painter.drawEllipse(handle_rect)

    def paintEvent(self, event: QPaintEvent) -> None:
        """Paint track, filled portion, and handle using QPainter directly.

        Args:
            event: The paint event (unused — we repaint the full widget).
        """
        is_disabled = not self.isEnabled()
        is_hover = self.underMouse()

        if is_disabled:
            state = "disabled"
        elif is_hover:
            state = "hover"
        else:
            state = "base"

        style = self._style_data[state]

        opacity = style.get("opacity", 1.0)

        painter = QPainter(self)
        painter.setRenderHint(QPainter.RenderHint.Antialiasing)

        if opacity < 1.0:
            buffer = QPixmap(self.size() * self.devicePixelRatioF())
            buffer.setDevicePixelRatio(self.devicePixelRatioF())
            buffer.fill(Qt.GlobalColor.transparent)

            bp = QPainter(buffer)
            bp.setRenderHint(QPainter.RenderHint.Antialiasing)
            self._draw_slider(bp, style)
            bp.end()

            painter.setOpacity(opacity)
            painter.drawPixmap(0, 0, buffer)
        else:
            self._draw_slider(painter, style)

        painter.end()

paintEvent(event)

Paint track, filled portion, and handle using QPainter directly.

Parameters:

Name Type Description Default
event QPaintEvent

The paint event (unused — we repaint the full widget).

required
Source code in client/ayon_ui_qt/components/slider.py
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
def paintEvent(self, event: QPaintEvent) -> None:
    """Paint track, filled portion, and handle using QPainter directly.

    Args:
        event: The paint event (unused — we repaint the full widget).
    """
    is_disabled = not self.isEnabled()
    is_hover = self.underMouse()

    if is_disabled:
        state = "disabled"
    elif is_hover:
        state = "hover"
    else:
        state = "base"

    style = self._style_data[state]

    opacity = style.get("opacity", 1.0)

    painter = QPainter(self)
    painter.setRenderHint(QPainter.RenderHint.Antialiasing)

    if opacity < 1.0:
        buffer = QPixmap(self.size() * self.devicePixelRatioF())
        buffer.setDevicePixelRatio(self.devicePixelRatioF())
        buffer.fill(Qt.GlobalColor.transparent)

        bp = QPainter(buffer)
        bp.setRenderHint(QPainter.RenderHint.Antialiasing)
        self._draw_slider(bp, style)
        bp.end()

        painter.setOpacity(opacity)
        painter.drawPixmap(0, 0, buffer)
    else:
        self._draw_slider(painter, style)

    painter.end()