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