VisualizationGraphWidget.cpp
385 lines
| 13.4 KiB
| text/x-c
|
CppLexer
r95 | #include "Visualization/VisualizationGraphWidget.h" | |||
Alexandre Leroux
|
r207 | #include "Visualization/IVisualizationWidgetVisitor.h" | ||
Alexandre Leroux
|
r582 | #include "Visualization/VisualizationDefs.h" | ||
r243 | #include "Visualization/VisualizationGraphHelper.h" | |||
Alexandre Leroux
|
r480 | #include "Visualization/VisualizationGraphRenderingDelegate.h" | ||
r839 | #include "Visualization/VisualizationZoneWidget.h" | |||
r58 | #include "ui_VisualizationGraphWidget.h" | |||
r848 | #include <Common/MimeTypesDef.h> | |||
r235 | #include <Data/ArrayData.h> | |||
#include <Data/IDataSeries.h> | ||||
r844 | #include <DragDropHelper.h> | |||
Alexandre Leroux
|
r470 | #include <Settings/SqpSettingsDefs.h> | ||
r235 | #include <SqpApplication.h> | |||
r118 | #include <Variable/Variable.h> | |||
r235 | #include <Variable/VariableController.h> | |||
r118 | #include <unordered_map> | |||
Alexandre Leroux
|
r219 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget") | ||
Alexandre Leroux
|
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 { | |||
Alexandre Leroux
|
r724 | explicit VisualizationGraphWidgetPrivate(const QString &name) | ||
: m_Name{name}, | ||||
m_DoAcquisition{true}, | ||||
m_IsCalibration{false}, | ||||
m_RenderingDelegate{nullptr} | ||||
Alexandre Leroux
|
r480 | { | ||
} | ||||
r445 | ||||
Alexandre Leroux
|
r724 | QString m_Name; | ||
r118 | // 1 variable -> n qcpplot | |||
Alexandre Leroux
|
r582 | std::map<std::shared_ptr<Variable>, PlottablesMap> m_VariableToPlotMultiMap; | ||
r539 | bool m_DoAcquisition; | |||
r445 | bool m_IsCalibration; | |||
Alexandre Leroux
|
r480 | QCPItemTracer *m_TextTracer; | ||
/// Delegate used to attach rendering features to the plot | ||||
std::unique_ptr<VisualizationGraphRenderingDelegate> m_RenderingDelegate; | ||||
r118 | }; | |||
Alexandre Leroux
|
r205 | VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent) | ||
r839 | : VisualizationDragWidget{parent}, | |||
r120 | ui{new Ui::VisualizationGraphWidget}, | |||
Alexandre Leroux
|
r724 | impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>(name)} | ||
r58 | { | |||
ui->setupUi(this); | ||||
Alexandre Leroux
|
r178 | |||
Alexandre Leroux
|
r266 | // 'Close' options : widget is deleted when closed | ||
setAttribute(Qt::WA_DeleteOnClose); | ||||
Alexandre Leroux
|
r196 | |||
Alexandre Leroux
|
r178 | // Set qcpplot properties : | ||
Alexandre Leroux
|
r180 | // - Drag (on x-axis) and zoom are enabled | ||
Alexandre Leroux
|
r179 | // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation | ||
Alexandre Leroux
|
r727 | ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectItems); | ||
Alexandre Leroux
|
r180 | ui->widget->axisRect()->setRangeDrag(Qt::Horizontal); | ||
Alexandre Leroux
|
r481 | |||
Alexandre Leroux
|
r724 | // The delegate must be initialized after the ui as it uses the plot | ||
impl->m_RenderingDelegate = std::make_unique<VisualizationGraphRenderingDelegate>(*this); | ||||
r445 | connect(ui->widget, &QCustomPlot::mousePress, this, &VisualizationGraphWidget::onMousePress); | |||
connect(ui->widget, &QCustomPlot::mouseRelease, this, | ||||
&VisualizationGraphWidget::onMouseRelease); | ||||
Alexandre Leroux
|
r481 | connect(ui->widget, &QCustomPlot::mouseMove, this, &VisualizationGraphWidget::onMouseMove); | ||
Alexandre Leroux
|
r179 | connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel); | ||
r845 | connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>( | |||
&QCPAxis::rangeChanged), | ||||
this, &VisualizationGraphWidget::onRangeChanged, Qt::DirectConnection); | ||||
Alexandre Leroux
|
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); | ||||
r571 | ||||
connect(&sqpApp->variableController(), &VariableController::updateVarDisplaying, this, | ||||
&VisualizationGraphWidget::onUpdateVarDisplaying); | ||||
r58 | } | |||
Alexandre Leroux
|
r227 | |||
r58 | VisualizationGraphWidget::~VisualizationGraphWidget() | |||
{ | ||||
delete ui; | ||||
} | ||||
r118 | ||||
r839 | VisualizationZoneWidget *VisualizationGraphWidget::parentZoneWidget() const noexcept | |||
{ | ||||
auto parent = parentWidget(); | ||||
r846 | while (parent != nullptr && !qobject_cast<VisualizationZoneWidget *>(parent)) { | |||
r839 | parent = parent->parentWidget(); | |||
r846 | } | |||
r839 | ||||
r844 | return qobject_cast<VisualizationZoneWidget *>(parent); | |||
r839 | } | |||
r539 | void VisualizationGraphWidget::enableAcquisition(bool enable) | |||
r444 | { | |||
r539 | impl->m_DoAcquisition = enable; | |||
r444 | } | |||
r548 | void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable, SqpRange range) | |||
r118 | { | |||
Alexandre Leroux
|
r184 | // Uses delegate to create the qcpplot components according to the variable | ||
Alexandre Leroux
|
r582 | auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); | ||
impl->m_VariableToPlotMultiMap.insert({variable, std::move(createdPlottables)}); | ||||
r235 | ||||
Alexandre Leroux
|
r729 | // Set axes properties according to the units of the data series | ||
/// @todo : for the moment, no control is performed on the axes: the units and the tickers | ||||
/// are fixed for the default x-axis and y-axis of the plot, and according to the new graph | ||||
auto xAxisUnit = Unit{}; | ||||
auto valuesUnit = Unit{}; | ||||
if (auto dataSeries = variable->dataSeries()) { | ||||
dataSeries->lockRead(); | ||||
xAxisUnit = dataSeries->xAxisUnit(); | ||||
valuesUnit = dataSeries->valuesUnit(); | ||||
dataSeries->unlock(); | ||||
} | ||||
impl->m_RenderingDelegate->setAxesProperties(xAxisUnit, valuesUnit); | ||||
r298 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); | |||
r540 | ||||
r548 | this->enableAcquisition(false); | |||
this->setGraphRange(range); | ||||
this->enableAcquisition(true); | ||||
r314 | ||||
r811 | emit requestDataLoading(QVector<std::shared_ptr<Variable> >() << variable, range, false); | |||
r548 | ||||
emit variableAdded(variable); | ||||
r314 | } | |||
Alexandre Leroux
|
r270 | void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept | ||
{ | ||||
Alexandre Leroux
|
r271 | // Each component associated to the variable : | ||
// - is removed from qcpplot (which deletes it) | ||||
// - is no longer referenced in the map | ||||
Alexandre Leroux
|
r582 | auto variableIt = impl->m_VariableToPlotMultiMap.find(variable); | ||
if (variableIt != impl->m_VariableToPlotMultiMap.cend()) { | ||||
Alexandre Leroux
|
r737 | emit variableAboutToBeRemoved(variable); | ||
Alexandre Leroux
|
r582 | 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
|
r271 | } | ||
// Updates graph | ||||
ui->widget->replot(); | ||||
Alexandre Leroux
|
r270 | } | ||
r844 | QList<std::shared_ptr<Variable> > VisualizationGraphWidget::variables() const | |||
r839 | { | |||
r844 | auto variables = QList<std::shared_ptr<Variable> >{}; | |||
for (auto it = std::cbegin(impl->m_VariableToPlotMultiMap); | ||||
it != std::cend(impl->m_VariableToPlotMultiMap); ++it) { | ||||
r839 | variables << it->first; | |||
} | ||||
return variables; | ||||
} | ||||
r548 | void VisualizationGraphWidget::setYRange(const SqpRange &range) | |||
{ | ||||
ui->widget->yAxis->setRange(range.m_TStart, range.m_TEnd); | ||||
} | ||||
r512 | SqpRange VisualizationGraphWidget::graphRange() const noexcept | |||
r444 | { | |||
r545 | auto graphRange = ui->widget->xAxis->range(); | |||
return SqpRange{graphRange.lower, graphRange.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 | } | |||
Alexandre Leroux
|
r207 | void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor) | ||
r118 | { | |||
Alexandre Leroux
|
r208 | if (visitor) { | ||
visitor->visit(this); | ||||
} | ||||
Alexandre Leroux
|
r219 | else { | ||
qCCritical(LOG_VisualizationGraphWidget()) | ||||
<< tr("Can't visit widget : the visitor is null"); | ||||
} | ||||
r118 | } | |||
Alexandre Leroux
|
r209 | 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
|
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 | { | |||
Alexandre Leroux
|
r724 | return impl->m_Name; | ||
r118 | } | |||
r839 | QMimeData *VisualizationGraphWidget::mimeData() const | |||
{ | ||||
r846 | auto mimeData = new QMimeData; | |||
r848 | mimeData->setData(MIME_TYPE_GRAPH, QByteArray()); | |||
r839 | ||||
r844 | return mimeData; | |||
r839 | } | |||
bool VisualizationGraphWidget::isDragAllowed() const | ||||
{ | ||||
return true; | ||||
} | ||||
Alexandre Leroux
|
r738 | void VisualizationGraphWidget::closeEvent(QCloseEvent *event) | ||
{ | ||||
Q_UNUSED(event); | ||||
// Prevents that all variables will be removed from graph when it will be closed | ||||
for (auto &variableEntry : impl->m_VariableToPlotMultiMap) { | ||||
emit variableAboutToBeRemoved(variableEntry.first); | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r728 | void VisualizationGraphWidget::enterEvent(QEvent *event) | ||
{ | ||||
Q_UNUSED(event); | ||||
impl->m_RenderingDelegate->showGraphOverlay(true); | ||||
} | ||||
void VisualizationGraphWidget::leaveEvent(QEvent *event) | ||||
{ | ||||
Q_UNUSED(event); | ||||
impl->m_RenderingDelegate->showGraphOverlay(false); | ||||
} | ||||
Alexandre Leroux
|
r725 | QCustomPlot &VisualizationGraphWidget::plot() noexcept | ||
{ | ||||
return *ui->widget; | ||||
} | ||||
Alexandre Leroux
|
r269 | void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept | ||
r118 | { | |||
Alexandre Leroux
|
r269 | QMenu graphMenu{}; | ||
Alexandre Leroux
|
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); }); | ||||
Alexandre Leroux
|
r196 | } | ||
Alexandre Leroux
|
r269 | |||
if (!graphMenu.isEmpty()) { | ||||
Alexandre Leroux
|
r655 | graphMenu.exec(QCursor::pos()); | ||
Alexandre Leroux
|
r196 | } | ||
r118 | } | |||
Alexandre Leroux
|
r179 | |||
r444 | void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2) | |||
Alexandre Leroux
|
r227 | { | ||
r845 | 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 | } | |||
r811 | emit requestDataLoading(std::move(variableUnderGraphVector), graphRange, | |||
r539 | !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 | } | |||
Alexandre Leroux
|
r227 | } | ||
} | ||||
Alexandre Leroux
|
r481 | void VisualizationGraphWidget::onMouseMove(QMouseEvent *event) noexcept | ||
{ | ||||
// Handles plot rendering when mouse is moving | ||||
impl->m_RenderingDelegate->onMouseMove(event); | ||||
r839 | ||||
VisualizationDragWidget::mouseMoveEvent(event); | ||||
Alexandre Leroux
|
r481 | } | ||
Alexandre Leroux
|
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 | ||||
Alexandre Leroux
|
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); | ||||
r839 | ||||
plot().setInteraction(QCP::iRangeDrag, !event->modifiers().testFlag(Qt::AltModifier)); | ||||
VisualizationDragWidget::mousePressEvent(event); | ||||
r445 | } | |||
void VisualizationGraphWidget::onMouseRelease(QMouseEvent *event) noexcept | ||||
{ | ||||
impl->m_IsCalibration = false; | ||||
} | ||||
r235 | void VisualizationGraphWidget::onDataCacheVariableUpdated() | |||
{ | ||||
r545 | auto graphRange = ui->widget->xAxis->range(); | |||
auto dateTime = SqpRange{graphRange.lower, graphRange.upper}; | ||||
r433 | ||||
Alexandre Leroux
|
r582 | for (auto &variableEntry : impl->m_VariableToPlotMultiMap) { | ||
auto variable = variableEntry.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())) { | |||
Alexandre Leroux
|
r587 | VisualizationGraphHelper::updateData(variableEntry.second, variable->dataSeries(), | ||
Alexandre Leroux
|
r582 | variable->range()); | ||
r433 | } | |||
r235 | } | |||
} | ||||
r571 | ||||
void VisualizationGraphWidget::onUpdateVarDisplaying(std::shared_ptr<Variable> variable, | ||||
const SqpRange &range) | ||||
{ | ||||
Alexandre Leroux
|
r582 | auto it = impl->m_VariableToPlotMultiMap.find(variable); | ||
if (it != impl->m_VariableToPlotMultiMap.end()) { | ||||
Alexandre Leroux
|
r587 | VisualizationGraphHelper::updateData(it->second, variable->dataSeries(), range); | ||
r571 | } | |||
} | ||||