@@ -1,76 +1,97 | |||
|
1 | 1 | #include "Visualization/VisualizationGraphRenderingDelegate.h" |
|
2 | 2 | #include "Visualization/qcustomplot.h" |
|
3 | 3 | |
|
4 | 4 | namespace { |
|
5 | 5 | |
|
6 | 6 | const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss:zzz"); |
|
7 | 7 | |
|
8 | 8 | const auto TEXT_TRACER_FORMAT = QStringLiteral("key: %1\nvalue: %2"); |
|
9 | 9 | |
|
10 | 10 | /// Timeout after which a tracer is displayed |
|
11 | 11 | const auto TRACER_TIMEOUT = 500; |
|
12 | 12 | |
|
13 | 13 | /// Formats a data value according to the axis on which it is present |
|
14 | 14 | QString formatValue(double value, const QCPAxis &axis) |
|
15 | 15 | { |
|
16 | 16 | // If the axis is a time axis, formats the value as a date |
|
17 | 17 | return qSharedPointerDynamicCast<QCPAxisTickerDateTime>(axis.ticker()) |
|
18 | 18 | ? QCPAxisTickerDateTime::keyToDateTime(value).toString(DATETIME_FORMAT) |
|
19 | 19 | : QString::number(value); |
|
20 | 20 | } |
|
21 | 21 | } // namespace |
|
22 | 22 | |
|
23 | 23 | struct VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegatePrivate { |
|
24 | 24 | explicit VisualizationGraphRenderingDelegatePrivate(QCustomPlot &plot) |
|
25 | 25 | : m_Plot{plot}, |
|
26 | 26 | m_PointTracer{new QCPItemTracer{&plot}}, |
|
27 | 27 | m_TextTracer{new QCPItemText{&plot}}, |
|
28 | 28 | m_TracerTimer{} |
|
29 | 29 | { |
|
30 | 30 | |
|
31 | 31 | m_TracerTimer.setInterval(TRACER_TIMEOUT); |
|
32 | 32 | m_TracerTimer.setSingleShot(true); |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | 35 | QCustomPlot &m_Plot; |
|
36 | 36 | QCPItemTracer *m_PointTracer; |
|
37 | 37 | QCPItemText *m_TextTracer; |
|
38 | 38 | QTimer m_TracerTimer; |
|
39 | 39 | }; |
|
40 | 40 | |
|
41 | 41 | VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegate(QCustomPlot &plot) |
|
42 | 42 | : impl{spimpl::make_unique_impl<VisualizationGraphRenderingDelegatePrivate>(plot)} |
|
43 | 43 | { |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | void VisualizationGraphRenderingDelegate::onMouseMove(QMouseEvent *event) noexcept |
|
47 | 47 | { |
|
48 | 48 | // Cancels pending refresh |
|
49 | 49 | impl->m_TracerTimer.disconnect(); |
|
50 | 50 | |
|
51 | 51 | auto showTracers = [ eventPos = event->pos(), this ]() |
|
52 | 52 | { |
|
53 | // Lambda function to display a tracer | |
|
54 | auto displayTracer = [this](auto &tracer) { | |
|
55 | // Tracer is set on top of the plot's main layer | |
|
56 | tracer.setLayer(impl->m_Plot.layer("main")); | |
|
57 | tracer.setVisible(true); | |
|
58 | impl->m_Plot.replot(); | |
|
59 | }; | |
|
60 | ||
|
53 | 61 | // Reinits tracers |
|
54 | 62 | impl->m_PointTracer->setGraph(nullptr); |
|
55 | 63 | impl->m_PointTracer->setVisible(false); |
|
56 | 64 | impl->m_TextTracer->setVisible(false); |
|
57 | 65 | impl->m_Plot.replot(); |
|
58 | 66 | |
|
59 | 67 | // Gets the graph under the mouse position |
|
60 | 68 | if (auto graph = qobject_cast<QCPGraph *>(impl->m_Plot.plottableAt(eventPos))) { |
|
61 | 69 | auto mouseKey = graph->keyAxis()->pixelToCoord(eventPos.x()); |
|
62 | 70 | auto graphData = graph->data(); |
|
63 | 71 | |
|
64 | 72 | // Gets the closest data point to the mouse |
|
65 | 73 | auto graphDataIt = graphData->findBegin(mouseKey); |
|
66 | 74 | if (graphDataIt != graphData->constEnd()) { |
|
67 | 75 | auto key = formatValue(graphDataIt->key, *graph->keyAxis()); |
|
68 | 76 | auto value = formatValue(graphDataIt->value, *graph->valueAxis()); |
|
69 | 77 | impl->m_TextTracer->setText(TEXT_TRACER_FORMAT.arg(key, value)); |
|
78 | ||
|
79 | // Displays point tracer | |
|
80 | impl->m_PointTracer->setGraph(graph); | |
|
81 | impl->m_PointTracer->setGraphKey(mouseKey); | |
|
82 | displayTracer(*impl->m_PointTracer); | |
|
83 | ||
|
84 | // Displays text tracer | |
|
85 | auto tracerPosition = impl->m_TextTracer->position; | |
|
86 | tracerPosition->setAxes(graph->keyAxis(), graph->valueAxis()); | |
|
87 | tracerPosition->setCoords(impl->m_PointTracer->position->key(), | |
|
88 | impl->m_PointTracer->position->value()); | |
|
89 | displayTracer(*impl->m_TextTracer); | |
|
90 | } | |
|
70 | 91 | } |
|
71 | 92 | }; |
|
72 | 93 | |
|
73 | 94 | // Starts the timer to display tracers at timeout |
|
74 | 95 | QObject::connect(&impl->m_TracerTimer, &QTimer::timeout, showTracers); |
|
75 | 96 | impl->m_TracerTimer.start(); |
|
76 | 97 | } |
General Comments 0
You need to be logged in to leave comments.
Login now