VisualizationGraphWidget.cpp
307 lines
| 11.6 KiB
| text/x-c
|
CppLexer
r95 | #include "Visualization/VisualizationGraphWidget.h" | |||
|
r207 | #include "Visualization/IVisualizationWidgetVisitor.h" | ||
r243 | #include "Visualization/VisualizationGraphHelper.h" | |||
|
r480 | #include "Visualization/VisualizationGraphRenderingDelegate.h" | ||
r58 | #include "ui_VisualizationGraphWidget.h" | |||
r235 | #include <Data/ArrayData.h> | |||
#include <Data/IDataSeries.h> | ||||
|
r470 | #include <Settings/SqpSettingsDefs.h> | ||
r235 | #include <SqpApplication.h> | |||
r118 | #include <Variable/Variable.h> | |||
r235 | #include <Variable/VariableController.h> | |||
r118 | #include <unordered_map> | |||
|
r219 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget") | ||
|
r179 | namespace { | ||
/// Key pressed to enable zoom on horizontal axis | ||||
const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier; | ||||
/// Key pressed to enable zoom on vertical axis | ||||
const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier; | ||||
} // namespace | ||||
r118 | struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate { | |||
|
r480 | explicit VisualizationGraphWidgetPrivate() | ||
r539 | : m_DoAcquisition{true}, m_IsCalibration{false}, m_RenderingDelegate{nullptr} | |||
|
r480 | { | ||
} | ||||
r445 | ||||
r118 | // 1 variable -> n qcpplot | |||
|
r270 | std::multimap<std::shared_ptr<Variable>, QCPAbstractPlottable *> m_VariableToPlotMultiMap; | ||
r539 | bool m_DoAcquisition; | |||
r445 | bool m_IsCalibration; | |||
|
r480 | QCPItemTracer *m_TextTracer; | ||
/// Delegate used to attach rendering features to the plot | ||||
std::unique_ptr<VisualizationGraphRenderingDelegate> m_RenderingDelegate; | ||||
r118 | }; | |||
|
r205 | VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent) | ||
r120 | : QWidget{parent}, | |||
ui{new Ui::VisualizationGraphWidget}, | ||||
r118 | impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()} | |||
r58 | { | |||
ui->setupUi(this); | ||||
|
r178 | |||
|
r480 | // The delegate must be initialized after the ui as it uses the plot | ||
impl->m_RenderingDelegate = std::make_unique<VisualizationGraphRenderingDelegate>(*ui->widget); | ||||
|
r266 | ui->graphNameLabel->setText(name); | ||
// 'Close' options : widget is deleted when closed | ||||
setAttribute(Qt::WA_DeleteOnClose); | ||||
connect(ui->closeButton, &QToolButton::clicked, this, &VisualizationGraphWidget::close); | ||||
ui->closeButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton)); | ||||
|
r196 | |||
|
r178 | // Set qcpplot properties : | ||
|
r180 | // - Drag (on x-axis) and zoom are enabled | ||
|
r179 | // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation | ||
|
r178 | ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); | ||
|
r180 | ui->widget->axisRect()->setRangeDrag(Qt::Horizontal); | ||
|
r481 | |||
r445 | connect(ui->widget, &QCustomPlot::mousePress, this, &VisualizationGraphWidget::onMousePress); | |||
connect(ui->widget, &QCustomPlot::mouseRelease, this, | ||||
&VisualizationGraphWidget::onMouseRelease); | ||||
|
r481 | connect(ui->widget, &QCustomPlot::mouseMove, this, &VisualizationGraphWidget::onMouseMove); | ||
|
r179 | connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel); | ||
r444 | connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>( | |||
&QCPAxis::rangeChanged), | ||||
r445 | this, &VisualizationGraphWidget::onRangeChanged, Qt::DirectConnection); | |||
|
r269 | |||
// Activates menu when right clicking on the graph | ||||
ui->widget->setContextMenuPolicy(Qt::CustomContextMenu); | ||||
connect(ui->widget, &QCustomPlot::customContextMenuRequested, this, | ||||
&VisualizationGraphWidget::onGraphMenuRequested); | ||||
r298 | ||||
connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(), | ||||
&VariableController::onRequestDataLoading); | ||||
r58 | } | |||
|
r227 | |||
r58 | VisualizationGraphWidget::~VisualizationGraphWidget() | |||
{ | ||||
delete ui; | ||||
} | ||||
r118 | ||||
r539 | void VisualizationGraphWidget::enableAcquisition(bool enable) | |||
r444 | { | |||
r539 | impl->m_DoAcquisition = enable; | |||
r444 | } | |||
r118 | void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable) | |||
{ | ||||
r540 | auto calibrationState = impl->m_IsCalibration; | |||
impl->m_IsCalibration = true; | ||||
|
r184 | // Uses delegate to create the qcpplot components according to the variable | ||
r243 | auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); | |||
r540 | impl->m_IsCalibration = calibrationState; | |||
|
r184 | |||
for (auto createdPlottable : qAsConst(createdPlottables)) { | ||||
r235 | impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable}); | |||
|
r184 | } | ||
r235 | ||||
r298 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); | |||
r540 | ||||
emit variableAdded(variable); | ||||
r118 | } | |||
r314 | void VisualizationGraphWidget::addVariableUsingGraph(std::shared_ptr<Variable> variable) | |||
{ | ||||
r540 | // Uses delegate to create the qcpplot components according to the variable | |||
this->addVariable(variable); | ||||
r314 | ||||
r540 | // Request range for the variable | |||
auto graphRange = ui->widget->xAxis->range(); | ||||
r314 | ||||
r540 | emit requestDataLoading(QVector<std::shared_ptr<Variable> >() << variable, | |||
SqpRange{graphRange.lower, graphRange.upper}, variable->range(), false); | ||||
r314 | } | |||
|
r270 | void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept | ||
{ | ||||
|
r271 | // Each component associated to the variable : | ||
// - is removed from qcpplot (which deletes it) | ||||
// - is no longer referenced in the map | ||||
auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable); | ||||
for (auto it = componentsIt.first; it != componentsIt.second;) { | ||||
ui->widget->removePlottable(it->second); | ||||
it = impl->m_VariableToPlotMultiMap.erase(it); | ||||
} | ||||
// Updates graph | ||||
ui->widget->replot(); | ||||
|
r270 | } | ||
r512 | void VisualizationGraphWidget::setRange(std::shared_ptr<Variable> variable, const SqpRange &range) | |||
r438 | { | |||
r447 | // Note: in case of different axes that depends on variable, we could start with a code like | |||
// that: | ||||
r438 | // auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable); | |||
// for (auto it = componentsIt.first; it != componentsIt.second;) { | ||||
// } | ||||
ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd); | ||||
r444 | ui->widget->replot(); | |||
} | ||||
r512 | SqpRange VisualizationGraphWidget::graphRange() const noexcept | |||
r444 | { | |||
auto grapheRange = ui->widget->xAxis->range(); | ||||
r512 | return SqpRange{grapheRange.lower, grapheRange.upper}; | |||
r444 | } | |||
r512 | void VisualizationGraphWidget::setGraphRange(const SqpRange &range) | |||
r444 | { | |||
r445 | qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange START"); | |||
r444 | ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd); | |||
ui->widget->replot(); | ||||
qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange END"); | ||||
r438 | } | |||
|
r207 | void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor) | ||
r118 | { | |||
|
r208 | if (visitor) { | ||
visitor->visit(this); | ||||
} | ||||
|
r219 | else { | ||
qCCritical(LOG_VisualizationGraphWidget()) | ||||
<< tr("Can't visit widget : the visitor is null"); | ||||
} | ||||
r118 | } | |||
|
r209 | bool VisualizationGraphWidget::canDrop(const Variable &variable) const | ||
{ | ||||
/// @todo : for the moment, a graph can always accomodate a variable | ||||
Q_UNUSED(variable); | ||||
return true; | ||||
} | ||||
|
r327 | bool VisualizationGraphWidget::contains(const Variable &variable) const | ||
{ | ||||
// Finds the variable among the keys of the map | ||||
auto variablePtr = &variable; | ||||
auto findVariable | ||||
= [variablePtr](const auto &entry) { return variablePtr == entry.first.get(); }; | ||||
auto end = impl->m_VariableToPlotMultiMap.cend(); | ||||
auto it = std::find_if(impl->m_VariableToPlotMultiMap.cbegin(), end, findVariable); | ||||
return it != end; | ||||
} | ||||
r119 | QString VisualizationGraphWidget::name() const | |||
r118 | { | |||
|
r266 | return ui->graphNameLabel->text(); | ||
r118 | } | |||
|
r269 | void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept | ||
r118 | { | |||
|
r269 | QMenu graphMenu{}; | ||
|
r270 | // Iterates on variables (unique keys) | ||
for (auto it = impl->m_VariableToPlotMultiMap.cbegin(), | ||||
end = impl->m_VariableToPlotMultiMap.cend(); | ||||
it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) { | ||||
// 'Remove variable' action | ||||
graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()), | ||||
[ this, var = it->first ]() { removeVariable(var); }); | ||||
|
r196 | } | ||
|
r269 | |||
if (!graphMenu.isEmpty()) { | ||||
graphMenu.exec(mapToGlobal(pos)); | ||||
|
r196 | } | ||
r118 | } | |||
|
r179 | |||
r444 | void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2) | |||
|
r227 | { | ||
r539 | qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: VisualizationGraphWidget::onRangeChanged") | |||
<< QThread::currentThread()->objectName() << "DoAcqui" | ||||
<< impl->m_DoAcquisition; | ||||
r444 | ||||
r539 | auto graphRange = SqpRange{t1.lower, t1.upper}; | |||
auto oldGraphRange = SqpRange{t2.lower, t2.upper}; | ||||
r235 | ||||
r539 | if (impl->m_DoAcquisition) { | |||
QVector<std::shared_ptr<Variable> > variableUnderGraphVector; | ||||
r258 | ||||
r539 | for (auto it = impl->m_VariableToPlotMultiMap.begin(), | |||
end = impl->m_VariableToPlotMultiMap.end(); | ||||
it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) { | ||||
variableUnderGraphVector.push_back(it->first); | ||||
r235 | } | |||
r539 | emit requestDataLoading(std::move(variableUnderGraphVector), graphRange, oldGraphRange, | |||
!impl->m_IsCalibration); | ||||
if (!impl->m_IsCalibration) { | ||||
r542 | qCDebug(LOG_VisualizationGraphWidget()) | |||
r539 | << tr("TORM: VisualizationGraphWidget::Synchronize notify !!") | |||
r540 | << QThread::currentThread()->objectName() << graphRange << oldGraphRange; | |||
r539 | emit synchronize(graphRange, oldGraphRange); | |||
r318 | } | |||
|
r227 | } | ||
} | ||||
|
r481 | void VisualizationGraphWidget::onMouseMove(QMouseEvent *event) noexcept | ||
{ | ||||
// Handles plot rendering when mouse is moving | ||||
impl->m_RenderingDelegate->onMouseMove(event); | ||||
} | ||||
|
r179 | void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept | ||
{ | ||||
auto zoomOrientations = QFlags<Qt::Orientation>{}; | ||||
r260 | // Lambda that enables a zoom orientation if the key modifier related to this orientation | |||
// has | ||||
|
r179 | // been pressed | ||
auto enableOrientation | ||||
= [&zoomOrientations, event](const auto &orientation, const auto &modifier) { | ||||
auto orientationEnabled = event->modifiers().testFlag(modifier); | ||||
zoomOrientations.setFlag(orientation, orientationEnabled); | ||||
}; | ||||
enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER); | ||||
enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER); | ||||
ui->widget->axisRect()->setRangeZoom(zoomOrientations); | ||||
} | ||||
r235 | ||||
r445 | void VisualizationGraphWidget::onMousePress(QMouseEvent *event) noexcept | |||
{ | ||||
impl->m_IsCalibration = event->modifiers().testFlag(Qt::ControlModifier); | ||||
} | ||||
void VisualizationGraphWidget::onMouseRelease(QMouseEvent *event) noexcept | ||||
{ | ||||
impl->m_IsCalibration = false; | ||||
} | ||||
r235 | void VisualizationGraphWidget::onDataCacheVariableUpdated() | |||
{ | ||||
r243 | // NOTE: | |||
r260 | // We don't want to call the method for each component of a variable unitarily, but for | |||
// all | ||||
r243 | // its components at once (eg its three components in the case of a vector). | |||
// The unordered_multimap does not do this easily, so the question is whether to: | ||||
// - use an ordered_multimap and the algos of std to group the values by key | ||||
// - use a map (unique keys) and store as values directly the list of components | ||||
r433 | auto grapheRange = ui->widget->xAxis->range(); | |||
r512 | auto dateTime = SqpRange{grapheRange.lower, grapheRange.upper}; | |||
r433 | ||||
r235 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); | |||
it != impl->m_VariableToPlotMultiMap.cend(); ++it) { | ||||
auto variable = it->first; | ||||
r441 | qCDebug(LOG_VisualizationGraphWidget()) | |||
r539 | << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" << variable->range(); | |||
r441 | qCDebug(LOG_VisualizationGraphWidget()) | |||
r433 | << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime; | |||
r539 | if (dateTime.contains(variable->range()) || dateTime.intersect(variable->range())) { | |||
r433 | ||||
VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second, | ||||
r539 | variable->dataSeries(), variable->range()); | |||
r433 | } | |||
r235 | } | |||
} | ||||