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