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