##// END OF EJS Templates
Tooltip for spectrograms (2)...
Alexandre Leroux -
r1066:e6d0518500ef
parent child
Show More
@@ -1,276 +1,277
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
32
33 /// Offset used to shift the tooltip of the mouse
33 /// Offset used to shift the tooltip of the mouse
34 const auto TOOLTIP_OFFSET = QPoint{20, 20};
34 const auto TOOLTIP_OFFSET = QPoint{20, 20};
35
35
36 /// Tooltip display rectangle (the tooltip is hidden when the mouse leaves this rectangle)
36 /// Tooltip display rectangle (the tooltip is hidden when the mouse leaves this rectangle)
37 const auto TOOLTIP_RECT = QRect{10, 10, 10, 10};
37 const auto TOOLTIP_RECT = QRect{10, 10, 10, 10};
38
38
39 /// Timeout after which the tooltip is displayed
39 /// Timeout after which the tooltip is displayed
40 const auto TOOLTIP_TIMEOUT = 500;
40 const auto TOOLTIP_TIMEOUT = 500;
41
41
42 void initPointTracerStyle(QCPItemTracer &tracer) noexcept
42 void initPointTracerStyle(QCPItemTracer &tracer) noexcept
43 {
43 {
44 tracer.setInterpolating(false);
44 tracer.setInterpolating(false);
45 tracer.setStyle(QCPItemTracer::tsCircle);
45 tracer.setStyle(QCPItemTracer::tsCircle);
46 tracer.setSize(3);
46 tracer.setSize(3);
47 tracer.setPen(QPen(Qt::black));
47 tracer.setPen(QPen(Qt::black));
48 tracer.setBrush(Qt::black);
48 tracer.setBrush(Qt::black);
49 }
49 }
50
50
51 QPixmap pixmap(const QString &iconPath) noexcept
51 QPixmap pixmap(const QString &iconPath) noexcept
52 {
52 {
53 return QIcon{iconPath}.pixmap(QSize{16, 16});
53 return QIcon{iconPath}.pixmap(QSize{16, 16});
54 }
54 }
55
55
56 void initClosePixmapStyle(QCPItemPixmap &pixmap) noexcept
56 void initClosePixmapStyle(QCPItemPixmap &pixmap) noexcept
57 {
57 {
58 // Icon
58 // Icon
59 pixmap.setPixmap(
59 pixmap.setPixmap(
60 sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton).pixmap(QSize{16, 16}));
60 sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton).pixmap(QSize{16, 16}));
61
61
62 // Position
62 // Position
63 pixmap.topLeft->setType(QCPItemPosition::ptAxisRectRatio);
63 pixmap.topLeft->setType(QCPItemPosition::ptAxisRectRatio);
64 pixmap.topLeft->setCoords(1, 0);
64 pixmap.topLeft->setCoords(1, 0);
65 pixmap.setClipToAxisRect(false);
65 pixmap.setClipToAxisRect(false);
66
66
67 // Can be selected
67 // Can be selected
68 pixmap.setSelectable(true);
68 pixmap.setSelectable(true);
69 }
69 }
70
70
71 void initXAxisPixmapStyle(QCPItemPixmap &itemPixmap) noexcept
71 void initXAxisPixmapStyle(QCPItemPixmap &itemPixmap) noexcept
72 {
72 {
73 // Icon
73 // Icon
74 itemPixmap.setPixmap(pixmap(HIDE_AXIS_ICON_PATH));
74 itemPixmap.setPixmap(pixmap(HIDE_AXIS_ICON_PATH));
75
75
76 // Position
76 // Position
77 itemPixmap.topLeft->setType(QCPItemPosition::ptAxisRectRatio);
77 itemPixmap.topLeft->setType(QCPItemPosition::ptAxisRectRatio);
78 itemPixmap.topLeft->setCoords(0, 1);
78 itemPixmap.topLeft->setCoords(0, 1);
79 itemPixmap.setClipToAxisRect(false);
79 itemPixmap.setClipToAxisRect(false);
80
80
81 // Can be selected
81 // Can be selected
82 itemPixmap.setSelectable(true);
82 itemPixmap.setSelectable(true);
83 }
83 }
84
84
85 void initTitleTextStyle(QCPItemText &text) noexcept
85 void initTitleTextStyle(QCPItemText &text) noexcept
86 {
86 {
87 // Font and background styles
87 // Font and background styles
88 text.setColor(Qt::gray);
88 text.setColor(Qt::gray);
89 text.setBrush(Qt::white);
89 text.setBrush(Qt::white);
90
90
91 // Position
91 // Position
92 text.setPositionAlignment(Qt::AlignTop | Qt::AlignLeft);
92 text.setPositionAlignment(Qt::AlignTop | Qt::AlignLeft);
93 text.position->setType(QCPItemPosition::ptAxisRectRatio);
93 text.position->setType(QCPItemPosition::ptAxisRectRatio);
94 text.position->setCoords(0.5, 0);
94 text.position->setCoords(0.5, 0);
95 }
95 }
96
96
97 } // namespace
97 } // namespace
98
98
99 struct VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegatePrivate {
99 struct VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegatePrivate {
100 explicit VisualizationGraphRenderingDelegatePrivate(VisualizationGraphWidget &graphWidget)
100 explicit VisualizationGraphRenderingDelegatePrivate(VisualizationGraphWidget &graphWidget)
101 : m_Plot{graphWidget.plot()},
101 : m_Plot{graphWidget.plot()},
102 m_PointTracer{new QCPItemTracer{&m_Plot}},
102 m_PointTracer{new QCPItemTracer{&m_Plot}},
103 m_TracerTimer{},
103 m_TracerTimer{},
104 m_ClosePixmap{new QCPItemPixmap{&m_Plot}},
104 m_ClosePixmap{new QCPItemPixmap{&m_Plot}},
105 m_TitleText{new QCPItemText{&m_Plot}},
105 m_TitleText{new QCPItemText{&m_Plot}},
106 m_XAxisPixmap{new QCPItemPixmap{&m_Plot}},
106 m_XAxisPixmap{new QCPItemPixmap{&m_Plot}},
107 m_ShowXAxis{true},
107 m_ShowXAxis{true},
108 m_XAxisLabel{},
108 m_XAxisLabel{},
109 m_ColorScale{SqpColorScale{m_Plot}}
109 m_ColorScale{SqpColorScale{m_Plot}}
110 {
110 {
111 initPointTracerStyle(*m_PointTracer);
111 initPointTracerStyle(*m_PointTracer);
112
112
113 m_TracerTimer.setInterval(TOOLTIP_TIMEOUT);
113 m_TracerTimer.setInterval(TOOLTIP_TIMEOUT);
114 m_TracerTimer.setSingleShot(true);
114 m_TracerTimer.setSingleShot(true);
115
115
116 // Inits "close button" in plot overlay
116 // Inits "close button" in plot overlay
117 m_ClosePixmap->setLayer(OVERLAY_LAYER);
117 m_ClosePixmap->setLayer(OVERLAY_LAYER);
118 initClosePixmapStyle(*m_ClosePixmap);
118 initClosePixmapStyle(*m_ClosePixmap);
119
119
120 // Connects pixmap selection to graph widget closing
120 // Connects pixmap selection to graph widget closing
121 QObject::connect(m_ClosePixmap, &QCPItemPixmap::selectionChanged,
121 QObject::connect(m_ClosePixmap, &QCPItemPixmap::selectionChanged,
122 [&graphWidget](bool selected) {
122 [&graphWidget](bool selected) {
123 if (selected) {
123 if (selected) {
124 graphWidget.close();
124 graphWidget.close();
125 }
125 }
126 });
126 });
127
127
128 // Inits graph name in plot overlay
128 // Inits graph name in plot overlay
129 m_TitleText->setLayer(OVERLAY_LAYER);
129 m_TitleText->setLayer(OVERLAY_LAYER);
130 m_TitleText->setText(graphWidget.name());
130 m_TitleText->setText(graphWidget.name());
131 initTitleTextStyle(*m_TitleText);
131 initTitleTextStyle(*m_TitleText);
132
132
133 // Inits "show x-axis button" in plot overlay
133 // Inits "show x-axis button" in plot overlay
134 m_XAxisPixmap->setLayer(OVERLAY_LAYER);
134 m_XAxisPixmap->setLayer(OVERLAY_LAYER);
135 initXAxisPixmapStyle(*m_XAxisPixmap);
135 initXAxisPixmapStyle(*m_XAxisPixmap);
136
136
137 // Connects pixmap selection to graph x-axis showing/hiding
137 // Connects pixmap selection to graph x-axis showing/hiding
138 QObject::connect(m_XAxisPixmap, &QCPItemPixmap::selectionChanged, [this]() {
138 QObject::connect(m_XAxisPixmap, &QCPItemPixmap::selectionChanged, [this]() {
139 if (m_XAxisPixmap->selected()) {
139 if (m_XAxisPixmap->selected()) {
140 // Changes the selection state and refreshes the x-axis
140 // Changes the selection state and refreshes the x-axis
141 m_ShowXAxis = !m_ShowXAxis;
141 m_ShowXAxis = !m_ShowXAxis;
142 updateXAxisState();
142 updateXAxisState();
143 m_Plot.layer(AXES_LAYER)->replot();
143 m_Plot.layer(AXES_LAYER)->replot();
144
144
145 // Deselects the x-axis pixmap and updates icon
145 // Deselects the x-axis pixmap and updates icon
146 m_XAxisPixmap->setSelected(false);
146 m_XAxisPixmap->setSelected(false);
147 m_XAxisPixmap->setPixmap(
147 m_XAxisPixmap->setPixmap(
148 pixmap(m_ShowXAxis ? HIDE_AXIS_ICON_PATH : SHOW_AXIS_ICON_PATH));
148 pixmap(m_ShowXAxis ? HIDE_AXIS_ICON_PATH : SHOW_AXIS_ICON_PATH));
149 m_Plot.layer(OVERLAY_LAYER)->replot();
149 m_Plot.layer(OVERLAY_LAYER)->replot();
150 }
150 }
151 });
151 });
152 }
152 }
153
153
154 /// Updates state of x-axis according to the current selection of x-axis pixmap
154 /// Updates state of x-axis according to the current selection of x-axis pixmap
155 /// @remarks the method doesn't call plot refresh
155 /// @remarks the method doesn't call plot refresh
156 void updateXAxisState() noexcept
156 void updateXAxisState() noexcept
157 {
157 {
158 m_Plot.xAxis->setTickLabels(m_ShowXAxis);
158 m_Plot.xAxis->setTickLabels(m_ShowXAxis);
159 m_Plot.xAxis->setLabel(m_ShowXAxis ? m_XAxisLabel : QString{});
159 m_Plot.xAxis->setLabel(m_ShowXAxis ? m_XAxisLabel : QString{});
160 }
160 }
161
161
162 QCustomPlot &m_Plot;
162 QCustomPlot &m_Plot;
163 QCPItemTracer *m_PointTracer;
163 QCPItemTracer *m_PointTracer;
164 QTimer m_TracerTimer;
164 QTimer m_TracerTimer;
165 QCPItemPixmap *m_ClosePixmap; /// Graph's close button
165 QCPItemPixmap *m_ClosePixmap; /// Graph's close button
166 QCPItemText *m_TitleText; /// Graph's title
166 QCPItemText *m_TitleText; /// Graph's title
167 QCPItemPixmap *m_XAxisPixmap;
167 QCPItemPixmap *m_XAxisPixmap;
168 bool m_ShowXAxis; /// X-axis properties are shown or hidden
168 bool m_ShowXAxis; /// X-axis properties are shown or hidden
169 QString m_XAxisLabel;
169 QString m_XAxisLabel;
170 SqpColorScale m_ColorScale; /// Color scale used for some types of graphs (as spectrograms)
170 SqpColorScale m_ColorScale; /// Color scale used for some types of graphs (as spectrograms)
171 };
171 };
172
172
173 VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegate(
173 VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegate(
174 VisualizationGraphWidget &graphWidget)
174 VisualizationGraphWidget &graphWidget)
175 : impl{spimpl::make_unique_impl<VisualizationGraphRenderingDelegatePrivate>(graphWidget)}
175 : impl{spimpl::make_unique_impl<VisualizationGraphRenderingDelegatePrivate>(graphWidget)}
176 {
176 {
177 }
177 }
178
178
179 void VisualizationGraphRenderingDelegate::onMouseDoubleClick(QMouseEvent *event) noexcept
179 void VisualizationGraphRenderingDelegate::onMouseDoubleClick(QMouseEvent *event) noexcept
180 {
180 {
181 // Opens color scale editor if color scale is double clicked
181 // Opens color scale editor if color scale is double clicked
182 auto colorScale = dynamic_cast<QCPColorScale *>(impl->m_Plot.layoutElementAt(event->pos()));
182 auto colorScale = dynamic_cast<QCPColorScale *>(impl->m_Plot.layoutElementAt(event->pos()));
183 if (impl->m_ColorScale.m_Scale == colorScale) {
183 if (impl->m_ColorScale.m_Scale == colorScale) {
184 if (ColorScaleEditor{impl->m_ColorScale}.exec() == QDialog::Accepted) {
184 if (ColorScaleEditor{impl->m_ColorScale}.exec() == QDialog::Accepted) {
185 impl->m_Plot.replot();
185 impl->m_Plot.replot();
186 }
186 }
187 }
187 }
188 }
188 }
189
189
190 void VisualizationGraphRenderingDelegate::onMouseMove(QMouseEvent *event) noexcept
190 void VisualizationGraphRenderingDelegate::onMouseMove(QMouseEvent *event) noexcept
191 {
191 {
192 // Cancels pending refresh
192 // Cancels pending refresh
193 impl->m_TracerTimer.disconnect();
193 impl->m_TracerTimer.disconnect();
194
194
195 // Reinits tracers
195 // Reinits tracers
196 impl->m_PointTracer->setGraph(nullptr);
196 impl->m_PointTracer->setGraph(nullptr);
197 impl->m_PointTracer->setVisible(false);
197 impl->m_PointTracer->setVisible(false);
198 impl->m_Plot.replot();
198 impl->m_Plot.replot();
199
199
200 QString tooltip{};
200 QString tooltip{};
201
201
202 // Gets the graph under the mouse position
202 // Gets the graph under the mouse position
203 auto eventPos = event->pos();
203 auto eventPos = event->pos();
204 if (auto graph = qobject_cast<QCPGraph *>(impl->m_Plot.plottableAt(eventPos))) {
204 if (auto graph = qobject_cast<QCPGraph *>(impl->m_Plot.plottableAt(eventPos))) {
205 auto mouseKey = graph->keyAxis()->pixelToCoord(eventPos.x());
205 auto mouseKey = graph->keyAxis()->pixelToCoord(eventPos.x());
206 auto graphData = graph->data();
206 auto graphData = graph->data();
207
207
208 // Gets the closest data point to the mouse
208 // Gets the closest data point to the mouse
209 auto graphDataIt = graphData->findBegin(mouseKey);
209 auto graphDataIt = graphData->findBegin(mouseKey);
210 if (graphDataIt != graphData->constEnd()) {
210 if (graphDataIt != graphData->constEnd()) {
211 // Sets tooltip
211 // Sets tooltip
212 auto key = formatValue(graphDataIt->key, *graph->keyAxis());
212 auto key = formatValue(graphDataIt->key, *graph->keyAxis());
213 auto value = formatValue(graphDataIt->value, *graph->valueAxis());
213 auto value = formatValue(graphDataIt->value, *graph->valueAxis());
214 tooltip = GRAPH_TOOLTIP_FORMAT.arg(key, value);
214 tooltip = GRAPH_TOOLTIP_FORMAT.arg(key, value);
215
215
216 // Displays point tracer
216 // Displays point tracer
217 impl->m_PointTracer->setGraph(graph);
217 impl->m_PointTracer->setGraph(graph);
218 impl->m_PointTracer->setGraphKey(graphDataIt->key);
218 impl->m_PointTracer->setGraphKey(graphDataIt->key);
219 impl->m_PointTracer->setLayer(
219 impl->m_PointTracer->setLayer(
220 impl->m_Plot.layer("main")); // Tracer is set on top of the plot's main layer
220 impl->m_Plot.layer("main")); // Tracer is set on top of the plot's main layer
221 impl->m_PointTracer->setVisible(true);
221 impl->m_PointTracer->setVisible(true);
222 impl->m_Plot.replot();
222 impl->m_Plot.replot();
223 }
223 }
224 }
224 }
225 else if (auto colorMap = qobject_cast<QCPColorMap *>(impl->m_Plot.plottableAt(eventPos))) {
225 else if (auto colorMap = qobject_cast<QCPColorMap *>(impl->m_Plot.plottableAt(eventPos))) {
226 /// @todo
226 // Gets x and y coords
227 tooltip = "TODO";
227 auto x = colorMap->keyAxis()->pixelToCoord(eventPos.x());
228 auto y = colorMap->valueAxis()->pixelToCoord(eventPos.y());
228 }
229 }
229
230
230 if (!tooltip.isEmpty()) {
231 if (!tooltip.isEmpty()) {
231 // Starts timer to show tooltip after timeout
232 // Starts timer to show tooltip after timeout
232 auto showTooltip = [tooltip, eventPos, this]() {
233 auto showTooltip = [tooltip, eventPos, this]() {
233 QToolTip::showText(impl->m_Plot.mapToGlobal(eventPos) + TOOLTIP_OFFSET, tooltip,
234 QToolTip::showText(impl->m_Plot.mapToGlobal(eventPos) + TOOLTIP_OFFSET, tooltip,
234 &impl->m_Plot, TOOLTIP_RECT);
235 &impl->m_Plot, TOOLTIP_RECT);
235 };
236 };
236
237
237 QObject::connect(&impl->m_TracerTimer, &QTimer::timeout, showTooltip);
238 QObject::connect(&impl->m_TracerTimer, &QTimer::timeout, showTooltip);
238 impl->m_TracerTimer.start();
239 impl->m_TracerTimer.start();
239 }
240 }
240 }
241 }
241
242
242 void VisualizationGraphRenderingDelegate::onPlotUpdated() noexcept
243 void VisualizationGraphRenderingDelegate::onPlotUpdated() noexcept
243 {
244 {
244 // Updates color scale bounds
245 // Updates color scale bounds
245 impl->m_ColorScale.updateDataRange();
246 impl->m_ColorScale.updateDataRange();
246 impl->m_Plot.replot();
247 impl->m_Plot.replot();
247 }
248 }
248
249
249 void VisualizationGraphRenderingDelegate::setAxesProperties(
250 void VisualizationGraphRenderingDelegate::setAxesProperties(
250 std::shared_ptr<IDataSeries> dataSeries) noexcept
251 std::shared_ptr<IDataSeries> dataSeries) noexcept
251 {
252 {
252 // Stores x-axis label to be able to retrieve it when x-axis pixmap is unselected
253 // Stores x-axis label to be able to retrieve it when x-axis pixmap is unselected
253 impl->m_XAxisLabel = dataSeries->xAxisUnit().m_Name;
254 impl->m_XAxisLabel = dataSeries->xAxisUnit().m_Name;
254
255
255 auto axisHelper = IAxisHelperFactory::create(dataSeries);
256 auto axisHelper = IAxisHelperFactory::create(dataSeries);
256 axisHelper->setProperties(impl->m_Plot, impl->m_ColorScale);
257 axisHelper->setProperties(impl->m_Plot, impl->m_ColorScale);
257
258
258 // Updates x-axis state
259 // Updates x-axis state
259 impl->updateXAxisState();
260 impl->updateXAxisState();
260
261
261 impl->m_Plot.layer(AXES_LAYER)->replot();
262 impl->m_Plot.layer(AXES_LAYER)->replot();
262 }
263 }
263
264
264 void VisualizationGraphRenderingDelegate::setPlottablesProperties(
265 void VisualizationGraphRenderingDelegate::setPlottablesProperties(
265 std::shared_ptr<IDataSeries> dataSeries, PlottablesMap &plottables) noexcept
266 std::shared_ptr<IDataSeries> dataSeries, PlottablesMap &plottables) noexcept
266 {
267 {
267 auto plottablesHelper = IPlottablesHelperFactory::create(dataSeries);
268 auto plottablesHelper = IPlottablesHelperFactory::create(dataSeries);
268 plottablesHelper->setProperties(plottables);
269 plottablesHelper->setProperties(plottables);
269 }
270 }
270
271
271 void VisualizationGraphRenderingDelegate::showGraphOverlay(bool show) noexcept
272 void VisualizationGraphRenderingDelegate::showGraphOverlay(bool show) noexcept
272 {
273 {
273 auto overlay = impl->m_Plot.layer(OVERLAY_LAYER);
274 auto overlay = impl->m_Plot.layer(OVERLAY_LAYER);
274 overlay->setVisible(show);
275 overlay->setVisible(show);
275 overlay->replot();
276 overlay->replot();
276 }
277 }
General Comments 0
You need to be logged in to leave comments. Login now