More robust legend handling for Well Log Plots

This commit is contained in:
Gaute Lindkvist 2019-11-29 15:03:02 +01:00
parent 9656692afc
commit 4f7fc18e1c
4 changed files with 74 additions and 50 deletions

View File

@ -150,7 +150,7 @@ void RiuGridPlotWindow::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 );
@ -159,11 +159,11 @@ void RiuGridPlotWindow::insertPlot( RiuQwtPlotWidget* plotWidget, size_t index )
legend->connect( plotWidget,
SIGNAL( legendDataChanged( const QVariant&, const QList<QwtLegendData>& ) ),
SLOT( updateLegend( const QVariant&, const QList<QwtLegendData>& ) ) );
QObject::connect( legend, SIGNAL( legendUpdated() ), this, SLOT( onLegendUpdated() ) );
legend->contentsWidget()->layout()->setAlignment( Qt::AlignBottom | Qt::AlignHCenter );
plotWidget->updateLegend();
m_legends.insert( static_cast<int>( index ), legend );
m_legendColumns.insert( static_cast<int>( index ), -1 );
scheduleUpdate();
}
@ -184,7 +184,6 @@ void RiuGridPlotWindow::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];
@ -318,42 +317,13 @@ QLabel* RiuGridPlotWindow::createTitleLabel() const
return plotTitle;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuGridPlotWindow::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 )
{
performUpdate();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuGridPlotWindow::showEvent( QShowEvent* event )
{
QWidget::showEvent( event );
performUpdate();
scheduleUpdate();
}
//--------------------------------------------------------------------------------------------------
@ -551,6 +521,13 @@ void RiuGridPlotWindow::performUpdate()
alignCanvasTops();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuGridPlotWindow::onLegendUpdated()
{
scheduleUpdate();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -596,16 +573,19 @@ void RiuGridPlotWindow::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;
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 );
@ -616,11 +596,6 @@ void RiuGridPlotWindow::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 )
{
@ -651,6 +626,7 @@ int RiuGridPlotWindow::alignCanvasTops()
CVF_ASSERT( m_legends.size() == m_plotWidgets.size() );
QList<QPointer<RiuQwtPlotWidget>> plotWidgets = visiblePlotWidgets();
QList<QPointer<RiuQwtPlotLegend>> legends = visibleLegends();
if ( plotWidgets.empty() ) return 0;
auto rowAndColumnCount = this->rowAndColumnCount( plotWidgets.size() );
@ -670,6 +646,7 @@ int RiuGridPlotWindow::alignCanvasTops()
{
int row = visibleIndex / rowAndColumnCount.second;
plotWidgets[visibleIndex]->axisScaleDraw( QwtPlot::xTop )->setMinimumExtent( maxExtents[row] );
legends[visibleIndex]->adjustSize();
}
return maxExtents[0];
}

View File

@ -81,7 +81,6 @@ protected:
void keyPressEvent( QKeyEvent* keyEvent ) override;
QLabel* createTitleLabel() const;
void resizeEvent( QResizeEvent* event ) override;
void showEvent( QShowEvent* event ) override;
void dragEnterEvent( QDragEnterEvent* event ) override;
void dragMoveEvent( QDragMoveEvent* event ) override;
@ -111,6 +110,7 @@ protected:
private slots:
virtual void performUpdate();
virtual void onLegendUpdated();
protected:
QPointer<QVBoxLayout> m_layout;
@ -118,7 +118,6 @@ protected:
QPointer<QFrame> m_plotWidgetFrame;
QPointer<QGridLayout> m_gridLayout;
QPointer<QLabel> m_plotTitle;
QList<int> m_legendColumns;
QList<QPointer<QLabel>> m_subTitles;
QList<QPointer<RiuQwtPlotLegend>> m_legends;
QList<QPointer<RiuQwtPlotWidget>> m_plotWidgets;

View File

@ -19,6 +19,7 @@
#include "qwt_dyngrid_layout.h"
#include <QDebug>
#include <QResizeEvent>
#include <utility>
@ -28,7 +29,7 @@
//--------------------------------------------------------------------------------------------------
RiuQwtPlotLegend::RiuQwtPlotLegend( QWidget* parent /*= nullptr */ )
: QwtLegend( parent )
, m_columnCount( -1 )
, m_columnCount( 1 )
{
}
@ -42,14 +43,56 @@ void RiuQwtPlotLegend::resizeEvent( QResizeEvent* event )
const QwtDynGridLayout* legendLayout = qobject_cast<QwtDynGridLayout*>( contentsWidget()->layout() );
if ( legendLayout )
{
m_columnCount = legendLayout->columnsForWidth( size.width() );
int left, right, top, bottom;
getContentsMargins( &left, &top, &right, &bottom );
m_columnCount = std::max( 1, (int)legendLayout->columnsForWidth( size.width() - left - right ) );
updateGeometry();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiuQwtPlotLegend::columnCount() const
QSize RiuQwtPlotLegend::sizeHint() const
{
return m_columnCount;
QSize fullSizeHint = QwtLegend::sizeHint();
// Update width
const QwtDynGridLayout* legendLayout = qobject_cast<QwtDynGridLayout*>( contentsWidget()->layout() );
if ( legendLayout )
{
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();
maxHeight = std::max( maxHeight, itemSize.height() );
}
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;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuQwtPlotLegend::updateLegend( const QVariant& variant, const QList<QwtLegendData>& legendItems )
{
QwtLegend::updateLegend( variant, legendItems );
emit legendUpdated();
}

View File

@ -24,9 +24,14 @@ class RiuQwtPlotLegend : public QwtLegend
Q_OBJECT
public:
RiuQwtPlotLegend( QWidget* parent = nullptr );
void resizeEvent( QResizeEvent* event );
int columnCount() const;
void resizeEvent( QResizeEvent* event );
QSize sizeHint() const override;
public slots:
void updateLegend( const QVariant&, const QList<QwtLegendData>& ) override;
signals:
void legendUpdated();
private:
mutable int m_columnCount;
int m_columnCount;
};