From b9f13f742dda4f1beb57be2310a49719e082ef52 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Thu, 24 Jul 2025 16:26:57 +0200 Subject: [PATCH] Select the first visible plot item in the Project Tree When clicking on a plot, this change ensures that the first visible ancestor plot in the hierarchy is selected in the project tree. --- .../UserInterface/RiuPlotMainWindowTools.cpp | 24 +++++++++++++++++++ .../UserInterface/RiuPlotMainWindowTools.h | 3 +++ .../UserInterface/RiuQwtPlotWidget.cpp | 5 +++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ApplicationLibCode/UserInterface/RiuPlotMainWindowTools.cpp b/ApplicationLibCode/UserInterface/RiuPlotMainWindowTools.cpp index 1910726831..2d95e094f5 100644 --- a/ApplicationLibCode/UserInterface/RiuPlotMainWindowTools.cpp +++ b/ApplicationLibCode/UserInterface/RiuPlotMainWindowTools.cpp @@ -74,6 +74,30 @@ void RiuPlotMainWindowTools::selectAsCurrentItem( const caf::PdmObject* object ) } } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +const caf::PdmObject* RiuPlotMainWindowTools::firstVisibleAncestorOrThis( const caf::PdmObject* object ) +{ + if ( RiaGuiApplication::isRunning() ) + { + RiuPlotMainWindow* mpw = RiaGuiApplication::instance()->mainPlotWindow(); + + auto current = const_cast( object ); + while ( current ) + { + if ( mpw->getTreeViewWithItem( current ) ) + { + return current; + } + + current = current->firstAncestorOfType(); + } + } + + return nullptr; +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationLibCode/UserInterface/RiuPlotMainWindowTools.h b/ApplicationLibCode/UserInterface/RiuPlotMainWindowTools.h index 96c6a1218b..a530a3e6cf 100644 --- a/ApplicationLibCode/UserInterface/RiuPlotMainWindowTools.h +++ b/ApplicationLibCode/UserInterface/RiuPlotMainWindowTools.h @@ -36,6 +36,9 @@ public: static void selectOrToggleObject( const caf::PdmObject* object, bool toggle ); static void refreshToolbars(); + // Returns the first visible ancestor of the object, or the object itself if it is visible. + static const caf::PdmObject* firstVisibleAncestorOrThis( const caf::PdmObject* object ); + // Use this function to select (and expand) an object in the project tree and update tool bars. Use the second // parameter to expand a different object than the object to be selected. static void onObjectAppended( const caf::PdmObject* objectToSelect, const caf::PdmObject* objectToExpand = nullptr ); diff --git a/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.cpp b/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.cpp index d23c106e3e..6121238ecd 100644 --- a/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.cpp +++ b/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.cpp @@ -574,7 +574,10 @@ bool RiuQwtPlotWidget::eventFilter( QObject* watched, QEvent* event ) ( mouseEvent->button() == Qt::LeftButton || mouseEvent->button() == Qt::RightButton ) && m_plotDefinition ) { // Select the plot clicked at in the Project Tree for both left and right mouse button clicks - RiuPlotMainWindowTools::selectAsCurrentItem( m_plotDefinition ); + // If we have plots contained in other plots, select the first visible plot item in the hierarchy + + auto firstVisibleItem = RiuPlotMainWindowTools::firstVisibleAncestorOrThis( m_plotDefinition ); + RiuPlotMainWindowTools::selectAsCurrentItem( firstVisibleItem ); } bool toggleItemInSelection = ( mouseEvent->modifiers() & Qt::ControlModifier ) != 0;