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