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