@@ -1,277 +1,325 | |||
|
1 | 1 | #include "Visualization/VisualizationGraphRenderingDelegate.h" |
|
2 | 2 | #include "Visualization/AxisRenderingUtils.h" |
|
3 | 3 | #include "Visualization/ColorScaleEditor.h" |
|
4 | 4 | #include "Visualization/PlottablesRenderingUtils.h" |
|
5 | 5 | #include "Visualization/SqpColorScale.h" |
|
6 | 6 | #include "Visualization/VisualizationGraphWidget.h" |
|
7 | 7 | #include "Visualization/qcustomplot.h" |
|
8 | 8 | |
|
9 | 9 | #include <Common/DateUtils.h> |
|
10 | 10 | |
|
11 | 11 | #include <Data/IDataSeries.h> |
|
12 | 12 | |
|
13 | 13 | #include <SqpApplication.h> |
|
14 | 14 | |
|
15 | 15 | namespace { |
|
16 | 16 | |
|
17 | 17 | /// Name of the axes layer in QCustomPlot |
|
18 | 18 | const auto AXES_LAYER = QStringLiteral("axes"); |
|
19 | 19 | |
|
20 | 20 | /// Icon used to show x-axis properties |
|
21 | 21 | const auto HIDE_AXIS_ICON_PATH = QStringLiteral(":/icones/down.png"); |
|
22 | 22 | |
|
23 | 23 | /// Name of the overlay layer in QCustomPlot |
|
24 | 24 | const auto OVERLAY_LAYER = QStringLiteral("overlay"); |
|
25 | 25 | |
|
26 | 26 | /// Pixmap used to show x-axis properties |
|
27 | 27 | const auto SHOW_AXIS_ICON_PATH = QStringLiteral(":/icones/up.png"); |
|
28 | 28 | |
|
29 | 29 | /// Tooltip format for graphs |
|
30 | 30 | const auto GRAPH_TOOLTIP_FORMAT = QStringLiteral("key: %1\nvalue: %2"); |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | /// Offset used to shift the tooltip of the mouse |
|
34 | 34 | const auto TOOLTIP_OFFSET = QPoint{20, 20}; |
|
35 | 35 | |
|
36 | 36 | /// Tooltip display rectangle (the tooltip is hidden when the mouse leaves this rectangle) |
|
37 | 37 | const auto TOOLTIP_RECT = QRect{10, 10, 10, 10}; |
|
38 | 38 | |
|
39 | 39 | /// Timeout after which the tooltip is displayed |
|
40 | 40 | const auto TOOLTIP_TIMEOUT = 500; |
|
41 | 41 | |
|
42 | 42 | void initPointTracerStyle(QCPItemTracer &tracer) noexcept |
|
43 | 43 | { |
|
44 | 44 | tracer.setInterpolating(false); |
|
45 | 45 | tracer.setStyle(QCPItemTracer::tsCircle); |
|
46 | 46 | tracer.setSize(3); |
|
47 | 47 | tracer.setPen(QPen(Qt::black)); |
|
48 | 48 | tracer.setBrush(Qt::black); |
|
49 | 49 | } |
|
50 | 50 | |
|
51 | 51 | QPixmap pixmap(const QString &iconPath) noexcept |
|
52 | 52 | { |
|
53 | 53 | return QIcon{iconPath}.pixmap(QSize{16, 16}); |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | void initClosePixmapStyle(QCPItemPixmap &pixmap) noexcept |
|
57 | 57 | { |
|
58 | 58 | // Icon |
|
59 | 59 | pixmap.setPixmap( |
|
60 | 60 | sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton).pixmap(QSize{16, 16})); |
|
61 | 61 | |
|
62 | 62 | // Position |
|
63 | 63 | pixmap.topLeft->setType(QCPItemPosition::ptAxisRectRatio); |
|
64 | 64 | pixmap.topLeft->setCoords(1, 0); |
|
65 | 65 | pixmap.setClipToAxisRect(false); |
|
66 | 66 | |
|
67 | 67 | // Can be selected |
|
68 | 68 | pixmap.setSelectable(true); |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | void initXAxisPixmapStyle(QCPItemPixmap &itemPixmap) noexcept |
|
72 | 72 | { |
|
73 | 73 | // Icon |
|
74 | 74 | itemPixmap.setPixmap(pixmap(HIDE_AXIS_ICON_PATH)); |
|
75 | 75 | |
|
76 | 76 | // Position |
|
77 | 77 | itemPixmap.topLeft->setType(QCPItemPosition::ptAxisRectRatio); |
|
78 | 78 | itemPixmap.topLeft->setCoords(0, 1); |
|
79 | 79 | itemPixmap.setClipToAxisRect(false); |
|
80 | 80 | |
|
81 | 81 | // Can be selected |
|
82 | 82 | itemPixmap.setSelectable(true); |
|
83 | 83 | } |
|
84 | 84 | |
|
85 | 85 | void initTitleTextStyle(QCPItemText &text) noexcept |
|
86 | 86 | { |
|
87 | 87 | // Font and background styles |
|
88 | 88 | text.setColor(Qt::gray); |
|
89 | 89 | text.setBrush(Qt::white); |
|
90 | 90 | |
|
91 | 91 | // Position |
|
92 | 92 | text.setPositionAlignment(Qt::AlignTop | Qt::AlignLeft); |
|
93 | 93 | text.position->setType(QCPItemPosition::ptAxisRectRatio); |
|
94 | 94 | text.position->setCoords(0.5, 0); |
|
95 | 95 | } |
|
96 | 96 | |
|
97 | /** | |
|
98 | * Returns the cell index (x or y) of a colormap according to the coordinate passed in parameter. | |
|
99 | * This method handles the fact that a colormap axis can be logarithmic or linear. | |
|
100 | * @param colormap the colormap for which to calculate the index | |
|
101 | * @param coord the coord to convert to cell index | |
|
102 | * @param xCoord calculates the x index if true, calculates y index if false | |
|
103 | * @return the cell index | |
|
104 | */ | |
|
105 | int colorMapCellIndex(const QCPColorMap &colormap, double coord, bool xCoord) | |
|
106 | { | |
|
107 | // Determines the axis of the colormap according to xCoord, and whether it is logarithmic or not | |
|
108 | auto isLogarithmic = (xCoord ? colormap.keyAxis() : colormap.valueAxis())->scaleType() | |
|
109 | == QCPAxis::stLogarithmic; | |
|
110 | ||
|
111 | if (isLogarithmic) { | |
|
112 | // For a logarithmic axis we can't use the conversion method of colormap, so we calculate | |
|
113 | // the index manually based on the position of the coordinate on the axis | |
|
114 | ||
|
115 | // Gets the axis range and the number of values between range bounds to calculate the step | |
|
116 | // between each value of the range | |
|
117 | auto range = xCoord ? colormap.data()->keyRange() : colormap.data()->valueRange(); | |
|
118 | auto nbValues = (xCoord ? colormap.data()->keySize() : colormap.data()->valueSize()) - 1; | |
|
119 | auto valueStep | |
|
120 | = (std::log10(range.upper) - std::log10(range.lower)) / static_cast<double>(nbValues); | |
|
121 | ||
|
122 | // According to the coord position, calculates the closest index in the range | |
|
123 | return std::round((std::log10(coord) - std::log10(range.lower)) / valueStep); | |
|
124 | } | |
|
125 | else { | |
|
126 | // For a linear axis, we use the conversion method of colormap | |
|
127 | int index; | |
|
128 | if (xCoord) { | |
|
129 | colormap.data()->coordToCell(coord, 0., &index, nullptr); | |
|
130 | } | |
|
131 | else { | |
|
132 | colormap.data()->coordToCell(0., coord, nullptr, &index); | |
|
133 | } | |
|
134 | ||
|
135 | return index; | |
|
136 | } | |
|
137 | } | |
|
138 | ||
|
97 | 139 | } // namespace |
|
98 | 140 | |
|
99 | 141 | struct VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegatePrivate { |
|
100 | 142 | explicit VisualizationGraphRenderingDelegatePrivate(VisualizationGraphWidget &graphWidget) |
|
101 | 143 | : m_Plot{graphWidget.plot()}, |
|
102 | 144 | m_PointTracer{new QCPItemTracer{&m_Plot}}, |
|
103 | 145 | m_TracerTimer{}, |
|
104 | 146 | m_ClosePixmap{new QCPItemPixmap{&m_Plot}}, |
|
105 | 147 | m_TitleText{new QCPItemText{&m_Plot}}, |
|
106 | 148 | m_XAxisPixmap{new QCPItemPixmap{&m_Plot}}, |
|
107 | 149 | m_ShowXAxis{true}, |
|
108 | 150 | m_XAxisLabel{}, |
|
109 | 151 | m_ColorScale{SqpColorScale{m_Plot}} |
|
110 | 152 | { |
|
111 | 153 | initPointTracerStyle(*m_PointTracer); |
|
112 | 154 | |
|
113 | 155 | m_TracerTimer.setInterval(TOOLTIP_TIMEOUT); |
|
114 | 156 | m_TracerTimer.setSingleShot(true); |
|
115 | 157 | |
|
116 | 158 | // Inits "close button" in plot overlay |
|
117 | 159 | m_ClosePixmap->setLayer(OVERLAY_LAYER); |
|
118 | 160 | initClosePixmapStyle(*m_ClosePixmap); |
|
119 | 161 | |
|
120 | 162 | // Connects pixmap selection to graph widget closing |
|
121 | 163 | QObject::connect(m_ClosePixmap, &QCPItemPixmap::selectionChanged, |
|
122 | 164 | [&graphWidget](bool selected) { |
|
123 | 165 | if (selected) { |
|
124 | 166 | graphWidget.close(); |
|
125 | 167 | } |
|
126 | 168 | }); |
|
127 | 169 | |
|
128 | 170 | // Inits graph name in plot overlay |
|
129 | 171 | m_TitleText->setLayer(OVERLAY_LAYER); |
|
130 | 172 | m_TitleText->setText(graphWidget.name()); |
|
131 | 173 | initTitleTextStyle(*m_TitleText); |
|
132 | 174 | |
|
133 | 175 | // Inits "show x-axis button" in plot overlay |
|
134 | 176 | m_XAxisPixmap->setLayer(OVERLAY_LAYER); |
|
135 | 177 | initXAxisPixmapStyle(*m_XAxisPixmap); |
|
136 | 178 | |
|
137 | 179 | // Connects pixmap selection to graph x-axis showing/hiding |
|
138 | 180 | QObject::connect(m_XAxisPixmap, &QCPItemPixmap::selectionChanged, [this]() { |
|
139 | 181 | if (m_XAxisPixmap->selected()) { |
|
140 | 182 | // Changes the selection state and refreshes the x-axis |
|
141 | 183 | m_ShowXAxis = !m_ShowXAxis; |
|
142 | 184 | updateXAxisState(); |
|
143 | 185 | m_Plot.layer(AXES_LAYER)->replot(); |
|
144 | 186 | |
|
145 | 187 | // Deselects the x-axis pixmap and updates icon |
|
146 | 188 | m_XAxisPixmap->setSelected(false); |
|
147 | 189 | m_XAxisPixmap->setPixmap( |
|
148 | 190 | pixmap(m_ShowXAxis ? HIDE_AXIS_ICON_PATH : SHOW_AXIS_ICON_PATH)); |
|
149 | 191 | m_Plot.layer(OVERLAY_LAYER)->replot(); |
|
150 | 192 | } |
|
151 | 193 | }); |
|
152 | 194 | } |
|
153 | 195 | |
|
154 | 196 | /// Updates state of x-axis according to the current selection of x-axis pixmap |
|
155 | 197 | /// @remarks the method doesn't call plot refresh |
|
156 | 198 | void updateXAxisState() noexcept |
|
157 | 199 | { |
|
158 | 200 | m_Plot.xAxis->setTickLabels(m_ShowXAxis); |
|
159 | 201 | m_Plot.xAxis->setLabel(m_ShowXAxis ? m_XAxisLabel : QString{}); |
|
160 | 202 | } |
|
161 | 203 | |
|
162 | 204 | QCustomPlot &m_Plot; |
|
163 | 205 | QCPItemTracer *m_PointTracer; |
|
164 | 206 | QTimer m_TracerTimer; |
|
165 | 207 | QCPItemPixmap *m_ClosePixmap; /// Graph's close button |
|
166 | 208 | QCPItemText *m_TitleText; /// Graph's title |
|
167 | 209 | QCPItemPixmap *m_XAxisPixmap; |
|
168 | 210 | bool m_ShowXAxis; /// X-axis properties are shown or hidden |
|
169 | 211 | QString m_XAxisLabel; |
|
170 | 212 | SqpColorScale m_ColorScale; /// Color scale used for some types of graphs (as spectrograms) |
|
171 | 213 | }; |
|
172 | 214 | |
|
173 | 215 | VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegate( |
|
174 | 216 | VisualizationGraphWidget &graphWidget) |
|
175 | 217 | : impl{spimpl::make_unique_impl<VisualizationGraphRenderingDelegatePrivate>(graphWidget)} |
|
176 | 218 | { |
|
177 | 219 | } |
|
178 | 220 | |
|
179 | 221 | void VisualizationGraphRenderingDelegate::onMouseDoubleClick(QMouseEvent *event) noexcept |
|
180 | 222 | { |
|
181 | 223 | // Opens color scale editor if color scale is double clicked |
|
182 | 224 | auto colorScale = dynamic_cast<QCPColorScale *>(impl->m_Plot.layoutElementAt(event->pos())); |
|
183 | 225 | if (impl->m_ColorScale.m_Scale == colorScale) { |
|
184 | 226 | if (ColorScaleEditor{impl->m_ColorScale}.exec() == QDialog::Accepted) { |
|
185 | 227 | impl->m_Plot.replot(); |
|
186 | 228 | } |
|
187 | 229 | } |
|
188 | 230 | } |
|
189 | 231 | |
|
190 | 232 | void VisualizationGraphRenderingDelegate::onMouseMove(QMouseEvent *event) noexcept |
|
191 | 233 | { |
|
192 | 234 | // Cancels pending refresh |
|
193 | 235 | impl->m_TracerTimer.disconnect(); |
|
194 | 236 | |
|
195 | 237 | // Reinits tracers |
|
196 | 238 | impl->m_PointTracer->setGraph(nullptr); |
|
197 | 239 | impl->m_PointTracer->setVisible(false); |
|
198 | 240 | impl->m_Plot.replot(); |
|
199 | 241 | |
|
200 | 242 | QString tooltip{}; |
|
201 | 243 | |
|
202 | 244 | // Gets the graph under the mouse position |
|
203 | 245 | auto eventPos = event->pos(); |
|
204 | 246 | if (auto graph = qobject_cast<QCPGraph *>(impl->m_Plot.plottableAt(eventPos))) { |
|
205 | 247 | auto mouseKey = graph->keyAxis()->pixelToCoord(eventPos.x()); |
|
206 | 248 | auto graphData = graph->data(); |
|
207 | 249 | |
|
208 | 250 | // Gets the closest data point to the mouse |
|
209 | 251 | auto graphDataIt = graphData->findBegin(mouseKey); |
|
210 | 252 | if (graphDataIt != graphData->constEnd()) { |
|
211 | 253 | // Sets tooltip |
|
212 | 254 | auto key = formatValue(graphDataIt->key, *graph->keyAxis()); |
|
213 | 255 | auto value = formatValue(graphDataIt->value, *graph->valueAxis()); |
|
214 | 256 | tooltip = GRAPH_TOOLTIP_FORMAT.arg(key, value); |
|
215 | 257 | |
|
216 | 258 | // Displays point tracer |
|
217 | 259 | impl->m_PointTracer->setGraph(graph); |
|
218 | 260 | impl->m_PointTracer->setGraphKey(graphDataIt->key); |
|
219 | 261 | impl->m_PointTracer->setLayer( |
|
220 | 262 | impl->m_Plot.layer("main")); // Tracer is set on top of the plot's main layer |
|
221 | 263 | impl->m_PointTracer->setVisible(true); |
|
222 | 264 | impl->m_Plot.replot(); |
|
223 | 265 | } |
|
224 | 266 | } |
|
225 | 267 | else if (auto colorMap = qobject_cast<QCPColorMap *>(impl->m_Plot.plottableAt(eventPos))) { |
|
226 | 268 | // Gets x and y coords |
|
227 | 269 | auto x = colorMap->keyAxis()->pixelToCoord(eventPos.x()); |
|
228 | 270 | auto y = colorMap->valueAxis()->pixelToCoord(eventPos.y()); |
|
271 | ||
|
272 | // Calculates x and y cell indexes, and retrieves the underlying value | |
|
273 | auto xCellIndex = colorMapCellIndex(*colorMap, x, true); | |
|
274 | auto yCellIndex = colorMapCellIndex(*colorMap, y, false); | |
|
275 | auto value = colorMap->data()->cell(xCellIndex, yCellIndex); | |
|
276 | ||
|
229 | 277 | } |
|
230 | 278 | |
|
231 | 279 | if (!tooltip.isEmpty()) { |
|
232 | 280 | // Starts timer to show tooltip after timeout |
|
233 | 281 | auto showTooltip = [tooltip, eventPos, this]() { |
|
234 | 282 | QToolTip::showText(impl->m_Plot.mapToGlobal(eventPos) + TOOLTIP_OFFSET, tooltip, |
|
235 | 283 | &impl->m_Plot, TOOLTIP_RECT); |
|
236 | 284 | }; |
|
237 | 285 | |
|
238 | 286 | QObject::connect(&impl->m_TracerTimer, &QTimer::timeout, showTooltip); |
|
239 | 287 | impl->m_TracerTimer.start(); |
|
240 | 288 | } |
|
241 | 289 | } |
|
242 | 290 | |
|
243 | 291 | void VisualizationGraphRenderingDelegate::onPlotUpdated() noexcept |
|
244 | 292 | { |
|
245 | 293 | // Updates color scale bounds |
|
246 | 294 | impl->m_ColorScale.updateDataRange(); |
|
247 | 295 | impl->m_Plot.replot(); |
|
248 | 296 | } |
|
249 | 297 | |
|
250 | 298 | void VisualizationGraphRenderingDelegate::setAxesProperties( |
|
251 | 299 | std::shared_ptr<IDataSeries> dataSeries) noexcept |
|
252 | 300 | { |
|
253 | 301 | // Stores x-axis label to be able to retrieve it when x-axis pixmap is unselected |
|
254 | 302 | impl->m_XAxisLabel = dataSeries->xAxisUnit().m_Name; |
|
255 | 303 | |
|
256 | 304 | auto axisHelper = IAxisHelperFactory::create(dataSeries); |
|
257 | 305 | axisHelper->setProperties(impl->m_Plot, impl->m_ColorScale); |
|
258 | 306 | |
|
259 | 307 | // Updates x-axis state |
|
260 | 308 | impl->updateXAxisState(); |
|
261 | 309 | |
|
262 | 310 | impl->m_Plot.layer(AXES_LAYER)->replot(); |
|
263 | 311 | } |
|
264 | 312 | |
|
265 | 313 | void VisualizationGraphRenderingDelegate::setPlottablesProperties( |
|
266 | 314 | std::shared_ptr<IDataSeries> dataSeries, PlottablesMap &plottables) noexcept |
|
267 | 315 | { |
|
268 | 316 | auto plottablesHelper = IPlottablesHelperFactory::create(dataSeries); |
|
269 | 317 | plottablesHelper->setProperties(plottables); |
|
270 | 318 | } |
|
271 | 319 | |
|
272 | 320 | void VisualizationGraphRenderingDelegate::showGraphOverlay(bool show) noexcept |
|
273 | 321 | { |
|
274 | 322 | auto overlay = impl->m_Plot.layer(OVERLAY_LAYER); |
|
275 | 323 | overlay->setVisible(show); |
|
276 | 324 | overlay->replot(); |
|
277 | 325 | } |
General Comments 0
You need to be logged in to leave comments.
Login now