diff --git a/gui/include/Visualization/VisualizationGraphHelper.h b/gui/include/Visualization/VisualizationGraphHelper.h index c0bf2cf..42eee9c 100644 --- a/gui/include/Visualization/VisualizationGraphHelper.h +++ b/gui/include/Visualization/VisualizationGraphHelper.h @@ -32,7 +32,7 @@ struct VisualizationGraphHelper { */ static PlottablesMap create(std::shared_ptr variable, QCustomPlot &plot) noexcept; - static void updateData(PlottablesMap &plottables, std::shared_ptr dataSeries, + static void updateData(PlottablesMap &plottables, std::shared_ptr variable, const SqpRange &dateTime); static void setYAxisRange(std::shared_ptr variable, QCustomPlot &plot) noexcept; diff --git a/gui/src/Visualization/VisualizationGraphHelper.cpp b/gui/src/Visualization/VisualizationGraphHelper.cpp index 363a7c0..bd0c047 100644 --- a/gui/src/Visualization/VisualizationGraphHelper.cpp +++ b/gui/src/Visualization/VisualizationGraphHelper.cpp @@ -25,7 +25,7 @@ public: */ template struct PlottablesCreator { - static PlottablesMap createPlottables(T &, QCustomPlot &) + static PlottablesMap createPlottables(QCustomPlot &) { qCCritical(LOG_DataSeries()) << QObject::tr("Can't create plottables: unmanaged data series type"); @@ -33,34 +33,37 @@ struct PlottablesCreator { } }; +PlottablesMap createGraphs(QCustomPlot &plot, int nbGraphs) +{ + PlottablesMap result{}; + + // Creates {nbGraphs} QCPGraph to add to the plot + for (auto i = 0; i < nbGraphs; ++i) { + auto graph = plot.addGraph(); + result.insert({i, graph}); + } + + plot.replot(); + + return result; +} + /** - * Specialization of PlottablesCreator for scalars and vectors + * Specialization of PlottablesCreator for scalars * @sa ScalarSeries - * @sa VectorSeries */ template -struct PlottablesCreator::value - or std::is_base_of::value> > { - static PlottablesMap createPlottables(T &dataSeries, QCustomPlot &plot) - { - PlottablesMap result{}; - - // Gets the number of components of the data series - dataSeries.lockRead(); - auto componentCount = dataSeries.valuesData()->componentCount(); - dataSeries.unlock(); - - // For each component of the data series, creates a QCPGraph to add to the plot - for (auto i = 0; i < componentCount; ++i) { - auto graph = plot.addGraph(); - result.insert({i, graph}); - } - - plot.replot(); +struct PlottablesCreator::value> > { + static PlottablesMap createPlottables(QCustomPlot &plot) { return createGraphs(plot, 1); } +}; - return result; - } +/** + * Specialization of PlottablesCreator for vectors + * @sa VectorSeries + */ +template +struct PlottablesCreator::value> > { + static PlottablesMap createPlottables(QCustomPlot &plot) { return createGraphs(plot, 3); } }; /** @@ -70,7 +73,7 @@ struct PlottablesCreator struct PlottablesCreator::value> > { - static PlottablesMap createPlottables(T &dataSeries, QCustomPlot &plot) + static PlottablesMap createPlottables(QCustomPlot &plot) { PlottablesMap result{}; result.insert({0, new QCPColorMap{plot.xAxis, plot.yAxis}}); @@ -264,41 +267,59 @@ struct IPlottablesHelper { */ template struct PlottablesHelper : public IPlottablesHelper { - explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {} + explicit PlottablesHelper(std::shared_ptr dataSeries) : m_DataSeries{dataSeries} {} PlottablesMap create(QCustomPlot &plot) const override { - return PlottablesCreator::createPlottables(m_DataSeries, plot); + return PlottablesCreator::createPlottables(plot); } void update(PlottablesMap &plottables, const SqpRange &range, bool rescaleAxes) const override { - PlottablesUpdater::updatePlottables(m_DataSeries, plottables, range, rescaleAxes); + if (m_DataSeries) { + PlottablesUpdater::updatePlottables(*m_DataSeries, plottables, range, rescaleAxes); + } + else { + qCCritical(LOG_VisualizationGraphHelper()) << "Can't update plottables: inconsistency " + "between the type of data series and the " + "type supposed"; + } } void setYAxisRange(const SqpRange &xAxisRange, QCustomPlot &plot) const override { - return PlottablesUpdater::setPlotYAxisRange(m_DataSeries, xAxisRange, plot); + if (m_DataSeries) { + PlottablesUpdater::setPlotYAxisRange(*m_DataSeries, xAxisRange, plot); + } + else { + qCCritical(LOG_VisualizationGraphHelper()) << "Can't update plottables: inconsistency " + "between the type of data series and the " + "type supposed"; + } } - T &m_DataSeries; + std::shared_ptr m_DataSeries; }; -/// Creates IPlottablesHelper according to a data series -std::unique_ptr createHelper(std::shared_ptr dataSeries) noexcept +/// Creates IPlottablesHelper according to the type of data series a variable holds +std::unique_ptr createHelper(std::shared_ptr 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); } } // namespace @@ -307,7 +328,7 @@ PlottablesMap VisualizationGraphHelper::create(std::shared_ptr variabl QCustomPlot &plot) noexcept { if (variable) { - auto helper = createHelper(variable->dataSeries()); + auto helper = createHelper(variable); auto plottables = helper->create(plot); return plottables; } @@ -322,7 +343,7 @@ void VisualizationGraphHelper::setYAxisRange(std::shared_ptr variable, QCustomPlot &plot) noexcept { if (variable) { - auto helper = createHelper(variable->dataSeries()); + auto helper = createHelper(variable); helper->setYAxisRange(variable->range(), plot); } else { @@ -332,9 +353,9 @@ void VisualizationGraphHelper::setYAxisRange(std::shared_ptr variable, } void VisualizationGraphHelper::updateData(PlottablesMap &plottables, - std::shared_ptr dataSeries, + std::shared_ptr variable, const SqpRange &dateTime) { - auto helper = createHelper(dataSeries); + auto helper = createHelper(variable); helper->update(plottables, dateTime); } diff --git a/gui/src/Visualization/VisualizationGraphWidget.cpp b/gui/src/Visualization/VisualizationGraphWidget.cpp index b1e2048..f252c06 100644 --- a/gui/src/Visualization/VisualizationGraphWidget.cpp +++ b/gui/src/Visualization/VisualizationGraphWidget.cpp @@ -65,10 +65,10 @@ struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate { { } - void updateData(PlottablesMap &plottables, std::shared_ptr dataSeries, + void updateData(PlottablesMap &plottables, std::shared_ptr variable, const SqpRange &range) { - VisualizationGraphHelper::updateData(plottables, dataSeries, range); + VisualizationGraphHelper::updateData(plottables, variable, range); // Prevents that data has changed to update rendering m_RenderingDelegate->onPlotUpdated(); @@ -988,7 +988,7 @@ void VisualizationGraphWidget::onDataCacheVariableUpdated() qCDebug(LOG_VisualizationGraphWidget()) << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime; if (dateTime.contains(variable->range()) || dateTime.intersect(variable->range())) { - impl->updateData(variableEntry.second, variable->dataSeries(), variable->range()); + impl->updateData(variableEntry.second, variable, variable->range()); } } } @@ -998,6 +998,6 @@ void VisualizationGraphWidget::onUpdateVarDisplaying(std::shared_ptr v { auto it = impl->m_VariableToPlotMultiMap.find(variable); if (it != impl->m_VariableToPlotMultiMap.end()) { - impl->updateData(it->second, variable->dataSeries(), range); + impl->updateData(it->second, variable, range); } }