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