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

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