@@ -1,331 +1,331 | |||||
1 | #include "Visualization/VisualizationGraphRenderingDelegate.h" |
|
1 | #include "Visualization/VisualizationGraphRenderingDelegate.h" | |
2 | #include "Visualization/AxisRenderingUtils.h" |
|
2 | #include "Visualization/AxisRenderingUtils.h" | |
3 | #include "Visualization/ColorScaleEditor.h" |
|
3 | #include "Visualization/ColorScaleEditor.h" | |
4 | #include "Visualization/PlottablesRenderingUtils.h" |
|
4 | #include "Visualization/PlottablesRenderingUtils.h" | |
5 | #include "Visualization/SqpColorScale.h" |
|
5 | #include "Visualization/SqpColorScale.h" | |
6 | #include "Visualization/VisualizationGraphWidget.h" |
|
6 | #include "Visualization/VisualizationGraphWidget.h" | |
7 | #include "Visualization/qcustomplot.h" |
|
7 | #include "Visualization/qcustomplot.h" | |
8 |
|
8 | |||
9 | #include <Common/DateUtils.h> |
|
9 | #include <Common/DateUtils.h> | |
10 |
|
10 | |||
11 | #include <Data/IDataSeries.h> |
|
11 | #include <Data/IDataSeries.h> | |
12 |
|
12 | |||
13 | #include <SqpApplication.h> |
|
13 | #include <SqpApplication.h> | |
14 |
|
14 | |||
15 | namespace { |
|
15 | namespace { | |
16 |
|
16 | |||
17 | /// Name of the axes layer in QCustomPlot |
|
17 | /// Name of the axes layer in QCustomPlot | |
18 | const auto AXES_LAYER = QStringLiteral("axes"); |
|
18 | const auto AXES_LAYER = QStringLiteral("axes"); | |
19 |
|
19 | |||
20 | /// Icon used to show x-axis properties |
|
20 | /// Icon used to show x-axis properties | |
21 | const auto HIDE_AXIS_ICON_PATH = QStringLiteral(":/icones/down.png"); |
|
21 | const auto HIDE_AXIS_ICON_PATH = QStringLiteral(":/icones/down.png"); | |
22 |
|
22 | |||
23 | /// Name of the overlay layer in QCustomPlot |
|
23 | /// Name of the overlay layer in QCustomPlot | |
24 | const auto OVERLAY_LAYER = QStringLiteral("overlay"); |
|
24 | const auto OVERLAY_LAYER = QStringLiteral("overlay"); | |
25 |
|
25 | |||
26 | /// Pixmap used to show x-axis properties |
|
26 | /// Pixmap used to show x-axis properties | |
27 | const auto SHOW_AXIS_ICON_PATH = QStringLiteral(":/icones/up.png"); |
|
27 | const auto SHOW_AXIS_ICON_PATH = QStringLiteral(":/icones/up.png"); | |
28 |
|
28 | |||
29 | /// Tooltip format for graphs |
|
29 | /// Tooltip format for graphs | |
30 | const auto GRAPH_TOOLTIP_FORMAT = QStringLiteral("key: %1\nvalue: %2"); |
|
30 | const auto GRAPH_TOOLTIP_FORMAT = QStringLiteral("key: %1\nvalue: %2"); | |
31 |
|
31 | |||
32 | /// Tooltip format for colormaps |
|
32 | /// Tooltip format for colormaps | |
33 | const auto COLORMAP_TOOLTIP_FORMAT = QStringLiteral("x: %1\ny: %2\nvalue: %3"); |
|
33 | const auto COLORMAP_TOOLTIP_FORMAT = QStringLiteral("x: %1\ny: %2\nvalue: %3"); | |
34 |
|
34 | |||
35 | /// Offset used to shift the tooltip of the mouse |
|
35 | /// Offset used to shift the tooltip of the mouse | |
36 | const auto TOOLTIP_OFFSET = QPoint{20, 20}; |
|
36 | const auto TOOLTIP_OFFSET = QPoint{20, 20}; | |
37 |
|
37 | |||
38 | /// Tooltip display rectangle (the tooltip is hidden when the mouse leaves this rectangle) |
|
38 | /// Tooltip display rectangle (the tooltip is hidden when the mouse leaves this rectangle) | |
39 | const auto TOOLTIP_RECT = QRect{10, 10, 10, 10}; |
|
39 | const auto TOOLTIP_RECT = QRect{10, 10, 10, 10}; | |
40 |
|
40 | |||
41 | /// Timeout after which the tooltip is displayed |
|
41 | /// Timeout after which the tooltip is displayed | |
42 | const auto TOOLTIP_TIMEOUT = 500; |
|
42 | const auto TOOLTIP_TIMEOUT = 500; | |
43 |
|
43 | |||
44 | void initPointTracerStyle(QCPItemTracer &tracer) noexcept |
|
44 | void initPointTracerStyle(QCPItemTracer &tracer) noexcept | |
45 | { |
|
45 | { | |
46 | tracer.setInterpolating(false); |
|
46 | tracer.setInterpolating(false); | |
47 | tracer.setStyle(QCPItemTracer::tsCircle); |
|
47 | tracer.setStyle(QCPItemTracer::tsCircle); | |
48 | tracer.setSize(3); |
|
48 | tracer.setSize(3); | |
49 | tracer.setPen(QPen(Qt::black)); |
|
49 | tracer.setPen(QPen(Qt::black)); | |
50 | tracer.setBrush(Qt::black); |
|
50 | tracer.setBrush(Qt::black); | |
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 | QPixmap pixmap(const QString &iconPath) noexcept |
|
53 | QPixmap pixmap(const QString &iconPath) noexcept | |
54 | { |
|
54 | { | |
55 | return QIcon{iconPath}.pixmap(QSize{16, 16}); |
|
55 | return QIcon{iconPath}.pixmap(QSize{16, 16}); | |
56 | } |
|
56 | } | |
57 |
|
57 | |||
58 | void initClosePixmapStyle(QCPItemPixmap &pixmap) noexcept |
|
58 | void initClosePixmapStyle(QCPItemPixmap &pixmap) noexcept | |
59 | { |
|
59 | { | |
60 | // Icon |
|
60 | // Icon | |
61 | pixmap.setPixmap( |
|
61 | pixmap.setPixmap( | |
62 | sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton).pixmap(QSize{16, 16})); |
|
62 | sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton).pixmap(QSize{16, 16})); | |
63 |
|
63 | |||
64 | // Position |
|
64 | // Position | |
65 | pixmap.topLeft->setType(QCPItemPosition::ptAxisRectRatio); |
|
65 | pixmap.topLeft->setType(QCPItemPosition::ptAxisRectRatio); | |
66 | pixmap.topLeft->setCoords(1, 0); |
|
66 | pixmap.topLeft->setCoords(1, 0); | |
67 | pixmap.setClipToAxisRect(false); |
|
67 | pixmap.setClipToAxisRect(false); | |
68 |
|
68 | |||
69 | // Can be selected |
|
69 | // Can be selected | |
70 | pixmap.setSelectable(true); |
|
70 | pixmap.setSelectable(true); | |
71 | } |
|
71 | } | |
72 |
|
72 | |||
73 | void initXAxisPixmapStyle(QCPItemPixmap &itemPixmap) noexcept |
|
73 | void initXAxisPixmapStyle(QCPItemPixmap &itemPixmap) noexcept | |
74 | { |
|
74 | { | |
75 | // Icon |
|
75 | // Icon | |
76 | itemPixmap.setPixmap(pixmap(HIDE_AXIS_ICON_PATH)); |
|
76 | itemPixmap.setPixmap(pixmap(HIDE_AXIS_ICON_PATH)); | |
77 |
|
77 | |||
78 | // Position |
|
78 | // Position | |
79 | itemPixmap.topLeft->setType(QCPItemPosition::ptAxisRectRatio); |
|
79 | itemPixmap.topLeft->setType(QCPItemPosition::ptAxisRectRatio); | |
80 | itemPixmap.topLeft->setCoords(0, 1); |
|
80 | itemPixmap.topLeft->setCoords(0, 1); | |
81 | itemPixmap.setClipToAxisRect(false); |
|
81 | itemPixmap.setClipToAxisRect(false); | |
82 |
|
82 | |||
83 | // Can be selected |
|
83 | // Can be selected | |
84 | itemPixmap.setSelectable(true); |
|
84 | itemPixmap.setSelectable(true); | |
85 | } |
|
85 | } | |
86 |
|
86 | |||
87 | void initTitleTextStyle(QCPItemText &text) noexcept |
|
87 | void initTitleTextStyle(QCPItemText &text) noexcept | |
88 | { |
|
88 | { | |
89 | // Font and background styles |
|
89 | // Font and background styles | |
90 | text.setColor(Qt::gray); |
|
90 | text.setColor(Qt::gray); | |
91 | text.setBrush(Qt::white); |
|
91 | text.setBrush(Qt::white); | |
92 |
|
92 | |||
93 | // Position |
|
93 | // Position | |
94 | text.setPositionAlignment(Qt::AlignTop | Qt::AlignLeft); |
|
94 | text.setPositionAlignment(Qt::AlignTop | Qt::AlignLeft); | |
95 | text.position->setType(QCPItemPosition::ptAxisRectRatio); |
|
95 | text.position->setType(QCPItemPosition::ptAxisRectRatio); | |
96 | text.position->setCoords(0.5, 0); |
|
96 | text.position->setCoords(0.5, 0); | |
97 | } |
|
97 | } | |
98 |
|
98 | |||
99 | /** |
|
99 | /** | |
100 | * Returns the cell index (x or y) of a colormap according to the coordinate passed in parameter. |
|
100 | * Returns the cell index (x or y) of a colormap according to the coordinate passed in parameter. | |
101 | * This method handles the fact that a colormap axis can be logarithmic or linear. |
|
101 | * This method handles the fact that a colormap axis can be logarithmic or linear. | |
102 | * @param colormap the colormap for which to calculate the index |
|
102 | * @param colormap the colormap for which to calculate the index | |
103 | * @param coord the coord to convert to cell index |
|
103 | * @param coord the coord to convert to cell index | |
104 | * @param xCoord calculates the x index if true, calculates y index if false |
|
104 | * @param xCoord calculates the x index if true, calculates y index if false | |
105 | * @return the cell index |
|
105 | * @return the cell index | |
106 | */ |
|
106 | */ | |
107 | int colorMapCellIndex(const QCPColorMap &colormap, double coord, bool xCoord) |
|
107 | int colorMapCellIndex(const QCPColorMap &colormap, double coord, bool xCoord) | |
108 | { |
|
108 | { | |
109 | // Determines the axis of the colormap according to xCoord, and whether it is logarithmic or not |
|
109 | // Determines the axis of the colormap according to xCoord, and whether it is logarithmic or not | |
110 | auto isLogarithmic = (xCoord ? colormap.keyAxis() : colormap.valueAxis())->scaleType() |
|
110 | auto isLogarithmic = (xCoord ? colormap.keyAxis() : colormap.valueAxis())->scaleType() | |
111 | == QCPAxis::stLogarithmic; |
|
111 | == QCPAxis::stLogarithmic; | |
112 |
|
112 | |||
113 | if (isLogarithmic) { |
|
113 | if (isLogarithmic) { | |
114 | // For a logarithmic axis we can't use the conversion method of colormap, so we calculate |
|
114 | // For a logarithmic axis we can't use the conversion method of colormap, so we calculate | |
115 | // the index manually based on the position of the coordinate on the axis |
|
115 | // the index manually based on the position of the coordinate on the axis | |
116 |
|
116 | |||
117 | // Gets the axis range and the number of values between range bounds to calculate the step |
|
117 | // Gets the axis range and the number of values between range bounds to calculate the step | |
118 | // between each value of the range |
|
118 | // between each value of the range | |
119 | auto range = xCoord ? colormap.data()->keyRange() : colormap.data()->valueRange(); |
|
119 | auto range = xCoord ? colormap.data()->keyRange() : colormap.data()->valueRange(); | |
120 | auto nbValues = (xCoord ? colormap.data()->keySize() : colormap.data()->valueSize()) - 1; |
|
120 | auto nbValues = (xCoord ? colormap.data()->keySize() : colormap.data()->valueSize()) - 1; | |
121 | auto valueStep |
|
121 | auto valueStep | |
122 | = (std::log10(range.upper) - std::log10(range.lower)) / static_cast<double>(nbValues); |
|
122 | = (std::log10(range.upper) - std::log10(range.lower)) / static_cast<double>(nbValues); | |
123 |
|
123 | |||
124 | // According to the coord position, calculates the closest index in the range |
|
124 | // According to the coord position, calculates the closest index in the range | |
125 | return std::round((std::log10(coord) - std::log10(range.lower)) / valueStep); |
|
125 | return std::round((std::log10(coord) - std::log10(range.lower)) / valueStep); | |
126 | } |
|
126 | } | |
127 | else { |
|
127 | else { | |
128 | // For a linear axis, we use the conversion method of colormap |
|
128 | // For a linear axis, we use the conversion method of colormap | |
129 | int index; |
|
129 | int index; | |
130 | if (xCoord) { |
|
130 | if (xCoord) { | |
131 | colormap.data()->coordToCell(coord, 0., &index, nullptr); |
|
131 | colormap.data()->coordToCell(coord, 0., &index, nullptr); | |
132 | } |
|
132 | } | |
133 | else { |
|
133 | else { | |
134 | colormap.data()->coordToCell(0., coord, nullptr, &index); |
|
134 | colormap.data()->coordToCell(0., coord, nullptr, &index); | |
135 | } |
|
135 | } | |
136 |
|
136 | |||
137 | return index; |
|
137 | return index; | |
138 | } |
|
138 | } | |
139 | } |
|
139 | } | |
140 |
|
140 | |||
141 | } // namespace |
|
141 | } // namespace | |
142 |
|
142 | |||
143 | struct VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegatePrivate { |
|
143 | struct VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegatePrivate { | |
144 | explicit VisualizationGraphRenderingDelegatePrivate(VisualizationGraphWidget &graphWidget) |
|
144 | explicit VisualizationGraphRenderingDelegatePrivate(VisualizationGraphWidget &graphWidget) | |
145 | : m_Plot{graphWidget.plot()}, |
|
145 | : m_Plot{graphWidget.plot()}, | |
146 | m_PointTracer{new QCPItemTracer{&m_Plot}}, |
|
146 | m_PointTracer{new QCPItemTracer{&m_Plot}}, | |
147 | m_TracerTimer{}, |
|
147 | m_TracerTimer{}, | |
148 | m_ClosePixmap{new QCPItemPixmap{&m_Plot}}, |
|
148 | m_ClosePixmap{new QCPItemPixmap{&m_Plot}}, | |
149 | m_TitleText{new QCPItemText{&m_Plot}}, |
|
149 | m_TitleText{new QCPItemText{&m_Plot}}, | |
150 | m_XAxisPixmap{new QCPItemPixmap{&m_Plot}}, |
|
150 | m_XAxisPixmap{new QCPItemPixmap{&m_Plot}}, | |
151 | m_ShowXAxis{true}, |
|
151 | m_ShowXAxis{true}, | |
152 | m_XAxisLabel{}, |
|
152 | m_XAxisLabel{}, | |
153 | m_ColorScale{SqpColorScale{m_Plot}} |
|
153 | m_ColorScale{SqpColorScale{m_Plot}} | |
154 | { |
|
154 | { | |
155 | initPointTracerStyle(*m_PointTracer); |
|
155 | initPointTracerStyle(*m_PointTracer); | |
156 |
|
156 | |||
157 | m_TracerTimer.setInterval(TOOLTIP_TIMEOUT); |
|
157 | m_TracerTimer.setInterval(TOOLTIP_TIMEOUT); | |
158 | m_TracerTimer.setSingleShot(true); |
|
158 | m_TracerTimer.setSingleShot(true); | |
159 |
|
159 | |||
160 | // Inits "close button" in plot overlay |
|
160 | // Inits "close button" in plot overlay | |
161 | m_ClosePixmap->setLayer(OVERLAY_LAYER); |
|
161 | m_ClosePixmap->setLayer(OVERLAY_LAYER); | |
162 | initClosePixmapStyle(*m_ClosePixmap); |
|
162 | initClosePixmapStyle(*m_ClosePixmap); | |
163 |
|
163 | |||
164 | // Connects pixmap selection to graph widget closing |
|
164 | // Connects pixmap selection to graph widget closing | |
165 | QObject::connect(m_ClosePixmap, &QCPItemPixmap::selectionChanged, |
|
165 | QObject::connect(m_ClosePixmap, &QCPItemPixmap::selectionChanged, | |
166 | [&graphWidget](bool selected) { |
|
166 | [&graphWidget](bool selected) { | |
167 | if (selected) { |
|
167 | if (selected) { | |
168 | graphWidget.close(); |
|
168 | graphWidget.close(); | |
169 | } |
|
169 | } | |
170 | }); |
|
170 | }); | |
171 |
|
171 | |||
172 | // Inits graph name in plot overlay |
|
172 | // Inits graph name in plot overlay | |
173 | m_TitleText->setLayer(OVERLAY_LAYER); |
|
173 | m_TitleText->setLayer(OVERLAY_LAYER); | |
174 | m_TitleText->setText(graphWidget.name()); |
|
174 | m_TitleText->setText(graphWidget.name()); | |
175 | initTitleTextStyle(*m_TitleText); |
|
175 | initTitleTextStyle(*m_TitleText); | |
176 |
|
176 | |||
177 | // Inits "show x-axis button" in plot overlay |
|
177 | // Inits "show x-axis button" in plot overlay | |
178 | m_XAxisPixmap->setLayer(OVERLAY_LAYER); |
|
178 | m_XAxisPixmap->setLayer(OVERLAY_LAYER); | |
179 | initXAxisPixmapStyle(*m_XAxisPixmap); |
|
179 | initXAxisPixmapStyle(*m_XAxisPixmap); | |
180 |
|
180 | |||
181 | // Connects pixmap selection to graph x-axis showing/hiding |
|
181 | // Connects pixmap selection to graph x-axis showing/hiding | |
182 | QObject::connect(m_XAxisPixmap, &QCPItemPixmap::selectionChanged, [this]() { |
|
182 | QObject::connect(m_XAxisPixmap, &QCPItemPixmap::selectionChanged, [this]() { | |
183 | if (m_XAxisPixmap->selected()) { |
|
183 | if (m_XAxisPixmap->selected()) { | |
184 | // Changes the selection state and refreshes the x-axis |
|
184 | // Changes the selection state and refreshes the x-axis | |
185 | m_ShowXAxis = !m_ShowXAxis; |
|
185 | m_ShowXAxis = !m_ShowXAxis; | |
186 | updateXAxisState(); |
|
186 | updateXAxisState(); | |
187 | m_Plot.layer(AXES_LAYER)->replot(); |
|
187 | m_Plot.layer(AXES_LAYER)->replot(); | |
188 |
|
188 | |||
189 | // Deselects the x-axis pixmap and updates icon |
|
189 | // Deselects the x-axis pixmap and updates icon | |
190 | m_XAxisPixmap->setSelected(false); |
|
190 | m_XAxisPixmap->setSelected(false); | |
191 | m_XAxisPixmap->setPixmap( |
|
191 | m_XAxisPixmap->setPixmap( | |
192 | pixmap(m_ShowXAxis ? HIDE_AXIS_ICON_PATH : SHOW_AXIS_ICON_PATH)); |
|
192 | pixmap(m_ShowXAxis ? HIDE_AXIS_ICON_PATH : SHOW_AXIS_ICON_PATH)); | |
193 | m_Plot.layer(OVERLAY_LAYER)->replot(); |
|
193 | m_Plot.layer(OVERLAY_LAYER)->replot(); | |
194 | } |
|
194 | } | |
195 | }); |
|
195 | }); | |
196 | } |
|
196 | } | |
197 |
|
197 | |||
198 | /// Updates state of x-axis according to the current selection of x-axis pixmap |
|
198 | /// Updates state of x-axis according to the current selection of x-axis pixmap | |
199 | /// @remarks the method doesn't call plot refresh |
|
199 | /// @remarks the method doesn't call plot refresh | |
200 | void updateXAxisState() noexcept |
|
200 | void updateXAxisState() noexcept | |
201 | { |
|
201 | { | |
202 | m_Plot.xAxis->setTickLabels(m_ShowXAxis); |
|
202 | m_Plot.xAxis->setTickLabels(m_ShowXAxis); | |
203 | m_Plot.xAxis->setLabel(m_ShowXAxis ? m_XAxisLabel : QString{}); |
|
203 | m_Plot.xAxis->setLabel(m_ShowXAxis ? m_XAxisLabel : QString{}); | |
204 | } |
|
204 | } | |
205 |
|
205 | |||
206 | QCustomPlot &m_Plot; |
|
206 | QCustomPlot &m_Plot; | |
207 | QCPItemTracer *m_PointTracer; |
|
207 | QCPItemTracer *m_PointTracer; | |
208 | QTimer m_TracerTimer; |
|
208 | QTimer m_TracerTimer; | |
209 | QCPItemPixmap *m_ClosePixmap; /// Graph's close button |
|
209 | QCPItemPixmap *m_ClosePixmap; /// Graph's close button | |
210 | QCPItemText *m_TitleText; /// Graph's title |
|
210 | QCPItemText *m_TitleText; /// Graph's title | |
211 | QCPItemPixmap *m_XAxisPixmap; |
|
211 | QCPItemPixmap *m_XAxisPixmap; | |
212 | bool m_ShowXAxis; /// X-axis properties are shown or hidden |
|
212 | bool m_ShowXAxis; /// X-axis properties are shown or hidden | |
213 | QString m_XAxisLabel; |
|
213 | QString m_XAxisLabel; | |
214 | SqpColorScale m_ColorScale; /// Color scale used for some types of graphs (as spectrograms) |
|
214 | SqpColorScale m_ColorScale; /// Color scale used for some types of graphs (as spectrograms) | |
215 | }; |
|
215 | }; | |
216 |
|
216 | |||
217 | VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegate( |
|
217 | VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegate( | |
218 | VisualizationGraphWidget &graphWidget) |
|
218 | VisualizationGraphWidget &graphWidget) | |
219 | : impl{spimpl::make_unique_impl<VisualizationGraphRenderingDelegatePrivate>(graphWidget)} |
|
219 | : impl{spimpl::make_unique_impl<VisualizationGraphRenderingDelegatePrivate>(graphWidget)} | |
220 | { |
|
220 | { | |
221 | } |
|
221 | } | |
222 |
|
222 | |||
223 | void VisualizationGraphRenderingDelegate::onMouseDoubleClick(QMouseEvent *event) noexcept |
|
223 | void VisualizationGraphRenderingDelegate::onMouseDoubleClick(QMouseEvent *event) noexcept | |
224 | { |
|
224 | { | |
225 | // Opens color scale editor if color scale is double clicked |
|
225 | // Opens color scale editor if color scale is double clicked | |
226 |
auto colorScale = |
|
226 | auto colorScale = static_cast<QCPColorScale *>(impl->m_Plot.layoutElementAt(event->pos())); | |
227 | if (impl->m_ColorScale.m_Scale == colorScale) { |
|
227 | if (impl->m_ColorScale.m_Scale == colorScale) { | |
228 | if (ColorScaleEditor{impl->m_ColorScale}.exec() == QDialog::Accepted) { |
|
228 | if (ColorScaleEditor{impl->m_ColorScale}.exec() == QDialog::Accepted) { | |
229 | impl->m_Plot.replot(); |
|
229 | impl->m_Plot.replot(); | |
230 | } |
|
230 | } | |
231 | } |
|
231 | } | |
232 | } |
|
232 | } | |
233 |
|
233 | |||
234 | void VisualizationGraphRenderingDelegate::onMouseMove(QMouseEvent *event) noexcept |
|
234 | void VisualizationGraphRenderingDelegate::onMouseMove(QMouseEvent *event) noexcept | |
235 | { |
|
235 | { | |
236 | // Cancels pending refresh |
|
236 | // Cancels pending refresh | |
237 | impl->m_TracerTimer.disconnect(); |
|
237 | impl->m_TracerTimer.disconnect(); | |
238 |
|
238 | |||
239 | // Reinits tracers |
|
239 | // Reinits tracers | |
240 | impl->m_PointTracer->setGraph(nullptr); |
|
240 | impl->m_PointTracer->setGraph(nullptr); | |
241 | impl->m_PointTracer->setVisible(false); |
|
241 | impl->m_PointTracer->setVisible(false); | |
242 | impl->m_Plot.replot(); |
|
242 | impl->m_Plot.replot(); | |
243 |
|
243 | |||
244 | QString tooltip{}; |
|
244 | QString tooltip{}; | |
245 |
|
245 | |||
246 | // Gets the graph under the mouse position |
|
246 | // Gets the graph under the mouse position | |
247 | auto eventPos = event->pos(); |
|
247 | auto eventPos = event->pos(); | |
248 | if (auto graph = qobject_cast<QCPGraph *>(impl->m_Plot.plottableAt(eventPos))) { |
|
248 | if (auto graph = qobject_cast<QCPGraph *>(impl->m_Plot.plottableAt(eventPos))) { | |
249 | auto mouseKey = graph->keyAxis()->pixelToCoord(eventPos.x()); |
|
249 | auto mouseKey = graph->keyAxis()->pixelToCoord(eventPos.x()); | |
250 | auto graphData = graph->data(); |
|
250 | auto graphData = graph->data(); | |
251 |
|
251 | |||
252 | // Gets the closest data point to the mouse |
|
252 | // Gets the closest data point to the mouse | |
253 | auto graphDataIt = graphData->findBegin(mouseKey); |
|
253 | auto graphDataIt = graphData->findBegin(mouseKey); | |
254 | if (graphDataIt != graphData->constEnd()) { |
|
254 | if (graphDataIt != graphData->constEnd()) { | |
255 | // Sets tooltip |
|
255 | // Sets tooltip | |
256 | auto key = formatValue(graphDataIt->key, *graph->keyAxis()); |
|
256 | auto key = formatValue(graphDataIt->key, *graph->keyAxis()); | |
257 | auto value = formatValue(graphDataIt->value, *graph->valueAxis()); |
|
257 | auto value = formatValue(graphDataIt->value, *graph->valueAxis()); | |
258 | tooltip = GRAPH_TOOLTIP_FORMAT.arg(key, value); |
|
258 | tooltip = GRAPH_TOOLTIP_FORMAT.arg(key, value); | |
259 |
|
259 | |||
260 | // Displays point tracer |
|
260 | // Displays point tracer | |
261 | impl->m_PointTracer->setGraph(graph); |
|
261 | impl->m_PointTracer->setGraph(graph); | |
262 | impl->m_PointTracer->setGraphKey(graphDataIt->key); |
|
262 | impl->m_PointTracer->setGraphKey(graphDataIt->key); | |
263 | impl->m_PointTracer->setLayer( |
|
263 | impl->m_PointTracer->setLayer( | |
264 | impl->m_Plot.layer("main")); // Tracer is set on top of the plot's main layer |
|
264 | impl->m_Plot.layer("main")); // Tracer is set on top of the plot's main layer | |
265 | impl->m_PointTracer->setVisible(true); |
|
265 | impl->m_PointTracer->setVisible(true); | |
266 | impl->m_Plot.replot(); |
|
266 | impl->m_Plot.replot(); | |
267 | } |
|
267 | } | |
268 | } |
|
268 | } | |
269 | else if (auto colorMap = qobject_cast<QCPColorMap *>(impl->m_Plot.plottableAt(eventPos))) { |
|
269 | else if (auto colorMap = qobject_cast<QCPColorMap *>(impl->m_Plot.plottableAt(eventPos))) { | |
270 | // Gets x and y coords |
|
270 | // Gets x and y coords | |
271 | auto x = colorMap->keyAxis()->pixelToCoord(eventPos.x()); |
|
271 | auto x = colorMap->keyAxis()->pixelToCoord(eventPos.x()); | |
272 | auto y = colorMap->valueAxis()->pixelToCoord(eventPos.y()); |
|
272 | auto y = colorMap->valueAxis()->pixelToCoord(eventPos.y()); | |
273 |
|
273 | |||
274 | // Calculates x and y cell indexes, and retrieves the underlying value |
|
274 | // Calculates x and y cell indexes, and retrieves the underlying value | |
275 | auto xCellIndex = colorMapCellIndex(*colorMap, x, true); |
|
275 | auto xCellIndex = colorMapCellIndex(*colorMap, x, true); | |
276 | auto yCellIndex = colorMapCellIndex(*colorMap, y, false); |
|
276 | auto yCellIndex = colorMapCellIndex(*colorMap, y, false); | |
277 | auto value = colorMap->data()->cell(xCellIndex, yCellIndex); |
|
277 | auto value = colorMap->data()->cell(xCellIndex, yCellIndex); | |
278 |
|
278 | |||
279 | // Sets tooltips |
|
279 | // Sets tooltips | |
280 | tooltip = COLORMAP_TOOLTIP_FORMAT.arg(formatValue(x, *colorMap->keyAxis()), |
|
280 | tooltip = COLORMAP_TOOLTIP_FORMAT.arg(formatValue(x, *colorMap->keyAxis()), | |
281 | formatValue(y, *colorMap->valueAxis()), |
|
281 | formatValue(y, *colorMap->valueAxis()), | |
282 | formatValue(value, *colorMap->colorScale()->axis())); |
|
282 | formatValue(value, *colorMap->colorScale()->axis())); | |
283 | } |
|
283 | } | |
284 |
|
284 | |||
285 | if (!tooltip.isEmpty()) { |
|
285 | if (!tooltip.isEmpty()) { | |
286 | // Starts timer to show tooltip after timeout |
|
286 | // Starts timer to show tooltip after timeout | |
287 | auto showTooltip = [tooltip, eventPos, this]() { |
|
287 | auto showTooltip = [tooltip, eventPos, this]() { | |
288 | QToolTip::showText(impl->m_Plot.mapToGlobal(eventPos) + TOOLTIP_OFFSET, tooltip, |
|
288 | QToolTip::showText(impl->m_Plot.mapToGlobal(eventPos) + TOOLTIP_OFFSET, tooltip, | |
289 | &impl->m_Plot, TOOLTIP_RECT); |
|
289 | &impl->m_Plot, TOOLTIP_RECT); | |
290 | }; |
|
290 | }; | |
291 |
|
291 | |||
292 | QObject::connect(&impl->m_TracerTimer, &QTimer::timeout, showTooltip); |
|
292 | QObject::connect(&impl->m_TracerTimer, &QTimer::timeout, showTooltip); | |
293 | impl->m_TracerTimer.start(); |
|
293 | impl->m_TracerTimer.start(); | |
294 | } |
|
294 | } | |
295 | } |
|
295 | } | |
296 |
|
296 | |||
297 | void VisualizationGraphRenderingDelegate::onPlotUpdated() noexcept |
|
297 | void VisualizationGraphRenderingDelegate::onPlotUpdated() noexcept | |
298 | { |
|
298 | { | |
299 | // Updates color scale bounds |
|
299 | // Updates color scale bounds | |
300 | impl->m_ColorScale.updateDataRange(); |
|
300 | impl->m_ColorScale.updateDataRange(); | |
301 | impl->m_Plot.replot(); |
|
301 | impl->m_Plot.replot(); | |
302 | } |
|
302 | } | |
303 |
|
303 | |||
304 | void VisualizationGraphRenderingDelegate::setAxesProperties( |
|
304 | void VisualizationGraphRenderingDelegate::setAxesProperties( | |
305 | std::shared_ptr<IDataSeries> dataSeries) noexcept |
|
305 | std::shared_ptr<IDataSeries> dataSeries) noexcept | |
306 | { |
|
306 | { | |
307 | // Stores x-axis label to be able to retrieve it when x-axis pixmap is unselected |
|
307 | // Stores x-axis label to be able to retrieve it when x-axis pixmap is unselected | |
308 | impl->m_XAxisLabel = dataSeries->xAxisUnit().m_Name; |
|
308 | impl->m_XAxisLabel = dataSeries->xAxisUnit().m_Name; | |
309 |
|
309 | |||
310 | auto axisHelper = IAxisHelperFactory::create(dataSeries); |
|
310 | auto axisHelper = IAxisHelperFactory::create(dataSeries); | |
311 | axisHelper->setProperties(impl->m_Plot, impl->m_ColorScale); |
|
311 | axisHelper->setProperties(impl->m_Plot, impl->m_ColorScale); | |
312 |
|
312 | |||
313 | // Updates x-axis state |
|
313 | // Updates x-axis state | |
314 | impl->updateXAxisState(); |
|
314 | impl->updateXAxisState(); | |
315 |
|
315 | |||
316 | impl->m_Plot.layer(AXES_LAYER)->replot(); |
|
316 | impl->m_Plot.layer(AXES_LAYER)->replot(); | |
317 | } |
|
317 | } | |
318 |
|
318 | |||
319 | void VisualizationGraphRenderingDelegate::setPlottablesProperties( |
|
319 | void VisualizationGraphRenderingDelegate::setPlottablesProperties( | |
320 | std::shared_ptr<IDataSeries> dataSeries, PlottablesMap &plottables) noexcept |
|
320 | std::shared_ptr<IDataSeries> dataSeries, PlottablesMap &plottables) noexcept | |
321 | { |
|
321 | { | |
322 | auto plottablesHelper = IPlottablesHelperFactory::create(dataSeries); |
|
322 | auto plottablesHelper = IPlottablesHelperFactory::create(dataSeries); | |
323 | plottablesHelper->setProperties(plottables); |
|
323 | plottablesHelper->setProperties(plottables); | |
324 | } |
|
324 | } | |
325 |
|
325 | |||
326 | void VisualizationGraphRenderingDelegate::showGraphOverlay(bool show) noexcept |
|
326 | void VisualizationGraphRenderingDelegate::showGraphOverlay(bool show) noexcept | |
327 | { |
|
327 | { | |
328 | auto overlay = impl->m_Plot.layer(OVERLAY_LAYER); |
|
328 | auto overlay = impl->m_Plot.layer(OVERLAY_LAYER); | |
329 | overlay->setVisible(show); |
|
329 | overlay->setVisible(show); | |
330 | overlay->replot(); |
|
330 | overlay->replot(); | |
331 | } |
|
331 | } |
General Comments 0
You need to be logged in to leave comments.
Login now