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