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