diff --git a/ApplicationCode/ProjectDataModel/GridCrossPlots/RimGridCrossPlot.cpp b/ApplicationCode/ProjectDataModel/GridCrossPlots/RimGridCrossPlot.cpp index 13e843485a..3c66d6ee2f 100644 --- a/ApplicationCode/ProjectDataModel/GridCrossPlots/RimGridCrossPlot.cpp +++ b/ApplicationCode/ProjectDataModel/GridCrossPlots/RimGridCrossPlot.cpp @@ -464,8 +464,13 @@ void RimGridCrossPlot::fieldChangedByUi( const caf::PdmFieldHandle* changedField const QVariant& oldValue, const QVariant& newValue ) { + if ( changedField == &m_showLegend ) + { + m_plotWidget->setLegendVisible( m_showLegend() ); + } if ( changedField == &m_legendFontSize ) { + m_plotWidget->setLegendFontSize( m_legendFontSize() ); for ( auto dataSet : m_crossPlotDataSets ) { dataSet->updateLegendIcons(); @@ -541,20 +546,8 @@ void RimGridCrossPlot::updatePlot() dataSet->setParentQwtPlotNoReplot( m_plotWidget ); } - if ( m_showLegend() ) - { - // Will be released in plot destructor or when a new legend is set - QwtLegend* legend = new QwtLegend( m_plotWidget ); - - auto font = legend->font(); - font.setPointSize( m_legendFontSize() ); - legend->setFont( font ); - m_plotWidget->insertLegend( legend, QwtPlot::BottomLegend ); - } - else - { - m_plotWidget->insertLegend( nullptr ); - } + m_plotWidget->setLegendVisible( m_showLegend() ); + m_plotWidget->setLegendFontSize( m_legendFontSize() ); m_plotWidget->updateLegendSizesToMatchPlot(); m_plotWidget->scheduleReplot(); } diff --git a/ApplicationCode/ProjectDataModel/Summary/RimSummaryPlot.cpp b/ApplicationCode/ProjectDataModel/Summary/RimSummaryPlot.cpp index 2549a080bc..5e24850383 100644 --- a/ApplicationCode/ProjectDataModel/Summary/RimSummaryPlot.cpp +++ b/ApplicationCode/ProjectDataModel/Summary/RimSummaryPlot.cpp @@ -1381,13 +1381,22 @@ void RimSummaryPlot::fieldChangedByUi( const caf::PdmFieldHandle* changedField, { RimViewWindow::fieldChangedByUi( changedField, oldValue, newValue ); - if ( changedField == &m_description || changedField == &m_showPlotTitle || changedField == &m_showLegend || - changedField == &m_legendFontSize || changedField == &m_useAutoPlotTitle ) + if ( changedField == &m_description || changedField == &m_showPlotTitle || changedField == &m_useAutoPlotTitle ) { updatePlotTitle(); updateConnectedEditors(); } + if ( changedField == &m_showLegend ) + { + if ( m_plotWidget ) m_plotWidget->setLegendVisible( m_showLegend ); + } + + if ( changedField == &m_legendFontSize ) + { + if ( m_plotWidget ) m_plotWidget->setLegendFontSize( m_legendFontSize() ); + } + if ( changedField == &m_useAutoPlotTitle && !m_useAutoPlotTitle ) { // When auto name of plot is turned off, update the auto name for all curves @@ -1504,7 +1513,12 @@ void RimSummaryPlot::onLoadDataAndUpdate() curve->loadDataAndUpdate( false ); } - if ( m_plotWidget ) m_plotWidget->updateLegend(); + if ( m_plotWidget ) + { + m_plotWidget->setLegendVisible( m_showLegend ); + m_plotWidget->setLegendFontSize( m_legendFontSize() ); + m_plotWidget->updateLegend(); + } this->updateAxes(); m_textCurveSetEditor->updateTextFilter(); @@ -1799,21 +1813,6 @@ void RimSummaryPlot::updateMdiWindowTitle() { m_plotWidget->setTitle( "" ); } - - if ( m_showLegend ) - { - // Will be released in plot destructor or when a new legend is set - QwtLegend* legend = new QwtLegend( m_plotWidget ); - - auto font = legend->font(); - font.setPointSize( m_legendFontSize() ); - legend->setFont( font ); - m_plotWidget->insertLegend( legend, QwtPlot::BottomLegend ); - } - else - { - m_plotWidget->insertLegend( nullptr ); - } } } diff --git a/ApplicationCode/UserInterface/RiuGridCrossQwtPlot.cpp b/ApplicationCode/UserInterface/RiuGridCrossQwtPlot.cpp index 9bd13a1011..a50f43821f 100644 --- a/ApplicationCode/UserInterface/RiuGridCrossQwtPlot.cpp +++ b/ApplicationCode/UserInterface/RiuGridCrossQwtPlot.cpp @@ -42,6 +42,8 @@ #include "RimPlotAxisProperties.h" #include "RiuPlotAnnotationTool.h" +#include "qwt_legend.h" +#include "qwt_legend_label.h" #include "qwt_plot_panner.h" #include "qwt_scale_draw.h" #include "qwt_scale_widget.h" @@ -104,6 +106,8 @@ RiuGridCrossQwtPlot::RiuGridCrossQwtPlot( RimPlotInterface* plotDefinition, QWid this->installEventFilter( this ); this->canvas()->installEventFilter( this ); + + setLegendVisible( true ); } //-------------------------------------------------------------------------------------------------- @@ -243,6 +247,42 @@ RimViewWindow* RiuGridCrossQwtPlot::ownerViewWindow() const return dynamic_cast( plotDefinition() ); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuGridCrossQwtPlot::setLegendFontSize( int fontSize ) +{ + if ( legend() ) + { + QFont font = legend()->font(); + font.setPointSize( fontSize ); + legend()->setFont( font ); + // Set font size for all existing labels + QList labels = legend()->findChildren(); + for ( QwtLegendLabel* label : labels ) + { + label->setFont( font ); + } + } + updateInfoBoxLayout(); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuGridCrossQwtPlot::setLegendVisible( bool visible ) +{ + if ( visible ) + { + QwtLegend* legend = new QwtLegend( this ); + this->insertLegend( legend, BottomLegend ); + } + else + { + this->insertLegend( nullptr ); + } +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -291,9 +331,9 @@ void RiuGridCrossQwtPlot::updateInfoBoxLayout() if ( !infoText.empty() ) { m_infoBox->label()->setText( infoText.join( "\n" ) ); - QFont font = m_infoBox->font(); + QFont font = m_infoBox->label()->font(); font.setPointSize( crossPlot->legendFontSize() ); - m_infoBox->setFont( font ); + m_infoBox->label()->setFont( font ); m_infoBox->adjustSize(); QRect infoRect = m_infoBox->frameGeometry(); QRect canvasRect = canvas()->frameGeometry(); diff --git a/ApplicationCode/UserInterface/RiuGridCrossQwtPlot.h b/ApplicationCode/UserInterface/RiuGridCrossQwtPlot.h index cd2124bfe1..c8fba07494 100644 --- a/ApplicationCode/UserInterface/RiuGridCrossQwtPlot.h +++ b/ApplicationCode/UserInterface/RiuGridCrossQwtPlot.h @@ -60,6 +60,9 @@ public: RimViewWindow* ownerViewWindow() const override; + void setLegendFontSize( int fontSize ); + void setLegendVisible( bool visible ); + protected: void updateLayout() override; void updateInfoBoxLayout(); diff --git a/ApplicationCode/UserInterface/RiuSummaryQwtPlot.cpp b/ApplicationCode/UserInterface/RiuSummaryQwtPlot.cpp index 00714bf26b..4cab2374db 100644 --- a/ApplicationCode/UserInterface/RiuSummaryQwtPlot.cpp +++ b/ApplicationCode/UserInterface/RiuSummaryQwtPlot.cpp @@ -53,6 +53,7 @@ #include "qwt_date_scale_engine.h" #include "qwt_interval.h" #include "qwt_legend.h" +#include "qwt_legend_label.h" #include "qwt_plot_curve.h" #include "qwt_plot_panner.h" #include "qwt_plot_zoomer.h" @@ -128,6 +129,8 @@ RiuSummaryQwtPlot::RiuSummaryQwtPlot( RimPlotInterface* plotDefinition, QWidget* this->installEventFilter( this ); this->canvas()->installEventFilter( this ); + + setLegendVisible( true ); } //-------------------------------------------------------------------------------------------------- @@ -204,6 +207,41 @@ RimViewWindow* RiuSummaryQwtPlot::ownerViewWindow() const return dynamic_cast( plotDefinition() ); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuSummaryQwtPlot::setLegendFontSize( int fontSize ) +{ + if ( legend() ) + { + QFont font = legend()->font(); + font.setPointSize( fontSize ); + legend()->setFont( font ); + // Set font size for all existing labels + QList labels = legend()->findChildren(); + for ( QwtLegendLabel* label : labels ) + { + label->setFont( font ); + } + } +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuSummaryQwtPlot::setLegendVisible( bool visible ) +{ + if ( visible ) + { + QwtLegend* legend = new QwtLegend( this ); + this->insertLegend( legend, BottomLegend ); + } + else + { + this->insertLegend( nullptr ); + } +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -247,11 +285,6 @@ void RiuSummaryQwtPlot::setDefaults() QString timeFormat = RiaApplication::instance()->preferences()->timeFormat(); useDateBasedTimeAxis( dateFormat, timeFormat ); - - // The legend will be deleted in the destructor of the plot or when - // another legend is inserted. - QwtLegend* legend = new QwtLegend( this ); - this->insertLegend( legend, BottomLegend ); } //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationCode/UserInterface/RiuSummaryQwtPlot.h b/ApplicationCode/UserInterface/RiuSummaryQwtPlot.h index 22c233f00b..2a1e6f5f30 100644 --- a/ApplicationCode/UserInterface/RiuSummaryQwtPlot.h +++ b/ApplicationCode/UserInterface/RiuSummaryQwtPlot.h @@ -55,6 +55,9 @@ public: RimViewWindow* ownerViewWindow() const override; + void setLegendFontSize( int fontSize ); + void setLegendVisible( bool visible ); + protected: void keyPressEvent( QKeyEvent* ) override; void contextMenuEvent( QContextMenuEvent* ) override;