Skip to content

SpanSlider

Qt Base Class: QSlider

Signature: QSlider(self, orientation: PySide6.QtCore.Qt.Orientation, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None QSlider(self, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None

Base classes

Name Children Inherits
Slider
prettyqt.widgets.slider

⋔ Inheritance diagram

graph TD
  1473367152624["custom_widgets.SpanSlider"]
  1473296205136["widgets.Slider"]
  1473293701904["widgets.AbstractSliderMixin"]
  1473293688240["widgets.WidgetMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473245548480["gui.PaintDeviceMixin"]
  1473365411008["QtWidgets.QSlider"]
  1473365440288["QtWidgets.QAbstractSlider"]
  1473290849680["QtWidgets.QWidget"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473300082368["QtGui.QPaintDevice"]
  1473296205136 --> 1473367152624
  1473293701904 --> 1473296205136
  1473293688240 --> 1473293701904
  1473299815024 --> 1473293688240
  140713234304496 --> 1473299815024
  1473245548480 --> 1473293688240
  140713234304496 --> 1473245548480
  1473365411008 --> 1473296205136
  1473365440288 --> 1473365411008
  1473290849680 --> 1473365440288
  1473288842240 --> 1473290849680
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208
  1473300082368 --> 1473290849680
  1473291690208 --> 1473300082368

🛈 DocStrings

Bases: Slider

Source code in prettyqt\custom_widgets\editors\spanslider.py
 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
class SpanSlider(widgets.Slider):
    value_changed = core.Signal(object)
    lower_pos_changed = core.Signal(float)
    upper_pos_changed = core.Signal(float)
    slider_pressed = core.Signal(object)

    def __init__(self, *args, object_name: str = "span_slider", **kwargs):
        super().__init__("horizontal", *args, object_name=object_name, **kwargs)
        self.rangeChanged.connect(self.update_range)
        self.sliderReleased.connect(self._move_pressed_handle)

        self.lower_val = 0.0
        self.upper_val = 0.0
        self.lower_pos = 0.0
        self.upper_pos = 0.0
        self.offset = 0
        self.position = 0.0
        self.last_pressed: str | None = None
        self.upper_pressed = widgets.Style.SubControl.SC_None
        self.lower_pressed = widgets.Style.SubControl.SC_None
        self.movement: MovementModeStr = "no_crossing"
        self._main_control: Literal["lower", "upper"] = "lower"
        self._first_movement = False
        self._block_tracking = False
        dark_color = self.palette().color(gui.Palette.ColorRole.Dark)
        self.gradient_left = dark_color.lighter(110)
        self.gradient_right = dark_color.lighter(110)

    def mousePressEvent(self, event):
        if self.minimum() == self.maximum() or event.buttons() ^ event.button():
            event.ignore()
            return

        self.upper_pressed = self._handle_mouse_press(
            event.position(), self.upper_pressed, self.upper_val, "upper"
        )
        if self.upper_pressed != HANDLE_STYLE:
            self.lower_pressed = self._handle_mouse_press(
                event.position(), self.lower_pressed, self.lower_val, "lower"
            )

        self._first_movement = True
        event.accept()

    def mouseMoveEvent(self, event):
        if self.lower_pressed != HANDLE_STYLE and self.upper_pressed != HANDLE_STYLE:
            event.ignore()
            return

        opt = widgets.StyleOptionSlider()
        self.initStyleOption(opt)
        m = self.style().pixelMetric(
            widgets.Style.PixelMetric.PM_MaximumDragDistance, opt, self
        )
        pixel_pos = int(self.pick(event.position()) - self.offset)
        new_pos = float(self._pixel_pos_to_value(pixel_pos))
        if m >= 0:
            r = self.rect().adjusted(-m, -m, m, m)
            if not r.contains(event.position().toPoint()):
                new_pos = self.position

        # pick the preferred handle on the first movement
        if self._first_movement:
            if self.lower_val == self.upper_val:
                if new_pos < self.get_lower_value():
                    self._swap_controls()
                    self._first_movement = False
            else:
                self._first_movement = False

        match HANDLE_STYLE, self.movement:
            case self.lower_pressed, "no_crossing":
                new_pos = min(new_pos, self.upper_val)
                self.set_lower_pos(new_pos)
            case self.lower_pressed, "no_overlap":
                new_pos = min(new_pos, self.upper_val - 1)
                self.set_lower_pos(new_pos)
            case self.lower_pressed, "free" if new_pos > self.upper_val:
                self._swap_controls()
                self.set_upper_pos(new_pos)
            case self.upper_pressed, "no_crossing":
                new_pos = max(new_pos, self.get_lower_value())
                self.set_upper_pos(new_pos)
            case self.upper_pressed, "no_overlap":
                new_pos = max(new_pos, self.get_lower_value() + 1)
                self.set_upper_pos(new_pos)
            case self.upper_pressed, "free" if new_pos < self.lower_val:
                self._swap_controls()
                self.set_lower_pos(new_pos)
        event.accept()

    def mouseReleaseEvent(self, event):
        super().mouseReleaseEvent(event)
        self.setSliderDown(False)
        self.lower_pressed = self.upper_pressed = widgets.Style.SubControl.SC_None
        self.update()

    def paintEvent(self, event):
        painter = widgets.StylePainter(self)

        # ticks
        opt = widgets.StyleOptionSlider()
        self.initStyleOption(opt)
        opt.subControls = widgets.Style.SubControl.SC_SliderTickmarks
        painter.draw_complex_control("slider", opt)

        # groove
        opt.sliderPosition = 20
        opt.sliderValue = 0
        opt.subControls = GROOVE_STYLE
        painter.draw_complex_control("slider", opt)

        # handle rects
        opt.sliderPosition = int(self.lower_pos)
        lr = self.style().subControlRect(SLIDER_STYLE, opt, HANDLE_STYLE, self)
        lrv = self.pick(lr.center())
        opt.sliderPosition = int(self.upper_pos)
        ur = self.style().subControlRect(SLIDER_STYLE, opt, HANDLE_STYLE, self)
        urv = self.pick(ur.center())

        # span
        minv = min(lrv, urv)
        maxv = max(lrv, urv)
        c = self.style().subControlRect(SLIDER_STYLE, opt, GROOVE_STYLE, self).center()
        if self.is_horizontal():
            rect = core.Rect(core.Point(minv, c.y() - 2), core.Point(maxv, c.y() + 1))
        else:
            rect = core.Rect(core.Point(c.x() - 2, minv), core.Point(c.x() + 1, maxv))
        self._draw_span(painter, rect)

        # handles
        if self.last_pressed == "lower":
            self.draw_handle(painter, "upper")
            self.draw_handle(painter, "lower")
        else:
            self.draw_handle(painter, "lower")
            self.draw_handle(painter, "upper")

    def get_lower_value(self) -> float:
        return min(self.lower_val, self.upper_val)

    def set_lower_value(self, lower: float):
        self.set_span(lower, self.upper_val)

    def get_upper_value(self) -> float:
        return max(self.lower_val, self.upper_val)

    def set_upper_value(self, upper: float):
        self.set_span(self.lower_val, upper)

    def on_value_change(self):
        self.value_changed.emit((self.lower_val, self.upper_val))

    def get_value(self) -> tuple[float, float]:
        return (self.lower_val, self.upper_val)

    def set_value(self, value: tuple[float, float]):
        self.set_lower_value(value[0])
        self.set_upper_value(value[1])

    def get_movement_mode(self) -> MovementModeStr:
        return self.movement

    def set_movement_mode(self, mode: MovementModeStr):
        """Set movement mode.

        Args:
            mode: movement mode for the main window

        Raises:
            ValueError: movement mode type does not exist
        """
        if mode not in MOVEMENT_MODE:
            raise ValueError("Invalid movement mode")
        self.movement = mode

    def set_span(self, lower: float, upper: float):
        low = clamp(min(lower, upper), self.minimum(), self.maximum())
        upp = clamp(max(lower, upper), self.minimum(), self.maximum())
        changed = False
        if low != self.lower_val:
            self.lower_val = low
            self.lower_pos = low
            changed = True
        if upp != self.upper_val:
            self.upper_val = upp
            self.upper_pos = upp
            changed = True
        if changed:
            self.on_value_change()
            self.update()

    def set_lower_pos(self, lower: float):
        if self.lower_pos == lower:
            return
        self.lower_pos = lower
        if not self.hasTracking():
            self.update()
        if self.isSliderDown():
            self.lower_pos_changed.emit(lower)
        if self.hasTracking() and not self._block_tracking:
            main = self._main_control == "lower"
            self.trigger_action("move", main)

    def set_upper_pos(self, upper: float):
        if self.upper_pos == upper:
            return
        self.upper_pos = upper
        if not self.hasTracking():
            self.update()
        if self.isSliderDown():
            self.upper_pos_changed.emit(upper)
        if self.hasTracking() and not self._block_tracking:
            main = self._main_control == "upper"
            self.trigger_action("move", main)

    def set_left_color(self, color: datatypes.ColorType):
        self.gradient_left = colors.get_color(color)
        self.update()

    def set_right_color(self, color: datatypes.ColorType):
        self.gradient_right = colors.get_color(color)
        self.update()

    def _move_pressed_handle(self):
        if self.last_pressed == "lower":
            if self.lower_pos != self.lower_val:
                main = self._main_control == "lower"
                self.trigger_action("move", main)
        elif self.last_pressed == "upper" and self.upper_pos != self.upper_val:
            main = self._main_control == "upper"
            self.trigger_action("move", main)

    def pick(self, p: datatypes.PointType) -> int:
        if isinstance(p, tuple):
            return p[0] if self.is_horizontal() else p[1]
        else:
            return p.x() if self.is_horizontal() else p.y()

    def trigger_action(self, action: ActionStr, main: bool):
        value = 0.0
        no = False
        up = False
        my_min = self.minimum()
        my_max = self.maximum()
        self._block_tracking = True
        main_control = main and self._main_control == "upper"
        alt_control = not main and self._main_control == "lower"
        is_upper_handle = main_control or alt_control
        val = self.upper_val if is_upper_handle else self.lower_val
        match action:
            case "single_step_add":
                up = is_upper_handle
                value = clamp(val + self.singleStep(), my_min, my_max)
            case "single_step_sub":
                up = is_upper_handle
                value = clamp(val - self.singleStep(), my_min, my_max)
            case "to_minimum":
                up = is_upper_handle
                value = my_min
            case "to_maximum":
                up = is_upper_handle
                value = my_max
            case "move":
                up = is_upper_handle
                no = True
            case "none":
                no = True

        if not no and not up:
            match self.movement:
                case "no_crossing":
                    value = min(value, self.upper_val)
                case "no_overlap":
                    value = min(value, self.upper_val - 1)

                case "free" if value > self.upper_val:
                    self._swap_controls()
                    self.set_upper_pos(value)
                case "free":
                    self.set_lower_pos(value)
        elif not no:
            match self.movement:
                case "no_crossing":
                    value = max(value, self.lower_val)
                case "no_overlap":
                    value = max(value, self.lower_val + 1)

                case "free" if value < self.lower_val:
                    self._swap_controls()
                    self.set_lower_pos(value)
                case "free":
                    self.set_upper_pos(value)

        self._block_tracking = False
        self.set_lower_value(self.lower_pos)
        self.set_upper_value(self.upper_pos)

    def _swap_controls(self):
        self.lower_val, self.upper_val = self.upper_val, self.lower_val
        self.lower_pressed, self.upper_pressed = self.upper_pressed, self.lower_pressed
        self.last_pressed = "upper" if self.last_pressed == "lower" else "lower"
        self._main_control = "upper" if self._main_control == "lower" else "lower"

    def update_range(self, min_, max_):
        # set_span() takes care of keeping span in range
        self.set_span(self.lower_val, self.upper_val)

    def _setup_painter(
        self,
        painter: widgets.StylePainter,
        orientation: Literal["horizontal", "vertical"],
        x1: int,
        y1: int,
        x2: int,
        y2: int,
    ):
        highlight = self.palette().color(gui.Palette.ColorRole.Highlight)
        gradient = gui.LinearGradient(x1, y1, x2, y2)
        gradient[0] = highlight.darker(120)
        gradient[1] = highlight.lighter(108)
        painter.setBrush(gradient)

        val = 130 if orientation == "horizontal" else 150
        painter.set_pen(color=highlight.darker(val), width=0)

    def _draw_span(self, painter: widgets.StylePainter, rect: core.Rect):
        opt = widgets.StyleOptionSlider()
        self.initStyleOption(opt)
        painter.set_pen(color=self.gradient_left, width=0)
        groove = self.style().subControlRect(SLIDER_STYLE, opt, GROOVE_STYLE, self)
        if opt.is_horizontal():
            groove.adjust(0, 0, -1, 0)
            self._setup_painter(
                painter,
                opt.get_orientation(),
                groove.center().x(),
                groove.top(),
                groove.center().x(),
                groove.bottom(),
            )
        else:
            groove.adjust(0, 0, 0, -1)
            self._setup_painter(
                painter,
                opt.get_orientation(),
                groove.left(),
                groove.center().y(),
                groove.right(),
                groove.center().y(),
            )

        # draw groove
        intersected = core.RectF(rect.intersected(groove))
        gradient = gui.LinearGradient(intersected.topLeft(), intersected.topRight())
        gradient[0] = self.gradient_left
        gradient[1] = self.gradient_right
        painter.fillRect(intersected, gradient)

    def draw_handle(self, painter: widgets.StylePainter, handle: HandleStr):
        opt = self.get_style_option(handle)
        opt.subControls = HANDLE_STYLE
        pressed = self.lower_pressed if handle == "lower" else self.upper_pressed
        if pressed == HANDLE_STYLE:
            opt.activeSubControls = pressed
            opt.state |= widgets.Style.StateFlag.State_Sunken
        painter.draw_complex_control("slider", opt)

    def get_style_option(self, handle: HandleStr) -> widgets.StyleOptionSlider:
        option = widgets.StyleOptionSlider()
        self.initStyleOption(option)
        if handle == "lower":
            option.sliderPosition = int(self.lower_pos)
            option.sliderValue = int(self.lower_val)
        else:
            option.sliderPosition = int(self.upper_pos)
            option.sliderValue = int(self.upper_val)
        return option

    def _handle_mouse_press(
        self, pos: core.QPointF, control, value: float, handle: HandleStr
    ):
        opt = self.get_style_option(handle)
        old_control = control
        control = self.style().hitTestComplexControl(
            SLIDER_STYLE, opt, pos.toPoint(), self
        )
        sr = self.style().subControlRect(SLIDER_STYLE, opt, HANDLE_STYLE, self)
        if control == HANDLE_STYLE:
            self.position = value
            self.offset = self.pick(pos.toPoint() - sr.topLeft())
            self.last_pressed = handle
            self.setSliderDown(True)
            self.slider_pressed.emit(handle)
        if control != old_control:
            self.update(sr)
        return control

    def _pixel_pos_to_value(self, pos: int) -> int:
        opt = widgets.StyleOptionSlider()
        self.initStyleOption(opt)
        gr = self.style().subControlRect(SLIDER_STYLE, opt, GROOVE_STYLE, self)
        sr = self.style().subControlRect(SLIDER_STYLE, opt, HANDLE_STYLE, self)
        if self.is_horizontal():
            len_slider = sr.width()
            slider_min = gr.x()
            slider_end = gr.right()
        else:
            len_slider = sr.height()
            slider_min = gr.y()
            slider_end = gr.bottom()

        return widgets.Style.sliderValueFromPosition(
            self.minimum(),
            self.maximum(),
            pos - slider_min,
            slider_end - len_slider + 1 - slider_min,
            opt.upsideDown,
        )

    lower_value = core.Property(
        float,
        get_lower_value,
        set_lower_value,
        doc="Lower value",
    )
    upper_value = core.Property(
        float,
        get_upper_value,
        set_upper_value,
        doc="Upper value",
    )

set_movement_mode(mode: MovementModeStr)

Set movement mode.

Parameters:

Name Type Description Default
mode MovementModeStr

movement mode for the main window

required

Raises:

Type Description
ValueError

movement mode type does not exist

Source code in prettyqt\custom_widgets\editors\spanslider.py
def set_movement_mode(self, mode: MovementModeStr):
    """Set movement mode.

    Args:
        mode: movement mode for the main window

    Raises:
        ValueError: movement mode type does not exist
    """
    if mode not in MOVEMENT_MODE:
        raise ValueError("Invalid movement mode")
    self.movement = mode

⌗ 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
minimum int
maximum int
singleStep int
pageStep int
value int
sliderPosition int
tracking bool
orientation Qt::Orientation
invertedAppearance bool
invertedControls bool
sliderDown bool
tickPosition QSlider::TickPosition
tickInterval int
lower_value double Lower value
upper_value double Upper value