VisualizationGraphWidget.cpp
307 lines
| 12.4 KiB
| text/x-c
|
CppLexer
r95 | #include "Visualization/VisualizationGraphWidget.h" | |||
|
r207 | #include "Visualization/IVisualizationWidgetVisitor.h" | ||
r243 | #include "Visualization/VisualizationGraphHelper.h" | |||
r58 | #include "ui_VisualizationGraphWidget.h" | |||
r235 | #include <Data/ArrayData.h> | |||
#include <Data/IDataSeries.h> | ||||
#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 { | |||
// 1 variable -> n qcpplot | ||||
|
r270 | std::multimap<std::shared_ptr<Variable>, QCPAbstractPlottable *> m_VariableToPlotMultiMap; | ||
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 | |||
|
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); | ||
|
r179 | connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel); | ||
r315 | connect(ui->widget->xAxis, | |||
static_cast<void (QCPAxis::*)(const QCPRange &)>(&QCPAxis::rangeChanged), this, | ||||
&VisualizationGraphWidget::onRangeChanged); | ||||
|
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 | ||||
void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable) | ||||
{ | ||||
|
r184 | // Uses delegate to create the qcpplot components according to the variable | ||
r243 | auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); | |||
|
r184 | |||
for (auto createdPlottable : qAsConst(createdPlottables)) { | ||||
r235 | impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable}); | |||
|
r184 | } | ||
r235 | ||||
r298 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); | |||
r118 | } | |||
r314 | 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; | ||||
// add 10% tolerance for each side | ||||
auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); | ||||
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); | ||||
} | ||||
|
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 | } | ||
r438 | void VisualizationGraphWidget::setRange(std::shared_ptr<Variable> variable, | |||
const SqpDateTime &range) | ||||
{ | ||||
// 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); | ||||
} | ||||
|
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 | |||
r315 | void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1) | |||
|
r227 | { | ||
r433 | qCInfo(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged") | |||
<< QThread::currentThread()->objectName(); | ||||
r235 | ||||
for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); | ||||
it != impl->m_VariableToPlotMultiMap.cend(); ++it) { | ||||
r258 | ||||
r235 | auto variable = it->first; | |||
r315 | auto dateTime = SqpDateTime{t1.lower, t1.upper}; | |||
r433 | auto dateTimeRange = dateTime; | |||
r235 | ||||
r438 | auto toleranceFactor = 0.2; | |||
auto tolerance = toleranceFactor * (dateTime.m_TEnd - dateTime.m_TStart); | ||||
r433 | auto variableDateTimeWithTolerance = dateTime; | |||
variableDateTimeWithTolerance.m_TStart -= tolerance; | ||||
variableDateTimeWithTolerance.m_TEnd += tolerance; | ||||
qCInfo(LOG_VisualizationGraphWidget()) << "v" << dateTime; | ||||
qCInfo(LOG_VisualizationGraphWidget()) << "vtol" << variableDateTimeWithTolerance; | ||||
// If new range with tol is upper than variable datetime parameters. we need to request new | ||||
// data | ||||
if (!variable->contains(variableDateTimeWithTolerance)) { | ||||
r258 | ||||
r260 | auto variableDateTimeWithTolerance = dateTime; | |||
r318 | if (!variable->isInside(dateTime)) { | |||
r258 | auto variableDateTime = variable->dateTime(); | |||
if (variableDateTime.m_TStart < dateTime.m_TStart) { | ||||
r433 | qCInfo(LOG_VisualizationGraphWidget()) << tr("TORM: Detection pan to right:"); | |||
r263 | ||||
auto diffEndToKeepDelta = dateTime.m_TEnd - variableDateTime.m_TEnd; | ||||
dateTime.m_TStart = variableDateTime.m_TStart + diffEndToKeepDelta; | ||||
// Tolerance have to be added to the right | ||||
r438 | // add tolerance for right (end) side | |||
tolerance = toleranceFactor * (dateTime.m_TEnd - dateTime.m_TStart); | ||||
r260 | variableDateTimeWithTolerance.m_TEnd += tolerance; | |||
r258 | } | |||
r318 | else if (variableDateTime.m_TEnd > dateTime.m_TEnd) { | |||
r433 | qCInfo(LOG_VisualizationGraphWidget()) << tr("TORM: Detection pan to left: "); | |||
r279 | auto diffStartToKeepDelta = variableDateTime.m_TStart - dateTime.m_TStart; | |||
r263 | dateTime.m_TEnd = variableDateTime.m_TEnd - diffStartToKeepDelta; | |||
// Tolerance have to be added to the left | ||||
r438 | // add tolerance for left (start) side | |||
tolerance = toleranceFactor * (dateTime.m_TEnd - dateTime.m_TStart); | ||||
r260 | variableDateTimeWithTolerance.m_TStart -= tolerance; | |||
r258 | } | |||
r318 | else { | |||
qCWarning(LOG_VisualizationGraphWidget()) | ||||
<< tr("Detection anormal zoom detection: "); | ||||
} | ||||
r258 | } | |||
else { | ||||
r433 | qCInfo(LOG_VisualizationGraphWidget()) << tr("Detection zoom out: "); | |||
r258 | // add 10% tolerance for each side | |||
r433 | tolerance = 0.2 * (dateTime.m_TEnd - dateTime.m_TStart); | |||
r260 | variableDateTimeWithTolerance.m_TStart -= tolerance; | |||
variableDateTimeWithTolerance.m_TEnd += tolerance; | ||||
r258 | } | |||
r433 | if (!variable->contains(dateTimeRange)) { | |||
r438 | qCInfo(LOG_VisualizationGraphWidget()) | |||
<< "TORM: Modif on variable datetime detected" << dateTime; | ||||
r433 | variable->setDateTime(dateTime); | |||
} | ||||
r260 | ||||
r433 | qCInfo(LOG_VisualizationGraphWidget()) << tr("Request data detection: "); | |||
r260 | // CHangement detected, we need to ask controller to request data loading | |||
r298 | emit requestDataLoading(variable, variableDateTimeWithTolerance); | |||
r235 | } | |||
r318 | else { | |||
r433 | qCInfo(LOG_VisualizationGraphWidget()) << tr("Detection zoom in: "); | |||
r318 | } | |||
|
r227 | } | ||
} | ||||
|
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 | ||||
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(); | |||
auto dateTime = SqpDateTime{grapheRange.lower, grapheRange.upper}; | ||||
r235 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); | |||
it != impl->m_VariableToPlotMultiMap.cend(); ++it) { | ||||
auto variable = it->first; | ||||
r433 | qCInfo(LOG_VisualizationGraphWidget()) | |||
<< "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" | ||||
<< variable->dateTime(); | ||||
qCInfo(LOG_VisualizationGraphWidget()) | ||||
<< "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime; | ||||
if (dateTime.contains(variable->dateTime()) || dateTime.intersect(variable->dateTime())) { | ||||
VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second, | ||||
variable->dataSeries(), variable->dateTime()); | ||||
} | ||||
r235 | } | |||
} | ||||