##// END OF EJS Templates
Handles QCustomPlot plottables for vectors (2)...
Alexandre Leroux -
r546:dd4269fbedea
parent child
Show More
@@ -1,320 +1,313
1 1 #include "Visualization/VisualizationGraphWidget.h"
2 2 #include "Visualization/IVisualizationWidgetVisitor.h"
3 #include "Visualization/VisualizationDefs.h"
3 4 #include "Visualization/VisualizationGraphHelper.h"
4 5 #include "Visualization/VisualizationGraphRenderingDelegate.h"
5 6 #include "ui_VisualizationGraphWidget.h"
6 7
7 8 #include <Data/ArrayData.h>
8 9 #include <Data/IDataSeries.h>
9 10 #include <Settings/SqpSettingsDefs.h>
10 11 #include <SqpApplication.h>
11 12 #include <Variable/Variable.h>
12 13 #include <Variable/VariableController.h>
13 14
14 15 #include <unordered_map>
15 16
16 17 Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget")
17 18
18 19 namespace {
19 20
20 21 /// Key pressed to enable zoom on horizontal axis
21 22 const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier;
22 23
23 24 /// Key pressed to enable zoom on vertical axis
24 25 const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier;
25 26
26 27 } // namespace
27 28
28 29 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
29 30
30 31 explicit VisualizationGraphWidgetPrivate()
31 32 : m_DoAcquisition{true}, m_IsCalibration{false}, m_RenderingDelegate{nullptr}
32 33 {
33 34 }
34 35
35 36 // 1 variable -> n qcpplot
36 std::multimap<std::shared_ptr<Variable>, QCPAbstractPlottable *> m_VariableToPlotMultiMap;
37 std::map<std::shared_ptr<Variable>, PlottablesMap> m_VariableToPlotMultiMap;
37 38 bool m_DoAcquisition;
38 39 bool m_IsCalibration;
39 40 QCPItemTracer *m_TextTracer;
40 41 /// Delegate used to attach rendering features to the plot
41 42 std::unique_ptr<VisualizationGraphRenderingDelegate> m_RenderingDelegate;
42 43 };
43 44
44 45 VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent)
45 46 : QWidget{parent},
46 47 ui{new Ui::VisualizationGraphWidget},
47 48 impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()}
48 49 {
49 50 ui->setupUi(this);
50 51
51 52 // The delegate must be initialized after the ui as it uses the plot
52 53 impl->m_RenderingDelegate = std::make_unique<VisualizationGraphRenderingDelegate>(*ui->widget);
53 54
54 55 ui->graphNameLabel->setText(name);
55 56
56 57 // 'Close' options : widget is deleted when closed
57 58 setAttribute(Qt::WA_DeleteOnClose);
58 59 connect(ui->closeButton, &QToolButton::clicked, this, &VisualizationGraphWidget::close);
59 60 ui->closeButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
60 61
61 62 // Set qcpplot properties :
62 63 // - Drag (on x-axis) and zoom are enabled
63 64 // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation
64 65 ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
65 66 ui->widget->axisRect()->setRangeDrag(Qt::Horizontal);
66 67
67 68 connect(ui->widget, &QCustomPlot::mousePress, this, &VisualizationGraphWidget::onMousePress);
68 69 connect(ui->widget, &QCustomPlot::mouseRelease, this,
69 70 &VisualizationGraphWidget::onMouseRelease);
70 71 connect(ui->widget, &QCustomPlot::mouseMove, this, &VisualizationGraphWidget::onMouseMove);
71 72 connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel);
72 73 connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>(
73 74 &QCPAxis::rangeChanged),
74 75 this, &VisualizationGraphWidget::onRangeChanged, Qt::DirectConnection);
75 76
76 77 // Activates menu when right clicking on the graph
77 78 ui->widget->setContextMenuPolicy(Qt::CustomContextMenu);
78 79 connect(ui->widget, &QCustomPlot::customContextMenuRequested, this,
79 80 &VisualizationGraphWidget::onGraphMenuRequested);
80 81
81 82 connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(),
82 83 &VariableController::onRequestDataLoading);
83 84
84 85 connect(&sqpApp->variableController(), &VariableController::updateVarDisplaying, this,
85 86 &VisualizationGraphWidget::onUpdateVarDisplaying);
86 87 }
87 88
88 89
89 90 VisualizationGraphWidget::~VisualizationGraphWidget()
90 91 {
91 92 delete ui;
92 93 }
93 94
94 95 void VisualizationGraphWidget::enableAcquisition(bool enable)
95 96 {
96 97 impl->m_DoAcquisition = enable;
97 98 }
98 99
99 100 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable, SqpRange range)
100 101 {
101 102 // Uses delegate to create the qcpplot components according to the variable
102 auto createdPlottables = VisualizationGraphHelper::createV2(variable, *ui->widget);
103
104 for (auto createdPlottable : qAsConst(createdPlottables)) {
105 impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable});
106 }
103 auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget);
104 impl->m_VariableToPlotMultiMap.insert({variable, std::move(createdPlottables)});
107 105
108 106 connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated()));
109 107
110 108 auto varRange = variable->range();
111 109
112 110 this->enableAcquisition(false);
113 111 this->setGraphRange(range);
114 112 this->enableAcquisition(true);
115 113
116 114 emit requestDataLoading(QVector<std::shared_ptr<Variable> >() << variable, range, varRange,
117 115 false);
118 116
119 117 emit variableAdded(variable);
120 118 }
121 119
122 120 void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept
123 121 {
124 122 // Each component associated to the variable :
125 123 // - is removed from qcpplot (which deletes it)
126 124 // - is no longer referenced in the map
127 auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable);
128 for (auto it = componentsIt.first; it != componentsIt.second;) {
129 ui->widget->removePlottable(it->second);
130 it = impl->m_VariableToPlotMultiMap.erase(it);
125 auto variableIt = impl->m_VariableToPlotMultiMap.find(variable);
126 if (variableIt != impl->m_VariableToPlotMultiMap.cend()) {
127 auto &plottablesMap = variableIt->second;
128
129 for (auto plottableIt = plottablesMap.cbegin(), plottableEnd = plottablesMap.cend();
130 plottableIt != plottableEnd;) {
131 ui->widget->removePlottable(plottableIt->second);
132 plottableIt = plottablesMap.erase(plottableIt);
133 }
134
135 impl->m_VariableToPlotMultiMap.erase(variableIt);
131 136 }
132 137
133 138 // Updates graph
134 139 ui->widget->replot();
135 140 }
136 141
137 142 void VisualizationGraphWidget::setRange(std::shared_ptr<Variable> variable, const SqpRange &range)
138 143 {
139 144 // Note: in case of different axes that depends on variable, we could start with a code like
140 145 // that:
141 146 // auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable);
142 147 // for (auto it = componentsIt.first; it != componentsIt.second;) {
143 148 // }
144 149 ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd);
145 150 ui->widget->replot();
146 151 }
147 152
148 153 void VisualizationGraphWidget::setYRange(const SqpRange &range)
149 154 {
150 155 ui->widget->yAxis->setRange(range.m_TStart, range.m_TEnd);
151 156 }
152 157
153 158 SqpRange VisualizationGraphWidget::graphRange() const noexcept
154 159 {
155 160 auto graphRange = ui->widget->xAxis->range();
156 161 return SqpRange{graphRange.lower, graphRange.upper};
157 162 }
158 163
159 164 void VisualizationGraphWidget::setGraphRange(const SqpRange &range)
160 165 {
161 166 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange START");
162 167 ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd);
163 168 ui->widget->replot();
164 169 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange END");
165 170 }
166 171
167 172 void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor)
168 173 {
169 174 if (visitor) {
170 175 visitor->visit(this);
171 176 }
172 177 else {
173 178 qCCritical(LOG_VisualizationGraphWidget())
174 179 << tr("Can't visit widget : the visitor is null");
175 180 }
176 181 }
177 182
178 183 bool VisualizationGraphWidget::canDrop(const Variable &variable) const
179 184 {
180 185 /// @todo : for the moment, a graph can always accomodate a variable
181 186 Q_UNUSED(variable);
182 187 return true;
183 188 }
184 189
185 190 bool VisualizationGraphWidget::contains(const Variable &variable) const
186 191 {
187 192 // Finds the variable among the keys of the map
188 193 auto variablePtr = &variable;
189 194 auto findVariable
190 195 = [variablePtr](const auto &entry) { return variablePtr == entry.first.get(); };
191 196
192 197 auto end = impl->m_VariableToPlotMultiMap.cend();
193 198 auto it = std::find_if(impl->m_VariableToPlotMultiMap.cbegin(), end, findVariable);
194 199 return it != end;
195 200 }
196 201
197 202 QString VisualizationGraphWidget::name() const
198 203 {
199 204 return ui->graphNameLabel->text();
200 205 }
201 206
202 207 void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept
203 208 {
204 209 QMenu graphMenu{};
205 210
206 211 // Iterates on variables (unique keys)
207 212 for (auto it = impl->m_VariableToPlotMultiMap.cbegin(),
208 213 end = impl->m_VariableToPlotMultiMap.cend();
209 214 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
210 215 // 'Remove variable' action
211 216 graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()),
212 217 [ this, var = it->first ]() { removeVariable(var); });
213 218 }
214 219
215 220 if (!graphMenu.isEmpty()) {
216 221 graphMenu.exec(mapToGlobal(pos));
217 222 }
218 223 }
219 224
220 225 void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2)
221 226 {
222 227 qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: VisualizationGraphWidget::onRangeChanged")
223 228 << QThread::currentThread()->objectName() << "DoAcqui"
224 229 << impl->m_DoAcquisition;
225 230
226 231 auto graphRange = SqpRange{t1.lower, t1.upper};
227 232 auto oldGraphRange = SqpRange{t2.lower, t2.upper};
228 233
229 234 if (impl->m_DoAcquisition) {
230 235 QVector<std::shared_ptr<Variable> > variableUnderGraphVector;
231 236
232 237 for (auto it = impl->m_VariableToPlotMultiMap.begin(),
233 238 end = impl->m_VariableToPlotMultiMap.end();
234 239 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
235 240 variableUnderGraphVector.push_back(it->first);
236 241 }
237 242 emit requestDataLoading(std::move(variableUnderGraphVector), graphRange, oldGraphRange,
238 243 !impl->m_IsCalibration);
239 244
240 245 if (!impl->m_IsCalibration) {
241 246 qCDebug(LOG_VisualizationGraphWidget())
242 247 << tr("TORM: VisualizationGraphWidget::Synchronize notify !!")
243 248 << QThread::currentThread()->objectName() << graphRange << oldGraphRange;
244 249 emit synchronize(graphRange, oldGraphRange);
245 250 }
246 251 }
247 252 }
248 253
249 254 void VisualizationGraphWidget::onMouseMove(QMouseEvent *event) noexcept
250 255 {
251 256 // Handles plot rendering when mouse is moving
252 257 impl->m_RenderingDelegate->onMouseMove(event);
253 258 }
254 259
255 260 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
256 261 {
257 262 auto zoomOrientations = QFlags<Qt::Orientation>{};
258 263
259 264 // Lambda that enables a zoom orientation if the key modifier related to this orientation
260 265 // has
261 266 // been pressed
262 267 auto enableOrientation
263 268 = [&zoomOrientations, event](const auto &orientation, const auto &modifier) {
264 269 auto orientationEnabled = event->modifiers().testFlag(modifier);
265 270 zoomOrientations.setFlag(orientation, orientationEnabled);
266 271 };
267 272 enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER);
268 273 enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER);
269 274
270 275 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
271 276 }
272 277
273 278 void VisualizationGraphWidget::onMousePress(QMouseEvent *event) noexcept
274 279 {
275 280 impl->m_IsCalibration = event->modifiers().testFlag(Qt::ControlModifier);
276 281 }
277 282
278 283 void VisualizationGraphWidget::onMouseRelease(QMouseEvent *event) noexcept
279 284 {
280 285 impl->m_IsCalibration = false;
281 286 }
282 287
283 288 void VisualizationGraphWidget::onDataCacheVariableUpdated()
284 289 {
285 // NOTE:
286 // We don't want to call the method for each component of a variable unitarily, but for
287 // all
288 // its components at once (eg its three components in the case of a vector).
289
290 // The unordered_multimap does not do this easily, so the question is whether to:
291 // - use an ordered_multimap and the algos of std to group the values by key
292 // - use a map (unique keys) and store as values directly the list of components
293
294 290 auto graphRange = ui->widget->xAxis->range();
295 291 auto dateTime = SqpRange{graphRange.lower, graphRange.upper};
296 292
297 for (auto it = impl->m_VariableToPlotMultiMap.cbegin();
298 it != impl->m_VariableToPlotMultiMap.cend(); ++it) {
299 auto variable = it->first;
293 for (auto &variableEntry : impl->m_VariableToPlotMultiMap) {
294 auto variable = variableEntry.first;
300 295 qCDebug(LOG_VisualizationGraphWidget())
301 296 << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" << variable->range();
302 297 qCDebug(LOG_VisualizationGraphWidget())
303 298 << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime;
304 299 if (dateTime.contains(variable->range()) || dateTime.intersect(variable->range())) {
305
306 VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second,
307 variable->dataSeries(), variable->range());
300 VisualizationGraphHelper::updateData(variableEntry.second, variable->dataSeries().get(),
301 variable->range());
308 302 }
309 303 }
310 304 }
311 305
312 306 void VisualizationGraphWidget::onUpdateVarDisplaying(std::shared_ptr<Variable> variable,
313 307 const SqpRange &range)
314 308 {
315 auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable);
316 for (auto it = componentsIt.first; it != componentsIt.second;) {
317 VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second,
318 variable->dataSeries(), range);
309 auto it = impl->m_VariableToPlotMultiMap.find(variable);
310 if (it != impl->m_VariableToPlotMultiMap.end()) {
311 VisualizationGraphHelper::updateData(it->second, variable->dataSeries().get(), range);
319 312 }
320 313 }
General Comments 0
You need to be logged in to leave comments. Login now