diff --git a/gui/include/Visualization/PlottablesRenderingUtils.h b/gui/include/Visualization/PlottablesRenderingUtils.h index 055a490..6c958d3 100644 --- a/gui/include/Visualization/PlottablesRenderingUtils.h +++ b/gui/include/Visualization/PlottablesRenderingUtils.h @@ -1,6 +1,8 @@ #ifndef SCIQLOP_PLOTTABLESRENDERINGUTILS_H #define SCIQLOP_PLOTTABLESRENDERINGUTILS_H +#include + #include #include @@ -9,9 +11,9 @@ Q_DECLARE_LOGGING_CATEGORY(LOG_PlottablesRenderingUtils) -class IDataSeries; class QCPColorScale; class QCustomPlot; +class Variable; /** * Helper used to handle plottables rendering @@ -25,9 +27,8 @@ struct IPlottablesHelper { }; struct IPlottablesHelperFactory { - /// Creates IPlottablesHelper 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_PLOTTABLESRENDERINGUTILS_H diff --git a/gui/src/Visualization/PlottablesRenderingUtils.cpp b/gui/src/Visualization/PlottablesRenderingUtils.cpp index e326eb1..b73fa91 100644 --- a/gui/src/Visualization/PlottablesRenderingUtils.cpp +++ b/gui/src/Visualization/PlottablesRenderingUtils.cpp @@ -6,6 +6,8 @@ #include #include +#include + #include Q_LOGGING_CATEGORY(LOG_PlottablesRenderingUtils, "PlottablesRenderingUtils") @@ -17,7 +19,7 @@ namespace { */ template struct PlottablesSetter { - static void setProperties(T &, PlottablesMap &) + static void setProperties(PlottablesMap &) { // Default implementation does nothing qCCritical(LOG_PlottablesRenderingUtils()) @@ -33,20 +35,25 @@ struct PlottablesSetter { template struct PlottablesSetter::value or std::is_base_of::value> > { - static void setProperties(T &dataSeries, PlottablesMap &plottables) + static void setProperties(PlottablesMap &plottables) { - // Gets the number of components of the data series - dataSeries.lockRead(); - auto componentCount = dataSeries.valuesData()->componentCount(); - dataSeries.unlock(); + // Finds the plottable with the highest index to determine the number of colors to generate + auto end = plottables.cend(); + auto maxPlottableIndexIt + = std::max_element(plottables.cbegin(), end, [](const auto &it1, const auto &it2) { + return it1.first < it2.first; + }); + auto componentCount = maxPlottableIndexIt != end ? maxPlottableIndexIt->first + 1 : 0; // Generates colors for each component auto colors = ColorUtils::colors(Qt::blue, Qt::red, 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 = plottables.at(i); - graph->setPen(QPen{colors.at(i)}); + auto graphIt = plottables.find(i); + if (graphIt != end) { + graphIt->second->setPen(QPen{colors.at(i)}); + } } } }; @@ -58,7 +65,7 @@ struct PlottablesSetter struct PlottablesSetter::value> > { - static void setProperties(T &, PlottablesMap &plottables) + static void setProperties(PlottablesMap &plottables) { // Checks that for a spectrogram there is only one plottable, that is a colormap if (plottables.size() != 1) { @@ -92,31 +99,28 @@ struct PlottablesSetter struct PlottablesHelper : public IPlottablesHelper { - explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {} - void setProperties(PlottablesMap &plottables) override { - PlottablesSetter::setProperties(m_DataSeries, plottables); + PlottablesSetter::setProperties(plottables); } - - T &m_DataSeries; }; } // namespace std::unique_ptr -IPlottablesHelperFactory::create(std::shared_ptr dataSeries) noexcept +IPlottablesHelperFactory::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 >(); + case DataSeriesType::SPECTROGRAM: + return std::make_unique >(); + case DataSeriesType::VECTOR: + return std::make_unique >(); + default: + // Returns default helper + break; } + + return std::make_unique >(); }