VisualizationGraphWidget.cpp
401 lines
| 16.4 KiB
| text/x-c
|
CppLexer
r93 | #include "Visualization/VisualizationGraphWidget.h" | |||
Alexandre Leroux
|
r192 | #include "Visualization/IVisualizationWidgetVisitor.h" | ||
r227 | #include "Visualization/VisualizationGraphHelper.h" | |||
r58 | #include "ui_VisualizationGraphWidget.h" | |||
r219 | #include <Data/ArrayData.h> | |||
#include <Data/IDataSeries.h> | ||||
#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 { | |||
r413 | explicit VisualizationGraphWidgetPrivate() : m_DoSynchronize{true}, m_IsCalibration{false} {} | |||
r411 | ||||
// Return the operation when range changed | ||||
VisualizationGraphWidgetZoomType getZoomType(const QCPRange &t1, const QCPRange &t2); | ||||
r410 | ||||
r111 | // 1 variable -> n qcpplot | |||
Alexandre Leroux
|
r250 | std::multimap<std::shared_ptr<Variable>, QCPAbstractPlottable *> m_VariableToPlotMultiMap; | ||
r410 | ||||
bool m_DoSynchronize; | ||||
r411 | bool m_IsCalibration; | |||
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
|
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); | ||
r411 | connect(ui->widget, &QCustomPlot::mousePress, this, &VisualizationGraphWidget::onMousePress); | |||
connect(ui->widget, &QCustomPlot::mouseRelease, this, | ||||
&VisualizationGraphWidget::onMouseRelease); | ||||
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); | ||||
r58 | } | |||
Alexandre Leroux
|
r211 | |||
r58 | VisualizationGraphWidget::~VisualizationGraphWidget() | |||
{ | ||||
delete ui; | ||||
} | ||||
r111 | ||||
r410 | void VisualizationGraphWidget::enableSynchronize(bool enable) | |||
{ | ||||
impl->m_DoSynchronize = enable; | ||||
} | ||||
r111 | void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable) | |||
{ | ||||
Alexandre Leroux
|
r171 | // Uses delegate to create the qcpplot components according to the variable | ||
r227 | auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); | |||
Alexandre Leroux
|
r171 | |||
for (auto createdPlottable : qAsConst(createdPlottables)) { | ||||
r219 | impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable}); | |||
Alexandre Leroux
|
r171 | } | ||
r219 | ||||
r276 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); | |||
r111 | } | |||
r289 | void VisualizationGraphWidget::addVariableUsingGraph(std::shared_ptr<Variable> variable) | |||
{ | ||||
// when adding a variable, we need to set its time range to the current graph range | ||||
auto grapheRange = ui->widget->xAxis->range(); | ||||
auto dateTime = SqpDateTime{grapheRange.lower, grapheRange.upper}; | ||||
variable->setDateTime(dateTime); | ||||
auto variableDateTimeWithTolerance = dateTime; | ||||
r413 | // add 20% tolerance for each side | |||
auto tolerance = 0.2 * (dateTime.m_TEnd - dateTime.m_TStart); | ||||
r289 | variableDateTimeWithTolerance.m_TStart -= tolerance; | |||
variableDateTimeWithTolerance.m_TEnd += tolerance; | ||||
// Uses delegate to create the qcpplot components according to the variable | ||||
auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); | ||||
for (auto createdPlottable : qAsConst(createdPlottables)) { | ||||
impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable}); | ||||
} | ||||
connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); | ||||
// CHangement detected, we need to ask controller to request data loading | ||||
emit requestDataLoading(variable, variableDateTimeWithTolerance); | ||||
} | ||||
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 | ||||
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(); | ||||
Alexandre Leroux
|
r250 | } | ||
r404 | void VisualizationGraphWidget::setRange(std::shared_ptr<Variable> variable, | |||
const SqpDateTime &range) | ||||
{ | ||||
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(); | |||
} | ||||
r413 | SqpDateTime VisualizationGraphWidget::graphRange() const noexcept | |||
r410 | { | |||
auto grapheRange = ui->widget->xAxis->range(); | ||||
return SqpDateTime{grapheRange.lower, grapheRange.upper}; | ||||
} | ||||
void VisualizationGraphWidget::setGraphRange(const SqpDateTime &range) | ||||
{ | ||||
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 | { | ||
r410 | qCInfo(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged") | |||
<< QThread::currentThread()->objectName(); | ||||
auto dateTimeRange = SqpDateTime{t1.lower, t1.upper}; | ||||
r219 | ||||
r411 | auto zoomType = impl->getZoomType(t1, t2); | |||
r219 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); | |||
it != impl->m_VariableToPlotMultiMap.cend(); ++it) { | ||||
r240 | ||||
r219 | auto variable = it->first; | |||
r410 | auto currentDateTime = dateTimeRange; | |||
r219 | ||||
r404 | auto toleranceFactor = 0.2; | |||
r410 | auto tolerance = toleranceFactor * (currentDateTime.m_TEnd - currentDateTime.m_TStart); | |||
auto variableDateTimeWithTolerance = currentDateTime; | ||||
r399 | variableDateTimeWithTolerance.m_TStart -= tolerance; | |||
variableDateTimeWithTolerance.m_TEnd += tolerance; | ||||
r410 | qCDebug(LOG_VisualizationGraphWidget()) << "r" << currentDateTime; | |||
qCDebug(LOG_VisualizationGraphWidget()) << "t" << variableDateTimeWithTolerance; | ||||
qCDebug(LOG_VisualizationGraphWidget()) << "v" << variable->dateTime(); | ||||
r399 | // If new range with tol is upper than variable datetime parameters. we need to request new | |||
// data | ||||
if (!variable->contains(variableDateTimeWithTolerance)) { | ||||
r240 | ||||
r410 | auto variableDateTimeWithTolerance = currentDateTime; | |||
if (!variable->isInside(currentDateTime)) { | ||||
r240 | auto variableDateTime = variable->dateTime(); | |||
r410 | if (variable->contains(variableDateTimeWithTolerance)) { | |||
r411 | qCDebug(LOG_VisualizationGraphWidget()) | |||
r410 | << tr("TORM: Detection zoom in that need request:"); | |||
Alexandre Leroux
|
r424 | // add tolerance for each side | ||
r410 | tolerance | |||
= toleranceFactor * (currentDateTime.m_TEnd - currentDateTime.m_TStart); | ||||
variableDateTimeWithTolerance.m_TStart -= tolerance; | ||||
variableDateTimeWithTolerance.m_TEnd += tolerance; | ||||
} | ||||
else if (variableDateTime.m_TStart < currentDateTime.m_TStart) { | ||||
qCInfo(LOG_VisualizationGraphWidget()) << tr("TORM: Detection pan to right:"); | ||||
r243 | ||||
r410 | auto diffEndToKeepDelta = currentDateTime.m_TEnd - variableDateTime.m_TEnd; | |||
currentDateTime.m_TStart = variableDateTime.m_TStart + diffEndToKeepDelta; | ||||
r243 | // Tolerance have to be added to the right | |||
r404 | // add tolerance for right (end) side | |||
r410 | tolerance | |||
= toleranceFactor * (currentDateTime.m_TEnd - currentDateTime.m_TStart); | ||||
r241 | variableDateTimeWithTolerance.m_TEnd += tolerance; | |||
r240 | } | |||
r410 | else if (variableDateTime.m_TEnd > currentDateTime.m_TEnd) { | |||
r411 | qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: Detection pan to left: "); | |||
r410 | auto diffStartToKeepDelta | |||
= variableDateTime.m_TStart - currentDateTime.m_TStart; | ||||
currentDateTime.m_TEnd = variableDateTime.m_TEnd - diffStartToKeepDelta; | ||||
r243 | // Tolerance have to be added to the left | |||
r404 | // add tolerance for left (start) side | |||
r410 | tolerance | |||
= toleranceFactor * (currentDateTime.m_TEnd - currentDateTime.m_TStart); | ||||
r241 | variableDateTimeWithTolerance.m_TStart -= tolerance; | |||
r240 | } | |||
r293 | else { | |||
r411 | qCCritical(LOG_VisualizationGraphWidget()) | |||
r293 | << tr("Detection anormal zoom detection: "); | |||
} | ||||
r240 | } | |||
else { | ||||
r411 | qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: Detection zoom out: "); | |||
Alexandre Leroux
|
r424 | // add tolerance for each side | ||
r410 | tolerance = toleranceFactor * (currentDateTime.m_TEnd - currentDateTime.m_TStart); | |||
r241 | variableDateTimeWithTolerance.m_TStart -= tolerance; | |||
variableDateTimeWithTolerance.m_TEnd += tolerance; | ||||
r410 | zoomType = VisualizationGraphWidgetZoomType::ZoomOut; | |||
r240 | } | |||
r399 | if (!variable->contains(dateTimeRange)) { | |||
r411 | qCDebug(LOG_VisualizationGraphWidget()) | |||
r410 | << "TORM: Modif on variable datetime detected" << currentDateTime; | |||
variable->setDateTime(currentDateTime); | ||||
r399 | } | |||
r241 | ||||
r411 | qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: Request data detection: "); | |||
r241 | // CHangement detected, we need to ask controller to request data loading | |||
r276 | emit requestDataLoading(variable, variableDateTimeWithTolerance); | |||
r219 | } | |||
r293 | else { | |||
r410 | qCInfo(LOG_VisualizationGraphWidget()) | |||
<< tr("TORM: Detection zoom in that doesn't need request: "); | ||||
zoomType = VisualizationGraphWidgetZoomType::ZoomIn; | ||||
r293 | } | |||
Alexandre Leroux
|
r211 | } | ||
r410 | ||||
r411 | if (impl->m_DoSynchronize && !impl->m_IsCalibration) { | |||
r410 | auto oldDateTime = SqpDateTime{t2.lower, t2.upper}; | |||
qCDebug(LOG_VisualizationGraphWidget()) | ||||
<< tr("TORM: VisualizationGraphWidget::Synchronize notify !!") | ||||
<< QThread::currentThread()->objectName(); | ||||
emit synchronize(dateTimeRange, oldDateTime, zoomType); | ||||
} | ||||
Alexandre Leroux
|
r211 | } | ||
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() | |||
{ | ||||
r227 | // NOTE: | |||
r241 | // We don't want to call the method for each component of a variable unitarily, but for | |||
// all | ||||
r227 | // 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 | ||||
r399 | auto grapheRange = ui->widget->xAxis->range(); | |||
auto dateTime = SqpDateTime{grapheRange.lower, grapheRange.upper}; | ||||
r219 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); | |||
it != impl->m_VariableToPlotMultiMap.cend(); ++it) { | ||||
auto variable = it->first; | ||||
r407 | qCDebug(LOG_VisualizationGraphWidget()) | |||
r399 | << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" | |||
<< variable->dateTime(); | ||||
r407 | qCDebug(LOG_VisualizationGraphWidget()) | |||
r399 | << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime; | |||
if (dateTime.contains(variable->dateTime()) || dateTime.intersect(variable->dateTime())) { | ||||
VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second, | ||||
variable->dataSeries(), variable->dateTime()); | ||||
} | ||||
r219 | } | |||
} | ||||
r411 | ||||
VisualizationGraphWidgetZoomType | ||||
VisualizationGraphWidget::VisualizationGraphWidgetPrivate::getZoomType(const QCPRange &t1, | ||||
const QCPRange &t2) | ||||
{ | ||||
// t1.lower <= t2.lower && t2.upper <= t1.upper | ||||
auto zoomType = VisualizationGraphWidgetZoomType::Unknown; | ||||
if (t1.lower <= t2.lower && t2.upper <= t1.upper) { | ||||
zoomType = VisualizationGraphWidgetZoomType::ZoomOut; | ||||
} | ||||
else if (t1.lower > t2.lower && t1.upper > t2.upper) { | ||||
zoomType = VisualizationGraphWidgetZoomType::PanRight; | ||||
} | ||||
else if (t1.lower < t2.lower && t1.upper < t2.upper) { | ||||
zoomType = VisualizationGraphWidgetZoomType::PanLeft; | ||||
} | ||||
else if (t1.lower > t2.lower && t2.upper > t1.upper) { | ||||
zoomType = VisualizationGraphWidgetZoomType::ZoomIn; | ||||
} | ||||
else { | ||||
qCCritical(LOG_VisualizationGraphWidget()) << "getZoomType: Unknown type detected"; | ||||
} | ||||
return zoomType; | ||||
} | ||||