From 32fe3e50e7d951a456764dc1692b83f812bb2a87 Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Tue, 21 May 2024 11:32:28 -0400 Subject: [PATCH] Add "natural" scrolling support for trackpads (#5510) Adds QWheelEvent::inverted() support to spinboxes, knobs, sliders --- src/gui/clips/MidiClipView.cpp | 6 +++--- src/gui/editors/PianoRoll.cpp | 3 ++- src/gui/widgets/ComboBox.cpp | 3 ++- src/gui/widgets/Fader.cpp | 10 ++-------- src/gui/widgets/FloatModelEditorBase.cpp | 5 +++++ src/gui/widgets/LcdSpinBox.cpp | 4 +++- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/gui/clips/MidiClipView.cpp b/src/gui/clips/MidiClipView.cpp index 0a6fece31..b735913e4 100644 --- a/src/gui/clips/MidiClipView.cpp +++ b/src/gui/clips/MidiClipView.cpp @@ -332,7 +332,8 @@ void MidiClipView::wheelEvent(QWheelEvent * we) } Note * n = m_clip->noteAtStep( step ); - if(!n && we->angleDelta().y() > 0) + const int direction = (we->angleDelta().y() > 0 ? 1 : -1) * (we->inverted() ? -1 : 1); + if(!n && direction > 0) { n = m_clip->addStepNote( step ); n->setVolume( 0 ); @@ -340,8 +341,7 @@ void MidiClipView::wheelEvent(QWheelEvent * we) if( n != nullptr ) { int vol = n->getVolume(); - - if(we->angleDelta().y() > 0) + if(direction > 0) { n->setVolume( qMin( 100, vol + 5 ) ); } diff --git a/src/gui/editors/PianoRoll.cpp b/src/gui/editors/PianoRoll.cpp index 2e99aab4d..4723004e8 100644 --- a/src/gui/editors/PianoRoll.cpp +++ b/src/gui/editors/PianoRoll.cpp @@ -3774,7 +3774,8 @@ void PianoRoll::wheelEvent(QWheelEvent * we ) } if( nv.size() > 0 ) { - const int step = we->angleDelta().y() > 0 ? 1 : -1; + const int step = (we->angleDelta().y() > 0 ? 1 : -1) * (we->inverted() ? -1 : 1); + if( m_noteEditMode == NoteEditMode::Volume ) { for ( Note * n : nv ) diff --git a/src/gui/widgets/ComboBox.cpp b/src/gui/widgets/ComboBox.cpp index eb019876a..0daae1b24 100644 --- a/src/gui/widgets/ComboBox.cpp +++ b/src/gui/widgets/ComboBox.cpp @@ -220,7 +220,8 @@ void ComboBox::wheelEvent( QWheelEvent* event ) { if( model() ) { - model()->setInitValue(model()->value() + ((event->angleDelta().y() < 0) ? 1 : -1)); + const int direction = (event->angleDelta().y() < 0 ? 1 : -1) * (event->inverted() ? -1 : 1); + model()->setInitValue(model()->value() + direction); update(); event->accept(); } diff --git a/src/gui/widgets/Fader.cpp b/src/gui/widgets/Fader.cpp index 1eb06756a..d2d7c1c2e 100644 --- a/src/gui/widgets/Fader.cpp +++ b/src/gui/widgets/Fader.cpp @@ -193,15 +193,9 @@ void Fader::mouseReleaseEvent(QMouseEvent* mouseEvent) void Fader::wheelEvent (QWheelEvent* ev) { ev->accept(); + const int direction = (ev->angleDelta().y() > 0 ? 1 : -1) * (ev->inverted() ? -1 : 1); - if (ev->angleDelta().y() > 0) - { - model()->incValue(1); - } - else - { - model()->incValue(-1); - } + model()->incValue(direction); updateTextFloat(); s_textFloat->setVisibilityTimeOut(1000); } diff --git a/src/gui/widgets/FloatModelEditorBase.cpp b/src/gui/widgets/FloatModelEditorBase.cpp index dd6be8958..ebd0d3d9d 100644 --- a/src/gui/widgets/FloatModelEditorBase.cpp +++ b/src/gui/widgets/FloatModelEditorBase.cpp @@ -326,6 +326,11 @@ void FloatModelEditorBase::wheelEvent(QWheelEvent * we) } } + // Handle "natural" scrolling, which is common on trackpads and touch devices + if (we->inverted()) { + direction = -direction; + } + // Compute the number of steps but make sure that we always do at least one step const float stepMult = std::max(range / numberOfStepsForFullSweep / step, 1.f); const int inc = direction * stepMult; diff --git a/src/gui/widgets/LcdSpinBox.cpp b/src/gui/widgets/LcdSpinBox.cpp index b53d7ddb5..3f12360cc 100644 --- a/src/gui/widgets/LcdSpinBox.cpp +++ b/src/gui/widgets/LcdSpinBox.cpp @@ -141,7 +141,9 @@ void LcdSpinBox::mouseReleaseEvent(QMouseEvent*) void LcdSpinBox::wheelEvent(QWheelEvent * we) { we->accept(); - model()->setValue(model()->value() + ((we->angleDelta().y() > 0) ? 1 : -1) * model()->step()); + const int direction = (we->angleDelta().y() > 0 ? 1 : -1) * (we->inverted() ? -1 : 1); + + model()->setValue(model()->value() + direction * model()->step()); emit manualChange(); }