##// END OF EJS Templates
refactoring + fix created graph range
trabillard -
r1346:b5241a465dc2
parent child
Show More
@@ -70,6 +70,7 public:
70 70 void setYRange(std::shared_ptr<Variable> variable);
71 71 SqpRange graphRange() const noexcept;
72 72 void setGraphRange(const SqpRange &range, bool calibration = false);
73 void setAutoRangeOnVariableInitialization(bool value);
73 74
74 75 // Zones
75 76 /// Returns the ranges of all the selection zones on the graph
@@ -27,6 +27,9 Q_LOGGING_CATEGORY(LOG_CatalogueEventsWidget, "CatalogueEventsWidget")
27 27 /// Fixed size of the validation column
28 28 const auto VALIDATION_COLUMN_SIZE = 35;
29 29
30 /// Percentage added to the range of a event when it is displayed
31 const auto EVENT_RANGE_MARGE = 30; // in %
32
30 33 struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate {
31 34
32 35 CatalogueEventsModel *m_Model = nullptr;
@@ -185,27 +188,9 struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate {
185 188 }
186 189 }
187 190
188 void updateForGraphMode(QTreeView *treeView)
191 QVector<SqpRange> getGraphRanges(const std::shared_ptr<DBEvent> &event)
189 192 {
190 auto selectedRows = treeView->selectionModel()->selectedRows();
191
192 if (selectedRows.count() == 1) {
193 auto event = m_Model->getEvent(selectedRows.first());
194 if (m_VisualizationWidget && event) {
195 if (auto tab = m_VisualizationWidget->currentTabWidget()) {
196 if (auto zone = tab->getZoneWithName(m_ZoneForGraphMode)) {
197
198 for (auto graph : m_CustomGraphs) {
199 graph->close();
200 auto variables = graph->variables().toVector();
201
202 QMetaObject::invokeMethod(
203 &sqpApp->variableController(), "deleteVariables",
204 Qt::QueuedConnection,
205 Q_ARG(QVector<std::shared_ptr<Variable> >, variables));
206 }
207 m_CustomGraphs.clear();
208
193 // Retrieves the range of each product and the maximum size
209 194 QVector<SqpRange> graphRanges;
210 195 double maxDt = 0;
211 196 for (auto eventProduct : event->getEventProducts()) {
@@ -220,6 +205,10 struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate {
220 205 }
221 206 }
222 207
208 // Adds the marge
209 maxDt *= (100.0 + EVENT_RANGE_MARGE) / 100.0;
210
211 // Corrects the graph ranges so that they all have the same size
223 212 QVector<SqpRange> correctedGraphRanges;
224 213 for (auto range : graphRanges) {
225 214 auto dt = range.m_TEnd - range.m_TStart;
@@ -232,52 +221,97 struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate {
232 221 correctedGraphRanges << correctedRange;
233 222 }
234 223
235 auto itRange = correctedGraphRanges.cbegin();
224 return correctedGraphRanges;
225 }
226
227 void updateForGraphMode(QTreeView *treeView)
228 {
229 auto selectedRows = treeView->selectionModel()->selectedRows();
230 if (selectedRows.count() != 1) {
231 qCWarning(LOG_CatalogueEventsWidget())
232 << "updateGraphMode: not compatible with multiple events selected";
233 return;
234 }
235
236 if (!m_VisualizationWidget) {
237 qCWarning(LOG_CatalogueEventsWidget())
238 << "updateGraphMode: visualization widget not found";
239 return;
240 }
241
242 auto event = m_Model->getEvent(selectedRows.first());
243 if (!event) {
244 // A event product is probably selected
245 qCInfo(LOG_CatalogueEventsWidget()) << "updateGraphMode: no events are selected";
246 return;
247 }
248
249 auto tab = m_VisualizationWidget->currentTabWidget();
250 if (!tab) {
251 qCWarning(LOG_CatalogueEventsWidget())
252 << "updateGraphMode: no tab found in the visualization";
253 return;
254 }
255
256 auto zone = tab->getZoneWithName(m_ZoneForGraphMode);
257 if (!zone) {
258 qCWarning(LOG_CatalogueEventsWidget()) << "updateGraphMode: zone not found";
259 return;
260 }
261
262 // Close the previous graph and delete the asociated variables
263 for (auto graph : m_CustomGraphs) {
264 graph->close();
265 auto variables = graph->variables().toVector();
266
267 QMetaObject::invokeMethod(&sqpApp->variableController(), "deleteVariables",
268 Qt::QueuedConnection,
269 Q_ARG(QVector<std::shared_ptr<Variable> >, variables));
270 }
271 m_CustomGraphs.clear();
272
273 // Calculates the range of each graph which will be created
274 auto graphRange = getGraphRanges(event);
275
276 // Loops through the event products and create the graph
277 auto itRange = graphRange.cbegin();
236 278 for (auto eventProduct : event->getEventProducts()) {
237 279 auto productId = eventProduct.getProductId();
238 280
239 281 auto range = *itRange;
240 282 ++itRange;
241 283
284 SqpRange productRange;
285 productRange.m_TStart = eventProduct.getTStart();
286 productRange.m_TEnd = eventProduct.getTEnd();
287
242 288 auto context = new QObject{treeView};
243 289 QObject::connect(
244 &sqpApp->variableController(), &VariableController::variableAdded,
245 context,
246 [this, zone, context, range, productId](auto variable) {
290 &sqpApp->variableController(), &VariableController::variableAdded, context,
291 [this, zone, context, range, productRange, productId](auto variable) {
247 292
248 if (variable->metadata()
249 .value(DataSourceItem::ID_DATA_KEY, "UnknownID")
250 .toString()
293 if (variable->metadata().value(DataSourceItem::ID_DATA_KEY).toString()
251 294 == productId) {
252 295 auto graph = zone->createGraph(variable);
296 graph->setAutoRangeOnVariableInitialization(false);
297
298 graph->addSelectionZones({productRange});
253 299 m_CustomGraphs << graph;
254 300
255 301 graph->setGraphRange(range, true);
256 302
303 // Removes the graph from the graph list if it is closed manually
304 QObject::connect(graph, &VisualizationGraphWidget::destroyed,
305 [this, graph]() { m_CustomGraphs.removeAll(graph); });
306
257 307 delete context; // removes the connection
258 308 }
259 309 },
260 310 Qt::QueuedConnection);
261 311
262 QMetaObject::invokeMethod(
263 &sqpApp->dataSourceController(), "requestVariableFromProductIdKey",
264 Qt::QueuedConnection, Q_ARG(QString, productId));
265 }
266 }
267 }
268 else {
269 qCWarning(LOG_CatalogueEventsWidget())
270 << "updateGraphMode: no tab found in the visualization";
271 }
272 }
273 else {
274 qCWarning(LOG_CatalogueEventsWidget())
275 << "updateGraphMode: visualization widget not found";
276 }
277 }
278 else {
279 qCWarning(LOG_CatalogueEventsWidget())
280 << "updateGraphMode: not compatible with multiple events selected";
312 QMetaObject::invokeMethod(&sqpApp->dataSourceController(),
313 "requestVariableFromProductIdKey", Qt::QueuedConnection,
314 Q_ARG(QString, productId));
281 315 }
282 316 }
283 317
@@ -94,6 +94,8 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
94 94
95 95 bool m_HasMovedMouse = false; // Indicates if the mouse moved in a releaseMouse even
96 96
97 bool m_VariableAutoRangeOnInit = true;
98
97 99 void startDrawingRect(const QPoint &pos, QCustomPlot &plot)
98 100 {
99 101 removeDrawingRect(plot);
@@ -314,7 +316,10 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable, S
314 316 if (auto var = varW.lock()) {
315 317 // If the variable is the first added in the graph, we load its range
316 318 auto firstVariableInGraph = range == INVALID_RANGE;
317 auto loadedRange = firstVariableInGraph ? var->range() : range;
319 auto loadedRange = graphRange();
320 if (impl->m_VariableAutoRangeOnInit) {
321 loadedRange = firstVariableInGraph ? var->range() : range;
322 }
318 323 loadRange(var, loadedRange);
319 324 setYRange(var);
320 325 }
@@ -406,6 +411,11 void VisualizationGraphWidget::setGraphRange(const SqpRange &range, bool calibra
406 411 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange END");
407 412 }
408 413
414 void VisualizationGraphWidget::setAutoRangeOnVariableInitialization(bool value)
415 {
416 impl->m_VariableAutoRangeOnInit = value;
417 }
418
409 419 QVector<SqpRange> VisualizationGraphWidget::selectionZoneRanges() const
410 420 {
411 421 QVector<SqpRange> ranges;
General Comments 0
You need to be logged in to leave comments. Login now