##// END OF EJS Templates
Handles rendering properties for spectrograms...
Alexandre Leroux -
r922:76b9deb45018
parent child
Show More
@@ -1,79 +1,120
1 1 #include "Visualization/PlottablesRenderingUtils.h"
2 2
3 3 #include <Common/ColorUtils.h>
4 4
5 5 #include <Data/ScalarSeries.h>
6 #include <Data/SpectrogramSeries.h>
6 7 #include <Data/VectorSeries.h>
7 8
8 9 #include <Visualization/qcustomplot.h>
9 10
10 11 namespace {
11 12
13 /// Default gradient used for colormap
14 const auto DEFAULT_COLORMAP_GRADIENT = QCPColorGradient::gpJet;
15
12 16 /**
13 17 * Delegate used to set plottables properties
14 18 */
15 19 template <typename T, typename Enabled = void>
16 20 struct PlottablesSetter {
17 21 static void setProperties(T &, PlottablesMap &)
18 22 {
19 23 // Default implementation does nothing
20 24 }
21 25 };
22 26
23 27 /**
24 28 * Specialization of PlottablesSetter for scalars and vectors
25 29 * @sa ScalarSeries
26 30 * @sa VectorSeries
27 31 */
28 32 template <typename T>
29 33 struct PlottablesSetter<T, typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
30 34 or std::is_base_of<VectorSeries, T>::value> > {
31 35 static void setProperties(T &dataSeries, PlottablesMap &plottables)
32 36 {
33 37 // Gets the number of components of the data series
34 38 dataSeries.lockRead();
35 39 auto componentCount = dataSeries.valuesData()->componentCount();
36 40 dataSeries.unlock();
37 41
38 42 // Generates colors for each component
39 43 auto colors = ColorUtils::colors(Qt::blue, Qt::red, componentCount);
40 44
41 45 // For each component of the data series, creates a QCPGraph to add to the plot
42 46 for (auto i = 0; i < componentCount; ++i) {
43 47 auto graph = plottables.at(i);
44 48 graph->setPen(QPen{colors.at(i)});
45 49 }
46 50 }
47 51 };
48 52
49 53 /**
54 * Specialization of PlottablesSetter for spectrograms
55 * @sa SpectrogramSeries
56 */
57 template <typename T>
58 struct PlottablesSetter<T,
59 typename std::enable_if_t<std::is_base_of<SpectrogramSeries, T>::value> > {
60 static void setProperties(T &, PlottablesMap &plottables)
61 {
62 // Checks that for a spectrogram there is only one plottable, that is a colormap
63 if (plottables.size() != 1) {
64 return;
65 }
66
67 if (auto colormap = dynamic_cast<QCPColorMap *>(plottables.begin()->second)) {
68 colormap->setInterpolate(false); // No value interpolation
69 colormap->setTightBoundary(true);
70
71 // Finds color scale in the colormap's plot to associate with it
72 auto plot = colormap->parentPlot();
73 auto plotElements = plot->plotLayout()->elements(false);
74 for (auto plotElement : plotElements) {
75 if (auto colorScale = dynamic_cast<QCPColorScale *>(plotElement)) {
76 colormap->setColorScale(colorScale);
77 }
78 }
79
80 // Sets gradient used for color scale
81 colormap->setGradient(DEFAULT_COLORMAP_GRADIENT);
82 colormap->rescaleDataRange();
83 }
84 }
85 };
86
87 /**
50 88 * Default implementation of IPlottablesHelper, which takes data series to set plottables properties
51 89 * @tparam T the data series' type
52 90 */
53 91 template <typename T>
54 92 struct PlottablesHelper : public IPlottablesHelper {
55 93 explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {}
56 94
57 95 void setProperties(PlottablesMap &plottables) override
58 96 {
59 97 PlottablesSetter<T>::setProperties(m_DataSeries, plottables);
60 98 }
61 99
62 100 T &m_DataSeries;
63 101 };
64 102
65 103 } // namespace
66 104
67 105 std::unique_ptr<IPlottablesHelper>
68 106 IPlottablesHelperFactory::create(std::shared_ptr<IDataSeries> dataSeries) noexcept
69 107 {
70 108 if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) {
71 109 return std::make_unique<PlottablesHelper<ScalarSeries> >(*scalarSeries);
72 110 }
111 else if (auto spectrogramSeries = std::dynamic_pointer_cast<SpectrogramSeries>(dataSeries)) {
112 return std::make_unique<PlottablesHelper<SpectrogramSeries> >(*spectrogramSeries);
113 }
73 114 else if (auto vectorSeries = std::dynamic_pointer_cast<VectorSeries>(dataSeries)) {
74 115 return std::make_unique<PlottablesHelper<VectorSeries> >(*vectorSeries);
75 116 }
76 117 else {
77 118 return std::make_unique<PlottablesHelper<IDataSeries> >(*dataSeries);
78 119 }
79 120 }
General Comments 0
You need to be logged in to leave comments. Login now