##// 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 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 /// Offset used to shift the tooltip of the mouse
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 21 /// Formats a data value according to the axis on which it is present
16 22 QString formatValue(double value, const QCPAxis &axis)
@@ -27,40 +33,26 QString formatValue(double value, const QCPAxis &axis)
27 33 void initPointTracerStyle(QCPItemTracer &tracer) noexcept
28 34 {
29 35 tracer.setInterpolating(false);
30 tracer.setStyle(QCPItemTracer::tsPlus);
36 tracer.setStyle(QCPItemTracer::tsCircle);
37 tracer.setSize(3);
31 38 tracer.setPen(QPen(Qt::black));
32 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 42 } // namespace
46 43
47 44 struct VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegatePrivate {
48 45 explicit VisualizationGraphRenderingDelegatePrivate(QCustomPlot &plot)
49 : m_Plot{plot},
50 m_PointTracer{new QCPItemTracer{&plot}},
51 m_TextTracer{new QCPItemText{&plot}},
52 m_TracerTimer{}
46 : m_Plot{plot}, m_PointTracer{new QCPItemTracer{&plot}}, m_TracerTimer{}
53 47 {
54 48 initPointTracerStyle(*m_PointTracer);
55 initTextTracerStyle(*m_TextTracer);
56 49
57 m_TracerTimer.setInterval(TRACER_TIMEOUT);
50 m_TracerTimer.setInterval(TOOLTIP_TIMEOUT);
58 51 m_TracerTimer.setSingleShot(true);
59 52 }
60 53
61 54 QCustomPlot &m_Plot;
62 55 QCPItemTracer *m_PointTracer;
63 QCPItemText *m_TextTracer;
64 56 QTimer m_TracerTimer;
65 57 };
66 58
@@ -74,23 +66,13 void VisualizationGraphRenderingDelegate::onMouseMove(QMouseEvent *event) noexce
74 66 // Cancels pending refresh
75 67 impl->m_TracerTimer.disconnect();
76 68
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);
84 impl->m_Plot.replot();
85 };
86
87 69 // Reinits tracers
88 70 impl->m_PointTracer->setGraph(nullptr);
89 71 impl->m_PointTracer->setVisible(false);
90 impl->m_TextTracer->setVisible(false);
91 72 impl->m_Plot.replot();
92 73
93 74 // Gets the graph under the mouse position
75 auto eventPos = event->pos();
94 76 if (auto graph = qobject_cast<QCPGraph *>(impl->m_Plot.plottableAt(eventPos))) {
95 77 auto mouseKey = graph->keyAxis()->pixelToCoord(eventPos.x());
96 78 auto graphData = graph->data();
@@ -100,24 +82,24 void VisualizationGraphRenderingDelegate::onMouseMove(QMouseEvent *event) noexce
100 82 if (graphDataIt != graphData->constEnd()) {
101 83 auto key = formatValue(graphDataIt->key, *graph->keyAxis());
102 84 auto value = formatValue(graphDataIt->value, *graph->valueAxis());
103 impl->m_TextTracer->setText(TEXT_TRACER_FORMAT.arg(key, value));
104 85
105 86 // Displays point tracer
106 87 impl->m_PointTracer->setGraph(graph);
107 88 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 }
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);
92 impl->m_Plot.replot();
93
94 // Starts timer to show tooltip after timeout
95 auto showTooltip = [ tooltip = TOOLTIP_FORMAT.arg(key, value), eventPos, this ]()
96 {
97 QToolTip::showText(impl->m_Plot.mapToGlobal(eventPos) + TOOLTIP_OFFSET, tooltip,
98 &impl->m_Plot, TOOLTIP_RECT);
118 99 };
119 100
120 // Starts the timer to display tracers at timeout
121 QObject::connect(&impl->m_TracerTimer, &QTimer::timeout, showTracers);
101 QObject::connect(&impl->m_TracerTimer, &QTimer::timeout, showTooltip);
122 102 impl->m_TracerTimer.start();
123 103 }
104 }
105 }
General Comments 0
You need to be logged in to leave comments. Login now