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