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