VisualizationGraphWidget.cpp
313 lines
| 11.6 KiB
| text/x-c
|
CppLexer
r93 | #include "Visualization/VisualizationGraphWidget.h" | |||
Alexandre Leroux
|
r192 | #include "Visualization/IVisualizationWidgetVisitor.h" | ||
Alexandre Leroux
|
r546 | #include "Visualization/VisualizationDefs.h" | ||
r227 | #include "Visualization/VisualizationGraphHelper.h" | |||
Alexandre Leroux
|
r442 | #include "Visualization/VisualizationGraphRenderingDelegate.h" | ||
r58 | #include "ui_VisualizationGraphWidget.h" | |||
r219 | #include <Data/ArrayData.h> | |||
#include <Data/IDataSeries.h> | ||||
Alexandre Leroux
|
r434 | #include <Settings/SqpSettingsDefs.h> | ||
r219 | #include <SqpApplication.h> | |||
r111 | #include <Variable/Variable.h> | |||
r219 | #include <Variable/VariableController.h> | |||
r111 | #include <unordered_map> | |||
Alexandre Leroux
|
r204 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget") | ||
Alexandre Leroux
|
r166 | 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 | ||||
r111 | struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate { | |||
Alexandre Leroux
|
r442 | explicit VisualizationGraphWidgetPrivate() | ||
r510 | : m_DoAcquisition{true}, m_IsCalibration{false}, m_RenderingDelegate{nullptr} | |||
Alexandre Leroux
|
r442 | { | ||
} | ||||
r411 | ||||
r111 | // 1 variable -> n qcpplot | |||
Alexandre Leroux
|
r546 | std::map<std::shared_ptr<Variable>, PlottablesMap> m_VariableToPlotMultiMap; | ||
r510 | bool m_DoAcquisition; | |||
r411 | bool m_IsCalibration; | |||
Alexandre Leroux
|
r442 | QCPItemTracer *m_TextTracer; | ||
/// Delegate used to attach rendering features to the plot | ||||
std::unique_ptr<VisualizationGraphRenderingDelegate> m_RenderingDelegate; | ||||
r111 | }; | |||
Alexandre Leroux
|
r191 | VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent) | ||
r113 | : QWidget{parent}, | |||
ui{new Ui::VisualizationGraphWidget}, | ||||
r111 | impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()} | |||
r58 | { | |||
ui->setupUi(this); | ||||
Alexandre Leroux
|
r165 | |||
Alexandre Leroux
|
r442 | // The delegate must be initialized after the ui as it uses the plot | ||
impl->m_RenderingDelegate = std::make_unique<VisualizationGraphRenderingDelegate>(*ui->widget); | ||||
Alexandre Leroux
|
r246 | 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)); | ||||
Alexandre Leroux
|
r182 | |||
Alexandre Leroux
|
r165 | // Set qcpplot properties : | ||
Alexandre Leroux
|
r167 | // - Drag (on x-axis) and zoom are enabled | ||
Alexandre Leroux
|
r166 | // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation | ||
Alexandre Leroux
|
r165 | ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); | ||
Alexandre Leroux
|
r167 | ui->widget->axisRect()->setRangeDrag(Qt::Horizontal); | ||
Alexandre Leroux
|
r443 | |||
r411 | connect(ui->widget, &QCustomPlot::mousePress, this, &VisualizationGraphWidget::onMousePress); | |||
connect(ui->widget, &QCustomPlot::mouseRelease, this, | ||||
&VisualizationGraphWidget::onMouseRelease); | ||||
Alexandre Leroux
|
r443 | connect(ui->widget, &QCustomPlot::mouseMove, this, &VisualizationGraphWidget::onMouseMove); | ||
Alexandre Leroux
|
r166 | connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel); | ||
r410 | connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>( | |||
&QCPAxis::rangeChanged), | ||||
r411 | this, &VisualizationGraphWidget::onRangeChanged, Qt::DirectConnection); | |||
Alexandre Leroux
|
r249 | |||
// Activates menu when right clicking on the graph | ||||
ui->widget->setContextMenuPolicy(Qt::CustomContextMenu); | ||||
connect(ui->widget, &QCustomPlot::customContextMenuRequested, this, | ||||
&VisualizationGraphWidget::onGraphMenuRequested); | ||||
r276 | ||||
connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(), | ||||
&VariableController::onRequestDataLoading); | ||||
r538 | ||||
connect(&sqpApp->variableController(), &VariableController::updateVarDisplaying, this, | ||||
&VisualizationGraphWidget::onUpdateVarDisplaying); | ||||
r58 | } | |||
Alexandre Leroux
|
r211 | |||
r58 | VisualizationGraphWidget::~VisualizationGraphWidget() | |||
{ | ||||
delete ui; | ||||
} | ||||
r111 | ||||
r510 | void VisualizationGraphWidget::enableAcquisition(bool enable) | |||
r410 | { | |||
r510 | impl->m_DoAcquisition = enable; | |||
r410 | } | |||
r518 | void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable, SqpRange range) | |||
r111 | { | |||
Alexandre Leroux
|
r171 | // Uses delegate to create the qcpplot components according to the variable | ||
Alexandre Leroux
|
r546 | auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); | ||
impl->m_VariableToPlotMultiMap.insert({variable, std::move(createdPlottables)}); | ||||
r219 | ||||
r276 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); | |||
r511 | ||||
r518 | auto varRange = variable->range(); | |||
r289 | ||||
r518 | this->enableAcquisition(false); | |||
this->setGraphRange(range); | ||||
this->enableAcquisition(true); | ||||
r289 | ||||
r518 | emit requestDataLoading(QVector<std::shared_ptr<Variable> >() << variable, range, varRange, | |||
false); | ||||
emit variableAdded(variable); | ||||
r289 | } | |||
Alexandre Leroux
|
r250 | void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept | ||
{ | ||||
Alexandre Leroux
|
r251 | // Each component associated to the variable : | ||
// - is removed from qcpplot (which deletes it) | ||||
// - is no longer referenced in the map | ||||
Alexandre Leroux
|
r546 | auto variableIt = impl->m_VariableToPlotMultiMap.find(variable); | ||
if (variableIt != impl->m_VariableToPlotMultiMap.cend()) { | ||||
auto &plottablesMap = variableIt->second; | ||||
for (auto plottableIt = plottablesMap.cbegin(), plottableEnd = plottablesMap.cend(); | ||||
plottableIt != plottableEnd;) { | ||||
ui->widget->removePlottable(plottableIt->second); | ||||
plottableIt = plottablesMap.erase(plottableIt); | ||||
} | ||||
impl->m_VariableToPlotMultiMap.erase(variableIt); | ||||
Alexandre Leroux
|
r251 | } | ||
// Updates graph | ||||
ui->widget->replot(); | ||||
Alexandre Leroux
|
r250 | } | ||
r471 | void VisualizationGraphWidget::setRange(std::shared_ptr<Variable> variable, const SqpRange &range) | |||
r404 | { | |||
r413 | // Note: in case of different axes that depends on variable, we could start with a code like | |||
// that: | ||||
r404 | // 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); | ||||
r410 | ui->widget->replot(); | |||
} | ||||
r518 | void VisualizationGraphWidget::setYRange(const SqpRange &range) | |||
{ | ||||
ui->widget->yAxis->setRange(range.m_TStart, range.m_TEnd); | ||||
} | ||||
r471 | SqpRange VisualizationGraphWidget::graphRange() const noexcept | |||
r410 | { | |||
r516 | auto graphRange = ui->widget->xAxis->range(); | |||
return SqpRange{graphRange.lower, graphRange.upper}; | ||||
r410 | } | |||
r471 | void VisualizationGraphWidget::setGraphRange(const SqpRange &range) | |||
r410 | { | |||
r411 | qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange START"); | |||
r410 | ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd); | |||
ui->widget->replot(); | ||||
qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange END"); | ||||
r404 | } | |||
Alexandre Leroux
|
r192 | void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor) | ||
r111 | { | |||
Alexandre Leroux
|
r193 | if (visitor) { | ||
visitor->visit(this); | ||||
} | ||||
Alexandre Leroux
|
r204 | else { | ||
qCCritical(LOG_VisualizationGraphWidget()) | ||||
<< tr("Can't visit widget : the visitor is null"); | ||||
} | ||||
r111 | } | |||
Alexandre Leroux
|
r194 | bool VisualizationGraphWidget::canDrop(const Variable &variable) const | ||
{ | ||||
/// @todo : for the moment, a graph can always accomodate a variable | ||||
Q_UNUSED(variable); | ||||
return true; | ||||
} | ||||
Alexandre Leroux
|
r301 | 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; | ||||
} | ||||
r112 | QString VisualizationGraphWidget::name() const | |||
r111 | { | |||
Alexandre Leroux
|
r246 | return ui->graphNameLabel->text(); | ||
r111 | } | |||
Alexandre Leroux
|
r249 | void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept | ||
r111 | { | |||
Alexandre Leroux
|
r249 | QMenu graphMenu{}; | ||
Alexandre Leroux
|
r250 | // 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); }); | ||||
Alexandre Leroux
|
r182 | } | ||
Alexandre Leroux
|
r249 | |||
if (!graphMenu.isEmpty()) { | ||||
graphMenu.exec(mapToGlobal(pos)); | ||||
Alexandre Leroux
|
r182 | } | ||
r111 | } | |||
Alexandre Leroux
|
r166 | |||
r410 | void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2) | |||
Alexandre Leroux
|
r211 | { | ||
r510 | qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: VisualizationGraphWidget::onRangeChanged") | |||
<< QThread::currentThread()->objectName() << "DoAcqui" | ||||
<< impl->m_DoAcquisition; | ||||
r410 | ||||
r510 | auto graphRange = SqpRange{t1.lower, t1.upper}; | |||
auto oldGraphRange = SqpRange{t2.lower, t2.upper}; | ||||
r219 | ||||
r510 | if (impl->m_DoAcquisition) { | |||
QVector<std::shared_ptr<Variable> > variableUnderGraphVector; | ||||
r240 | ||||
r510 | 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); | ||||
r219 | } | |||
r510 | emit requestDataLoading(std::move(variableUnderGraphVector), graphRange, oldGraphRange, | |||
!impl->m_IsCalibration); | ||||
if (!impl->m_IsCalibration) { | ||||
r513 | qCDebug(LOG_VisualizationGraphWidget()) | |||
r510 | << tr("TORM: VisualizationGraphWidget::Synchronize notify !!") | |||
r511 | << QThread::currentThread()->objectName() << graphRange << oldGraphRange; | |||
r510 | emit synchronize(graphRange, oldGraphRange); | |||
r293 | } | |||
Alexandre Leroux
|
r211 | } | ||
} | ||||
Alexandre Leroux
|
r443 | void VisualizationGraphWidget::onMouseMove(QMouseEvent *event) noexcept | ||
{ | ||||
// Handles plot rendering when mouse is moving | ||||
impl->m_RenderingDelegate->onMouseMove(event); | ||||
} | ||||
Alexandre Leroux
|
r166 | void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept | ||
{ | ||||
auto zoomOrientations = QFlags<Qt::Orientation>{}; | ||||
r241 | // Lambda that enables a zoom orientation if the key modifier related to this orientation | |||
// has | ||||
Alexandre Leroux
|
r166 | // 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); | ||||
} | ||||
r219 | ||||
r411 | void VisualizationGraphWidget::onMousePress(QMouseEvent *event) noexcept | |||
{ | ||||
impl->m_IsCalibration = event->modifiers().testFlag(Qt::ControlModifier); | ||||
} | ||||
void VisualizationGraphWidget::onMouseRelease(QMouseEvent *event) noexcept | ||||
{ | ||||
impl->m_IsCalibration = false; | ||||
} | ||||
r219 | void VisualizationGraphWidget::onDataCacheVariableUpdated() | |||
{ | ||||
r516 | auto graphRange = ui->widget->xAxis->range(); | |||
auto dateTime = SqpRange{graphRange.lower, graphRange.upper}; | ||||
r399 | ||||
Alexandre Leroux
|
r546 | for (auto &variableEntry : impl->m_VariableToPlotMultiMap) { | ||
auto variable = variableEntry.first; | ||||
r407 | qCDebug(LOG_VisualizationGraphWidget()) | |||
r510 | << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" << variable->range(); | |||
r407 | qCDebug(LOG_VisualizationGraphWidget()) | |||
r399 | << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime; | |||
r510 | if (dateTime.contains(variable->range()) || dateTime.intersect(variable->range())) { | |||
Alexandre Leroux
|
r551 | VisualizationGraphHelper::updateData(variableEntry.second, variable->dataSeries(), | ||
Alexandre Leroux
|
r546 | variable->range()); | ||
r399 | } | |||
r219 | } | |||
} | ||||
r538 | ||||
void VisualizationGraphWidget::onUpdateVarDisplaying(std::shared_ptr<Variable> variable, | ||||
const SqpRange &range) | ||||
{ | ||||
Alexandre Leroux
|
r546 | auto it = impl->m_VariableToPlotMultiMap.find(variable); | ||
if (it != impl->m_VariableToPlotMultiMap.end()) { | ||||
Alexandre Leroux
|
r551 | VisualizationGraphHelper::updateData(it->second, variable->dataSeries(), range); | ||
r538 | } | |||
} | ||||