Skip to content

Commit 9f46b9c

Browse files
committed
plugin-volume: add support for hires wheel
Some mouses send a lot of events with "small" movement of the wheel. This invalidate some evaluation of the total stroke of the wheel. In the Volume Control plugin, the volume was computed on the basis of the following formula: new_volume = old_volume + event->angleDelta().y() / QWheelEvent::DefaultDeltasPerStep * volumeSlider->singleStep() However if event->angleDelta().y() is smaller than QWheelEvent::DefaultDeltasPerStep, their ratio is a value less than 1, which is zero when the math is integer based. To avoid this issue the volumeSlider->singleStep() will be increased by a factor of 1000 and the multiplication will be performed before the division. Of course all others computation will be scaled according: - the range of the volume slider will be 0..100*1000 (where previous was 0..100) - the volume will be computed as: volume = slider->value() / 1000 - slider->singleStep will be increased by a factor of 1000
1 parent 38c8a8d commit 9f46b9c

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

plugin-volume/volumepopup.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ VolumePopup::VolumePopup(QWidget* parent):
6262

6363
m_volumeSlider = new QSlider(Qt::Vertical, this);
6464
m_volumeSlider->setTickPosition(QSlider::TicksBothSides);
65-
m_volumeSlider->setTickInterval(10);
66-
// the volume slider shows 0-100 and volumes of all devices
67-
// should be converted to percentages.
68-
m_volumeSlider->setRange(0, 100);
65+
m_volumeSlider->setTickInterval(10 * 1000);
66+
// volume is in the range 0..100, and the slider is in the range 0..100*1000
67+
m_volumeSlider->setRange(0, 100 * 1000);
6968
m_volumeSlider->installEventFilter(this);
7069

7170
m_muteToggleButton = new QPushButton(this);
@@ -126,7 +125,8 @@ void VolumePopup::handleSliderValueChanged(int value)
126125
if (!m_device)
127126
return;
128127
// qDebug("VolumePopup::handleSliderValueChanged: %d\n", value);
129-
m_device->setVolume(value);
128+
// volume is in the range 0..100, and the slider is in the range 0..100*1000
129+
m_device->setVolume(value / 1000);
130130
QTimer::singleShot(0, this, [this] { QToolTip::showText(QCursor::pos(), m_volumeSlider->toolTip()); });
131131
}
132132

@@ -140,13 +140,14 @@ void VolumePopup::handleMuteToggleClicked()
140140

141141
void VolumePopup::handleDeviceVolumeChanged(int volume)
142142
{
143-
// qDebug() << "handleDeviceVolumeChanged" << "volume" << volume << "max" << max;
143+
// qDebug() << "handleDeviceVolumeChanged" << "volume" << volume;
144144
// calling m_volumeSlider->setValue will trigger
145145
// handleSliderValueChanged(), which set the device volume
146146
// again, so we have to block the signals to avoid recursive
147147
// signal emission.
148148
m_volumeSlider->blockSignals(true);
149-
m_volumeSlider->setValue(volume);
149+
// volume is in the range 0..100, and the slider is in the range 0..100*1000
150+
m_volumeSlider->setValue(volume * 1000);
150151
m_volumeSlider->setToolTip(QStringLiteral("%1%").arg(volume));
151152
dynamic_cast<QWidget&>(*parent()).setToolTip(m_volumeSlider->toolTip()); //parent is the button on panel
152153
m_volumeSlider->blockSignals(false);
@@ -198,7 +199,9 @@ void VolumePopup::openAt(QPoint pos, Qt::Corner anchor)
198199
void VolumePopup::handleWheelEvent(QWheelEvent *event)
199200
{
200201
m_volumeSlider->setSliderPosition(m_volumeSlider->sliderPosition()
201-
+ (event->angleDelta().y() / QWheelEvent::DefaultDeltasPerStep * m_volumeSlider->singleStep()));
202+
+ (event->angleDelta().y() *
203+
m_volumeSlider->singleStep() /
204+
QWheelEvent::DefaultDeltasPerStep));
202205
}
203206

204207
void VolumePopup::setDevice(AudioDevice *device)
@@ -225,6 +228,7 @@ void VolumePopup::setDevice(AudioDevice *device)
225228

226229
void VolumePopup::setSliderStep(int step)
227230
{
231+
step *= 1000; // step is in range 0..100
228232
m_volumeSlider->setSingleStep(step);
229233
m_volumeSlider->setPageStep(step * 10);
230234
}

0 commit comments

Comments
 (0)