Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintain the current scroll position when zooming #172

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions plotview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ void PlotView::exportSamples(std::shared_ptr<AbstractSampleSource> src)

if (dialog.exec()) {
QStringList fileNames = dialog.selectedFiles();

size_t start, end;
if (cursorSelection.isChecked()) {
start = selectedSamples.minimum;
Expand All @@ -350,7 +350,7 @@ void PlotView::exportSamples(std::shared_ptr<AbstractSampleSource> src)
size_t step = viewRange.length();

for (index = start; index < end; index += step) {
size_t length = std::min(step, end - index);
size_t length = std::min(step, end - index);
auto samples = sampleSrc->getSamples(index, length);
if (samples != nullptr) {
for (auto i = 0; i < length; i += decimation.value()) {
Expand Down Expand Up @@ -387,6 +387,8 @@ void PlotView::setCursorSegments(int segments)

void PlotView::setFFTAndZoom(int size, int zoom)
{
auto oldSamplesPerColumn = samplesPerColumn();

// Set new FFT size
fftSize = size;
if (spectrogramPlot != nullptr)
Expand All @@ -401,7 +403,7 @@ void PlotView::setFFTAndZoom(int size, int zoom)
horizontalScrollBar()->setSingleStep(10);
horizontalScrollBar()->setPageStep(100);

updateView(true);
updateView(true, samplesPerColumn() < oldSamplesPerColumn);
}

void PlotView::setPowerMin(int power)
Expand Down Expand Up @@ -542,15 +544,20 @@ void PlotView::updateViewRange(bool reCenter)
sampleToColumn(zoomSample) - zoomPos
);
}
// zoomSample = viewRange.minimum + viewRange.length() / 2;
// zoomPos = width() / 2;
zoomSample = viewRange.minimum + viewRange.length() / 2;
zoomPos = width() / 2;
}

void PlotView::updateView(bool reCenter)
void PlotView::updateView(bool reCenter, bool expanding)
{
if (!expanding) {
updateViewRange(reCenter);
}
horizontalScrollBar()->setMaximum(std::max(0, sampleToColumn(mainSampleSource->count()) - width()));
verticalScrollBar()->setMaximum(std::max(0, plotsHeight() - viewport()->height()));
updateViewRange(reCenter);
if (expanding) {
updateViewRange(reCenter);
}

// Update cursors
range_t<int> newSelection = {
Expand Down
4 changes: 2 additions & 2 deletions plotview.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public slots:
size_t zoomSample;

int fftSize = 1024;
int zoomLevel = 0;
int zoomLevel = 1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to 1 because 0 is invalid and causes PlotView::samplesPerColumn() to crash due to a division by zero.

int powerMin;
int powerMax;
bool cursorsEnabled;
Expand All @@ -87,7 +87,7 @@ public slots:
int plotsHeight();
size_t samplesPerColumn();
void updateViewRange(bool reCenter);
void updateView(bool reCenter = false);
void updateView(bool reCenter = false, bool expanding = false);
void paintTimeScale(QPainter &painter, QRect &rect, range_t<size_t> sampleRange);

int sampleToColumn(size_t sample);
Expand Down