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