diff --git a/core/include/Data/ArrayData.h b/core/include/Data/ArrayData.h index 18eb70c..5cd885b 100644 --- a/core/include/Data/ArrayData.h +++ b/core/include/Data/ArrayData.h @@ -218,6 +218,8 @@ public: } } + int componentCount() const noexcept { return m_Data.size(); } + /** * @return the data of a component * @param componentIndex the index of the component to retrieve the data diff --git a/gui/src/Visualization/VisualizationGraphHelper.cpp b/gui/src/Visualization/VisualizationGraphHelper.cpp index 80ae762..bcff078 100644 --- a/gui/src/Visualization/VisualizationGraphHelper.cpp +++ b/gui/src/Visualization/VisualizationGraphHelper.cpp @@ -2,6 +2,7 @@ #include "Visualization/qcustomplot.h" #include +#include #include @@ -35,161 +36,199 @@ QSharedPointer axisTicker(bool isTimeAxis) } } -void updateScalarData(QCPAbstractPlottable *component, std::shared_ptr scalarSeries, - const SqpRange &range) +/// Sets axes properties according to the properties of a data series +template +void setAxesProperties(const DataSeries &dataSeries, QCustomPlot &plot) noexcept { - qCDebug(LOG_VisualizationGraphHelper()) << "TORM: updateScalarData" - << QThread::currentThread()->objectName(); - if (auto qcpGraph = dynamic_cast(component)) { - scalarSeries->lockRead(); - { - auto sqpDataContainer = QSharedPointer::create(); - qcpGraph->setData(sqpDataContainer); - auto bounds = scalarSeries->subData(range.m_TStart, range.m_TEnd); - for (auto it = bounds.first; it != bounds.second; ++it) { - sqpDataContainer->appendGraphData(QCPGraphData(it->x(), it->value())); - } - - qCInfo(LOG_VisualizationGraphHelper()) << "TODEBUG: Current points displayed" - << sqpDataContainer->size(); - } - scalarSeries->unlock(); - + /// @todo : for the moment, no control is performed on the axes: the units and the tickers + /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph + auto setAxisProperties = [](auto axis, const auto &unit) { + // label (unit name) + axis->setLabel(unit.m_Name); + + // ticker (depending on the type of unit) + axis->setTicker(axisTicker(unit.m_TimeUnit)); + }; + setAxisProperties(plot.xAxis, dataSeries.xAxisUnit()); + setAxisProperties(plot.yAxis, dataSeries.valuesUnit()); +} - // Display all data - component->parentPlot()->replot(); - } - else { - /// @todo DEBUG +/** + * Struct used to create plottables, depending on the type of the data series from which to create them + * @tparam T the data series' type + * @remarks Default implementation can't create plottables + */ +template +struct PlottablesCreator { + static PlottablesMap createPlottables(T &, QCustomPlot &) + { + qCCritical(LOG_DataSeries()) + << QObject::tr("Can't create plottables: unmanaged data series type"); + return {}; } -} +}; -QCPAbstractPlottable *createScalarSeriesComponentV2(std::shared_ptr scalarSeries, - QCustomPlot &plot) -{ - auto component = plot.addGraph(); +/** + * Specialization of PlottablesCreator for scalars and vectors + * @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 + auto componentCount = dataSeries.valuesData()->componentCount(); + + // 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}); + } - if (component) { // Axes properties - /// @todo : for the moment, no control is performed on the axes: the units and the tickers - /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph - - auto setAxisProperties = [](auto axis, const auto &unit) { - // label (unit name) - axis->setLabel(unit.m_Name); - - // ticker (depending on the type of unit) - axis->setTicker(axisTicker(unit.m_TimeUnit)); - }; - setAxisProperties(plot.xAxis, scalarSeries->xAxisUnit()); - setAxisProperties(plot.yAxis, scalarSeries->valuesUnit()); + setAxesProperties(dataSeries, plot); + + plot.replot(); + + return result; } - return component; -} +}; -QCPAbstractPlottable *createScalarSeriesComponent(std::shared_ptr scalarSeries, - QCustomPlot &plot, const SqpRange &dateTime) -{ - auto component = plot.addGraph(); +/** + * Struct used to update plottables, depending on the type of the data series from which to update them + * @tparam T the data series' type + * @remarks Default implementation can't update plottables + */ +template +struct PlottablesUpdater { + static void updatePlottables(T &, PlottablesMap &, const SqpRange &, bool) + { + qCCritical(LOG_DataSeries()) + << QObject::tr("Can't update plottables: unmanaged data series type"); + } +}; - if (component) { - // // Graph data - component->setData(scalarSeries->xAxisData()->data(), scalarSeries->valuesData()->data(), - true); +/** + * Specialization of PlottablesUpdater for scalars and vectors + * @sa ScalarSeries + * @sa VectorSeries + */ +template +struct PlottablesUpdater::value + or std::is_base_of::value> > { + static void updatePlottables(T &dataSeries, PlottablesMap &plottables, const SqpRange &range, + bool rescaleAxes) + { + dataSeries.lockRead(); + + // For each plottable to update, resets its data + std::map > dataContainers{}; + for (const auto &plottable : plottables) { + if (auto graph = dynamic_cast(plottable.second)) { + auto dataContainer = QSharedPointer::create(); + graph->setData(dataContainer); + + dataContainers.insert({plottable.first, dataContainer}); + } + } - updateScalarData(component, scalarSeries, dateTime); + // - Gets the data of the series included in the current range + // - Updates each plottable by adding, for each data item, a point that takes x-axis data and value data. The correct value is retrieved according to the index of the component + auto subDataIts = dataSeries.subData(range.m_TStart, range.m_TEnd); + for (auto it = subDataIts.first; it != subDataIts.second; ++it) { + for (const auto &dataContainer : dataContainers) { + auto componentIndex = dataContainer.first; + dataContainer.second->appendGraphData( + QCPGraphData(it->x(), it->value(componentIndex))); + } + } - // Axes properties - /// @todo : for the moment, no control is performed on the axes: the units and the tickers - /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph + dataSeries.unlock(); - auto setAxisProperties = [](auto axis, const auto &unit) { - // label (unit name) - axis->setLabel(unit.m_Name); + if (!plottables.empty()) { + auto plot = plottables.begin()->second->parentPlot(); - // ticker (depending on the type of unit) - axis->setTicker(axisTicker(unit.m_TimeUnit)); - }; - setAxisProperties(plot.xAxis, scalarSeries->xAxisUnit()); - setAxisProperties(plot.yAxis, scalarSeries->valuesUnit()); + if (rescaleAxes) { + plot->rescaleAxes(); + } - // Display all data - component->rescaleAxes(); - plot.replot(); + plot->replot(); + } } - else { - qCDebug(LOG_VisualizationGraphHelper()) - << QObject::tr("Can't create graph for the scalar series"); +}; + +/** + * Helper used to create/update plottables + */ +struct IPlottablesHelper { + virtual ~IPlottablesHelper() noexcept = default; + virtual PlottablesMap create(QCustomPlot &plot) const = 0; + virtual void update(PlottablesMap &plottables, const SqpRange &range, + bool rescaleAxes = false) const = 0; +}; + +/** + * Default implementation of IPlottablesHelper, which takes data series to create/update plottables + * @tparam T the data series' type + */ +template +struct PlottablesHelper : public IPlottablesHelper { + explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {} + + PlottablesMap create(QCustomPlot &plot) const override + { + return PlottablesCreator::createPlottables(m_DataSeries, plot); } - return component; -} + void update(PlottablesMap &plottables, const SqpRange &range, bool rescaleAxes) const override + { + PlottablesUpdater::updatePlottables(m_DataSeries, plottables, range, rescaleAxes); + } -} // namespace + T &m_DataSeries; +}; -QVector -VisualizationGraphHelper::createV2(std::shared_ptr variable, QCustomPlot &plot) noexcept +/// Creates IPlottablesHelper according to a data series +std::unique_ptr createHelper(IDataSeries *dataSeries) noexcept { - auto result = QVector{}; - - if (variable) { - // Gets the data series of the variable to call the creation of the right components - // according to its type - if (auto scalarSeries = std::dynamic_pointer_cast(variable->dataSeries())) { - result.append(createScalarSeriesComponentV2(scalarSeries, plot)); - } - else { - qCDebug(LOG_VisualizationGraphHelper()) - << QObject::tr("Can't create graph plottables : unmanaged data series type"); - } + if (auto scalarSeries = dynamic_cast(dataSeries)) { + return std::make_unique >(*scalarSeries); + } + else if (auto vectorSeries = dynamic_cast(dataSeries)) { + return std::make_unique >(*vectorSeries); } else { - qCDebug(LOG_VisualizationGraphHelper()) - << QObject::tr("Can't create graph plottables : the variable is null"); + return std::make_unique >(*dataSeries); } - - return result; } -QVector VisualizationGraphHelper::create(std::shared_ptr variable, - QCustomPlot &plot) noexcept -{ - auto result = QVector{}; +} // namespace +PlottablesMap VisualizationGraphHelper::create(std::shared_ptr variable, + QCustomPlot &plot) noexcept +{ if (variable) { - // Gets the data series of the variable to call the creation of the right components - // according to its type - if (auto scalarSeries = std::dynamic_pointer_cast(variable->dataSeries())) { - result.append(createScalarSeriesComponent(scalarSeries, plot, variable->range())); - } - else { - qCDebug(LOG_VisualizationGraphHelper()) - << QObject::tr("Can't create graph plottables : unmanaged data series type"); - } + auto helper = createHelper(variable->dataSeries().get()); + auto plottables = helper->create(plot); + return plottables; } else { qCDebug(LOG_VisualizationGraphHelper()) << QObject::tr("Can't create graph plottables : the variable is null"); + return PlottablesMap{}; } - - return result; } -void VisualizationGraphHelper::updateData(QVector plotableVect, - std::shared_ptr dataSeries, +void VisualizationGraphHelper::updateData(PlottablesMap &plottables, IDataSeries *dataSeries, const SqpRange &dateTime) { - if (auto scalarSeries = std::dynamic_pointer_cast(dataSeries)) { - if (plotableVect.size() == 1) { - updateScalarData(plotableVect.at(0), scalarSeries, dateTime); - } - else { - qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr( - "Can't update Data of a scalarSeries because there is not only one component " - "associated"); - } - } - else { - /// @todo DEBUG - } + auto helper = createHelper(dataSeries); + helper->update(plottables, dateTime); }