diff --git a/gui/include/Visualization/VisualizationGraphWidget.h b/gui/include/Visualization/VisualizationGraphWidget.h index bc96a90..8ce15f1 100644 --- a/gui/include/Visualization/VisualizationGraphWidget.h +++ b/gui/include/Visualization/VisualizationGraphWidget.h @@ -70,6 +70,7 @@ public: void setYRange(std::shared_ptr variable); SqpRange graphRange() const noexcept; void setGraphRange(const SqpRange &range, bool calibration = false); + void setAutoRangeOnVariableInitialization(bool value); // Zones /// Returns the ranges of all the selection zones on the graph diff --git a/gui/src/Catalogue/CatalogueEventsWidget.cpp b/gui/src/Catalogue/CatalogueEventsWidget.cpp index 702cd0b..3c01c4f 100644 --- a/gui/src/Catalogue/CatalogueEventsWidget.cpp +++ b/gui/src/Catalogue/CatalogueEventsWidget.cpp @@ -27,6 +27,9 @@ Q_LOGGING_CATEGORY(LOG_CatalogueEventsWidget, "CatalogueEventsWidget") /// Fixed size of the validation column const auto VALIDATION_COLUMN_SIZE = 35; +/// Percentage added to the range of a event when it is displayed +const auto EVENT_RANGE_MARGE = 30; // in % + struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate { CatalogueEventsModel *m_Model = nullptr; @@ -185,99 +188,130 @@ struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate { } } + QVector getGraphRanges(const std::shared_ptr &event) + { + // Retrieves the range of each product and the maximum size + QVector graphRanges; + double maxDt = 0; + for (auto eventProduct : event->getEventProducts()) { + SqpRange eventRange; + eventRange.m_TStart = eventProduct.getTStart(); + eventRange.m_TEnd = eventProduct.getTEnd(); + graphRanges << eventRange; + + auto dt = eventRange.m_TEnd - eventRange.m_TStart; + if (dt > maxDt) { + maxDt = dt; + } + } + + // Adds the marge + maxDt *= (100.0 + EVENT_RANGE_MARGE) / 100.0; + + // Corrects the graph ranges so that they all have the same size + QVector correctedGraphRanges; + for (auto range : graphRanges) { + auto dt = range.m_TEnd - range.m_TStart; + auto diff = qAbs((maxDt - dt) / 2.0); + + SqpRange correctedRange; + correctedRange.m_TStart = range.m_TStart - diff; + correctedRange.m_TEnd = range.m_TEnd + diff; + + correctedGraphRanges << correctedRange; + } + + return correctedGraphRanges; + } + void updateForGraphMode(QTreeView *treeView) { auto selectedRows = treeView->selectionModel()->selectedRows(); + if (selectedRows.count() != 1) { + qCWarning(LOG_CatalogueEventsWidget()) + << "updateGraphMode: not compatible with multiple events selected"; + return; + } - if (selectedRows.count() == 1) { - auto event = m_Model->getEvent(selectedRows.first()); - if (m_VisualizationWidget && event) { - if (auto tab = m_VisualizationWidget->currentTabWidget()) { - if (auto zone = tab->getZoneWithName(m_ZoneForGraphMode)) { - - for (auto graph : m_CustomGraphs) { - graph->close(); - auto variables = graph->variables().toVector(); - - QMetaObject::invokeMethod( - &sqpApp->variableController(), "deleteVariables", - Qt::QueuedConnection, - Q_ARG(QVector >, variables)); - } - m_CustomGraphs.clear(); - - QVector graphRanges; - double maxDt = 0; - for (auto eventProduct : event->getEventProducts()) { - SqpRange eventRange; - eventRange.m_TStart = eventProduct.getTStart(); - eventRange.m_TEnd = eventProduct.getTEnd(); - graphRanges << eventRange; - - auto dt = eventRange.m_TEnd - eventRange.m_TStart; - if (dt > maxDt) { - maxDt = dt; - } - } + if (!m_VisualizationWidget) { + qCWarning(LOG_CatalogueEventsWidget()) + << "updateGraphMode: visualization widget not found"; + return; + } + + auto event = m_Model->getEvent(selectedRows.first()); + if (!event) { + // A event product is probably selected + qCInfo(LOG_CatalogueEventsWidget()) << "updateGraphMode: no events are selected"; + return; + } + + auto tab = m_VisualizationWidget->currentTabWidget(); + if (!tab) { + qCWarning(LOG_CatalogueEventsWidget()) + << "updateGraphMode: no tab found in the visualization"; + return; + } - QVector correctedGraphRanges; - for (auto range : graphRanges) { - auto dt = range.m_TEnd - range.m_TStart; - auto diff = qAbs((maxDt - dt) / 2.0); + auto zone = tab->getZoneWithName(m_ZoneForGraphMode); + if (!zone) { + qCWarning(LOG_CatalogueEventsWidget()) << "updateGraphMode: zone not found"; + return; + } - SqpRange correctedRange; - correctedRange.m_TStart = range.m_TStart - diff; - correctedRange.m_TEnd = range.m_TEnd + diff; + // Close the previous graph and delete the asociated variables + for (auto graph : m_CustomGraphs) { + graph->close(); + auto variables = graph->variables().toVector(); - correctedGraphRanges << correctedRange; - } + QMetaObject::invokeMethod(&sqpApp->variableController(), "deleteVariables", + Qt::QueuedConnection, + Q_ARG(QVector >, variables)); + } + m_CustomGraphs.clear(); - auto itRange = correctedGraphRanges.cbegin(); - for (auto eventProduct : event->getEventProducts()) { - auto productId = eventProduct.getProductId(); + // Calculates the range of each graph which will be created + auto graphRange = getGraphRanges(event); - auto range = *itRange; - ++itRange; + // Loops through the event products and create the graph + auto itRange = graphRange.cbegin(); + for (auto eventProduct : event->getEventProducts()) { + auto productId = eventProduct.getProductId(); - auto context = new QObject{treeView}; - QObject::connect( - &sqpApp->variableController(), &VariableController::variableAdded, - context, - [this, zone, context, range, productId](auto variable) { + auto range = *itRange; + ++itRange; - if (variable->metadata() - .value(DataSourceItem::ID_DATA_KEY, "UnknownID") - .toString() - == productId) { - auto graph = zone->createGraph(variable); - m_CustomGraphs << graph; + SqpRange productRange; + productRange.m_TStart = eventProduct.getTStart(); + productRange.m_TEnd = eventProduct.getTEnd(); - graph->setGraphRange(range, true); + auto context = new QObject{treeView}; + QObject::connect( + &sqpApp->variableController(), &VariableController::variableAdded, context, + [this, zone, context, range, productRange, productId](auto variable) { - delete context; // removes the connection - } - }, - Qt::QueuedConnection); + if (variable->metadata().value(DataSourceItem::ID_DATA_KEY).toString() + == productId) { + auto graph = zone->createGraph(variable); + graph->setAutoRangeOnVariableInitialization(false); - QMetaObject::invokeMethod( - &sqpApp->dataSourceController(), "requestVariableFromProductIdKey", - Qt::QueuedConnection, Q_ARG(QString, productId)); - } + graph->addSelectionZones({productRange}); + m_CustomGraphs << graph; + + graph->setGraphRange(range, true); + + // Removes the graph from the graph list if it is closed manually + QObject::connect(graph, &VisualizationGraphWidget::destroyed, + [this, graph]() { m_CustomGraphs.removeAll(graph); }); + + delete context; // removes the connection } - } - else { - qCWarning(LOG_CatalogueEventsWidget()) - << "updateGraphMode: no tab found in the visualization"; - } - } - else { - qCWarning(LOG_CatalogueEventsWidget()) - << "updateGraphMode: visualization widget not found"; - } - } - else { - qCWarning(LOG_CatalogueEventsWidget()) - << "updateGraphMode: not compatible with multiple events selected"; + }, + Qt::QueuedConnection); + + QMetaObject::invokeMethod(&sqpApp->dataSourceController(), + "requestVariableFromProductIdKey", Qt::QueuedConnection, + Q_ARG(QString, productId)); } } diff --git a/gui/src/Visualization/VisualizationGraphWidget.cpp b/gui/src/Visualization/VisualizationGraphWidget.cpp index 716e420..1b7bbe8 100644 --- a/gui/src/Visualization/VisualizationGraphWidget.cpp +++ b/gui/src/Visualization/VisualizationGraphWidget.cpp @@ -94,6 +94,8 @@ struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate { bool m_HasMovedMouse = false; // Indicates if the mouse moved in a releaseMouse even + bool m_VariableAutoRangeOnInit = true; + void startDrawingRect(const QPoint &pos, QCustomPlot &plot) { removeDrawingRect(plot); @@ -314,7 +316,10 @@ void VisualizationGraphWidget::addVariable(std::shared_ptr variable, S if (auto var = varW.lock()) { // If the variable is the first added in the graph, we load its range auto firstVariableInGraph = range == INVALID_RANGE; - auto loadedRange = firstVariableInGraph ? var->range() : range; + auto loadedRange = graphRange(); + if (impl->m_VariableAutoRangeOnInit) { + loadedRange = firstVariableInGraph ? var->range() : range; + } loadRange(var, loadedRange); setYRange(var); } @@ -406,6 +411,11 @@ void VisualizationGraphWidget::setGraphRange(const SqpRange &range, bool calibra qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange END"); } +void VisualizationGraphWidget::setAutoRangeOnVariableInitialization(bool value) +{ + impl->m_VariableAutoRangeOnInit = value; +} + QVector VisualizationGraphWidget::selectionZoneRanges() const { QVector ranges;