Skip to content

Commit

Permalink
BufferPreviewer: rework previewer to use PlotMagnifier
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Popa <[email protected]>
  • Loading branch information
andrei47w committed Sep 12, 2024
1 parent 781c2c4 commit 463c5ce
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 32 deletions.
3 changes: 3 additions & 0 deletions gui/include/gui/plotmagnifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class SCOPY_GUI_EXPORT PlotMagnifier : public QObject
bool isXAxisEn() const;
bool isYAxisEn() const;

static double scaleToFactor(double scale, QwtAxisId axisId, QwtPlot *plot);
static double factorToScale(double factor, QwtAxisId axisId, QwtPlot *plot);

Q_SIGNALS:
void reset();
void zoomed(double factor, QPointF cursorPos = QPointF());
Expand Down
3 changes: 3 additions & 0 deletions gui/include/gui/plotnavigator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class SCOPY_GUI_EXPORT PlotNavigator : public QObject
void removeChannel(PlotChannel *channel);

bool isZoomed();
void forcePan(QwtAxisId axisId, double factor);
void forceMagnify(QwtAxisId axisId, double factor, QPointF cursorPos);
void forceZoom(QwtAxisId axisId, const QRectF &rect);

void setZoomerXAxesEn(bool en);
void setZoomerYAxesEn(bool en);
Expand Down
3 changes: 1 addition & 2 deletions gui/include/gui/widgets/plotbufferpreviewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ public Q_SLOTS:
private:
double m_bufferDataLimitMin;
double m_bufferDataLimitMax;
double m_bufferPrevInitMin;
double m_bufferPrevInitMax;
bool m_manualDataLimits;

void setupBufferPreviewer();
PlotWidget *m_plot;
BufferPreviewer *m_bufferPreviewer;
double m_lastMin;
};

} // namespace scopy
Expand Down
16 changes: 15 additions & 1 deletion gui/src/plotmagnifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,20 @@ bool PlotMagnifier::eventFilter(QObject *object, QEvent *event)
return QObject::eventFilter(object, event);
}

double PlotMagnifier::scaleToFactor(double scale, QwtAxisId axisId, QwtPlot *plot)
{
double v1 = plot->axisInterval(axisId).minValue();
double v2 = plot->axisInterval(axisId).maxValue();
return (scale / 0.5 - (v2 - v1)) / (v1 - v2);
}

double PlotMagnifier::factorToScale(double factor, QwtAxisId axisId, QwtPlot *plot)
{
double v1 = plot->axisInterval(axisId).minValue();
double v2 = plot->axisInterval(axisId).maxValue();
return ((v2 - v1) - (v2 - v1) * factor) * 0.5;
}

void PlotMagnifier::panRescale(double factor)
{
if(isXAxisEn()) {
Expand All @@ -190,7 +204,7 @@ void PlotMagnifier::panRescale(double factor)

double v1 = plot()->axisInterval(axisId).minValue();
double v2 = plot()->axisInterval(axisId).maxValue();
double pan_amount = (((v2 - v1) - (v2 - v1) * factor) * 0.5);
double pan_amount = factorToScale(factor, axisId, plot());
bool isInverted = v1 > v2;

if(isInverted) {
Expand Down
30 changes: 30 additions & 0 deletions gui/src/plotnavigator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,36 @@ bool PlotNavigator::isZoomed()
return zoomed;
}

void PlotNavigator::forcePan(QwtAxisId axisId, double factor)
{
for(Navigator *nav : *m_navigators) {
if((nav->magnifier->isXAxisEn() && nav->magnifier->getXAxis() == axisId) ||
(nav->magnifier->isYAxisEn() && nav->magnifier->getYAxis() == axisId)) {
nav->magnifier->pan(factor);
}
}
}

void PlotNavigator::forceMagnify(QwtAxisId axisId, double factor, QPointF cursorPos)
{
for(Navigator *nav : *m_navigators) {
if((nav->magnifier->isXAxisEn() && nav->magnifier->getXAxis() == axisId) ||
(nav->magnifier->isYAxisEn() && nav->magnifier->getYAxis() == axisId)) {
nav->magnifier->zoom(factor, cursorPos);
}
}
}

void PlotNavigator::forceZoom(QwtAxisId axisId, const QRectF &rect)
{
for(Navigator *nav : *m_navigators) {
if((nav->zoomer->isXAxisEn() && nav->zoomer->getXAxis() == axisId) ||
(nav->zoomer->isYAxisEn() && nav->zoomer->getYAxis() == axisId)) {
nav->zoomer->zoom(rect);
}
}
}

void PlotNavigator::setZoomerXAxesEn(bool en)
{
m_visibleZoomer->setXAxisEn(en);
Expand Down
48 changes: 19 additions & 29 deletions gui/src/widgets/plotbufferpreviewer.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include "plotbufferpreviewer.h"
#include "plotaxis.h"
#include <cmath>
#include <plotmagnifier.hpp>
#include <plotnavigator.hpp>

using namespace scopy;

PlotBufferPreviewer::PlotBufferPreviewer(PlotWidget *p, BufferPreviewer *b, QWidget *parent)
: QWidget{parent}
, m_manualDataLimits(false)
, m_lastMin(p->xAxis()->visibleMin())
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
QVBoxLayout *layout = new QVBoxLayout(this);
Expand Down Expand Up @@ -37,43 +39,31 @@ void PlotBufferPreviewer::setupBufferPreviewer()

updateDataLimits(m_plot->xAxis()->min(), m_plot->xAxis()->max());

connect(m_bufferPreviewer, &BufferPreviewer::bufferStopDrag, this, [=]() {});

connect(m_bufferPreviewer, &BufferPreviewer::bufferStartDrag, this, [=]() {
// reset the buffer preview position to current visible section
// using lower and upper bound to also consider zoom level
m_bufferPrevInitMin = m_plot->xAxis()->visibleMin();
m_bufferPrevInitMax = m_plot->xAxis()->visibleMax();
m_lastMin = m_plot->xAxis()->visibleMin();
});

connect(m_bufferPreviewer, &BufferPreviewer::bufferMovedBy, this, [=](int value) {
double moveTo = 0.0;

int width = m_bufferPreviewer->width();
double xAxisWidth = abs(m_bufferDataLimitMax - m_bufferDataLimitMin);

if(m_plot->xAxis()->min() > m_plot->xAxis()->max()) {
value *= -1;
}

moveTo = value * xAxisWidth / width;

m_plot->plot()->setAxisScale(m_plot->xAxis()->axisId(), m_bufferPrevInitMin + moveTo,
m_bufferPrevInitMax + moveTo);
m_plot->replot();

connect(m_bufferPreviewer, &BufferPreviewer::bufferMovedBy, this, [=](int bufferPos) {
double bufferWidth = m_bufferPreviewer->width();
double axisWidth = m_bufferDataLimitMax - m_bufferDataLimitMin;
double newAxisPos = bufferPos * axisWidth / bufferWidth;
double axisOffset = m_lastMin - m_plot->xAxis()->visibleMin();
if(axisWidth < 0)
axisOffset *= -1;

double panAmount = PlotMagnifier::scaleToFactor(newAxisPos + axisOffset, m_plot->xAxis()->axisId(),
m_plot->plot());

bool bounded = m_plot->navigator()->isBounded();
m_plot->navigator()->setBounded(false);
m_plot->navigator()->forcePan(m_plot->xAxis()->axisId(), panAmount);
m_plot->navigator()->setBounded(bounded);
updateBufferPreviewer();
});

connect(m_bufferPreviewer, &BufferPreviewer::bufferResetPosition, this, [=]() {
if(m_plot->xAxis()->min() > m_plot->xAxis()->max()) {
m_plot->plot()->setAxisScale(m_plot->xAxis()->axisId(), m_bufferDataLimitMax,
m_bufferDataLimitMin);
} else {
m_plot->plot()->setAxisScale(m_plot->xAxis()->axisId(), m_bufferDataLimitMin,
m_bufferDataLimitMax);
}
m_plot->xAxis()->updateAxisScale();
Q_EMIT m_plot->navigator()->reset();
updateBufferPreviewer();
});

Expand Down

0 comments on commit 463c5ce

Please sign in to comment.