From 0ce69d41dd56845360c5759ad0c9695d996c9c32 Mon Sep 17 00:00:00 2001 From: Gaute Lindkvist Date: Wed, 13 Mar 2019 08:38:18 +0100 Subject: [PATCH] Swap axis settings when swapping Grid Cross Plot axes + fix inverted axes for summary plot --- ...icSwapGridCrossPlotCurveSetAxesFeature.cpp | 2 +- .../GridCrossPlots/RimGridCrossPlot.cpp | 47 ++++++--- .../GridCrossPlots/RimGridCrossPlot.h | 2 +- .../RimPlotAxisProperties.cpp | 97 +++++++++++++++++++ .../ProjectDataModel/RimPlotAxisProperties.h | 26 +++++ .../Summary/RimSummaryCurvesCalculator.cpp | 85 ---------------- .../Summary/RimSummaryCurvesCalculator.h | 21 +--- .../Summary/RimSummaryPlot.cpp | 12 ++- 8 files changed, 173 insertions(+), 119 deletions(-) diff --git a/ApplicationCode/Commands/GridCrossPlotCommands/RicSwapGridCrossPlotCurveSetAxesFeature.cpp b/ApplicationCode/Commands/GridCrossPlotCommands/RicSwapGridCrossPlotCurveSetAxesFeature.cpp index 065dd37b76..708ddec4f6 100644 --- a/ApplicationCode/Commands/GridCrossPlotCommands/RicSwapGridCrossPlotCurveSetAxesFeature.cpp +++ b/ApplicationCode/Commands/GridCrossPlotCommands/RicSwapGridCrossPlotCurveSetAxesFeature.cpp @@ -40,7 +40,7 @@ void RicSwapGridCrossPlotCurveSetAxesFeature::onActionTriggered(bool isChecked) else if (caf::SelectionManager::instance()->selectedItemOfType()) { auto plot = caf::SelectionManager::instance()->selectedItemOfType(); - plot->swapAllAxisProperties(); + plot->swapAxes(); } } diff --git a/ApplicationCode/ProjectDataModel/GridCrossPlots/RimGridCrossPlot.cpp b/ApplicationCode/ProjectDataModel/GridCrossPlots/RimGridCrossPlot.cpp index ded3e5e677..2462cfe60f 100644 --- a/ApplicationCode/ProjectDataModel/GridCrossPlots/RimGridCrossPlot.cpp +++ b/ApplicationCode/ProjectDataModel/GridCrossPlots/RimGridCrossPlot.cpp @@ -521,13 +521,29 @@ void RimGridCrossPlot::updateCurveNamesAndPlotTitle() //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void RimGridCrossPlot::swapAllAxisProperties() +void RimGridCrossPlot::swapAxes() { + RimPlotAxisProperties* xAxisProperties = m_xAxisProperties(); + RimPlotAxisProperties* yAxisProperties = m_yAxisProperties(); + + QString tmpName = xAxisProperties->name(); + QwtPlot::Axis tmpAxis = xAxisProperties->qwtPlotAxisType(); + xAxisProperties->setNameAndAxis(yAxisProperties->name(), yAxisProperties->qwtPlotAxisType()); + yAxisProperties->setNameAndAxis(tmpName, tmpAxis); + + m_xAxisProperties.removeChildObject(xAxisProperties); + m_yAxisProperties.removeChildObject(yAxisProperties); + m_yAxisProperties = xAxisProperties; + m_xAxisProperties = yAxisProperties; + for (auto curveSet : m_crossPlotCurveSets) { curveSet->swapAxisProperties(false); } + loadDataAndUpdate(); + + updateAxisDisplay(); } //-------------------------------------------------------------------------------------------------- @@ -672,6 +688,15 @@ void RimGridCrossPlot::updateAxisInQwt(RiaDefines::PlotAxis axisType) m_qwtPlot->setAxisScaleEngine(axisProperties->qwtPlotAxisType(), new QwtLogScaleEngine); m_qwtPlot->setAxisMaxMinor(axisProperties->qwtPlotAxisType(), 5); } + + if (axisProperties->isAutoZoom()) + { + m_qwtPlot->setAxisAutoScale(qwtAxisId); + } + else + { + m_qwtPlot->setAxisScale(qwtAxisId, axisProperties->visibleRangeMin, axisProperties->visibleRangeMax); + } } else { @@ -682,19 +707,17 @@ void RimGridCrossPlot::updateAxisInQwt(RiaDefines::PlotAxis axisType) m_qwtPlot->setAxisScaleEngine(axisProperties->qwtPlotAxisType(), new QwtLinearScaleEngine); m_qwtPlot->setAxisMaxMinor(axisProperties->qwtPlotAxisType(), 3); } + + if (axisProperties->isAutoZoom()) + { + m_qwtPlot->setAxisAutoScale(qwtAxisId); + } + else + { + m_qwtPlot->setAxisScale(qwtAxisId, axisProperties->visibleRangeMin, axisProperties->visibleRangeMax); + } } m_qwtPlot->axisScaleEngine(axisProperties->qwtPlotAxisType())->setAttribute(QwtScaleEngine::Inverted, axisProperties->isAxisInverted()); - - if (axisProperties->isAutoZoom()) - { - m_qwtPlot->setAxisAutoScale(qwtAxisId); - } - else - { - m_qwtPlot->setAxisScale(qwtAxisId, axisProperties->visibleRangeMin, axisProperties->visibleRangeMax); - } - - } else { diff --git a/ApplicationCode/ProjectDataModel/GridCrossPlots/RimGridCrossPlot.h b/ApplicationCode/ProjectDataModel/GridCrossPlots/RimGridCrossPlot.h index f1feaaa722..0b049fd64e 100644 --- a/ApplicationCode/ProjectDataModel/GridCrossPlots/RimGridCrossPlot.h +++ b/ApplicationCode/ProjectDataModel/GridCrossPlots/RimGridCrossPlot.h @@ -70,7 +70,7 @@ public: void detachAllCurves(); void performAutoNameUpdate() override; void updateCurveNamesAndPlotTitle(); - void swapAllAxisProperties(); + void swapAxes(); QString asciiTitleForPlotExport(int curveSetIndex) const; QString asciiDataForPlotExport(int curveSetIndex) const; RiuGridCrossQwtPlot* qwtPlot() const; diff --git a/ApplicationCode/ProjectDataModel/RimPlotAxisProperties.cpp b/ApplicationCode/ProjectDataModel/RimPlotAxisProperties.cpp index 812aba04bc..c5c761d97b 100644 --- a/ApplicationCode/ProjectDataModel/RimPlotAxisProperties.cpp +++ b/ApplicationCode/ProjectDataModel/RimPlotAxisProperties.cpp @@ -20,6 +20,7 @@ #include "RimPlotAxisProperties.h" #include "RiaDefines.h" +#include "RigStatisticsCalculator.h" #include "RimRiuQwtPlotOwnerInterface.h" @@ -27,6 +28,8 @@ #include +#include + // clang-format off namespace caf { @@ -222,6 +225,14 @@ QwtPlot::Axis RimPlotAxisProperties::qwtPlotAxisType() const return m_axis; } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QString RimPlotAxisProperties::name() const +{ + return m_name; +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -358,3 +369,89 @@ caf::PdmFieldHandle* RimPlotAxisProperties::objectToggleField() { return &m_isActive; } + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +RimPlotAxisRangeCalculator::RimPlotAxisRangeCalculator(QwtPlot::Axis axis, + const std::vector& qwtCurves, + const std::vector& axisValuesForAllCurves) + : m_singleCurves(qwtCurves) + , m_axisValuesForAllCurves(axisValuesForAllCurves) +{ +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimPlotAxisRangeCalculator::computeAxisRange(double* min, double* max) const +{ + double minValue = HUGE_VAL; + double maxValue = -HUGE_VAL; + + for (QwtPlotCurve* curve : m_singleCurves) + { + double minCurveValue = HUGE_VAL; + double maxCurveValue = -HUGE_VAL; + + if (curveValueRange(curve, &minCurveValue, &maxCurveValue)) + { + if (minCurveValue < minValue) + { + minValue = minCurveValue; + } + + if (maxCurveValue > maxValue) + { + maxValue = maxCurveValue; + } + } + } + + if (minValue == HUGE_VAL) + { + minValue = RiaDefines::minimumDefaultValuePlot(); + maxValue = RiaDefines::maximumDefaultValuePlot(); + } + + // For logarithmic auto scaling, compute positive curve value closest to zero and use + // this value as the plot visible minimum + + double pos = HUGE_VAL; + double neg = -HUGE_VAL; + + RigStatisticsCalculator::posNegClosestToZero(m_axisValuesForAllCurves, pos, neg); + + if (pos != HUGE_VAL) + { + minValue = pos; + } + + *min = minValue; + *max = maxValue; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool RimPlotAxisRangeCalculator::curveValueRange(const QwtPlotCurve* qwtCurve, double* min, double* max) const +{ + if (!qwtCurve) return false; + + if (qwtCurve->data()->size() < 1) + { + return false; + } + + if (m_axis == QwtPlot::xBottom || m_axis == QwtPlot::xTop) + { + *min = qwtCurve->minXValue(); + *max = qwtCurve->maxXValue(); + } + else + { + *min = qwtCurve->minYValue(); + *max = qwtCurve->maxYValue(); + } + return true; +} diff --git a/ApplicationCode/ProjectDataModel/RimPlotAxisProperties.h b/ApplicationCode/ProjectDataModel/RimPlotAxisProperties.h index 5229599534..66ac22b7a2 100644 --- a/ApplicationCode/ProjectDataModel/RimPlotAxisProperties.h +++ b/ApplicationCode/ProjectDataModel/RimPlotAxisProperties.h @@ -57,7 +57,9 @@ public: void setEnableTitleTextSettings(bool enable); void setNameAndAxis(const QString& name, QwtPlot::Axis axis); + QwtPlot::Axis qwtPlotAxisType() const; + QString name() const; RiaDefines::PlotAxis plotAxisType() const; bool useAutoTitle() const; bool showDescription() const; @@ -111,3 +113,27 @@ private: bool m_enableTitleTextSettings; }; + +class QwtPlotCurve; + +//================================================================================================== +/// +/// +//================================================================================================== +class RimPlotAxisRangeCalculator +{ +public: + RimPlotAxisRangeCalculator(QwtPlot::Axis axis, + const std::vector& qwtCurves, + const std::vector& axisValuesForAllCurves); + + void computeAxisRange(double* min, double* max) const; + +private: + bool curveValueRange(const QwtPlotCurve* qwtCurve, double* min, double* max) const; + +private: + QwtPlot::Axis m_axis; + const std::vector m_singleCurves; + const std::vector m_axisValuesForAllCurves; +}; diff --git a/ApplicationCode/ProjectDataModel/Summary/RimSummaryCurvesCalculator.cpp b/ApplicationCode/ProjectDataModel/Summary/RimSummaryCurvesCalculator.cpp index cff5a9fc9f..edecd02b51 100644 --- a/ApplicationCode/ProjectDataModel/Summary/RimSummaryCurvesCalculator.cpp +++ b/ApplicationCode/ProjectDataModel/Summary/RimSummaryCurvesCalculator.cpp @@ -18,8 +18,6 @@ #include "RimSummaryCurvesCalculator.h" -#include "RigStatisticsCalculator.h" - #include "RiaDefines.h" #include "RimSummaryCurve.h" #include "RimPlotAxisProperties.h" @@ -319,86 +317,3 @@ std::string RimSummaryPlotYAxisFormatter::shortCalculationName(const std::string return calculationShortName.toStdString(); } - - - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -RimSummaryPlotYAxisRangeCalculator::RimSummaryPlotYAxisRangeCalculator( - const std::vector& qwtCurves, - const std::vector& yValuesForAllCurves) - : - m_singleCurves(qwtCurves), - m_yValuesForAllCurves(yValuesForAllCurves) -{ -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void RimSummaryPlotYAxisRangeCalculator::computeYRange(double* min, double* max) const -{ - double minValue = HUGE_VAL; - double maxValue = -HUGE_VAL; - - for (QwtPlotCurve* curve : m_singleCurves) - { - double minCurveValue = HUGE_VAL; - double maxCurveValue = -HUGE_VAL; - - if (curveValueRangeY(curve, &minCurveValue, &maxCurveValue)) - { - if (minCurveValue < minValue) - { - minValue = minCurveValue; - } - - if (maxCurveValue > maxValue) - { - maxValue = maxCurveValue; - } - } - } - - if (minValue == HUGE_VAL) - { - minValue = RiaDefines::minimumDefaultValuePlot(); - maxValue = RiaDefines::maximumDefaultValuePlot(); - } - - // For logarithmic auto scaling, compute positive curve value closest to zero and use - // this value as the plot visible minimum - - double pos = HUGE_VAL; - double neg = -HUGE_VAL; - - RigStatisticsCalculator::posNegClosestToZero(m_yValuesForAllCurves, pos, neg); - - if (pos != HUGE_VAL) - { - minValue = pos; - } - - *min = minValue; - *max = maxValue; -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -bool RimSummaryPlotYAxisRangeCalculator::curveValueRangeY(const QwtPlotCurve* qwtCurve, double* min, double* max) const -{ - if (!qwtCurve) return false; - - if (qwtCurve->data()->size() < 1) - { - return false; - } - - *min = qwtCurve->minYValue(); - *max = qwtCurve->maxYValue(); - - return true; -} - diff --git a/ApplicationCode/ProjectDataModel/Summary/RimSummaryCurvesCalculator.h b/ApplicationCode/ProjectDataModel/Summary/RimSummaryCurvesCalculator.h index 4cf4e6199f..ac06254368 100644 --- a/ApplicationCode/ProjectDataModel/Summary/RimSummaryCurvesCalculator.h +++ b/ApplicationCode/ProjectDataModel/Summary/RimSummaryCurvesCalculator.h @@ -22,6 +22,8 @@ #include #include +#include + class RimAsciiDataCurve; class RimSummaryCurve; class RimPlotAxisProperties; @@ -46,26 +48,9 @@ private: static std::string shortCalculationName(const std::string& calculationName); private: - RimPlotAxisProperties* m_axisProperties; + RimPlotAxisProperties* m_axisProperties; const std::vector m_summaryCurves; const std::vector m_asciiDataCurves; const std::set m_timeHistoryCurveQuantities; }; - -class RimSummaryPlotYAxisRangeCalculator -{ -public: - RimSummaryPlotYAxisRangeCalculator( const std::vector& qwtCurves, - const std::vector& yValuesForAllCurves); - - void computeYRange(double* min, double* max) const; - -private: - bool curveValueRangeY(const QwtPlotCurve* qwtCurve, double* min, double* max) const; - -private: - const std::vector m_singleCurves; - const std::vector m_yValuesForAllCurves; -}; - diff --git a/ApplicationCode/ProjectDataModel/Summary/RimSummaryPlot.cpp b/ApplicationCode/ProjectDataModel/Summary/RimSummaryPlot.cpp index a4ac146f06..171bb7e502 100644 --- a/ApplicationCode/ProjectDataModel/Summary/RimSummaryPlot.cpp +++ b/ApplicationCode/ProjectDataModel/Summary/RimSummaryPlot.cpp @@ -54,6 +54,7 @@ #include "qwt_plot_curve.h" #include "qwt_plot_renderer.h" #include "qwt_plot_textlabel.h" +#include "qwt_scale_engine.h" #include #include @@ -659,8 +660,13 @@ void RimSummaryPlot::updateZoomForAxis(RiaDefines::PlotAxis plotAxis) } double min, max; - RimSummaryPlotYAxisRangeCalculator calc(plotCurves, yValues); - calc.computeYRange(&min, &max); + RimPlotAxisRangeCalculator calc(QwtPlot::yLeft, plotCurves, yValues); + calc.computeAxisRange(&min, &max); + + if (yAxisProps->isAxisInverted()) + { + std::swap(min, max); + } m_qwtPlot->setAxisScale(yAxisProps->qwtPlotAxisType(), min, max); } @@ -673,6 +679,8 @@ void RimSummaryPlot::updateZoomForAxis(RiaDefines::PlotAxis plotAxis) { m_qwtPlot->setAxisScale(yAxisProps->qwtPlotAxisType(), yAxisProps->visibleRangeMin(), yAxisProps->visibleRangeMax()); } + + m_qwtPlot->axisScaleEngine(yAxisProps->qwtPlotAxisType())->setAttribute(QwtScaleEngine::Inverted, yAxisProps->isAxisInverted()); } }