Wobuzz/wobuzz/ui/track_progress_slider.py

47 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/python3
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QMouseEvent
from PyQt6.QtWidgets import QSlider, QStyle, QStyleOptionSlider
class TrackProgressSlider(QSlider):
2024-12-22 19:42:48 +01:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.dragged = False
option = QStyleOptionSlider()
style = self.style()
self.handle_width = style.pixelMetric(QStyle.PixelMetric.PM_SliderThickness, option, self)
2024-12-22 19:42:48 +01:00
self.sliderPressed.connect(self.on_press)
self.sliderReleased.connect(self.on_release)
def mousePressEvent(self, event: QMouseEvent):
# we need a custom press event bc. PyQt doesn't set the slider position when clicked
if event.button() == Qt.MouseButton.LeftButton:
event.accept()
x = event.pos().x()
# old value calculation:
# value = self.maximum() * x // self.width()
2025-01-25 13:02:23 +01:00
# but we need that new calculation because the handle width is limiting the range, and we need to set
# the slider handle's center to the click position, not to the start of the handle
# (self.width() - self.handle_width) calculates the usable width and
# (x - self.handle_width // 2) offsets the position by minus half of the handle width
value = self.maximum() * (x - self.handle_width // 2) // (self.width() - self.handle_width)
self.setValue(value)
return super().mousePressEvent(event)
2024-12-22 19:42:48 +01:00
def on_press(self):
self.dragged = True
def on_release(self):
self.dragged = False