More robust legend rendering in Well Log Plots

This commit is contained in:
Gaute Lindkvist
2019-11-28 18:23:15 +01:00
parent 01be3f3c74
commit c6bb4414c5
4 changed files with 63 additions and 9 deletions

View File

@@ -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<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();
@@ -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 )
{

View File

@@ -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<QVBoxLayout> m_layout;

View File

@@ -19,6 +19,7 @@
#include "qwt_dyngrid_layout.h"
#include <QDebug>
#include <QResizeEvent>
#include <utility>
@@ -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<QwtDynGridLayout*>( 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<int>( maxHeight * rowCount + totalSpacing + 4 ) ) );
}
return fullSizeHint;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuQwtPlotLegend::updateLegend( const QVariant& variant, const QList<QwtLegendData>& legendItems )
{
QwtLegend::updateLegend( variant, legendItems );
emit legendUpdated();
}

View File

@@ -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<QwtLegendData>& ) override;
signals:
void legendUpdated();
private:
mutable int m_columnCount;