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