Move qwt axis functions to RimPlotAxisTools

This commit is contained in:
Magne Sjaastad 2024-05-03 09:54:34 +02:00
parent 6d478ddb3c
commit b7e5867430
5 changed files with 199 additions and 146 deletions

View File

@ -15,11 +15,8 @@
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RimGridCrossPlot.h"
#include "RiaDefines.h"
#include "RiaFontCache.h"
#include "RiaPreferences.h"
#include "RimGridCrossPlot.h"
#include "RifTextDataTableFormatter.h"
#include "RiuDraggableOverlayFrame.h"
@ -27,25 +24,15 @@
#include "RiuPlotMainWindowTools.h"
#include "RiuQwtPlotTools.h"
#include "RimGridCrossPlotCollection.h"
#include "RimGridCrossPlotCurve.h"
#include "RimGridCrossPlotDataSet.h"
#include "RimMultiPlot.h"
#include "RimPlotAxisLogRangeCalculator.h"
#include "RimPlotAxisProperties.h"
#include "cafPdmUiCheckBoxEditor.h"
#include "Tools/RimPlotAxisTools.h"
#include "cafPdmUiTreeOrdering.h"
#include "cafProgressInfo.h"
#include "cvfAssert.h"
#include "qwt_legend.h"
#include "qwt_plot.h"
#include "qwt_plot_curve.h"
#include <QDebug>
CAF_PDM_SOURCE_INIT( RimGridCrossPlot, "RimGridCrossPlot" );
//--------------------------------------------------------------------------------------------------
@ -447,6 +434,26 @@ void RimGridCrossPlot::axisLogarithmicChanged( const caf::SignalEmitter* emitter
loadDataAndUpdate();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimGridCrossPlot::applyPropertiesOnPlotAxes()
{
auto curves = visibleCurves();
RimPlotAxisTools::updatePlotWidgetFromAxisProperties( m_plotWidget,
RiuPlotAxis::defaultBottom(),
m_xAxisProperties(),
xAxisParameterString(),
curves );
RimPlotAxisTools::updatePlotWidgetFromAxisProperties( m_plotWidget,
RiuPlotAxis::defaultLeft(),
m_yAxisProperties(),
yAxisParameterString(),
curves );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -574,8 +581,7 @@ void RimGridCrossPlot::updateAxes()
{
if ( !m_plotWidget ) return;
updateAxisInQwt( RiaDefines::PlotAxis::PLOT_AXIS_BOTTOM );
updateAxisInQwt( RiaDefines::PlotAxis::PLOT_AXIS_LEFT );
applyPropertiesOnPlotAxes();
m_plotWidget->updateAnnotationObjects( m_xAxisProperties );
m_plotWidget->updateAnnotationObjects( m_yAxisProperties );
@ -770,8 +776,8 @@ void RimGridCrossPlot::updatePlotWidgetFromAxisRanges()
{
if ( m_plotWidget )
{
updateAxisInQwt( RiaDefines::PlotAxis::PLOT_AXIS_LEFT );
updateAxisInQwt( RiaDefines::PlotAxis::PLOT_AXIS_BOTTOM );
applyPropertiesOnPlotAxes();
m_plotWidget->qwtPlot()->updateAxes();
updateAxisRangesFromPlotWidget();
m_plotWidget->scheduleReplot();
@ -783,8 +789,8 @@ void RimGridCrossPlot::updatePlotWidgetFromAxisRanges()
//--------------------------------------------------------------------------------------------------
void RimGridCrossPlot::updateAxisRangesFromPlotWidget()
{
updateAxisFromQwt( RiaDefines::PlotAxis::PLOT_AXIS_LEFT );
updateAxisFromQwt( RiaDefines::PlotAxis::PLOT_AXIS_BOTTOM );
RimPlotAxisTools::updateVisibleRangesFromPlotWidget( m_xAxisProperties(), RiuPlotAxis::defaultBottom(), m_plotWidget );
RimPlotAxisTools::updateVisibleRangesFromPlotWidget( m_yAxisProperties(), RiuPlotAxis::defaultLeft(), m_plotWidget );
}
//--------------------------------------------------------------------------------------------------
@ -835,125 +841,6 @@ QString RimGridCrossPlot::yAxisParameterString() const
return yAxisParams.join( ", " );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimGridCrossPlot::updateAxisInQwt( RiaDefines::PlotAxis axisType )
{
if ( !m_plotWidget ) return;
RimPlotAxisProperties* axisProperties = m_xAxisProperties();
QString axisParameterString = xAxisParameterString();
if ( axisType == RiaDefines::PlotAxis::PLOT_AXIS_LEFT )
{
axisProperties = m_yAxisProperties();
axisParameterString = yAxisParameterString();
}
RiuPlotAxis axis = axisProperties->plotAxis();
if ( axisProperties->isActive() )
{
m_plotWidget->enableAxis( axis, true );
Qt::AlignmentFlag alignment = Qt::AlignCenter;
if ( axisProperties->titlePosition() == RimPlotAxisPropertiesInterface::AXIS_TITLE_END )
{
alignment = Qt::AlignRight;
}
m_plotWidget->setAxisFontsAndAlignment( axis,
caf::FontTools::pointSizeToPixelSize( axisProperties->titleFontSize() ),
caf::FontTools::pointSizeToPixelSize( axisProperties->valuesFontSize() ),
true,
alignment );
m_plotWidget->setAxisTitleText( axis, axisParameterString );
m_plotWidget->setAxisTitleEnabled( axis, true );
if ( axisProperties->isLogarithmicScaleEnabled() )
{
bool isLogScale = m_plotWidget->axisScaleType( axis ) == RiuQwtPlotWidget::AxisScaleType::LOGARITHMIC;
if ( !isLogScale )
{
m_plotWidget->setAxisScaleType( axis, RiuQwtPlotWidget::AxisScaleType::LOGARITHMIC );
m_plotWidget->setAxisMaxMinor( axis, 5 );
}
double min = axisProperties->visibleRangeMin();
double max = axisProperties->visibleRangeMax();
if ( axisProperties->isAutoZoom() )
{
std::vector<const RimPlotCurve*> plotCurves = visibleCurves();
RimPlotAxisLogRangeCalculator logRangeCalculator( axis.axis(), plotCurves );
logRangeCalculator.computeAxisRange( &min, &max );
}
if ( axisProperties->isAxisInverted() )
{
std::swap( min, max );
}
m_plotWidget->setAxisScale( axis, min, max );
}
else
{
bool isLinearScale = m_plotWidget->axisScaleType( axis ) == RiuQwtPlotWidget::AxisScaleType::LINEAR;
if ( !isLinearScale )
{
m_plotWidget->setAxisScaleType( axis, RiuQwtPlotWidget::AxisScaleType::LINEAR );
m_plotWidget->setAxisMaxMinor( axis, 3 );
}
if ( axisProperties->isAutoZoom() )
{
m_plotWidget->setAxisAutoScale( axis, true );
m_plotWidget->setAxisInverted( axis, axisProperties->isAxisInverted() );
}
else
{
double min = axisProperties->visibleRangeMin();
double max = axisProperties->visibleRangeMax();
if ( axisProperties->isAxisInverted() )
{
std::swap( min, max );
}
m_plotWidget->setAxisScale( axis, min, max );
}
}
}
else
{
m_plotWidget->enableAxis( axis, false );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimGridCrossPlot::updateAxisFromQwt( RiaDefines::PlotAxis axisType )
{
if ( !m_plotWidget ) return;
auto [xAxisRangeMin, xAxisRangeMax] = m_plotWidget->axisRange( RiuPlotAxis::defaultBottom() );
RimPlotAxisProperties* axisProperties = m_xAxisProperties();
double axisRangeMin = xAxisRangeMin;
double axisRangeMax = xAxisRangeMax;
if ( axisType == RiaDefines::PlotAxis::PLOT_AXIS_LEFT )
{
axisProperties = m_yAxisProperties();
auto [yAxisRangeMin, yAxisRangeMax] = m_plotWidget->axisRange( RiuPlotAxis::defaultLeft() );
axisRangeMin = yAxisRangeMin;
axisRangeMax = yAxisRangeMax;
}
axisProperties->setVisibleRangeMin( std::min( axisRangeMin, axisRangeMax ) );
axisProperties->setVisibleRangeMax( std::max( axisRangeMin, axisRangeMax ) );
axisProperties->updateConnectedEditors();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -21,7 +21,6 @@
#include "cafPdmChildField.h"
#include "cafPdmObject.h"
#include "RiaPlotDefines.h"
#include "RimNameConfig.h"
#include "RimPlot.h"
@ -125,8 +124,6 @@ protected:
virtual QString xAxisParameterString() const;
QString yAxisParameterString() const;
void updateAxisInQwt( RiaDefines::PlotAxis axisType );
void updateAxisFromQwt( RiaDefines::PlotAxis axisType );
std::vector<const RimPlotCurve*> visibleCurves() const;
RimPlotAxisProperties* xAxisProperties();
@ -148,6 +145,7 @@ private:
void connectAxisSignals( RimPlotAxisProperties* axis );
void axisSettingsChanged( const caf::SignalEmitter* emitter );
void axisLogarithmicChanged( const caf::SignalEmitter* emitter, bool isLogarithmic );
void applyPropertiesOnPlotAxes();
private slots:
void onPlotZoomed();

View File

@ -1,6 +1,10 @@
set(SOURCE_GROUP_HEADER_FILES ${CMAKE_CURRENT_LIST_DIR}/RiaVariableMapper.h)
set(SOURCE_GROUP_HEADER_FILES ${CMAKE_CURRENT_LIST_DIR}/RiaVariableMapper.h
${CMAKE_CURRENT_LIST_DIR}/RimPlotAxisTools.h
)
set(SOURCE_GROUP_SOURCE_FILES ${CMAKE_CURRENT_LIST_DIR}/RiaVariableMapper.cpp)
set(SOURCE_GROUP_SOURCE_FILES ${CMAKE_CURRENT_LIST_DIR}/RiaVariableMapper.cpp
${CMAKE_CURRENT_LIST_DIR}/RimPlotAxisTools.cpp
)
list(APPEND CODE_HEADER_FILES ${SOURCE_GROUP_HEADER_FILES})
list(APPEND CODE_SOURCE_FILES ${SOURCE_GROUP_SOURCE_FILES})

View File

@ -0,0 +1,125 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RimPlotAxisTools.h"
#include "RimPlotAxisLogRangeCalculator.h"
#include "RimPlotAxisProperties.h"
#include "RimPlotCurve.h"
#include "RiuPlotAxis.h"
#include "RiuPlotWidget.h"
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimPlotAxisTools::updateVisibleRangesFromPlotWidget( RimPlotAxisProperties* axisProperties,
RiuPlotAxis plotAxis,
const RiuPlotWidget* const plotWidget )
{
if ( !plotWidget || !axisProperties ) return;
auto [axisRangeMin, axisRangeMax] = plotWidget->axisRange( plotAxis );
axisProperties->setVisibleRangeMin( std::min( axisRangeMin, axisRangeMax ) );
axisProperties->setVisibleRangeMax( std::max( axisRangeMin, axisRangeMax ) );
axisProperties->updateConnectedEditors();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimPlotAxisTools::updatePlotWidgetFromAxisProperties( RiuPlotWidget* plotWidget,
RiuPlotAxis axis,
const RimPlotAxisProperties* const axisProperties,
const QString& axisTitle,
std::vector<const RimPlotCurve*> plotCurves )
{
if ( !plotWidget || !axisProperties ) return;
if ( axisProperties->isActive() )
{
plotWidget->enableAxis( axis, true );
Qt::AlignmentFlag alignment = Qt::AlignCenter;
if ( axisProperties->titlePosition() == RimPlotAxisPropertiesInterface::AXIS_TITLE_END )
{
alignment = Qt::AlignRight;
}
plotWidget->setAxisFontsAndAlignment( axis, axisProperties->titleFontSize(), axisProperties->valuesFontSize(), false, alignment );
if ( !axisTitle.isEmpty() )
{
plotWidget->setAxisTitleText( axis, axisTitle );
}
plotWidget->setAxisTitleEnabled( axis, true );
if ( axisProperties->isLogarithmicScaleEnabled() )
{
bool isLogScale = plotWidget->axisScaleType( axis ) == RiuPlotWidget::AxisScaleType::LOGARITHMIC;
if ( !isLogScale )
{
plotWidget->setAxisScaleType( axis, RiuPlotWidget::AxisScaleType::LOGARITHMIC );
plotWidget->setAxisMaxMinor( axis, 5 );
}
double min = axisProperties->visibleRangeMin();
double max = axisProperties->visibleRangeMax();
if ( axisProperties->isAutoZoom() )
{
RimPlotAxisLogRangeCalculator logRangeCalculator( axis.axis(), plotCurves );
logRangeCalculator.computeAxisRange( &min, &max );
}
if ( axisProperties->isAxisInverted() )
{
std::swap( min, max );
}
plotWidget->setAxisScale( axis, min, max );
}
else
{
bool isLinearScale = plotWidget->axisScaleType( axis ) == RiuPlotWidget::AxisScaleType::LINEAR;
if ( !isLinearScale )
{
plotWidget->setAxisScaleType( axis, RiuPlotWidget::AxisScaleType::LINEAR );
plotWidget->setAxisMaxMinor( axis, 3 );
}
if ( axisProperties->isAutoZoom() )
{
plotWidget->setAxisAutoScale( axis, true );
plotWidget->setAxisInverted( axis, axisProperties->isAxisInverted() );
}
else
{
double min = axisProperties->visibleRangeMin();
double max = axisProperties->visibleRangeMax();
if ( axisProperties->isAxisInverted() )
{
std::swap( min, max );
}
plotWidget->setAxisScale( axis, min, max );
}
}
}
else
{
plotWidget->enableAxis( axis, false );
}
}

View File

@ -0,0 +1,39 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
class RimPlotAxisProperties;
class RimPlotCurve;
class RiuPlotAxis;
class RiuPlotWidget;
#include <QString>
#include <vector>
namespace RimPlotAxisTools
{
void updateVisibleRangesFromPlotWidget( RimPlotAxisProperties* axisProperties, RiuPlotAxis plotAxis, const RiuPlotWidget* const plotWidget );
void updatePlotWidgetFromAxisProperties( RiuPlotWidget* plotWidget,
RiuPlotAxis plotAxis,
const RimPlotAxisProperties* const axisProperties,
const QString& axisTitle,
std::vector<const RimPlotCurve*> plotCurves );
}; // namespace RimPlotAxisTools