#9209 Refactor RiuMultiPlotPage/RiuSummaryMultiPlotPage plot reinsertion.

This commit is contained in:
Kristian Bendiksen 2022-08-19 10:05:50 +02:00
parent b6973a3644
commit 8eab80f446
3 changed files with 189 additions and 151 deletions

View File

@ -654,89 +654,164 @@ void RiuMultiPlotPage::reinsertPlotWidgets()
int column = 0;
for ( int visibleIndex = 0; visibleIndex < plotWidgets.size(); ++visibleIndex )
{
int expectedColSpan = static_cast<int>( plotWidgets[visibleIndex]->colSpan() );
auto plotWidget = plotWidgets[visibleIndex];
auto legend = legends[visibleIndex];
auto legendFrame = legendFrames[visibleIndex];
auto subTitle = subTitles[visibleIndex];
int expectedColSpan = plotWidget->colSpan();
int colSpan = std::min( expectedColSpan, columnCount );
int rowSpan = plotWidgets[visibleIndex]->rowSpan();
int rowSpan = plotWidget->rowSpan();
auto position = findAvailableRowAndColumn( row, column, colSpan, columnCount );
std::tie( row, column ) = position;
m_visibleIndexToPositionMapping[visibleIndex] = position;
m_gridLayout->addWidget( subTitles[visibleIndex], 3 * row, column, 1, colSpan );
if ( legends[visibleIndex] )
{
if ( m_plotDefinition->legendPosition() == RimPlotWindow::LegendPosition::ABOVE )
{
m_gridLayout->addWidget( legends[visibleIndex],
3 * row + 1,
column,
1,
colSpan,
Qt::AlignHCenter | Qt::AlignBottom );
}
else
{
CAF_ASSERT( m_plotDefinition->legendPosition() == RimPlotWindow::LegendPosition::INSIDE );
auto overlayFrame = new RiuQwtLegendOverlayContentFrame;
overlayFrame->setLegend( legends[visibleIndex] );
legendFrames[visibleIndex]->setContentFrame( overlayFrame );
legendFrames[visibleIndex]->setAnchorCorner( RiuDraggableOverlayFrame::AnchorCorner::TopRight );
plotWidgets[visibleIndex]->addOverlayFrame( legendFrames[visibleIndex] );
}
}
m_gridLayout->addWidget( plotWidgets[visibleIndex], 3 * row + 2, column, 1 + ( rowSpan - 1 ) * 3, colSpan );
subTitles[visibleIndex]->setVisible( m_showSubTitles );
QFont subTitleFont = subTitles[visibleIndex]->font();
subTitleFont.setPixelSize( m_subTitleFontPixelSize );
subTitles[visibleIndex]->setFont( subTitleFont );
plotWidgets[visibleIndex]->setAxisLabelsAndTicksEnabled( RiuPlotAxis::defaultLeft(),
showYAxis( row, column ),
showYAxis( row, column ) );
plotWidgets[visibleIndex]->setAxisTitleEnabled( RiuPlotAxis::defaultLeft(), showYAxis( row, column ) );
plotWidgets[visibleIndex]->setAxesFontsAndAlignment( m_axisTitleFontSize, m_axisValueFontSize );
{
auto margins = plotWidgets[visibleIndex]->contentsMargins();
margins.setBottom( 40 );
// Adjust the space below a graph to make sure the heading of the row below is closest to the
// corresponding graph
plotWidgets[visibleIndex]->setContentsMargins( margins );
}
plotWidgets[visibleIndex]->show();
if ( legends[visibleIndex] )
{
if ( m_plotDefinition->legendsVisible() )
{
updateLegendColumns( legends[visibleIndex] );
updateLegendFont( legends[visibleIndex] );
legends[visibleIndex]->show();
}
else
{
legends[visibleIndex]->hide();
}
}
// Set basic row and column stretches
for ( int r = row; r < row + rowSpan; ++r )
{
m_gridLayout->setRowStretch( 3 * r + 2, 1 );
}
for ( int c = column; c < column + colSpan; ++c )
{
int colStretch = 6; // Empirically chosen to try to counter the width of the axis on the first track
if ( showYAxis( row, column ) ) colStretch += 1;
m_gridLayout->setColumnStretch( c, std::max( colStretch, m_gridLayout->columnStretch( c ) ) );
}
reinsertPlotWidget( plotWidget, legend, legendFrame, subTitle, row, column, rowSpan, colSpan );
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMultiPlotPage::reinsertPlotWidget( RiuPlotWidget* plotWidget,
RiuQwtPlotLegend* legend,
RiuDraggableOverlayFrame* legendFrame,
QLabel* subTitle,
int row,
int column,
int rowSpan,
int colSpan )
{
m_gridLayout->addWidget( subTitle, 3 * row, column, 1, colSpan );
addLegendWidget( plotWidget, legend, legendFrame, 3 * row + 1, column, colSpan );
m_gridLayout->addWidget( plotWidget, 3 * row + 2, column, 1 + ( rowSpan - 1 ) * 3, colSpan );
updateSubTitleVisibility( subTitle );
setDefaultAxisProperties( plotWidget, row, column );
adjustHeadingSpacing( plotWidget );
plotWidget->show();
updateLegendVisibility( plotWidget, legend, legendFrame );
setRowAndColumnStretches( row, column, rowSpan, colSpan );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMultiPlotPage::addLegendWidget( RiuPlotWidget* plotWidget,
RiuQwtPlotLegend* legend,
RiuDraggableOverlayFrame* legendFrame,
int row,
int column,
int colSpan )
{
if ( m_plotDefinition->legendPosition() == RimPlotWindow::LegendPosition::ABOVE )
{
m_gridLayout->addWidget( legend, row, column, 1, colSpan, Qt::AlignHCenter | Qt::AlignBottom );
}
else
{
CAF_ASSERT( m_plotDefinition->legendPosition() == RimPlotWindow::LegendPosition::INSIDE );
auto overlayFrame = new RiuQwtLegendOverlayContentFrame;
overlayFrame->setLegend( legend );
legendFrame->setContentFrame( overlayFrame );
legendFrame->setAnchorCorner( RiuDraggableOverlayFrame::AnchorCorner::TopRight );
plotWidget->removeOverlayFrame( legendFrame );
plotWidget->addOverlayFrame( legendFrame );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMultiPlotPage::updateLegendVisibility( RiuPlotWidget* plotWidget,
RiuQwtPlotLegend* legend,
RiuDraggableOverlayFrame* legendFrame )
{
if ( m_plotDefinition->legendsVisible() && !legend->isEmpty() )
{
updateLegendColumns( legend );
updateLegendFont( legend );
legend->show();
if ( m_plotDefinition->legendPosition() == RimPlotWindow::LegendPosition::INSIDE )
legendFrame->show();
else
{
plotWidget->removeOverlayFrame( legendFrame );
legendFrame->hide();
}
}
else
{
legend->hide();
legendFrame->hide();
plotWidget->removeOverlayFrame( legendFrame );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMultiPlotPage::updateSubTitleVisibility( QLabel* subTitle )
{
bool isSubTitleVisible = m_showSubTitles && !subTitle->text().isEmpty();
subTitle->setVisible( isSubTitleVisible );
QFont subTitleFont = subTitle->font();
subTitleFont.setPixelSize( m_subTitleFontPixelSize );
subTitle->setFont( subTitleFont );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMultiPlotPage::setDefaultAxisProperties( RiuPlotWidget* plotWidget, int row, int column )
{
plotWidget->setAxisLabelsAndTicksEnabled( RiuPlotAxis::defaultLeft(), showYAxis( row, column ), showYAxis( row, column ) );
plotWidget->setAxisTitleEnabled( RiuPlotAxis::defaultLeft(), showYAxis( row, column ) );
plotWidget->setAxesFontsAndAlignment( m_axisTitleFontSize, m_axisValueFontSize );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMultiPlotPage::adjustHeadingSpacing( RiuPlotWidget* plotWidget )
{
// Adjust the space below a graph to make sure the heading of the row below is closest to the
// corresponding graph
auto margins = plotWidget->contentsMargins();
margins.setBottom( 40 );
plotWidget->setContentsMargins( margins );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMultiPlotPage::setRowAndColumnStretches( int row, int column, int rowSpan, int colSpan )
{
// Set basic row and column stretches
for ( int r = row; r < row + rowSpan; ++r )
{
m_gridLayout->setRowStretch( 3 * r + 2, 1 );
}
for ( int c = column; c < column + colSpan; ++c )
{
int colStretch = 6; // Empirically chosen to try to counter the width of the axis on the first track
if ( showYAxis( row, column ) ) colStretch += 1;
m_gridLayout->setColumnStretch( c, std::max( colStretch, m_gridLayout->columnStretch( c ) ) );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -122,6 +122,32 @@ protected:
virtual void reinsertPlotWidgets();
virtual void refreshLegends();
void addLegendWidget( RiuPlotWidget* plotWidget,
RiuQwtPlotLegend* legend,
RiuDraggableOverlayFrame* legendFrame,
int row,
int column,
int colSpan );
void reinsertPlotWidget( RiuPlotWidget* plotWidget,
RiuQwtPlotLegend* legend,
RiuDraggableOverlayFrame* legendFrame,
QLabel* subTitle,
int row,
int column,
int rowSpan,
int colSpan );
void updateSubTitleVisibility( QLabel* subTitle );
void setDefaultAxisProperties( RiuPlotWidget* plotWidget, int row, int column );
void adjustHeadingSpacing( RiuPlotWidget* plotWidget );
void setRowAndColumnStretches( int row, int column, int rowSpan, int colSpan );
void updateLegendVisibility( RiuPlotWidget* plotWidget, RiuQwtPlotLegend* legend, RiuDraggableOverlayFrame* legendFrame );
void updateTitleFont();
int alignCanvasTops();

View File

@ -82,99 +82,36 @@ void RiuSummaryMultiPlotPage::reinsertPlotWidgets()
for ( int row = 0; row < rows; row++ )
{
for ( int col = 0; col < cols; col++ )
for ( int column = 0; column < cols; column++ )
{
if ( visibleIndex >= nPlots )
{
m_gridLayout->addWidget( m_placeholderWidgets[phIndex], row * 3 + 2, col );
m_gridLayout->addWidget( m_placeholderWidgets[phIndex], row * 3 + 2, column );
m_gridLayout->setRowStretch( row * 3 + 2, 1 );
m_gridLayout->setColumnStretch( col, 6 );
m_gridLayout->setColumnStretch( column, 6 );
m_placeholderWidgets[phIndex]->show();
phIndex++;
continue;
}
m_visibleIndexToPositionMapping[visibleIndex] = std::make_pair( row, col );
auto plotWidget = plotWidgets[visibleIndex];
auto legend = legends[visibleIndex];
auto legendFrame = legendFrames[visibleIndex];
auto subTitle = subTitles[visibleIndex];
auto plotWidget = plotWidgets[visibleIndex];
int expectedColSpan = plotWidget->colSpan();
int colSpan = std::min( expectedColSpan, cols - col );
int expectedColSpan = plotWidget->colSpan();
int colSpan = std::min( expectedColSpan, cols - column );
int rowSpan = 1;
m_visibleIndexToPositionMapping[visibleIndex] = std::make_pair( row, column );
reinsertPlotWidget( plotWidget, legend, legendFrame, subTitle, row, column, rowSpan, colSpan );
m_gridLayout->addWidget( subTitles[visibleIndex], 3 * row, col, 1, colSpan );
if ( legends[visibleIndex] )
{
if ( m_plotDefinition->legendPosition() == RimPlotWindow::LegendPosition::ABOVE )
{
m_gridLayout->addWidget( legends[visibleIndex], 3 * row + 1, col, 1, colSpan, Qt::AlignHCenter | Qt::AlignBottom );
}
else
{
CAF_ASSERT( m_plotDefinition->legendPosition() == RimPlotWindow::LegendPosition::INSIDE );
auto overlayFrame = new RiuQwtLegendOverlayContentFrame;
overlayFrame->setLegend( legends[visibleIndex] );
legendFrames[visibleIndex]->setContentFrame( overlayFrame );
legendFrames[visibleIndex]->setAnchorCorner( RiuDraggableOverlayFrame::AnchorCorner::TopRight );
plotWidgets[visibleIndex]->removeOverlayFrame( legendFrames[visibleIndex] );
plotWidgets[visibleIndex]->addOverlayFrame( legendFrames[visibleIndex] );
}
}
m_gridLayout->addWidget( plotWidget, 3 * row + 2, col, 1, colSpan );
auto summaryPlot = dynamic_cast<RimSummaryPlot*>( plotWidget->plotDefinition() );
if ( summaryPlot ) m_summaryMultiPlot->setLayoutInfo( summaryPlot, row, col );
bool isSubTitleVisible = m_showSubTitles && !subTitles[visibleIndex]->text().isEmpty();
subTitles[visibleIndex]->setVisible( isSubTitleVisible );
QFont subTitleFont = subTitles[visibleIndex]->font();
subTitleFont.setPixelSize( m_subTitleFontPixelSize );
subTitles[visibleIndex]->setFont( subTitleFont );
plotWidget->setAxisLabelsAndTicksEnabled( RiuPlotAxis::defaultLeft(),
showYAxis( row, col ),
showYAxis( row, col ) );
plotWidget->setAxisTitleEnabled( RiuPlotAxis::defaultLeft(), showYAxis( row, col ) );
plotWidget->setAxesFontsAndAlignment( m_axisTitleFontSize, m_axisValueFontSize );
// Adjust the space below a graph to make sure the heading of the row below is closest to the
// corresponding graph
auto margins = plotWidget->contentsMargins();
margins.setBottom( 40 );
plotWidget->setContentsMargins( margins );
plotWidget->show();
if ( legends[visibleIndex] )
{
if ( m_plotDefinition->legendsVisible() && !legends[visibleIndex]->isEmpty() )
{
updateLegendColumns( legends[visibleIndex] );
updateLegendFont( legends[visibleIndex] );
legends[visibleIndex]->show();
if ( m_plotDefinition->legendPosition() == RimPlotWindow::LegendPosition::INSIDE )
legendFrames[visibleIndex]->show();
else
{
plotWidget->removeOverlayFrame( legendFrames[visibleIndex] );
legendFrames[visibleIndex]->hide();
}
}
else
{
legends[visibleIndex]->hide();
legendFrames[visibleIndex]->hide();
plotWidget->removeOverlayFrame( legendFrames[visibleIndex] );
}
}
// Set basic row and column stretches
m_gridLayout->setRowStretch( 3 * row + 2, 1 );
for ( int c = col; c < col + colSpan; c++ )
{
int colStretch = 6; // Empirically chosen to try to counter the width of the axis on the first track
if ( showYAxis( row, col ) ) colStretch += 1;
m_gridLayout->setColumnStretch( c, std::max( colStretch, m_gridLayout->columnStretch( c ) ) );
}
if ( summaryPlot ) m_summaryMultiPlot->setLayoutInfo( summaryPlot, row, column );
visibleIndex++;
col += colSpan - 1;
column += colSpan - 1;
}
}
}