@@ -1,172 +1,174 | |||
|
1 | 1 | #include "Visualization/AxisRenderingUtils.h" |
|
2 | 2 | |
|
3 | 3 | #include <Data/ScalarSeries.h> |
|
4 | 4 | #include <Data/SpectrogramSeries.h> |
|
5 | 5 | #include <Data/VectorSeries.h> |
|
6 | 6 | |
|
7 | 7 | #include <Visualization/qcustomplot.h> |
|
8 | 8 | |
|
9 | 9 | Q_LOGGING_CATEGORY(LOG_AxisRenderingUtils, "AxisRenderingUtils") |
|
10 | 10 | |
|
11 | 11 | namespace { |
|
12 | 12 | |
|
13 | 13 | const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss:zzz"); |
|
14 | 14 | |
|
15 | 15 | /// Format for datetimes on a axis |
|
16 | 16 | const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss"); |
|
17 | 17 | |
|
18 | 18 | /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or |
|
19 | 19 | /// non-time data |
|
20 | 20 | QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis, QCPAxis::ScaleType scaleType) |
|
21 | 21 | { |
|
22 | 22 | if (isTimeAxis) { |
|
23 | 23 | auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create(); |
|
24 | 24 | dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT); |
|
25 | 25 | dateTicker->setDateTimeSpec(Qt::UTC); |
|
26 | 26 | |
|
27 | 27 | return dateTicker; |
|
28 | 28 | } |
|
29 | 29 | else if (scaleType == QCPAxis::stLogarithmic) { |
|
30 | 30 | return QSharedPointer<QCPAxisTickerLog>::create(); |
|
31 | 31 | } |
|
32 | 32 | else { |
|
33 | 33 | // default ticker |
|
34 | 34 | return QSharedPointer<QCPAxisTicker>::create(); |
|
35 | 35 | } |
|
36 | 36 | } |
|
37 | 37 | |
|
38 | 38 | /** |
|
39 | 39 | * Sets properties of the axis passed as parameter |
|
40 | 40 | * @param axis the axis to set |
|
41 | 41 | * @param unit the unit to set for the axis |
|
42 | 42 | * @param scaleType the scale type to set for the axis |
|
43 | 43 | */ |
|
44 | 44 | void setAxisProperties(QCPAxis &axis, const Unit &unit, |
|
45 | 45 | QCPAxis::ScaleType scaleType = QCPAxis::stLinear) |
|
46 | 46 | { |
|
47 | 47 | // label (unit name) |
|
48 | 48 | axis.setLabel(unit.m_Name); |
|
49 | 49 | |
|
50 | 50 | // scale type |
|
51 | 51 | axis.setScaleType(scaleType); |
|
52 | 52 | if (scaleType == QCPAxis::stLogarithmic) { |
|
53 | 53 | // Scientific notation |
|
54 | 54 | axis.setNumberPrecision(0); |
|
55 | 55 | axis.setNumberFormat("eb"); |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | // ticker (depending on the type of unit) |
|
59 | 59 | axis.setTicker(axisTicker(unit.m_TimeUnit, scaleType)); |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | /** |
|
63 | 63 | * Delegate used to set axes properties |
|
64 | 64 | */ |
|
65 | 65 | template <typename T, typename Enabled = void> |
|
66 | 66 | struct AxisSetter { |
|
67 | 67 | static void setProperties(T &, QCustomPlot &, QCPColorScale &) |
|
68 | 68 | { |
|
69 | 69 | // Default implementation does nothing |
|
70 | 70 | qCCritical(LOG_AxisRenderingUtils()) << "Can't set axis properties: unmanaged type of data"; |
|
71 | 71 | } |
|
72 | 72 | }; |
|
73 | 73 | |
|
74 | 74 | /** |
|
75 | 75 | * Specialization of AxisSetter for scalars and vectors |
|
76 | 76 | * @sa ScalarSeries |
|
77 | 77 | * @sa VectorSeries |
|
78 | 78 | */ |
|
79 | 79 | template <typename T> |
|
80 | 80 | struct AxisSetter<T, typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value |
|
81 | 81 | or std::is_base_of<VectorSeries, T>::value> > { |
|
82 | 82 | static void setProperties(T &dataSeries, QCustomPlot &plot, QCPColorScale &) |
|
83 | 83 | { |
|
84 | 84 | dataSeries.lockRead(); |
|
85 | 85 | auto xAxisUnit = dataSeries.xAxisUnit(); |
|
86 | 86 | auto valuesUnit = dataSeries.valuesUnit(); |
|
87 | 87 | dataSeries.unlock(); |
|
88 | 88 | |
|
89 | 89 | setAxisProperties(*plot.xAxis, xAxisUnit); |
|
90 | 90 | setAxisProperties(*plot.yAxis, valuesUnit); |
|
91 | 91 | } |
|
92 | 92 | }; |
|
93 | 93 | |
|
94 | 94 | /** |
|
95 | 95 | * Specialization of AxisSetter for spectrograms |
|
96 | 96 | * @sa SpectrogramSeries |
|
97 | 97 | */ |
|
98 | 98 | template <typename T> |
|
99 | 99 | struct AxisSetter<T, typename std::enable_if_t<std::is_base_of<SpectrogramSeries, T>::value> > { |
|
100 | 100 | static void setProperties(T &dataSeries, QCustomPlot &plot, QCPColorScale &colorScale) |
|
101 | 101 | { |
|
102 | 102 | dataSeries.lockRead(); |
|
103 | 103 | auto xAxisUnit = dataSeries.xAxisUnit(); |
|
104 | 104 | auto yAxisUnit = dataSeries.yAxisUnit(); |
|
105 | 105 | auto valuesUnit = dataSeries.valuesUnit(); |
|
106 | 106 | dataSeries.unlock(); |
|
107 | 107 | |
|
108 | 108 | setAxisProperties(*plot.xAxis, xAxisUnit); |
|
109 | 109 | setAxisProperties(*plot.yAxis, yAxisUnit, QCPAxis::stLogarithmic); |
|
110 | 110 | |
|
111 | 111 | // Displays color scale in plot |
|
112 | 112 | plot.plotLayout()->insertRow(0); |
|
113 | 113 | plot.plotLayout()->addElement(0, 0, &colorScale); |
|
114 | 114 | colorScale.setType(QCPAxis::atTop); |
|
115 | 115 | colorScale.setMinimumMargins(QMargins{0, 0, 0, 0}); |
|
116 | 116 | |
|
117 | 117 | // Aligns color scale with axes |
|
118 | 118 | auto marginGroups = plot.axisRect()->marginGroups(); |
|
119 | 119 | for (auto it = marginGroups.begin(), end = marginGroups.end(); it != end; ++it) { |
|
120 | 120 | colorScale.setMarginGroup(it.key(), it.value()); |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | // Set color scale properties |
|
124 | 124 | setAxisProperties(*colorScale.axis(), valuesUnit, QCPAxis::stLogarithmic); |
|
125 | /// @todo ALX: temp data range, remove it when widget to set data range is implemented | |
|
126 | colorScale.setDataRange(QCPRange{8.32e2, 1.77e7}); | |
|
125 | 127 | } |
|
126 | 128 | }; |
|
127 | 129 | |
|
128 | 130 | /** |
|
129 | 131 | * Default implementation of IAxisHelper, which takes data series to set axes properties |
|
130 | 132 | * @tparam T the data series' type |
|
131 | 133 | */ |
|
132 | 134 | template <typename T> |
|
133 | 135 | struct AxisHelper : public IAxisHelper { |
|
134 | 136 | explicit AxisHelper(T &dataSeries) : m_DataSeries{dataSeries} {} |
|
135 | 137 | |
|
136 | 138 | void setProperties(QCustomPlot &plot, QCPColorScale &colorScale) override |
|
137 | 139 | { |
|
138 | 140 | AxisSetter<T>::setProperties(m_DataSeries, plot, colorScale); |
|
139 | 141 | } |
|
140 | 142 | |
|
141 | 143 | T &m_DataSeries; |
|
142 | 144 | }; |
|
143 | 145 | |
|
144 | 146 | } // namespace |
|
145 | 147 | |
|
146 | 148 | QString formatValue(double value, const QCPAxis &axis) |
|
147 | 149 | { |
|
148 | 150 | // If the axis is a time axis, formats the value as a date |
|
149 | 151 | if (auto axisTicker = qSharedPointerDynamicCast<QCPAxisTickerDateTime>(axis.ticker())) { |
|
150 | 152 | return DateUtils::dateTime(value, axisTicker->dateTimeSpec()).toString(DATETIME_FORMAT); |
|
151 | 153 | } |
|
152 | 154 | else { |
|
153 | 155 | return QString::number(value); |
|
154 | 156 | } |
|
155 | 157 | } |
|
156 | 158 | |
|
157 | 159 | std::unique_ptr<IAxisHelper> |
|
158 | 160 | IAxisHelperFactory::create(std::shared_ptr<IDataSeries> dataSeries) noexcept |
|
159 | 161 | { |
|
160 | 162 | if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) { |
|
161 | 163 | return std::make_unique<AxisHelper<ScalarSeries> >(*scalarSeries); |
|
162 | 164 | } |
|
163 | 165 | else if (auto spectrogramSeries = std::dynamic_pointer_cast<SpectrogramSeries>(dataSeries)) { |
|
164 | 166 | return std::make_unique<AxisHelper<SpectrogramSeries> >(*spectrogramSeries); |
|
165 | 167 | } |
|
166 | 168 | else if (auto vectorSeries = std::dynamic_pointer_cast<VectorSeries>(dataSeries)) { |
|
167 | 169 | return std::make_unique<AxisHelper<VectorSeries> >(*vectorSeries); |
|
168 | 170 | } |
|
169 | 171 | else { |
|
170 | 172 | return std::make_unique<AxisHelper<IDataSeries> >(*dataSeries); |
|
171 | 173 | } |
|
172 | 174 | } |
General Comments 0
You need to be logged in to leave comments.
Login now