diff --git a/ApplicationCode/UserInterface/RiuMultiPlotWindow.cpp b/ApplicationCode/UserInterface/RiuMultiPlotWindow.cpp index 8bb444ea19..2d41c43fbd 100644 --- a/ApplicationCode/UserInterface/RiuMultiPlotWindow.cpp +++ b/ApplicationCode/UserInterface/RiuMultiPlotWindow.cpp @@ -155,12 +155,11 @@ void RiuMultiPlotWindow::insertPlot( RiuQwtPlotWidget* plotWidget, size_t index int legendColumns = 1; if ( m_plotDefinition->legendsHorizontal() ) { - legendColumns = 0; // unlimited + legendColumns = 4; // unlimited } legend->setMaxColumns( legendColumns ); legend->horizontalScrollBar()->setVisible( false ); 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& ) ) ); @@ -169,7 +168,6 @@ void RiuMultiPlotWindow::insertPlot( RiuQwtPlotWidget* plotWidget, size_t index legend->contentsWidget()->layout()->setAlignment( Qt::AlignBottom | Qt::AlignHCenter ); plotWidget->updateLegend(); m_legends.insert( static_cast( index ), legend ); - m_legendColumns.insert( static_cast( index ), -1 ); scheduleUpdate(); } @@ -190,7 +188,6 @@ void RiuMultiPlotWindow::removePlot( RiuQwtPlotWidget* plotWidget ) RiuQwtPlotLegend* legend = m_legends[plotWidgetIdx]; legend->setParent( nullptr ); m_legends.removeAt( plotWidgetIdx ); - m_legendColumns.removeAt( plotWidgetIdx ); delete legend; QLabel* subTitle = m_subTitles[plotWidgetIdx]; @@ -316,35 +313,6 @@ QLabel* RiuMultiPlotWindow::createTitleLabel() const return plotTitle; } -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void RiuMultiPlotWindow::resizeEvent( QResizeEvent* event ) -{ - QWidget::resizeEvent( event ); - bool needsUpdate = false; - for ( int i = 0; i < m_legends.size(); ++i ) - { - if ( m_legends[i]->isVisible() ) - { - int columnCount = m_legends[i]->columnCount(); - if ( columnCount != m_legendColumns[i] ) - { - int oldColumnCount = m_legendColumns[i]; - m_legendColumns[i] = columnCount; - if ( oldColumnCount != -1 ) - { - needsUpdate = true; - } - } - } - } - if ( needsUpdate ) - { - scheduleUpdate(); - } -} - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -613,11 +581,9 @@ void RiuMultiPlotWindow::reinsertPlotWidgets() int legendColumns = 1; if ( m_plotDefinition->legendsHorizontal() ) { - legendColumns = 0; // unlimited + legendColumns = 4; // unlimited } legends[visibleIndex]->setMaxColumns( legendColumns ); - int minimumHeight = legends[visibleIndex]->heightForWidth( plotWidgets[visibleIndex]->width() ); - legends[visibleIndex]->setMinimumHeight( minimumHeight ); QFont legendFont = legends[visibleIndex]->font(); legendFont.setPointSize( m_plotDefinition->legendFontSize() ); legends[visibleIndex]->setFont( legendFont ); @@ -651,6 +617,7 @@ int RiuMultiPlotWindow::alignCanvasTops() CVF_ASSERT( m_legends.size() == m_plotWidgets.size() ); QList> plotWidgets = visiblePlotWidgets(); + QList> legends = visibleLegends(); if ( plotWidgets.empty() ) return 0; auto rowAndColumnCount = this->rowAndColumnCount( plotWidgets.size() ); @@ -670,6 +637,7 @@ int RiuMultiPlotWindow::alignCanvasTops() { int row = visibleIndex / rowAndColumnCount.second; plotWidgets[visibleIndex]->axisScaleDraw( QwtPlot::xTop )->setMinimumExtent( maxExtents[row] ); + legends[visibleIndex]->adjustSize(); } return maxExtents[0]; } diff --git a/ApplicationCode/UserInterface/RiuMultiPlotWindow.h b/ApplicationCode/UserInterface/RiuMultiPlotWindow.h index ff138f8b61..73fa01d0ce 100644 --- a/ApplicationCode/UserInterface/RiuMultiPlotWindow.h +++ b/ApplicationCode/UserInterface/RiuMultiPlotWindow.h @@ -82,7 +82,6 @@ protected: void contextMenuEvent( QContextMenuEvent* ) override; QLabel* createTitleLabel() const; - void resizeEvent( QResizeEvent* event ) override; void showEvent( QShowEvent* event ) override; void dragEnterEvent( QDragEnterEvent* event ) override; void dragMoveEvent( QDragMoveEvent* event ) override; @@ -120,7 +119,6 @@ protected: QPointer m_plotWidgetFrame; QPointer m_gridLayout; QPointer m_plotTitle; - QList m_legendColumns; QList> m_subTitles; QList> m_legends; QList> m_plotWidgets; diff --git a/ApplicationCode/UserInterface/RiuQwtPlotLegend.cpp b/ApplicationCode/UserInterface/RiuQwtPlotLegend.cpp index fcd4dc3367..04ef9b9020 100644 --- a/ApplicationCode/UserInterface/RiuQwtPlotLegend.cpp +++ b/ApplicationCode/UserInterface/RiuQwtPlotLegend.cpp @@ -29,7 +29,7 @@ //-------------------------------------------------------------------------------------------------- RiuQwtPlotLegend::RiuQwtPlotLegend( QWidget* parent /*= nullptr */ ) : QwtLegend( parent ) - , m_columnCount( -1 ) + , m_columnCount( 1 ) { } @@ -43,16 +43,13 @@ void RiuQwtPlotLegend::resizeEvent( QResizeEvent* event ) const QwtDynGridLayout* legendLayout = qobject_cast( contentsWidget()->layout() ); if ( legendLayout ) { - m_columnCount = legendLayout->columnsForWidth( size.width() ); - } -} + int left, right, top, bottom; + getContentsMargins( &left, &top, &right, &bottom ); -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -int RiuQwtPlotLegend::columnCount() const -{ - return m_columnCount; + m_columnCount = std::max( 1, (int)legendLayout->columnsForWidth( size.width() - left - right ) ); + + updateGeometry(); + } } //-------------------------------------------------------------------------------------------------- @@ -65,18 +62,28 @@ QSize RiuQwtPlotLegend::sizeHint() const 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 numColumns = m_columnCount; + int numRows = legendLayout->itemCount() / numColumns; + if ( legendLayout->itemCount() % numColumns ) numRows++; + + int width = numColumns * legendLayout->maxItemWidth(); + 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() ); + maxHeight = std::max( maxHeight, itemSize.height() ); } - int totalSpacing = ( rowCount + 1 ) * legendLayout->spacing(); - fullSizeHint.setHeight( - std::max( fullSizeHint.height(), static_cast( maxHeight * rowCount + totalSpacing + 4 ) ) ); + QMargins margins = legendLayout->contentsMargins(); + int totalSpacing = ( numRows + 1 ) * legendLayout->spacing() + margins.top() + margins.bottom(); + + int height = maxHeight * numRows + totalSpacing; + + QSize layoutSize( width, height ); + QSize frameSize = layoutSize + QSize( 2 * frameWidth(), 2 * frameWidth() ); + + fullSizeHint.setWidth( std::max( fullSizeHint.width(), frameSize.width() ) ); + fullSizeHint.setHeight( std::max( fullSizeHint.height(), frameSize.height() ) ); } return fullSizeHint; } diff --git a/ApplicationCode/UserInterface/RiuQwtPlotLegend.h b/ApplicationCode/UserInterface/RiuQwtPlotLegend.h index d649c12703..2209c8238a 100644 --- a/ApplicationCode/UserInterface/RiuQwtPlotLegend.h +++ b/ApplicationCode/UserInterface/RiuQwtPlotLegend.h @@ -25,9 +25,7 @@ class RiuQwtPlotLegend : public QwtLegend public: RiuQwtPlotLegend( QWidget* parent = nullptr ); void resizeEvent( QResizeEvent* event ); - int columnCount() const; QSize sizeHint() const override; - public slots: void updateLegend( const QVariant&, const QList& ) override; @@ -35,5 +33,5 @@ signals: void legendUpdated(); private: - mutable int m_columnCount; + int m_columnCount; };