Skip to content

Commit

Permalink
PlotNavigator: added sync function for all axes
Browse files Browse the repository at this point in the history
- code optimizations

Signed-off-by: Andrei Popa <[email protected]>
  • Loading branch information
andrei47w committed Sep 12, 2024
1 parent c69caf8 commit df62562
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 32 deletions.
6 changes: 5 additions & 1 deletion gui/include/gui/plotnavigator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ class SCOPY_GUI_EXPORT PlotNavigator : public QObject
Qt::KeyboardModifier getZoomerXYModifier();

static void syncPlotNavigators(PlotNavigator *pNav1, PlotNavigator *pNav2, QSet<QwtAxisId> *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;
Expand All @@ -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<QwtAxisId> *axes);

private:
bool m_en;
Expand Down
104 changes: 73 additions & 31 deletions gui/src/plotnavigator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Navigator *> 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()
Expand Down Expand Up @@ -284,8 +302,6 @@ QSet<PlotChannel *> *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)) {
Expand All @@ -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<Navigator *> 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);
Expand Down Expand Up @@ -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);
Expand All @@ -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<QwtAxisId> *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<QwtAxisId> *axes)
{
for(Navigator *nav1 : *pNav1->navigators()) {
if((nav1->magnifier->isXAxisEn() && axes->contains(nav1->magnifier->getXAxis())) ||
(nav1->magnifier->isYAxisEn() && axes->contains(nav1->magnifier->getYAxis()))) {
Expand All @@ -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<QwtAxisId> *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<QwtAxisId> *axes = new QSet<QwtAxisId>(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"

0 comments on commit df62562

Please sign in to comment.