From 5cfd840025f3384254e33a77a324bd8c12275d1c Mon Sep 17 00:00:00 2001 From: Gaute Lindkvist Date: Mon, 9 Sep 2019 14:25:30 +0200 Subject: [PATCH] Fix track widths and canvas top alignment --- .../ProjectDataModel/RimWellLogPlot.cpp | 1 + .../ProjectDataModel/RimWellLogTrack.cpp | 2 +- .../ProjectDataModel/RimWellLogTrack.h | 8 +- .../UserInterface/RiuWellLogPlot.cpp | 106 +++++++++++++++++- .../UserInterface/RiuWellLogPlot.h | 15 ++- 5 files changed, 116 insertions(+), 16 deletions(-) diff --git a/ApplicationCode/ProjectDataModel/RimWellLogPlot.cpp b/ApplicationCode/ProjectDataModel/RimWellLogPlot.cpp index 31caee66bb..43a4e2ba19 100644 --- a/ApplicationCode/ProjectDataModel/RimWellLogPlot.cpp +++ b/ApplicationCode/ProjectDataModel/RimWellLogPlot.cpp @@ -211,6 +211,7 @@ void RimWellLogPlot::fieldChangedByUi( const caf::PdmFieldHandle* changedField, changedField == &m_depthAxisGridVisibility ) { updateTracks(); + if ( m_viewer ) m_viewer->updateChildrenLayout(); } else if ( changedField == &m_showTitleInPlot ) { diff --git a/ApplicationCode/ProjectDataModel/RimWellLogTrack.cpp b/ApplicationCode/ProjectDataModel/RimWellLogTrack.cpp index 9980f67449..43daad31ea 100644 --- a/ApplicationCode/ProjectDataModel/RimWellLogTrack.cpp +++ b/ApplicationCode/ProjectDataModel/RimWellLogTrack.cpp @@ -481,7 +481,7 @@ void RimWellLogTrack::updateParentPlotLayout() RiuWellLogPlot* wellLogPlotViewer = dynamic_cast( wellLogPlot->viewWidget() ); if ( wellLogPlotViewer ) { - wellLogPlotViewer->update(); + wellLogPlotViewer->updateChildrenLayout(); } } } diff --git a/ApplicationCode/ProjectDataModel/RimWellLogTrack.h b/ApplicationCode/ProjectDataModel/RimWellLogTrack.h index b4d52f8131..004704c952 100644 --- a/ApplicationCode/ProjectDataModel/RimWellLogTrack.h +++ b/ApplicationCode/ProjectDataModel/RimWellLogTrack.h @@ -82,10 +82,10 @@ public: }; enum WidthScaleFactor { - EXTRA_NARROW_TRACK = 2, - NARROW_TRACK = 3, - NORMAL_TRACK = 4, - WIDE_TRACK = 6, + EXTRA_NARROW_TRACK = 3, + NARROW_TRACK = 4, + NORMAL_TRACK = 5, + WIDE_TRACK = 7, EXTRA_WIDE_TRACK = 10 }; diff --git a/ApplicationCode/UserInterface/RiuWellLogPlot.cpp b/ApplicationCode/UserInterface/RiuWellLogPlot.cpp index ef56e85a72..41e02e4f46 100644 --- a/ApplicationCode/UserInterface/RiuWellLogPlot.cpp +++ b/ApplicationCode/UserInterface/RiuWellLogPlot.cpp @@ -57,6 +57,8 @@ RiuWellLogPlot::RiuWellLogPlot( RimWellLogPlot* plotDefinition, QWidget* parent m_plotDefinition = plotDefinition; m_layout = new QVBoxLayout( this ); + m_layout->setMargin( 0 ); + m_layout->setSpacing( 2 ); m_plotTitle = createTitleLabel(); m_layout->addWidget( m_plotTitle ); @@ -69,6 +71,8 @@ RiuWellLogPlot::RiuWellLogPlot( RimWellLogPlot* plotDefinition, QWidget* parent m_plotLayout->addWidget( m_plotFrame, 1 ); m_trackLayout = new QGridLayout( m_plotFrame ); + m_trackLayout->setMargin( 0 ); + m_trackLayout->setSpacing( 2 ); QPalette newPalette( palette() ); newPalette.setColor( QPalette::Background, Qt::white ); @@ -192,7 +196,7 @@ void RiuWellLogPlot::setDepthZoomAndReplot( double minDepth, double maxDepth ) void RiuWellLogPlot::setPlotTitle( const QString& plotTitle ) { m_plotTitle->setText( plotTitle ); - this->update(); + this->updateChildrenLayout(); } //-------------------------------------------------------------------------------------------------- @@ -224,9 +228,62 @@ QSize RiuWellLogPlot::preferredSize() const void RiuWellLogPlot::setTitleVisible( bool visible ) { m_plotTitle->setVisible( visible ); + this->updateChildrenLayout(); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuWellLogPlot::updateChildrenLayout() +{ + int trackCount = m_trackPlots.size(); + int numTracksAlreadyShown = 0; + for ( int tIdx = 0; tIdx < trackCount; ++tIdx ) + { + if ( m_plotDefinition->areTrackLegendsVisible() && 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(); + } + RiuWellLogTrack* riuTrack = m_trackPlots[tIdx]; + m_trackLayout->setColumnStretch( tIdx, riuTrack->widthScaleFactor() ); + } + + alignCanvasTops(); this->update(); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuWellLogPlot::showEvent( QShowEvent* ) +{ + updateChildrenLayout(); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuWellLogPlot::changeEvent( QEvent* event ) +{ + if ( event->type() == QEvent::WindowStateChange ) + { + updateChildrenLayout(); + } +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -251,17 +308,17 @@ void RiuWellLogPlot::contextMenuEvent( QContextMenuEvent* event ) //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -QSize RiuWellLogPlot::sizeHint() const +void RiuWellLogPlot::keyPressEvent( QKeyEvent* keyEvent ) { - return QSize( 1, 1 ); + m_plotDefinition->handleKeyPressEvent( keyEvent ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void RiuWellLogPlot::keyPressEvent( QKeyEvent* keyEvent ) +QSize RiuWellLogPlot::sizeHint() const { - m_plotDefinition->handleKeyPressEvent( keyEvent ); + return QSize( 1, 1 ); } //-------------------------------------------------------------------------------------------------- @@ -302,6 +359,45 @@ void RiuWellLogPlot::updateScrollBar( double minDepth, double maxDepth ) m_scrollBar->blockSignals( false ); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuWellLogPlot::alignCanvasTops() +{ + CVF_ASSERT( m_legends.size() == m_trackPlots.size() ); + + 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(); + int canvasMargins = m_trackPlots[tIdx]->plotLayout()->canvasMargin( QwtPlot::xTop ); + maxCanvasOffset = std::max( maxCanvasOffset, static_cast( canvasRect.top() + canvasMargins ) ); + } + } + + for ( int tIdx = 0; tIdx < m_trackPlots.size(); ++tIdx ) + { + if ( m_trackPlots[tIdx]->isVisible() ) + { + // 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 canvasMargins = m_trackPlots[tIdx]->plotLayout()->canvasMargin( QwtPlot::xTop ); + int myCanvasOffset = static_cast( canvasRect.top() ) + canvasMargins; + int canvasShift = std::max( 0, maxCanvasOffset - myCanvasOffset ); + + QMargins margins = m_trackPlots[tIdx]->contentsMargins(); + margins.setTop( margins.top() + canvasShift ); + m_trackPlots[tIdx]->setContentsMargins( margins ); + } + } +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationCode/UserInterface/RiuWellLogPlot.h b/ApplicationCode/UserInterface/RiuWellLogPlot.h index 9a051d9c0a..08b475e974 100644 --- a/ApplicationCode/UserInterface/RiuWellLogPlot.h +++ b/ApplicationCode/UserInterface/RiuWellLogPlot.h @@ -67,19 +67,22 @@ public: void setTitleVisible( bool visible ); -protected: - void contextMenuEvent( QContextMenuEvent* ) override; - QSize sizeHint() const override; +public slots: + void updateChildrenLayout(); +protected: + void showEvent( QShowEvent* ) override; + void changeEvent( QEvent* event ) override; + void contextMenuEvent( QContextMenuEvent* ) override; void keyPressEvent( QKeyEvent* keyEvent ) override; + QSize sizeHint() const override; + QLabel* createTitleLabel() const; private: void updateScrollBar( double minDepth, double maxDepth ); - // std::map calculateTrackWidthsToMatchFrame( int frameWidth ) const; - // void placeChildWidgets( int frameHeight, int frameWidth ); - // void positionTitle( int frameWidth ); + void alignCanvasTops(); private slots: void slotSetMinDepth( int value );