##// END OF EJS Templates
Updates PlottablesRenderingUtils to use variable's type instead of dataseries
Alexandre Leroux -
r1307:a119058eb032
parent child
Show More
@@ -1,33 +1,34
1 #ifndef SCIQLOP_PLOTTABLESRENDERINGUTILS_H
1 #ifndef SCIQLOP_PLOTTABLESRENDERINGUTILS_H
2 #define SCIQLOP_PLOTTABLESRENDERINGUTILS_H
2 #define SCIQLOP_PLOTTABLESRENDERINGUTILS_H
3
3
4 #include <Data/DataSeriesType.h>
5
4 #include <Visualization/VisualizationDefs.h>
6 #include <Visualization/VisualizationDefs.h>
5
7
6 #include <memory>
8 #include <memory>
7
9
8 #include <QtCore/QLoggingCategory>
10 #include <QtCore/QLoggingCategory>
9
11
10 Q_DECLARE_LOGGING_CATEGORY(LOG_PlottablesRenderingUtils)
12 Q_DECLARE_LOGGING_CATEGORY(LOG_PlottablesRenderingUtils)
11
13
12 class IDataSeries;
13 class QCPColorScale;
14 class QCPColorScale;
14 class QCustomPlot;
15 class QCustomPlot;
16 class Variable;
15
17
16 /**
18 /**
17 * Helper used to handle plottables rendering
19 * Helper used to handle plottables rendering
18 */
20 */
19 struct IPlottablesHelper {
21 struct IPlottablesHelper {
20 virtual ~IPlottablesHelper() noexcept = default;
22 virtual ~IPlottablesHelper() noexcept = default;
21
23
22 /// Set properties of the plottables passed as parameter
24 /// Set properties of the plottables passed as parameter
23 /// @param plottables the plottables for which to set properties
25 /// @param plottables the plottables for which to set properties
24 virtual void setProperties(PlottablesMap &plottables) = 0;
26 virtual void setProperties(PlottablesMap &plottables) = 0;
25 };
27 };
26
28
27 struct IPlottablesHelperFactory {
29 struct IPlottablesHelperFactory {
28 /// Creates IPlottablesHelper according to a data series
30 /// Creates IPlottablesHelper according to the type of data series a variable holds
29 static std::unique_ptr<IPlottablesHelper>
31 static std::unique_ptr<IPlottablesHelper> create(const Variable &variable) noexcept;
30 create(std::shared_ptr<IDataSeries> dataSeries) noexcept;
31 };
32 };
32
33
33 #endif // SCIQLOP_PLOTTABLESRENDERINGUTILS_H
34 #endif // SCIQLOP_PLOTTABLESRENDERINGUTILS_H
@@ -1,122 +1,126
1 #include "Visualization/PlottablesRenderingUtils.h"
1 #include "Visualization/PlottablesRenderingUtils.h"
2
2
3 #include <Common/ColorUtils.h>
3 #include <Common/ColorUtils.h>
4
4
5 #include <Data/ScalarSeries.h>
5 #include <Data/ScalarSeries.h>
6 #include <Data/SpectrogramSeries.h>
6 #include <Data/SpectrogramSeries.h>
7 #include <Data/VectorSeries.h>
7 #include <Data/VectorSeries.h>
8
8
9 #include <Variable/Variable.h>
10
9 #include <Visualization/qcustomplot.h>
11 #include <Visualization/qcustomplot.h>
10
12
11 Q_LOGGING_CATEGORY(LOG_PlottablesRenderingUtils, "PlottablesRenderingUtils")
13 Q_LOGGING_CATEGORY(LOG_PlottablesRenderingUtils, "PlottablesRenderingUtils")
12
14
13 namespace {
15 namespace {
14
16
15 /**
17 /**
16 * Delegate used to set plottables properties
18 * Delegate used to set plottables properties
17 */
19 */
18 template <typename T, typename Enabled = void>
20 template <typename T, typename Enabled = void>
19 struct PlottablesSetter {
21 struct PlottablesSetter {
20 static void setProperties(T &, PlottablesMap &)
22 static void setProperties(PlottablesMap &)
21 {
23 {
22 // Default implementation does nothing
24 // Default implementation does nothing
23 qCCritical(LOG_PlottablesRenderingUtils())
25 qCCritical(LOG_PlottablesRenderingUtils())
24 << "Can't set plottables properties: unmanaged type of data";
26 << "Can't set plottables properties: unmanaged type of data";
25 }
27 }
26 };
28 };
27
29
28 /**
30 /**
29 * Specialization of PlottablesSetter for scalars and vectors
31 * Specialization of PlottablesSetter for scalars and vectors
30 * @sa ScalarSeries
32 * @sa ScalarSeries
31 * @sa VectorSeries
33 * @sa VectorSeries
32 */
34 */
33 template <typename T>
35 template <typename T>
34 struct PlottablesSetter<T, typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
36 struct PlottablesSetter<T, typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
35 or std::is_base_of<VectorSeries, T>::value> > {
37 or std::is_base_of<VectorSeries, T>::value> > {
36 static void setProperties(T &dataSeries, PlottablesMap &plottables)
38 static void setProperties(PlottablesMap &plottables)
37 {
39 {
38 // Gets the number of components of the data series
40 // Finds the plottable with the highest index to determine the number of colors to generate
39 dataSeries.lockRead();
41 auto end = plottables.cend();
40 auto componentCount = dataSeries.valuesData()->componentCount();
42 auto maxPlottableIndexIt
41 dataSeries.unlock();
43 = std::max_element(plottables.cbegin(), end, [](const auto &it1, const auto &it2) {
44 return it1.first < it2.first;
45 });
46 auto componentCount = maxPlottableIndexIt != end ? maxPlottableIndexIt->first + 1 : 0;
42
47
43 // Generates colors for each component
48 // Generates colors for each component
44 auto colors = ColorUtils::colors(Qt::blue, Qt::red, componentCount);
49 auto colors = ColorUtils::colors(Qt::blue, Qt::red, componentCount);
45
50
46 // For each component of the data series, creates a QCPGraph to add to the plot
51 // For each component of the data series, creates a QCPGraph to add to the plot
47 for (auto i = 0; i < componentCount; ++i) {
52 for (auto i = 0; i < componentCount; ++i) {
48 auto graph = plottables.at(i);
53 auto graphIt = plottables.find(i);
49 graph->setPen(QPen{colors.at(i)});
54 if (graphIt != end) {
55 graphIt->second->setPen(QPen{colors.at(i)});
56 }
50 }
57 }
51 }
58 }
52 };
59 };
53
60
54 /**
61 /**
55 * Specialization of PlottablesSetter for spectrograms
62 * Specialization of PlottablesSetter for spectrograms
56 * @sa SpectrogramSeries
63 * @sa SpectrogramSeries
57 */
64 */
58 template <typename T>
65 template <typename T>
59 struct PlottablesSetter<T,
66 struct PlottablesSetter<T,
60 typename std::enable_if_t<std::is_base_of<SpectrogramSeries, T>::value> > {
67 typename std::enable_if_t<std::is_base_of<SpectrogramSeries, T>::value> > {
61 static void setProperties(T &, PlottablesMap &plottables)
68 static void setProperties(PlottablesMap &plottables)
62 {
69 {
63 // Checks that for a spectrogram there is only one plottable, that is a colormap
70 // Checks that for a spectrogram there is only one plottable, that is a colormap
64 if (plottables.size() != 1) {
71 if (plottables.size() != 1) {
65 return;
72 return;
66 }
73 }
67
74
68 if (auto colormap = dynamic_cast<QCPColorMap *>(plottables.begin()->second)) {
75 if (auto colormap = dynamic_cast<QCPColorMap *>(plottables.begin()->second)) {
69 colormap->setInterpolate(false); // No value interpolation
76 colormap->setInterpolate(false); // No value interpolation
70 colormap->setTightBoundary(true);
77 colormap->setTightBoundary(true);
71
78
72 // Finds color scale in the colormap's plot to associate with it
79 // Finds color scale in the colormap's plot to associate with it
73 auto plot = colormap->parentPlot();
80 auto plot = colormap->parentPlot();
74 auto plotElements = plot->plotLayout()->elements(false);
81 auto plotElements = plot->plotLayout()->elements(false);
75 for (auto plotElement : plotElements) {
82 for (auto plotElement : plotElements) {
76 if (auto colorScale = dynamic_cast<QCPColorScale *>(plotElement)) {
83 if (auto colorScale = dynamic_cast<QCPColorScale *>(plotElement)) {
77 colormap->setColorScale(colorScale);
84 colormap->setColorScale(colorScale);
78 }
85 }
79 }
86 }
80
87
81 colormap->rescaleDataRange();
88 colormap->rescaleDataRange();
82 }
89 }
83 else {
90 else {
84 qCCritical(LOG_PlottablesRenderingUtils()) << "Can't get colormap of the spectrogram";
91 qCCritical(LOG_PlottablesRenderingUtils()) << "Can't get colormap of the spectrogram";
85 }
92 }
86 }
93 }
87 };
94 };
88
95
89 /**
96 /**
90 * Default implementation of IPlottablesHelper, which takes data series to set plottables properties
97 * Default implementation of IPlottablesHelper, which takes data series to set plottables properties
91 * @tparam T the data series' type
98 * @tparam T the data series' type
92 */
99 */
93 template <typename T>
100 template <typename T>
94 struct PlottablesHelper : public IPlottablesHelper {
101 struct PlottablesHelper : public IPlottablesHelper {
95 explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {}
96
97 void setProperties(PlottablesMap &plottables) override
102 void setProperties(PlottablesMap &plottables) override
98 {
103 {
99 PlottablesSetter<T>::setProperties(m_DataSeries, plottables);
104 PlottablesSetter<T>::setProperties(plottables);
100 }
105 }
101
102 T &m_DataSeries;
103 };
106 };
104
107
105 } // namespace
108 } // namespace
106
109
107 std::unique_ptr<IPlottablesHelper>
110 std::unique_ptr<IPlottablesHelper>
108 IPlottablesHelperFactory::create(std::shared_ptr<IDataSeries> dataSeries) noexcept
111 IPlottablesHelperFactory::create(const Variable &variable) noexcept
109 {
112 {
110 if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) {
113 switch (variable.type()) {
111 return std::make_unique<PlottablesHelper<ScalarSeries> >(*scalarSeries);
114 case DataSeriesType::SCALAR:
112 }
115 return std::make_unique<PlottablesHelper<ScalarSeries> >();
113 else if (auto spectrogramSeries = std::dynamic_pointer_cast<SpectrogramSeries>(dataSeries)) {
116 case DataSeriesType::SPECTROGRAM:
114 return std::make_unique<PlottablesHelper<SpectrogramSeries> >(*spectrogramSeries);
117 return std::make_unique<PlottablesHelper<SpectrogramSeries> >();
115 }
118 case DataSeriesType::VECTOR:
116 else if (auto vectorSeries = std::dynamic_pointer_cast<VectorSeries>(dataSeries)) {
119 return std::make_unique<PlottablesHelper<VectorSeries> >();
117 return std::make_unique<PlottablesHelper<VectorSeries> >(*vectorSeries);
120 default:
118 }
121 // Returns default helper
119 else {
122 break;
120 return std::make_unique<PlottablesHelper<IDataSeries> >(*dataSeries);
121 }
123 }
124
125 return std::make_unique<PlottablesHelper<IDataSeries> >();
122 }
126 }
General Comments 0
You need to be logged in to leave comments. Login now