diff --git a/ApplicationCode/UserInterface/RiuMultiPlotWindow.cpp b/ApplicationCode/UserInterface/RiuMultiPlotWindow.cpp index c847ae7b9a..8bb444ea19 100644 --- a/ApplicationCode/UserInterface/RiuMultiPlotWindow.cpp +++ b/ApplicationCode/UserInterface/RiuMultiPlotWindow.cpp @@ -159,11 +159,12 @@ void RiuMultiPlotWindow::insertPlot( RiuQwtPlotWidget* plotWidget, size_t index } legend->setMaxColumns( legendColumns ); legend->horizontalScrollBar()->setVisible( false ); - legend->verticalScrollBar()->setVisible( true ); + legend->verticalScrollBar()->setVisible( false ); legend->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ); legend->connect( plotWidget, SIGNAL( legendDataChanged( const QVariant&, const QList& ) ), SLOT( updateLegend( const QVariant&, const QList& ) ) ); + QObject::connect( legend, SIGNAL( legendUpdated() ), this, SLOT( onLegendUpdated() ) ); legend->contentsWidget()->layout()->setAlignment( Qt::AlignBottom | Qt::AlignHCenter ); plotWidget->updateLegend(); @@ -340,7 +341,7 @@ void RiuMultiPlotWindow::resizeEvent( QResizeEvent* event ) } if ( needsUpdate ) { - performUpdate(); + scheduleUpdate(); } } @@ -548,6 +549,14 @@ void RiuMultiPlotWindow::performUpdate() alignCanvasTops(); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuMultiPlotWindow::onLegendUpdated() +{ + scheduleUpdate(); +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -594,6 +603,11 @@ void RiuMultiPlotWindow::reinsertPlotWidgets() subTitles[visibleIndex]->setVisible( m_plotDefinition->showPlotTitles() ); + plotWidgets[visibleIndex]->setAxisLabelsAndTicksEnabled( QwtPlot::yLeft, showYAxis( row, column ) ); + plotWidgets[visibleIndex]->setAxisTitleEnabled( QwtPlot::yLeft, showYAxis( row, column ) ); + + plotWidgets[visibleIndex]->show(); + if ( m_plotDefinition->legendsVisible() ) { int legendColumns = 1; @@ -614,11 +628,6 @@ void RiuMultiPlotWindow::reinsertPlotWidgets() legends[visibleIndex]->hide(); } - plotWidgets[visibleIndex]->setAxisLabelsAndTicksEnabled( QwtPlot::yLeft, showYAxis( row, column ) ); - plotWidgets[visibleIndex]->setAxisTitleEnabled( QwtPlot::yLeft, showYAxis( row, column ) ); - - plotWidgets[visibleIndex]->show(); - // Set basic row and column stretches for ( int r = row; r < row + rowSpan; ++r ) { diff --git a/ApplicationCode/UserInterface/RiuMultiPlotWindow.h b/ApplicationCode/UserInterface/RiuMultiPlotWindow.h index 7eb42f9203..ff138f8b61 100644 --- a/ApplicationCode/UserInterface/RiuMultiPlotWindow.h +++ b/ApplicationCode/UserInterface/RiuMultiPlotWindow.h @@ -42,6 +42,7 @@ class RiuQwtPlotWidget; class QFocusEvent; class QLabel; class QScrollBar; +class QwtLegendData; //================================================================================================== // @@ -111,6 +112,7 @@ protected: private slots: virtual void performUpdate(); + void onLegendUpdated(); protected: QPointer m_layout; diff --git a/ApplicationCode/UserInterface/RiuQwtPlotLegend.cpp b/ApplicationCode/UserInterface/RiuQwtPlotLegend.cpp index 1a008193c7..fcd4dc3367 100644 --- a/ApplicationCode/UserInterface/RiuQwtPlotLegend.cpp +++ b/ApplicationCode/UserInterface/RiuQwtPlotLegend.cpp @@ -19,6 +19,7 @@ #include "qwt_dyngrid_layout.h" +#include #include #include @@ -53,3 +54,38 @@ int RiuQwtPlotLegend::columnCount() const { return m_columnCount; } + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QSize RiuQwtPlotLegend::sizeHint() const +{ + QSize fullSizeHint = QwtLegend::sizeHint(); + // Update width + const QwtDynGridLayout* legendLayout = qobject_cast( contentsWidget()->layout() ); + if ( legendLayout ) + { + int rowCount = std::max( 1, (int)std::ceil( legendLayout->itemCount() / (double)std::max( m_columnCount, 1 ) ) ); + int maxHeight = 0; + for ( unsigned int i = 0; i < legendLayout->itemCount(); ++i ) + { + auto itemSize = legendLayout->itemAt( i )->sizeHint(); + int width = itemSize.width(); + fullSizeHint.setWidth( std::max( fullSizeHint.width(), width ) ); + maxHeight = std::max( maxHeight, itemSize.height() ); + } + int totalSpacing = ( rowCount + 1 ) * legendLayout->spacing(); + fullSizeHint.setHeight( + std::max( fullSizeHint.height(), static_cast( maxHeight * rowCount + totalSpacing + 4 ) ) ); + } + return fullSizeHint; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuQwtPlotLegend::updateLegend( const QVariant& variant, const QList& legendItems ) +{ + QwtLegend::updateLegend( variant, legendItems ); + emit legendUpdated(); +} diff --git a/ApplicationCode/UserInterface/RiuQwtPlotLegend.h b/ApplicationCode/UserInterface/RiuQwtPlotLegend.h index b8f9836d9a..d649c12703 100644 --- a/ApplicationCode/UserInterface/RiuQwtPlotLegend.h +++ b/ApplicationCode/UserInterface/RiuQwtPlotLegend.h @@ -24,8 +24,15 @@ class RiuQwtPlotLegend : public QwtLegend Q_OBJECT public: RiuQwtPlotLegend( QWidget* parent = nullptr ); - void resizeEvent( QResizeEvent* event ); - int columnCount() const; + void resizeEvent( QResizeEvent* event ); + int columnCount() const; + QSize sizeHint() const override; + +public slots: + void updateLegend( const QVariant&, const QList& ) override; + +signals: + void legendUpdated(); private: mutable int m_columnCount;