mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Merge pull request #8461 from OPM/8460-plot-curve-fixes
Guard access to QtCharts objects that might be null Fix visibility of items in legend based on state in parent plot
This commit is contained in:
@@ -982,11 +982,13 @@ void RimPlotCurve::setParentPlotNoReplot( RiuPlotWidget* plotWidget )
|
||||
if ( !plotWidget ) return;
|
||||
|
||||
m_parentPlot = plotWidget;
|
||||
if ( !m_plotCurve )
|
||||
if ( m_plotCurve )
|
||||
{
|
||||
m_plotCurve = m_parentPlot->createPlotCurve( this, "", RiaColorTools::toQColor( m_curveAppearance->color() ) );
|
||||
m_plotCurve->attachToPlot( plotWidget );
|
||||
return;
|
||||
}
|
||||
|
||||
m_plotCurve = m_parentPlot->createPlotCurve( this, "", RiaColorTools::toQColor( m_curveAppearance->color() ) );
|
||||
m_plotCurve->attachToPlot( plotWidget );
|
||||
}
|
||||
|
||||
|
||||
@@ -500,10 +500,12 @@ QString RimSummaryCurve::createCurveAutoName()
|
||||
std::vector<const RimSummaryNameHelper*> nameHelpers;
|
||||
{
|
||||
RimSummaryPlot* plot = nullptr;
|
||||
firstAncestorOrThisOfTypeAsserted( plot );
|
||||
auto nameHelper = plot->plotTitleHelper();
|
||||
|
||||
if ( nameHelper ) nameHelpers.push_back( nameHelper );
|
||||
firstAncestorOrThisOfType( plot );
|
||||
if ( plot )
|
||||
{
|
||||
auto nameHelper = plot->plotTitleHelper();
|
||||
if ( nameHelper ) nameHelpers.push_back( nameHelper );
|
||||
}
|
||||
}
|
||||
{
|
||||
RimSummaryMultiPlot* summaryMultiPlot = nullptr;
|
||||
|
||||
@@ -151,7 +151,7 @@ void RimSummaryCurveCollection::reattachPlotCurves()
|
||||
{
|
||||
for ( RimSummaryCurve* curve : m_curves )
|
||||
{
|
||||
curve->reattach();
|
||||
if ( curve->isCurveVisible() ) curve->reattach();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -761,6 +761,11 @@ void RimSummaryPlot::updateLegend()
|
||||
if ( plotWidget() )
|
||||
{
|
||||
plotWidget()->setInternalLegendVisible( m_showPlotLegends && !isSubPlot() );
|
||||
|
||||
for ( auto c : summaryCurves() )
|
||||
{
|
||||
c->updateLegendEntryVisibilityNoPlotUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
reattachAllCurves();
|
||||
@@ -1525,6 +1530,8 @@ void RimSummaryPlot::fieldChangedByUi( const caf::PdmFieldHandle* changedField,
|
||||
}
|
||||
}
|
||||
|
||||
if ( changedField == &m_showPlotLegends ) updateLegend();
|
||||
|
||||
#ifdef USE_QTCHARTS
|
||||
if ( changedField == &m_useQtChartsPlot )
|
||||
{
|
||||
@@ -2172,7 +2179,7 @@ void RimSummaryPlot::updateCurveNames()
|
||||
{
|
||||
for ( auto c : summaryCurves() )
|
||||
{
|
||||
c->updateCurveNameNoLegendUpdate();
|
||||
if ( c->isCurveVisible() ) c->updateCurveNameNoLegendUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -69,6 +69,8 @@ RiuQtChartsPlotCurve::~RiuQtChartsPlotCurve()
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuQtChartsPlotCurve::setTitle( const QString& title )
|
||||
{
|
||||
if ( !isQtChartObjectsPresent() ) return;
|
||||
|
||||
lineSeries()->setName( title );
|
||||
scatterSeries()->setName( title );
|
||||
}
|
||||
@@ -82,6 +84,8 @@ void RiuQtChartsPlotCurve::setAppearance( RiuQwtPlotCurveDefines::LineStyleEnum
|
||||
const QColor& curveColor,
|
||||
const QBrush& fillBrush /* = QBrush( Qt::NoBrush )*/ )
|
||||
{
|
||||
if ( !isQtChartObjectsPresent() ) return;
|
||||
|
||||
Qt::PenStyle penStyle = RiuQwtPlotCurveDefines::convertToPenStyle( lineStyle );
|
||||
|
||||
QPen curvePen( curveColor );
|
||||
@@ -97,6 +101,8 @@ void RiuQtChartsPlotCurve::setAppearance( RiuQwtPlotCurveDefines::LineStyleEnum
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuQtChartsPlotCurve::setBrush( const QBrush& brush )
|
||||
{
|
||||
if ( !isQtChartObjectsPresent() ) return;
|
||||
|
||||
lineSeries()->setBrush( brush );
|
||||
}
|
||||
|
||||
@@ -116,7 +122,10 @@ void RiuQtChartsPlotCurve::attachToPlot( RiuPlotWidget* plotWidget )
|
||||
}
|
||||
else
|
||||
{
|
||||
m_plotWidget->attach( this, lineSeries(), scatterSeries(), m_axisX, m_axisY );
|
||||
if ( !m_lineSeries ) m_lineSeries = new QtCharts::QLineSeries();
|
||||
if ( !m_scatterSeries ) m_scatterSeries = new QtCharts::QScatterSeries();
|
||||
|
||||
m_plotWidget->attach( this, m_lineSeries, m_scatterSeries, m_axisX, m_axisY );
|
||||
// Plot widget takes ownership.
|
||||
m_lineSeries = nullptr;
|
||||
m_scatterSeries = nullptr;
|
||||
@@ -175,6 +184,8 @@ void RiuQtChartsPlotCurve::setSamplesInPlot( const std::vector<double>& xValues,
|
||||
const std::vector<double>& yValues,
|
||||
int numValues )
|
||||
{
|
||||
if ( !isQtChartObjectsPresent() ) return;
|
||||
|
||||
CAF_ASSERT( xValues.size() == yValues.size() );
|
||||
CAF_ASSERT( numValues <= static_cast<int>( xValues.size() ) );
|
||||
CAF_ASSERT( numValues >= 0 );
|
||||
@@ -191,6 +202,16 @@ void RiuQtChartsPlotCurve::setSamplesInPlot( const std::vector<double>& xValues,
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RiuQtChartsPlotCurve::isQtChartObjectsPresent() const
|
||||
{
|
||||
if ( !lineSeries() ) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@@ -243,6 +264,8 @@ void RiuQtChartsPlotCurve::setYAxis( RiaDefines::PlotAxis axis )
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
int RiuQtChartsPlotCurve::numSamples() const
|
||||
{
|
||||
if ( !lineSeries() ) return 0;
|
||||
|
||||
return lineSeries()->count();
|
||||
}
|
||||
|
||||
@@ -298,13 +321,27 @@ void RiuQtChartsPlotCurve::setVisibleInLegend( bool isVisibleInLegend )
|
||||
CAF_ASSERT( m_plotWidget->qtChart() );
|
||||
CAF_ASSERT( m_plotWidget->qtChart()->legend() );
|
||||
|
||||
// The markers can be set visible independent to the visibility state of the containing legend. Use the visibility
|
||||
// state of the legend to override the visibility flag
|
||||
if ( !m_plotWidget->qtChart()->legend()->isAttachedToChart() ) isVisibleInLegend = false;
|
||||
if ( !m_plotWidget->qtChart()->legend()->isVisible() ) isVisibleInLegend = false;
|
||||
|
||||
bool showScatterMarker = isVisibleInLegend;
|
||||
if ( !m_symbol ) showScatterMarker = false;
|
||||
|
||||
if ( scatterSeries() )
|
||||
{
|
||||
auto markers = m_plotWidget->qtChart()->legend()->markers( scatterSeries() );
|
||||
if ( !markers.isEmpty() ) markers[0]->setVisible( isVisibleInLegend );
|
||||
if ( !markers.isEmpty() ) markers[0]->setVisible( showScatterMarker );
|
||||
}
|
||||
|
||||
bool showLineMarker = isVisibleInLegend;
|
||||
if ( showScatterMarker ) showLineMarker = false;
|
||||
|
||||
if ( lineSeries() )
|
||||
{
|
||||
auto lineSeriesMarkers = m_plotWidget->qtChart()->legend()->markers( lineSeries() );
|
||||
if ( !lineSeriesMarkers.isEmpty() ) lineSeriesMarkers[0]->setVisible( false );
|
||||
if ( !lineSeriesMarkers.isEmpty() ) lineSeriesMarkers[0]->setVisible( showLineMarker );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -77,14 +77,16 @@ public:
|
||||
|
||||
RiuPlotCurveSymbol* createSymbol( RiuPlotCurveSymbol::PointSymbolEnum symbol ) const override;
|
||||
|
||||
protected:
|
||||
private:
|
||||
void setSamplesInPlot( const std::vector<double>&, const std::vector<double>&, int ) override;
|
||||
|
||||
bool isQtChartObjectsPresent() const;
|
||||
QtCharts::QLineSeries* lineSeries() const;
|
||||
QtCharts::QScatterSeries* scatterSeries() const;
|
||||
|
||||
cvf::BoundingBox computeBoundingBox() const;
|
||||
|
||||
private:
|
||||
QtCharts::QLineSeries* m_lineSeries;
|
||||
QtCharts::QScatterSeries* m_scatterSeries;
|
||||
std::shared_ptr<RiuPlotCurveSymbol> m_symbol;
|
||||
|
||||
Reference in New Issue
Block a user