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