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