From cb6bba8f1e8dcd224e5333c78ece76e6d93ce84a Mon Sep 17 00:00:00 2001
From: Jacob Gilbert <jacob.gilbert@protonmail.com>
Date: Sat, 24 Jul 2021 06:04:06 -0600
Subject: [PATCH] when increasing the FFT size, the plot should maintain the
 current center frequency

Signed-off-by: Jacob Gilbert <jacob.gilbert@protonmail.com>
---
 src/plotview.cpp | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/plotview.cpp b/src/plotview.cpp
index 914c2c9..f5b2bd1 100644
--- a/src/plotview.cpp
+++ b/src/plotview.cpp
@@ -388,6 +388,7 @@ void PlotView::setCursorSegments(int segments)
     selectedSamples.maximum = selectedSamples.minimum + (segments * sampPerSeg + 0.5f);
 
     cursors.setSegments(segments);
+
     updateView();
     emitTimeSelection();
 }
@@ -395,6 +396,9 @@ void PlotView::setCursorSegments(int segments)
 void PlotView::setFFTAndZoom(int size, int zoom)
 {
     auto oldSamplesPerColumn = samplesPerColumn();
+    float oldPlotCenter = (verticalScrollBar()->value() + viewport()->height() / 2.0) / plotsHeight();
+    if (verticalScrollBar()->maximum() == 0)
+        oldPlotCenter = 0.5;
 
     // Set new FFT size
     fftSize = size;
@@ -411,6 +415,10 @@ void PlotView::setFFTAndZoom(int size, int zoom)
     horizontalScrollBar()->setPageStep(100);
 
     updateView(true, samplesPerColumn() < oldSamplesPerColumn);
+
+    // maintain the relative position of the vertical scroll bar
+    if (verticalScrollBar()->maximum())
+        verticalScrollBar()->setValue((int )(oldPlotCenter * plotsHeight() - viewport()->height() / 2.0 + 0.5f));
 }
 
 void PlotView::setPowerMin(int power)
@@ -567,6 +575,7 @@ void PlotView::updateView(bool reCenter, bool expanding)
     }
     horizontalScrollBar()->setMaximum(std::max(0, sampleToColumn(mainSampleSource->count()) - width()));
     verticalScrollBar()->setMaximum(std::max(0, plotsHeight() - viewport()->height()));
+
     if (expanding) {
         updateViewRange(reCenter);
     }