Skip to content

Commit

Permalink
Allow to change the volume using a mouse hires wheel
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kreijack committed Jan 12, 2023
1 parent 45f26c2 commit 195978b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions plugin-volume/volumepopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 195978b

Please sign in to comment.