From 391d13565ee64215ff8a68a342feb186cc3cfcad Mon Sep 17 00:00:00 2001 From: Kristian Bendiksen Date: Mon, 4 Apr 2022 10:11:21 +0200 Subject: [PATCH] RiuPlotWidget: highlight relevant curves when clicking on an axis. --- .../UserInterface/RiuQwtPlotWidget.cpp | 40 ++++++++++++++++--- .../UserInterface/RiuQwtPlotWidget.h | 3 +- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.cpp b/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.cpp index ff8c87a4be..28dde58841 100644 --- a/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.cpp +++ b/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.cpp @@ -793,7 +793,12 @@ void RiuQwtPlotWidget::onAxisSelected( QwtScaleWidget* scale, bool toggleItemInS QwtAxisId axisId( pos, id ); if ( scale == m_plot->axisWidget( axisId ) ) { - emit axisSelected( findPlotAxisForQwtAxis( axisId ), toggleItemInSelection ); + resetPlotItemHighlighting(); + highlightPlotItemsForQwtAxis( axisId ); + scheduleReplot(); + + RiuPlotAxis plotAxis = findPlotAxisForQwtAxis( axisId ); + emit axisSelected( plotAxis, toggleItemInSelection ); return; } } @@ -919,7 +924,8 @@ void RiuQwtPlotWidget::selectClosestPlotItem( const QPoint& pos, bool toggleItem if ( closestItem && distanceFromClick < 20 ) { // TODO: highlight all selected curves - highlightPlotItem( closestItem ); + std::set plotItems = { closestItem }; + highlightPlotItems( plotItems ); auto plotItem = std::make_shared( closestItem ); emit plotItemSelected( plotItem, toggleItemInSelection, distanceFromClick < 10 ? closestCurvePoint : -1 ); @@ -950,7 +956,7 @@ void RiuQwtPlotWidget::replot() //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void RiuQwtPlotWidget::highlightPlotItem( const QwtPlotItem* closestItem ) +void RiuQwtPlotWidget::highlightPlotItems( const std::set& closestItems ) { // NB! Create a copy of the item list before the loop to avoid invalidated iterators when iterating the list // plotCurve->setZ() causes the ordering of items in the list to change @@ -976,7 +982,7 @@ void RiuQwtPlotWidget::highlightPlotItem( const QwtPlotItem* closestItem ) } double zValue = plotCurve->z(); - if ( plotCurve == closestItem ) + if ( closestItems.count( plotCurve ) > 0 ) { plotCurve->setZ( zValue + 100.0 ); } @@ -998,7 +1004,7 @@ void RiuQwtPlotWidget::highlightPlotItem( const QwtPlotItem* closestItem ) m_originalCurveColors.insert( std::make_pair( plotCurve, curveColors ) ); m_originalZValues.insert( std::make_pair( plotCurve, zValue ) ); } - else if ( plotShapeItem && plotItem == closestItem ) + else if ( plotShapeItem && closestItems.count( plotItem ) > 0 ) { QPen pen = plotShapeItem->pen(); pen.setColor( QColor( Qt::green ) ); @@ -1052,6 +1058,30 @@ void RiuQwtPlotWidget::resetPlotItemHighlighting() m_originalZValues.clear(); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuQwtPlotWidget::highlightPlotItemsForQwtAxis( QwtAxisId axisId ) +{ + std::set plotItems; + auto plotItemList = m_plot->itemList(); + for ( QwtPlotItem* plotItem : plotItemList ) + { + QwtPlotCurve* plotCurve = dynamic_cast( plotItem ); + if ( plotCurve ) + { + QwtAxisId xAxis = plotCurve->xAxis(); + QwtAxisId yAxis = plotCurve->yAxis(); + if ( xAxis == axisId || yAxis == axisId ) + { + plotItems.insert( plotItem ); + } + } + } + + highlightPlotItems( plotItems ); +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.h b/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.h index 0649e8b5a9..26e81ca227 100644 --- a/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.h +++ b/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.h @@ -207,7 +207,8 @@ private: static int defaultMinimumWidth(); void replot() override; - void highlightPlotItem( const QwtPlotItem* closestItem ); + void highlightPlotItemsForQwtAxis( QwtAxisId axisId ); + void highlightPlotItems( const std::set& closestItems ); void resetPlotItemHighlighting(); void onAxisSelected( QwtScaleWidget* scale, bool toggleItemInSelection ); void recalculateAxisExtents( RiuPlotAxis axis );