From 705600ef50b453b2aa2950da169933bcc413f008 Mon Sep 17 00:00:00 2001 From: Kevin Mehall Date: Sat, 4 Feb 2017 09:56:07 -0800 Subject: [PATCH 1/2] Make ctrl+scroll zoom in and out of cursor point, rather than center --- plotview.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/plotview.cpp b/plotview.cpp index e71e76a..ac4d106 100644 --- a/plotview.cpp +++ b/plotview.cpp @@ -170,9 +170,14 @@ bool PlotView::viewportEvent(QEvent *event) { if (event->type() == QEvent::Wheel) { QWheelEvent *wheelEvent = (QWheelEvent*)event; if (QApplication::keyboardModifiers() & Qt::ControlModifier) { - if (wheelEvent->angleDelta().y() > 0) { + // `updateViewRange()` keeps the center sample in the same place after zoom. Apply + // a scroll adjustment to keep the sample under the mouse cursor in the same place instead. + int fromCenter = wheelEvent->pos().x() - width()/2; + if (wheelEvent->angleDelta().y() > 0 && zoomLevel < fftSize) { emit zoomIn(); - } else if (wheelEvent->angleDelta().y() < 0) { + horizontalScrollBar()->setValue(horizontalScrollBar()->value() + fromCenter * samplesPerLine()); + } else if (wheelEvent->angleDelta().y() < 0 && zoomLevel > 1) { + horizontalScrollBar()->setValue(horizontalScrollBar()->value() - fromCenter * samplesPerLine()); emit zoomOut(); } return true; From 90d798a93b64e1c8add62ab218ff870163b08fbf Mon Sep 17 00:00:00 2001 From: Kevin Mehall Date: Sat, 4 Feb 2017 10:21:26 -0800 Subject: [PATCH 2/2] Make scroll wheel zoom less jumpy on fractional-scroll devices like touchpads --- plotview.cpp | 27 ++++++++++++++++++--------- plotview.h | 1 + 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/plotview.cpp b/plotview.cpp index ac4d106..c8aa80c 100644 --- a/plotview.cpp +++ b/plotview.cpp @@ -170,15 +170,24 @@ bool PlotView::viewportEvent(QEvent *event) { if (event->type() == QEvent::Wheel) { QWheelEvent *wheelEvent = (QWheelEvent*)event; if (QApplication::keyboardModifiers() & Qt::ControlModifier) { - // `updateViewRange()` keeps the center sample in the same place after zoom. Apply - // a scroll adjustment to keep the sample under the mouse cursor in the same place instead. - int fromCenter = wheelEvent->pos().x() - width()/2; - if (wheelEvent->angleDelta().y() > 0 && zoomLevel < fftSize) { - emit zoomIn(); - horizontalScrollBar()->setValue(horizontalScrollBar()->value() + fromCenter * samplesPerLine()); - } else if (wheelEvent->angleDelta().y() < 0 && zoomLevel > 1) { - horizontalScrollBar()->setValue(horizontalScrollBar()->value() - fromCenter * samplesPerLine()); - emit zoomOut(); + bool canZoomIn = zoomLevel < fftSize; + bool canZoomOut = zoomLevel > 1; + int delta = wheelEvent->angleDelta().y(); + if ((delta > 0 && canZoomIn) || (delta < 0 && canZoomOut)) { + scrollZoomStepsAccumulated += delta; + + // `updateViewRange()` keeps the center sample in the same place after zoom. Apply + // a scroll adjustment to keep the sample under the mouse cursor in the same place instead. + int fromCenter = wheelEvent->pos().x() - width()/2; + if (scrollZoomStepsAccumulated >= 120) { + scrollZoomStepsAccumulated -= 120; + emit zoomIn(); + horizontalScrollBar()->setValue(horizontalScrollBar()->value() + fromCenter * samplesPerLine()); + } else if (scrollZoomStepsAccumulated <= -120) { + scrollZoomStepsAccumulated += 120; + horizontalScrollBar()->setValue(horizontalScrollBar()->value() - fromCenter * samplesPerLine()); + emit zoomOut(); + } } return true; } diff --git a/plotview.h b/plotview.h index 231939a..5cce114 100644 --- a/plotview.h +++ b/plotview.h @@ -75,6 +75,7 @@ private: bool cursorsEnabled; off_t sampleRate = 0; bool timeScaleEnabled; + int scrollZoomStepsAccumulated = 0; void addPlot(Plot *plot); void emitTimeSelection();