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