diff --git a/gui/include/Visualization/AxisRenderingUtils.h b/gui/include/Visualization/AxisRenderingUtils.h index 29fc11e..dbf1709 100644 --- a/gui/include/Visualization/AxisRenderingUtils.h +++ b/gui/include/Visualization/AxisRenderingUtils.h @@ -12,6 +12,7 @@ class IDataSeries; class QCPAxis; class QCustomPlot; class SqpColorScale; +class Variable; /// Formats a data value according to the axis on which it is present QString formatValue(double value, const QCPAxis &axis); @@ -27,11 +28,17 @@ struct IAxisHelper { /// @param plot the plot for which to set axe properties /// @param colorScale the color scale for which to set properties virtual void setProperties(QCustomPlot &plot, SqpColorScale &colorScale) = 0; + + /// Set the units of the plot's axes and the color scale associated to plot passed as + /// parameters + /// @param plot the plot for which to set axe units + /// @param colorScale the color scale for which to set unit + virtual void setUnits(QCustomPlot &plot, SqpColorScale &colorScale) = 0; }; struct IAxisHelperFactory { - /// Creates IAxisHelper according to a data series - static std::unique_ptr create(std::shared_ptr dataSeries) noexcept; + /// Creates IPlottablesHelper according to the type of data series a variable holds + static std::unique_ptr create(const Variable &variable) noexcept; }; #endif // SCIQLOP_AXISRENDERINGUTILS_H diff --git a/gui/src/Visualization/AxisRenderingUtils.cpp b/gui/src/Visualization/AxisRenderingUtils.cpp index 33f47a6..7fa33f0 100644 --- a/gui/src/Visualization/AxisRenderingUtils.cpp +++ b/gui/src/Visualization/AxisRenderingUtils.cpp @@ -4,6 +4,8 @@ #include #include +#include + #include #include @@ -68,11 +70,17 @@ void setAxisProperties(QCPAxis &axis, const Unit &unit, */ template struct AxisSetter { - static void setProperties(T &, QCustomPlot &, SqpColorScale &) + static void setProperties(QCustomPlot &, SqpColorScale &) { // Default implementation does nothing qCCritical(LOG_AxisRenderingUtils()) << "Can't set axis properties: unmanaged type of data"; } + + static void setUnits(T &, QCustomPlot &, SqpColorScale &) + { + // Default implementation does nothing + qCCritical(LOG_AxisRenderingUtils()) << "Can't set axis units: unmanaged type of data"; + } }; /** @@ -83,7 +91,12 @@ struct AxisSetter { template struct AxisSetter::value or std::is_base_of::value> > { - static void setProperties(T &dataSeries, QCustomPlot &plot, SqpColorScale &) + static void setProperties(QCustomPlot &, SqpColorScale &) + { + // Nothing to do + } + + static void setUnits(T &dataSeries, QCustomPlot &plot, SqpColorScale &) { dataSeries.lockRead(); auto xAxisUnit = dataSeries.xAxisUnit(); @@ -101,17 +114,8 @@ struct AxisSetter: */ template struct AxisSetter::value> > { - static void setProperties(T &dataSeries, QCustomPlot &plot, SqpColorScale &colorScale) + static void setProperties(QCustomPlot &plot, SqpColorScale &colorScale) { - dataSeries.lockRead(); - auto xAxisUnit = dataSeries.xAxisUnit(); - auto yAxisUnit = dataSeries.yAxisUnit(); - auto valuesUnit = dataSeries.valuesUnit(); - dataSeries.unlock(); - - setAxisProperties(*plot.xAxis, xAxisUnit); - setAxisProperties(*plot.yAxis, yAxisUnit, QCPAxis::stLogarithmic); - // Displays color scale in plot plot.plotLayout()->insertRow(0); plot.plotLayout()->addElement(0, 0, colorScale.m_Scale); @@ -125,9 +129,21 @@ struct AxisSetteraxis(), valuesUnit, QCPAxis::stLogarithmic); colorScale.m_AutomaticThreshold = true; } + + static void setUnits(T &dataSeries, QCustomPlot &plot, SqpColorScale &colorScale) + { + dataSeries.lockRead(); + auto xAxisUnit = dataSeries.xAxisUnit(); + auto yAxisUnit = dataSeries.yAxisUnit(); + auto valuesUnit = dataSeries.valuesUnit(); + dataSeries.unlock(); + + setAxisProperties(*plot.xAxis, xAxisUnit); + setAxisProperties(*plot.yAxis, yAxisUnit, QCPAxis::stLogarithmic); + setAxisProperties(*colorScale.m_Scale->axis(), valuesUnit, QCPAxis::stLogarithmic); + } }; /** @@ -136,14 +152,25 @@ struct AxisSetter struct AxisHelper : public IAxisHelper { - explicit AxisHelper(T &dataSeries) : m_DataSeries{dataSeries} {} + explicit AxisHelper(std::shared_ptr dataSeries) : m_DataSeries{dataSeries} {} void setProperties(QCustomPlot &plot, SqpColorScale &colorScale) override { - AxisSetter::setProperties(m_DataSeries, plot, colorScale); + AxisSetter::setProperties(plot, colorScale); } - T &m_DataSeries; + void setUnits(QCustomPlot &plot, SqpColorScale &colorScale) override + { + if (m_DataSeries) { + AxisSetter::setUnits(*m_DataSeries, plot, colorScale); + } + else { + qCCritical(LOG_AxisRenderingUtils()) << "Can't set units: inconsistency between the " + "type of data series and the type supposed"; + } + } + + std::shared_ptr m_DataSeries; }; } // namespace @@ -159,19 +186,22 @@ QString formatValue(double value, const QCPAxis &axis) } } -std::unique_ptr -IAxisHelperFactory::create(std::shared_ptr dataSeries) noexcept +std::unique_ptr IAxisHelperFactory::create(const Variable &variable) noexcept { - if (auto scalarSeries = std::dynamic_pointer_cast(dataSeries)) { - return std::make_unique >(*scalarSeries); - } - else if (auto spectrogramSeries = std::dynamic_pointer_cast(dataSeries)) { - return std::make_unique >(*spectrogramSeries); - } - else if (auto vectorSeries = std::dynamic_pointer_cast(dataSeries)) { - return std::make_unique >(*vectorSeries); - } - else { - return std::make_unique >(*dataSeries); + switch (variable.type()) { + case DataSeriesType::SCALAR: + return std::make_unique >( + std::dynamic_pointer_cast(variable.dataSeries())); + case DataSeriesType::SPECTROGRAM: + return std::make_unique >( + std::dynamic_pointer_cast(variable.dataSeries())); + case DataSeriesType::VECTOR: + return std::make_unique >( + std::dynamic_pointer_cast(variable.dataSeries())); + default: + // Creates default helper + break; } + + return std::make_unique >(nullptr); }