From df6256210d563a28951d4897fb6edc1c33e57b9e Mon Sep 17 00:00:00 2001 From: Andrei Popa Date: Tue, 10 Sep 2024 11:43:28 +0300 Subject: [PATCH] PlotNavigator: added sync function for all axes - code optimizations Signed-off-by: Andrei Popa --- gui/include/gui/plotnavigator.hpp | 6 +- gui/src/plotnavigator.cpp | 104 +++++++++++++++++++++--------- 2 files changed, 78 insertions(+), 32 deletions(-) diff --git a/gui/include/gui/plotnavigator.hpp b/gui/include/gui/plotnavigator.hpp index bcd7dfba7c..1f771ee844 100644 --- a/gui/include/gui/plotnavigator.hpp +++ b/gui/include/gui/plotnavigator.hpp @@ -98,12 +98,14 @@ class SCOPY_GUI_EXPORT PlotNavigator : public QObject Qt::KeyboardModifier getZoomerXYModifier(); static void syncPlotNavigators(PlotNavigator *pNav1, PlotNavigator *pNav2, QSet *axes); + static void syncPlotNavigators(PlotNavigator *pNav1, PlotNavigator *pNav2); void setResetButtonEn(bool en); Q_SIGNALS: void reset(); void undo(); void rectChanged(const QRectF &rect, navigationType type); + void addedNavigator(Navigator *nav); protected: virtual bool eventFilter(QObject *object, QEvent *event) QWT_OVERRIDE; @@ -118,11 +120,13 @@ class SCOPY_GUI_EXPORT PlotNavigator : public QObject PlotMagnifier *createMagnifier(QwtAxisId axisId); PlotZoomer *createZoomer(QwtAxisId axisId); void addNavigators(QwtAxisId axisId); - void removeNavigators(QwtAxisId axisId); + void removeNavigators(QwtAxisId axisId, PlotChannel *channel); void addRectToHistory(Navigator *nav, const QRectF &rect, navigationType type); void onUndo(); void onReset(); static void syncNavigators(PlotNavigator *pNav1, Navigator *nav1, PlotNavigator *pNav2, Navigator *nav2); + static void syncPlotNavigatorSignals(PlotNavigator *pNav1, PlotNavigator *pNav2); + static void syncPlotNavigatorAxes(PlotNavigator *pNav1, PlotNavigator *pNav2, QSet *axes); private: bool m_en; diff --git a/gui/src/plotnavigator.cpp b/gui/src/plotnavigator.cpp index 681f704551..e5fc092132 100644 --- a/gui/src/plotnavigator.cpp +++ b/gui/src/plotnavigator.cpp @@ -193,16 +193,34 @@ void PlotNavigator::addNavigators(QwtAxisId axisId) Q_EMIT reset(); } }); + + Q_EMIT addedNavigator(nav); } -void PlotNavigator::removeNavigators(QwtAxisId axisId) +void PlotNavigator::removeNavigators(QwtAxisId axisId, PlotChannel *channel) { + // remove axes if there aren't other channels using them + bool found = false; + + for(PlotChannel *ch : *m_channels) { + if((ch->xAxis()->axisId() == axisId) || (ch->yAxis()->axisId() == axisId)) { + found = true; + } + } + + QList toDelete; for(Navigator *nav : *m_navigators) { - if(nav->magnifier->getXAxis() == axisId || nav->magnifier->getYAxis() == axisId) { - delete nav; - m_navigators->remove(nav); + if((!found && nav->magnifier->getXAxis() == axisId) || + (!found && nav->magnifier->getYAxis() == axisId)) { + toDelete.push_back(nav); } } + + for(Navigator *n : toDelete) { + m_navigators->remove(n); + delete n; + m_axes->remove(axisId); + } } void PlotNavigator::onReset() @@ -284,8 +302,6 @@ QSet *PlotNavigator::channels() { return m_channels; } void PlotNavigator::addChannel(PlotChannel *channel) { - m_channels->insert(channel); - // insert axes if there aren't other channels already using them QwtAxisId axisId = channel->xAxis()->axisId(); if(!m_axes->contains(axisId)) { @@ -301,38 +317,18 @@ void PlotNavigator::addChannel(PlotChannel *channel) setBaseRect(axisId); } + m_channels->insert(channel); m_visibleZoomer->setEnabled(isZoomerEn()); } void PlotNavigator::removeChannel(PlotChannel *channel) { - m_channels->remove(channel); - // remove axes if there aren't other channels using them QwtAxisId xAxis = channel->xAxis()->axisId(); - bool xFound = false; QwtAxisId yAxis = channel->yAxis()->axisId(); - bool yFound = false; - - for(PlotChannel *ch : *m_channels) { - if(ch->xAxis()->axisId() == xAxis) { - xFound = true; - } - if(ch->yAxis()->axisId() == yAxis) { - yFound = true; - } - } - - QList toDelete; - for(Navigator *nav : *m_navigators) { - if((xFound && nav->magnifier->getXAxis() == xAxis) || (yFound && nav->magnifier->getYAxis() == yAxis)) { - toDelete.push_back(nav); - } - } - for(Navigator *n : toDelete) { - m_navigators->remove(n); - delete n; - } + removeNavigators(xAxis, channel); + removeNavigators(yAxis, channel); + m_channels->remove(channel); if(m_channels->empty()) { m_visibleZoomer->setEnabled(false); @@ -553,14 +549,17 @@ void PlotNavigator::syncNavigators(PlotNavigator *pNav1, Navigator *nav1, PlotNa connect(nav1->zoomer, &PlotZoomer::zoomed, pNav2, [=](const QRectF &rect) { nav2->zoomer->silentZoom(rect); pNav2->addRectToHistory(nav2, rect, navigationType::Zoom); + Q_EMIT pNav2->rectChanged(rect, navigationType::Zoom); }); connect(nav1->magnifier, &PlotMagnifier::zoomedRect, pNav2, [=](const QRectF &rect) { nav2->zoomer->silentZoom(rect); pNav2->addRectToHistory(nav2, rect, navigationType::Magnify); + Q_EMIT pNav2->rectChanged(rect, navigationType::Magnify); }); connect(nav1->magnifier, &PlotMagnifier::pannedRect, pNav2, [=](const QRectF &rect) { nav2->zoomer->silentZoom(rect); pNav2->addRectToHistory(nav2, rect, navigationType::Pan); + Q_EMIT pNav2->rectChanged(rect, navigationType::Pan); }); connect(pNav1, &PlotNavigator::reset, nav2->magnifier, &PlotMagnifier::reset); connect(pNav1, &PlotNavigator::reset, nav2->zoomer, &PlotZoomer::reset); @@ -569,27 +568,33 @@ void PlotNavigator::syncNavigators(PlotNavigator *pNav1, Navigator *nav1, PlotNa connect(nav2->zoomer, &PlotZoomer::zoomed, pNav1, [=](const QRectF &rect) { nav1->zoomer->silentZoom(rect); pNav1->addRectToHistory(nav1, rect, navigationType::Zoom); + Q_EMIT pNav1->rectChanged(rect, navigationType::Zoom); }); connect(nav2->magnifier, &PlotMagnifier::zoomedRect, pNav1, [=](const QRectF &rect) { nav1->zoomer->silentZoom(rect); pNav1->addRectToHistory(nav1, rect, navigationType::Magnify); + Q_EMIT pNav1->rectChanged(rect, navigationType::Magnify); }); connect(nav2->magnifier, &PlotMagnifier::pannedRect, pNav1, [=](const QRectF &rect) { nav1->zoomer->silentZoom(rect); pNav1->addRectToHistory(nav1, rect, navigationType::Pan); + Q_EMIT pNav1->rectChanged(rect, navigationType::Pan); }); connect(pNav2, &PlotNavigator::reset, nav1->magnifier, &PlotMagnifier::reset); connect(pNav2, &PlotNavigator::reset, nav1->zoomer, &PlotZoomer::reset); } -void PlotNavigator::syncPlotNavigators(PlotNavigator *pNav1, PlotNavigator *pNav2, QSet *axes) +void PlotNavigator::syncPlotNavigatorSignals(PlotNavigator *pNav1, PlotNavigator *pNav2) { connect(pNav1, &PlotNavigator::undo, pNav2, &PlotNavigator::onUndo); connect(pNav1, &PlotNavigator::reset, pNav2, &PlotNavigator::onReset); connect(pNav2, &PlotNavigator::undo, pNav1, &PlotNavigator::onUndo); connect(pNav2, &PlotNavigator::reset, pNav1, &PlotNavigator::onReset); +} +void PlotNavigator::syncPlotNavigatorAxes(PlotNavigator *pNav1, PlotNavigator *pNav2, QSet *axes) +{ for(Navigator *nav1 : *pNav1->navigators()) { if((nav1->magnifier->isXAxisEn() && axes->contains(nav1->magnifier->getXAxis())) || (nav1->magnifier->isYAxisEn() && axes->contains(nav1->magnifier->getYAxis()))) { @@ -603,6 +608,43 @@ void PlotNavigator::syncPlotNavigators(PlotNavigator *pNav1, PlotNavigator *pNav } } +// Sync plot navigators only on the specified axes +void PlotNavigator::syncPlotNavigators(PlotNavigator *pNav1, PlotNavigator *pNav2, QSet *axes) +{ + syncPlotNavigatorSignals(pNav1, pNav2); + syncPlotNavigatorAxes(pNav1, pNav2, axes); +} + +// Sync plot navigators on all axes +// this will also work for axes added/removed later +void PlotNavigator::syncPlotNavigators(PlotNavigator *pNav1, PlotNavigator *pNav2) +{ + syncPlotNavigatorSignals(pNav1, pNav2); + + // sync axes shared by both navigators + QSet *axes = new QSet(pNav1->axes()->begin(), pNav1->axes()->end()); + axes->intersect(*pNav2->axes()); + syncPlotNavigatorAxes(pNav1, pNav2, axes); + + // sync future axes + connect(pNav1, &PlotNavigator::addedNavigator, pNav1, [=](Navigator *nav1){ + for(Navigator *nav2 : *pNav2->navigators()) { + if((nav1->magnifier->isXAxisEn() && nav2->magnifier->isXAxisEn() && nav1->magnifier->getXAxis() == nav2->magnifier->getXAxis()) || + (nav1->magnifier->isYAxisEn() && nav2->magnifier->isYAxisEn() && nav1->magnifier->getYAxis() == nav2->magnifier->getYAxis())) { + syncNavigators(pNav1, nav1, pNav2, nav2); + } + } + }); + connect(pNav2, &PlotNavigator::addedNavigator, pNav2, [=](Navigator *nav2){ + for(Navigator *nav1 : *pNav1->navigators()) { + if((nav2->magnifier->isXAxisEn() && nav1->magnifier->isXAxisEn() && nav2->magnifier->getXAxis() == nav1->magnifier->getXAxis()) || + (nav2->magnifier->isYAxisEn() && nav1->magnifier->isYAxisEn() && nav2->magnifier->getYAxis() == nav1->magnifier->getYAxis())) { + syncNavigators(pNav2, nav2, pNav1, nav1); + } + } + }); +} + void PlotNavigator::setResetButtonEn(bool en) { m_resetHover->setVisible(en); } #include "moc_plotnavigator.cpp"