From e288f93b2968e8063307df5346d679ea450ee384 Mon Sep 17 00:00:00 2001 From: Mike Date: Sat, 5 Mar 2016 20:30:25 +0000 Subject: [PATCH] cursors: Re-position cursors on view change --- cursors.cpp | 7 +++++++ cursors.h | 1 + plotview.cpp | 22 +++++++++++++++------- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/cursors.cpp b/cursors.cpp index 909d934..38446de 100644 --- a/cursors.cpp +++ b/cursors.cpp @@ -100,3 +100,10 @@ range_t Cursors::selection() return {cursorPositions[1], cursorPositions[0]}; } } + +void Cursors::setSelection(range_t selection) +{ + cursorPositions[0] = selection.minimum; + cursorPositions[1] = selection.maximum; + emit cursorsMoved(); +} diff --git a/cursors.h b/cursors.h index 96d3b4c..13a7ceb 100644 --- a/cursors.h +++ b/cursors.h @@ -32,6 +32,7 @@ public: Cursors(QObject * parent); void paintFront(QPainter &painter, QRect &rect, range_t sampleRange); range_t selection(); + void setSelection(range_t selection); signals: void cursorsMoved(); diff --git a/plotview.cpp b/plotview.cpp index 327342c..28ab01c 100644 --- a/plotview.cpp +++ b/plotview.cpp @@ -104,6 +104,10 @@ void PlotView::cursorsMoved() void PlotView::enableCursors(bool enabled) { cursorsEnabled = enabled; + if (enabled) { + int margin = viewport()->rect().width() / 3; + cursors.setSelection({viewport()->rect().left() + margin, viewport()->rect().right() - margin}); + } viewport()->update(); } @@ -177,13 +181,6 @@ void PlotView::paintEvent(QPaintEvent *event) void PlotView::resizeEvent(QResizeEvent * event) { - QRect rect = viewport()->rect(); - - // Resize cursors - // TODO: don't hardcode this - int margin = rect.width() / 3; - //cursors.setGeometry(QRect(rect.left() + margin, rect.top(), rect.right() - rect.left() - 2 * margin, rect.height())); - updateView(); } @@ -199,9 +196,20 @@ void PlotView::scrollContentsBy(int dx, int dy) void PlotView::updateView() { + // Update current view viewRange = { horizontalScrollBar()->value(), horizontalScrollBar()->value() + width() * samplesPerLine() }; + + // Update cursors + QRect rect = viewport()->rect(); + range_t newSelection = { + (int)((selectedSamples.minimum - horizontalScrollBar()->value()) / samplesPerLine()), + (int)((selectedSamples.maximum - horizontalScrollBar()->value()) / samplesPerLine()) + }; + cursors.setSelection(newSelection); + + // Re-paint viewport()->update(); }