From 195978b8e8608494a48b873fd451c3b82526e479 Mon Sep 17 00:00:00 2001 From: Goffredo Baroncelli Date: Thu, 12 Jan 2023 21:12:36 +0100 Subject: [PATCH] Allow to change the volume using a mouse hires wheel The Volume control plugin allows to change the volume using the mouse wheel. However for hires wheel this doesn't work. The volume change is controlled by the following formula: m_volumeSlider->setSliderPosition(m_volumeSlider->sliderPosition() + (event->angleDelta().y() / QWheelEvent::DefaultDeltasPerStep * m_volumeSlider->singleStep())); For some kind of mouse where the wheel event is compose by a lot of small 'delta'; the ratio event->angleDelta().y() / QWheelEvent::DefaultDeltasPerStep is less than 1; and because this ratio is between two integers it is rounded to 0. So store the fraction part of the wheel stroke in a static variable. --- plugin-volume/volumepopup.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/plugin-volume/volumepopup.cpp b/plugin-volume/volumepopup.cpp index 4b85363ec..8b32d7603 100644 --- a/plugin-volume/volumepopup.cpp +++ b/plugin-volume/volumepopup.cpp @@ -197,8 +197,21 @@ void VolumePopup::openAt(QPoint pos, Qt::Corner anchor) void VolumePopup::handleWheelEvent(QWheelEvent *event) { - m_volumeSlider->setSliderPosition(m_volumeSlider->sliderPosition() - + (event->angleDelta().y() / QWheelEvent::DefaultDeltasPerStep * m_volumeSlider->singleStep())); + // With a hig res mouse wheel, event->angleDelta().y() may be + // less than QWheelEvent::DefaultDeltasPerStep, so we need to + // accomulate the fraction between each handleWheelEvent call. + // We can declare it static, because even if it would another + // instance of VolumePopup, this would impact onlythe start + // of the strokes. + static int fractionalAngleDelta = 0; + + fractionalAngleDelta += event->angleDelta().y(); + if (fractionalAngleDelta / QWheelEvent::DefaultDeltasPerStep != 0) { + m_volumeSlider->setSliderPosition(m_volumeSlider->sliderPosition() + + (fractionalAngleDelta / QWheelEvent::DefaultDeltasPerStep * + m_volumeSlider->singleStep())); + fractionalAngleDelta %= QWheelEvent::DefaultDeltasPerStep; + } } void VolumePopup::setDevice(AudioDevice *device)