diff --git a/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryCurveCollection.cpp b/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryCurveCollection.cpp index 11177c6976..c365d41e0d 100644 --- a/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryCurveCollection.cpp +++ b/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryCurveCollection.cpp @@ -502,9 +502,7 @@ void RimSummaryCurveCollection::defineEditorAttribute( const caf::PdmFieldHandle //-------------------------------------------------------------------------------------------------- void RimSummaryCurveCollection::onCurvesReordered( const SignalEmitter* emitter ) { - // detach and reattach to make sure curve legends are shown in correct order - detachPlotCurves(); - reattachPlotCurves(); + refreshCurveOrdering(); curvesChanged.send(); } @@ -543,3 +541,12 @@ void RimSummaryCurveCollection::defineObjectEditorAttribute( QString uiConfigNam myAttr->currentObject = m_currentSummaryCurve.p(); } } + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimSummaryCurveCollection::refreshCurveOrdering() +{ + detachPlotCurves(); + reattachPlotCurves(); +} diff --git a/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryCurveCollection.h b/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryCurveCollection.h index 952ecbf9fc..a8a133c4a0 100644 --- a/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryCurveCollection.h +++ b/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryCurveCollection.h @@ -61,6 +61,8 @@ public: void loadDataAndUpdate( bool updateParentPlot ); + void refreshCurveOrdering(); + private: void setParentPlotAndReplot( RiuPlotWidget* plot ); void setParentPlotNoReplot( RiuPlotWidget* plot ); diff --git a/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryPlot.cpp b/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryPlot.cpp index a8d00846f6..035af09d97 100644 --- a/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryPlot.cpp +++ b/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryPlot.cpp @@ -2456,6 +2456,8 @@ RiuPlotWidget* RimSummaryPlot::doCreatePlotViewWidget( QWidget* mainWindowParent m_summaryPlot = std::make_unique( this, mainWindowParent ); #endif + QObject::connect( plotWidget(), SIGNAL( curveOrderNeedsUpdate() ), this, SLOT( updateCurveOrder() ) ); + for ( const auto& axisProperties : m_axisProperties ) { plotWidget()->ensureAxisIsCreated( axisProperties->plotAxisType() ); @@ -3015,3 +3017,11 @@ RimSummaryPlotSourceStepping* RimSummaryPlot::sourceStepper() { return m_sourceStepping(); } + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimSummaryPlot::updateCurveOrder() +{ + m_summaryCurveCollection->refreshCurveOrdering(); +} diff --git a/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryPlot.h b/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryPlot.h index 14086b57cd..44a23813e3 100644 --- a/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryPlot.h +++ b/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryPlot.h @@ -252,6 +252,7 @@ protected: private slots: void onPlotZoomed(); + void updateCurveOrder(); private: std::vector visibleSummaryCurvesForAxis( RiuPlotAxis plotAxis ) const; diff --git a/ApplicationLibCode/UserInterface/RiuMultiPlotPage.cpp b/ApplicationLibCode/UserInterface/RiuMultiPlotPage.cpp index 23f8b82719..ebc5f2e758 100644 --- a/ApplicationLibCode/UserInterface/RiuMultiPlotPage.cpp +++ b/ApplicationLibCode/UserInterface/RiuMultiPlotPage.cpp @@ -189,11 +189,15 @@ void RiuMultiPlotPage::insertPlot( RiuPlotWidget* plotWidget, size_t index ) legend->setMaxColumns( legendColumns ); legend->horizontalScrollBar()->setVisible( false ); legend->verticalScrollBar()->setVisible( false ); + legend->setDefaultItemMode( QwtLegendData::Clickable ); if ( qwtPlotWidget ) { legend->connect( qwtPlotWidget->qwtPlot(), SIGNAL( legendDataChanged( const QVariant&, const QList& ) ), SLOT( updateLegend( const QVariant&, const QList& ) ) ); + qwtPlotWidget->connect( legend, + SIGNAL( clicked( const QVariant&, int ) ), + SLOT( onLegendClicked( const QVariant&, int ) ) ); } else { diff --git a/ApplicationLibCode/UserInterface/RiuPlotWidget.h b/ApplicationLibCode/UserInterface/RiuPlotWidget.h index 5067885226..ef4a0209ee 100644 --- a/ApplicationLibCode/UserInterface/RiuPlotWidget.h +++ b/ApplicationLibCode/UserInterface/RiuPlotWidget.h @@ -188,6 +188,9 @@ public: virtual void updateZoomDependentCurveProperties(); +signals: + void curveOrderNeedsUpdate(); + protected: void updateOverlayFrameLayout(); diff --git a/ApplicationLibCode/UserInterface/RiuQwtPlotLegend.cpp b/ApplicationLibCode/UserInterface/RiuQwtPlotLegend.cpp index c98fd6caf9..ec518927cb 100644 --- a/ApplicationLibCode/UserInterface/RiuQwtPlotLegend.cpp +++ b/ApplicationLibCode/UserInterface/RiuQwtPlotLegend.cpp @@ -17,11 +17,16 @@ ///////////////////////////////////////////////////////////////////////////////// #include "RiuQwtPlotLegend.h" +#include "RiuQwtPlotWidget.h" + #include "qwt_dyngrid_layout.h" #include "qwt_legend_label.h" +#include "qwt_plot.h" +#include "qwt_plot_item.h" #include #include +#include #include diff --git a/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.cpp b/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.cpp index 051d5bcd3d..0d4cd136fc 100644 --- a/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.cpp +++ b/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.cpp @@ -616,17 +616,6 @@ bool RiuQwtPlotWidget::eventFilter( QObject* watched, QEvent* event ) void RiuQwtPlotWidget::hideEvent( QHideEvent* event ) { resetPlotItemHighlighting(); - // TODO: remove? - // m_plot->hideEvent( event ); -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void RiuQwtPlotWidget::showEvent( QShowEvent* event ) -{ - // TODO: remove? - // m_plot->showEvent( event ); } //-------------------------------------------------------------------------------------------------- @@ -634,8 +623,6 @@ void RiuQwtPlotWidget::showEvent( QShowEvent* event ) //-------------------------------------------------------------------------------------------------- void RiuQwtPlotWidget::resizeEvent( QResizeEvent* event ) { - // TODO: remove??? - // QwtPlot::resizeEvent( event ); updateOverlayFrameLayout(); event->accept(); } @@ -936,10 +923,10 @@ void RiuQwtPlotWidget::selectClosestPlotItem( const QPoint& pos, bool toggleItem findClosestPlotItem( pos, &closestItem, &closestCurvePoint, &distanceFromClick ); RiuPlotMainWindowTools::showPlotMainWindow(); - resetPlotItemHighlighting(); if ( closestItem && distanceFromClick < 20 ) { - // TODO: highlight all selected curves + bool refreshCurveOrder = false; + resetPlotItemHighlighting( refreshCurveOrder ); std::set plotItems = { closestItem }; highlightPlotItems( plotItems ); auto plotItem = std::make_shared( closestItem ); @@ -947,6 +934,7 @@ void RiuQwtPlotWidget::selectClosestPlotItem( const QPoint& pos, bool toggleItem } else { + resetPlotItemHighlighting(); emit plotSelected( toggleItemInSelection ); } @@ -1015,13 +1003,13 @@ void RiuQwtPlotWidget::highlightPlotItems( const std::set& c m_originalCurveProperties.insert( std::make_pair( plotCurve, properties ) ); m_originalZValues.insert( std::make_pair( plotCurve, zValue ) ); + restoreCurveOrder(); + return; } } } - // 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 auto plotItemList = m_plot->itemList(); for ( QwtPlotItem* plotItem : plotItemList ) { @@ -1081,15 +1069,15 @@ void RiuQwtPlotWidget::highlightPlotItems( const std::set& c plotShapeItem->setZ( plotShapeItem->z() + 100.0 ); } } + + restoreCurveOrder(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void RiuQwtPlotWidget::resetPlotItemHighlighting() +void RiuQwtPlotWidget::resetPlotItemHighlighting( bool refreshCurveOrder ) { - // 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 auto plotItemList = m_plot->itemList(); for ( QwtPlotItem* plotItem : plotItemList ) { @@ -1129,6 +1117,8 @@ void RiuQwtPlotWidget::resetPlotItemHighlighting() m_originalZValues.clear(); resetPlotAxisHighlighting(); + + if ( refreshCurveOrder ) restoreCurveOrder(); } //-------------------------------------------------------------------------------------------------- @@ -1519,3 +1509,37 @@ QwtAxisId RiuQwtPlotWidget::toQwtPlotAxis( RiuPlotAxis plotAxis ) const return { -1, -1 }; } + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuQwtPlotWidget::highlightPlotItem( const QwtPlotItem* plotItem ) +{ + bool refreshCurveOrder = false; + resetPlotItemHighlighting( refreshCurveOrder ); + + std::set items; + items.insert( plotItem ); + highlightPlotItems( items ); + + replot(); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuQwtPlotWidget::onLegendClicked( const QVariant& itemInfo, int index ) +{ + if ( !itemInfo.canConvert() ) return; + + QwtPlotItem* plotItem = qvariant_cast( itemInfo ); + if ( plotItem ) highlightPlotItem( plotItem ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuQwtPlotWidget::restoreCurveOrder() +{ + emit curveOrderNeedsUpdate(); +} diff --git a/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.h b/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.h index 75f85ddbdf..4bcb928c29 100644 --- a/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.h +++ b/ApplicationLibCode/UserInterface/RiuQwtPlotWidget.h @@ -174,6 +174,11 @@ public: QwtAxisId toQwtPlotAxis( RiuPlotAxis axis ) const; + void highlightPlotItem( const QwtPlotItem* plotItem ); + +public slots: + void onLegendClicked( const QVariant& itemInfo, int index ); + signals: void plotSelected( bool toggleSelection ); void axisSelected( RiuPlotAxis axisId, bool toggleSelection ); @@ -185,7 +190,6 @@ signals: protected: bool eventFilter( QObject* watched, QEvent* event ) override; void hideEvent( QHideEvent* event ) override; - void showEvent( QShowEvent* event ) override; void resizeEvent( QResizeEvent* event ) override; void keyPressEvent( QKeyEvent* event ) override; @@ -211,13 +215,15 @@ private: void highlightPlotAxes( QwtAxisId axisIdX, QwtAxisId axisIdY ); void highlightPlotItemsForQwtAxis( QwtAxisId axisId ); void highlightPlotItems( const std::set& closestItems ); - void resetPlotItemHighlighting(); + void resetPlotItemHighlighting( bool refreshCurveOrder = true ); void resetPlotAxisHighlighting(); void onAxisSelected( QwtScaleWidget* scale, bool toggleItemInSelection ); void recalculateAxisExtents( RiuPlotAxis axis ); static int highlightItemWidthAdjustment(); + void restoreCurveOrder(); + private: struct CurveProperties {