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