Use layouts for tracks in Well Log Plot

This commit is contained in:
Gaute Lindkvist 2019-09-06 15:52:45 +02:00
parent ff42e8b1be
commit 9fe021c6ca
6 changed files with 77 additions and 329 deletions

View File

@ -734,6 +734,12 @@ void RimWellAllocationPlot::defineUiOrdering( QString uiConfigName, caf::PdmUiOr
optionGroup.add( &m_groupSmallContributions );
optionGroup.add( &m_smallContributionsThreshold );
m_smallContributionsThreshold.uiCapability()->setUiReadOnly( !m_groupSmallContributions() );
caf::PdmUiGroup* legendAndAxisGroup = uiOrdering.addNewGroup( "Legend and Axis" );
legendAndAxisGroup->setCollapsedByDefault( true );
uiOrderingForPlotSettings( *legendAndAxisGroup );
uiOrdering.skipRemainingFields( true );
}
//--------------------------------------------------------------------------------------------------
@ -743,7 +749,7 @@ void RimWellAllocationPlot::onLoadDataAndUpdate()
{
updateMdiWindowVisibility();
updateFromWell();
RimWellLogPlot::loadDataAndUpdate();
RimWellLogPlot::onLoadDataAndUpdate();
updateFormationNamesData();
}

View File

@ -204,11 +204,14 @@ void RimWellLogPlot::fieldChangedByUi( const caf::PdmFieldHandle* changedField,
m_isAutoScaleDepthEnabled = false;
}
else if ( changedField == &m_showTitleInPlot || changedField == &m_showTrackLegends ||
changedField == &m_trackLegendsHorizontal || changedField == &m_depthAxisGridVisibility )
else if ( changedField == &m_showTrackLegends || changedField == &m_trackLegendsHorizontal ||
changedField == &m_depthAxisGridVisibility )
{
updateTracks();
if ( m_viewer ) m_viewer->updateChildrenLayout();
}
else if ( changedField == &m_showTitleInPlot )
{
m_viewer->setTitleVisible( m_showTitleInPlot() );
}
else if ( changedField == &m_isAutoScaleDepthEnabled )
{

View File

@ -481,7 +481,7 @@ void RimWellLogTrack::updateParentPlotLayout()
RiuWellLogPlot* wellLogPlotViewer = dynamic_cast<RiuWellLogPlot*>( wellLogPlot->viewWidget() );
if ( wellLogPlotViewer )
{
wellLogPlotViewer->updateChildrenLayout();
wellLogPlotViewer->update();
}
}
}

View File

@ -46,20 +46,9 @@ RiuWellAllocationPlot::RiuWellAllocationPlot( RimWellAllocationPlot* plotDefinit
{
Q_ASSERT( m_plotDefinition );
QVBoxLayout* mainLayout = new QVBoxLayout();
this->setLayout( mainLayout );
this->layout()->setMargin( 0 );
this->layout()->setSpacing( 2 );
auto leftColumnLayout = new QVBoxLayout();
new RiuPlotObjectPicker( m_plotTitle, m_plotDefinition );
mainLayout->addWidget( m_plotTitle, 0, Qt::AlignCenter );
auto plotWidgetsLayout = new QHBoxLayout();
auto rightColumnLayout = new QVBoxLayout();
mainLayout->addLayout( plotWidgetsLayout );
plotWidgetsLayout->addLayout( rightColumnLayout );
m_plotLayout->insertLayout( 0, leftColumnLayout );
m_legendWidget = new RiuNightchartsWidget( this );
RimWellAllocationPlot* wellAllocationPlot = dynamic_cast<RimWellAllocationPlot*>( m_plotDefinition.p() );
@ -69,21 +58,18 @@ RiuWellAllocationPlot::RiuWellAllocationPlot( RimWellAllocationPlot* plotDefinit
menuBuilder << "RicShowTotalAllocationDataFeature";
new RiuContextMenuLauncher( m_legendWidget, menuBuilder );
rightColumnLayout->addWidget( m_legendWidget );
leftColumnLayout->addWidget( m_legendWidget );
m_legendWidget->showPie( false );
QWidget* totalFlowAllocationWidget = wellAllocationPlot->totalWellFlowPlot()->createViewWidget( this );
QWidget* totalFlowAllocationWidget = wellAllocationPlot->totalWellFlowPlot()->createViewWidget( nullptr );
new RiuPlotObjectPicker( totalFlowAllocationWidget, wellAllocationPlot->totalWellFlowPlot() );
new RiuContextMenuLauncher( totalFlowAllocationWidget, menuBuilder );
rightColumnLayout->addWidget( totalFlowAllocationWidget, Qt::AlignTop );
rightColumnLayout->addWidget( wellAllocationPlot->tofAccumulatedPhaseFractionsPlot()->createViewWidget( this ),
Qt::AlignTop );
rightColumnLayout->addStretch();
QWidget* wellFlowWidget = m_plotDefinition->createPlotWidget();
plotWidgetsLayout->addWidget( wellFlowWidget );
leftColumnLayout->addWidget( totalFlowAllocationWidget, Qt::AlignTop );
leftColumnLayout->addWidget( wellAllocationPlot->tofAccumulatedPhaseFractionsPlot()->createViewWidget( nullptr ),
Qt::AlignTop );
leftColumnLayout->addStretch();
this->update();
}
//--------------------------------------------------------------------------------------------------

View File

@ -52,26 +52,34 @@
//--------------------------------------------------------------------------------------------------
RiuWellLogPlot::RiuWellLogPlot( RimWellLogPlot* plotDefinition, QWidget* parent )
: QWidget( parent )
, m_scheduleUpdateChildrenLayoutTimer( nullptr )
{
Q_ASSERT( plotDefinition );
m_plotDefinition = plotDefinition;
m_layout = new QVBoxLayout( this );
m_plotTitle = createTitleLabel();
m_layout->addWidget( m_plotTitle );
m_plotLayout = new QHBoxLayout;
m_layout->addLayout( m_plotLayout );
m_plotFrame = new QFrame;
m_plotFrame->setVisible( true );
m_plotLayout->addWidget( m_plotFrame, 1 );
m_trackLayout = new QGridLayout( m_plotFrame );
QPalette newPalette( palette() );
newPalette.setColor( QPalette::Background, Qt::white );
setPalette( newPalette );
setAutoFillBackground( true );
m_plotTitle = new QLabel( "PLOT TITLE HERE", this );
QFont font = m_plotTitle->font();
font.setPointSize( 14 );
font.setBold( true );
m_plotTitle->setFont( font );
m_plotTitle->hide();
m_scrollBar = new QScrollBar( this );
m_scrollBar = new QScrollBar( m_plotFrame );
m_scrollBar->setOrientation( Qt::Vertical );
m_scrollBar->setVisible( true );
m_plotLayout->addWidget( m_scrollBar, 0 );
new RiuPlotObjectPicker( m_plotTitle, m_plotDefinition );
@ -106,11 +114,9 @@ void RiuWellLogPlot::addTrackPlot( RiuWellLogTrack* trackPlot )
//--------------------------------------------------------------------------------------------------
void RiuWellLogPlot::insertTrackPlot( RiuWellLogTrack* trackPlot, size_t index )
{
trackPlot->setParent( this );
m_trackPlots.insert( static_cast<int>( index ), trackPlot );
QwtLegend* legend = new QwtLegend( this );
QwtLegend* legend = new QwtLegend( nullptr );
int legendColumns = 1;
if ( m_plotDefinition->areTrackLegendsHorizontal() )
{
@ -127,9 +133,9 @@ void RiuWellLogPlot::insertTrackPlot( RiuWellLogTrack* trackPlot, size_t index )
legend->contentsWidget()->layout()->setAlignment( Qt::AlignBottom | Qt::AlignHCenter );
m_legends.insert( static_cast<int>( index ), legend );
this->connect( trackPlot,
SIGNAL( legendDataChanged( const QVariant&, const QList<QwtLegendData>& ) ),
SLOT( scheduleUpdateChildrenLayout() ) );
m_trackLayout->addWidget( legend, 0, static_cast<int>( index ) );
m_trackLayout->addWidget( trackPlot, 1, static_cast<int>( index ) );
m_trackLayout->setRowStretch( 1, 1 );
if ( !m_plotDefinition->areTrackLegendsVisible() )
{
@ -186,7 +192,7 @@ void RiuWellLogPlot::setDepthZoomAndReplot( double minDepth, double maxDepth )
void RiuWellLogPlot::setPlotTitle( const QString& plotTitle )
{
m_plotTitle->setText( plotTitle );
this->updateChildrenLayout();
this->update();
}
//--------------------------------------------------------------------------------------------------
@ -215,17 +221,10 @@ QSize RiuWellLogPlot::preferredSize() const
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuWellLogPlot::showTitle()
void RiuWellLogPlot::setTitleVisible( bool visible )
{
m_plotTitle->show();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuWellLogPlot::hideTitle()
{
m_plotTitle->hide();
m_plotTitle->setVisible( visible );
this->update();
}
//--------------------------------------------------------------------------------------------------
@ -265,6 +264,21 @@ void RiuWellLogPlot::keyPressEvent( QKeyEvent* keyEvent )
m_plotDefinition->handleKeyPressEvent( keyEvent );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QLabel* RiuWellLogPlot::createTitleLabel() const
{
QLabel* plotTitle = new QLabel( "PLOT TITLE HERE", nullptr );
QFont font = plotTitle->font();
font.setPointSize( 14 );
font.setBold( true );
plotTitle->setFont( font );
plotTitle->setVisible( m_plotDefinition->isPlotTitleVisible() );
plotTitle->setAlignment( Qt::AlignHCenter );
return plotTitle;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -317,265 +331,3 @@ RimViewWindow* RiuWellLogPlot::ownerViewWindow() const
{
return m_plotDefinition;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuWellLogPlot::resizeEvent( QResizeEvent* event )
{
int height = event->size().height();
int width = event->size().width();
placeChildWidgets( height, width );
QWidget::resizeEvent( event );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::map<int, int> RiuWellLogPlot::calculateTrackWidthsToMatchFrame( int frameWidth ) const
{
int trackCount = m_trackPlots.size();
int visibleTrackCount = 0;
int firstTrackAxisOffset = 0; // Account for first track having the y-axis labels and markers
for ( int tIdx = 0; tIdx < trackCount; ++tIdx )
{
if ( m_trackPlots[tIdx]->isVisible() )
{
if ( visibleTrackCount == 0 )
{
firstTrackAxisOffset = static_cast<int>( m_trackPlots[tIdx]->plotLayout()->canvasRect().left() );
}
else if ( visibleTrackCount == 1 )
{
// The others axes also have markers, and so we need to subtract for this to get the shift due to labels and title
int otherTrackAxisOffset = static_cast<int>( m_trackPlots[tIdx]->plotLayout()->canvasRect().left() );
firstTrackAxisOffset -= otherTrackAxisOffset;
}
++visibleTrackCount;
}
}
int scrollBarWidth = 0;
if ( m_scrollBar->isVisible() ) scrollBarWidth = m_scrollBar->sizeHint().width();
std::map<int, int> trackWidths;
if ( visibleTrackCount )
{
int totalTrackWidth = ( frameWidth - firstTrackAxisOffset - scrollBarWidth );
int trackWidthExtra = ( frameWidth - firstTrackAxisOffset - scrollBarWidth ) % visibleTrackCount;
int totalWidthWeights = 0;
for ( int tIdx = 0; tIdx < trackCount; ++tIdx )
{
if ( m_trackPlots[tIdx]->isVisible() )
{
totalWidthWeights += m_trackPlots[tIdx]->widthScaleFactor();
}
}
bool firstVisible = true;
for ( int tIdx = 0; tIdx < trackCount; ++tIdx )
{
if ( m_trackPlots[tIdx]->isVisible() )
{
int realTrackWidth = ( totalTrackWidth * m_trackPlots[tIdx]->widthScaleFactor() ) / totalWidthWeights;
if ( firstVisible )
{
realTrackWidth += firstTrackAxisOffset;
firstVisible = false;
}
if ( trackWidthExtra > 0 )
{
realTrackWidth += 1;
--trackWidthExtra;
}
trackWidths[tIdx] = realTrackWidth;
}
}
}
return trackWidths;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuWellLogPlot::placeChildWidgets( int frameHeight, int frameWidth )
{
CVF_ASSERT( m_legends.size() == m_trackPlots.size() );
positionTitle( frameWidth );
const int trackPadding = 2;
std::map<int, int> trackWidths = calculateTrackWidthsToMatchFrame( frameWidth );
size_t visibleTrackCount = trackWidths.size();
int maxLegendHeight = 0;
if ( m_plotDefinition && m_plotDefinition->areTrackLegendsVisible() )
{
for ( int tIdx = 0; tIdx < m_trackPlots.size(); ++tIdx )
{
if ( m_trackPlots[tIdx]->isVisible() )
{
int legendHeight = m_legends[tIdx]->heightForWidth( trackWidths[tIdx] - 2 * trackPadding );
if ( legendHeight > maxLegendHeight ) maxLegendHeight = legendHeight;
}
}
}
int titleHeight = 0;
if ( m_plotTitle && m_plotTitle->isVisible() )
{
titleHeight = m_plotTitle->height() + 2;
}
int trackHeight = frameHeight - maxLegendHeight - titleHeight;
int trackX = 0;
if ( visibleTrackCount )
{
int maxCanvasOffset = 0;
for ( int tIdx = 0; tIdx < m_trackPlots.size(); ++tIdx )
{
if ( m_trackPlots[tIdx]->isVisible() )
{
// Hack to align QWT plots. See below.
QRectF canvasRect = m_trackPlots[tIdx]->plotLayout()->canvasRect();
maxCanvasOffset = std::max( maxCanvasOffset, static_cast<int>( canvasRect.top() ) );
}
}
for ( int tIdx = 0; tIdx < m_trackPlots.size(); ++tIdx )
{
if ( m_trackPlots[tIdx]->isVisible() )
{
int adjustedVerticalPosition = titleHeight + maxLegendHeight + 10;
int adjustedTrackHeight = trackHeight;
{
// Hack to align QWT plots which doesn't have an x-axis with the other tracks.
// Since they are missing the axis, QWT will shift them upwards.
// So we shift the plot downwards and resize to match the others.
// TODO: Look into subclassing QwtPlotLayout instead.
QRectF canvasRect = m_trackPlots[tIdx]->plotLayout()->canvasRect();
int myCanvasOffset = static_cast<int>( canvasRect.top() );
int myMargins = m_trackPlots[tIdx]->plotLayout()->canvasMargin( QwtPlot::xTop );
int canvasShift = std::max( 0, maxCanvasOffset - myCanvasOffset );
adjustedVerticalPosition += canvasShift - myMargins;
adjustedTrackHeight -= canvasShift;
}
int realTrackWidth = trackWidths[tIdx];
m_legends[tIdx]->setGeometry( trackX + trackPadding,
titleHeight,
realTrackWidth - 2 * trackPadding,
maxLegendHeight );
m_trackPlots[tIdx]->setGeometry( trackX + trackPadding,
adjustedVerticalPosition,
realTrackWidth - 2 * trackPadding,
adjustedTrackHeight );
trackX += realTrackWidth;
}
}
}
if ( m_scrollBar->isVisible() )
{
m_scrollBar->setGeometry( trackX, titleHeight + maxLegendHeight, m_scrollBar->sizeHint().width(), trackHeight );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuWellLogPlot::positionTitle( int frameWidth )
{
if ( m_plotDefinition && m_plotDefinition->isPlotTitleVisible() )
{
int textWidth = m_plotTitle->sizeHint().width();
m_plotTitle->setGeometry( frameWidth / 2 - textWidth / 2, 0, textWidth, m_plotTitle->sizeHint().height() );
m_plotTitle->show();
}
else
{
m_plotTitle->hide();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuWellLogPlot::updateChildrenLayout()
{
int trackCount = m_trackPlots.size();
int numTracksAlreadyShown = 0;
for ( int tIdx = 0; tIdx < trackCount; ++tIdx )
{
if ( m_trackPlots[tIdx]->isVisible() )
{
int legendColumns = 1;
if ( m_plotDefinition->areTrackLegendsHorizontal() )
{
legendColumns = 0; // unlimited
}
m_legends[tIdx]->setMaxColumns( legendColumns );
m_legends[tIdx]->show();
m_trackPlots[tIdx]->enableDepthAxisLabelsAndTitle( numTracksAlreadyShown == 0 );
numTracksAlreadyShown++;
}
else
{
m_legends[tIdx]->hide();
}
}
placeChildWidgets( this->height(), this->width() );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuWellLogPlot::showEvent( QShowEvent* )
{
updateChildrenLayout();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuWellLogPlot::changeEvent( QEvent* event )
{
if ( event->type() == QEvent::WindowStateChange )
{
updateChildrenLayout();
}
}
//--------------------------------------------------------------------------------------------------
/// Schedule an update of the widget positions
/// Will happen just a bit after the event loop is entered
/// Used to delay the positioning to after the legend widgets is actually updated.
//--------------------------------------------------------------------------------------------------
void RiuWellLogPlot::scheduleUpdateChildrenLayout()
{
if ( !m_scheduleUpdateChildrenLayoutTimer )
{
m_scheduleUpdateChildrenLayoutTimer = new QTimer( this );
connect( m_scheduleUpdateChildrenLayoutTimer, SIGNAL( timeout() ), this, SLOT( updateChildrenLayout() ) );
}
if ( !m_scheduleUpdateChildrenLayoutTimer->isActive() )
{
m_scheduleUpdateChildrenLayoutTimer->setSingleShot( true );
m_scheduleUpdateChildrenLayoutTimer->start( 100 );
}
}

View File

@ -23,6 +23,9 @@
#include "cafPdmPointer.h"
#include <QFrame>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QList>
#include <QPointer>
#include <QWidget>
@ -48,6 +51,7 @@ class RiuWellLogPlot : public QWidget, public RiuInterfaceToViewWindow
public:
RiuWellLogPlot( RimWellLogPlot* plotDefinition, QWidget* parent = nullptr );
~RiuWellLogPlot() override;
RimWellLogPlot* ownerPlotDefinition();
@ -61,36 +65,33 @@ public:
void setPlotTitle( const QString& plotTitle );
virtual QSize preferredSize() const;
void showTitle();
void hideTitle();
public slots:
void updateChildrenLayout();
void setTitleVisible( bool visible );
protected:
void resizeEvent( QResizeEvent* event ) override;
void showEvent( QShowEvent* ) override;
void changeEvent( QEvent* ) override;
void contextMenuEvent( QContextMenuEvent* ) override;
QSize sizeHint() const override;
void keyPressEvent( QKeyEvent* keyEvent ) override;
QLabel* createTitleLabel() const;
private:
void updateScrollBar( double minDepth, double maxDepth );
std::map<int, int> calculateTrackWidthsToMatchFrame( int frameWidth ) const;
void placeChildWidgets( int frameHeight, int frameWidth );
void positionTitle( int frameWidth );
void updateScrollBar( double minDepth, double maxDepth );
// std::map<int, int> calculateTrackWidthsToMatchFrame( int frameWidth ) const;
// void placeChildWidgets( int frameHeight, int frameWidth );
// void positionTitle( int frameWidth );
private slots:
void slotSetMinDepth( int value );
void scheduleUpdateChildrenLayout();
protected:
QPointer<QVBoxLayout> m_layout;
QPointer<QHBoxLayout> m_plotLayout;
QPointer<QFrame> m_plotFrame;
QPointer<QGridLayout> m_trackLayout;
QPointer<QLabel> m_plotTitle;
QScrollBar* m_scrollBar;
QList<QPointer<QwtLegend>> m_legends;
QList<QPointer<RiuWellLogTrack>> m_trackPlots;
caf::PdmPointer<RimWellLogPlot> m_plotDefinition;
QTimer* m_scheduleUpdateChildrenLayoutTimer;
};