VisualizationGraphWidget.cpp
236 lines
| 9.0 KiB
| text/x-c
|
CppLexer
r93 | #include "Visualization/VisualizationGraphWidget.h" | |||
|
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> | |||
|
r204 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget") | ||
|
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 { | |||
// 1 variable -> n qcpplot | ||||
|
r250 | std::multimap<std::shared_ptr<Variable>, QCPAbstractPlottable *> m_VariableToPlotMultiMap; | ||
r111 | }; | |||
|
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); | ||||
|
r165 | |||
|
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)); | ||||
|
r182 | |||
|
r165 | // Set qcpplot properties : | ||
|
r167 | // - Drag (on x-axis) and zoom are enabled | ||
|
r166 | // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation | ||
|
r165 | ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); | ||
|
r167 | ui->widget->axisRect()->setRangeDrag(Qt::Horizontal); | ||
|
r166 | connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel); | ||
|
r211 | connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>( | ||
&QCPAxis::rangeChanged), | ||||
this, &VisualizationGraphWidget::onRangeChanged); | ||||
|
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 | } | |||
|
r211 | |||
r58 | VisualizationGraphWidget::~VisualizationGraphWidget() | |||
{ | ||||
delete ui; | ||||
} | ||||
r111 | ||||
void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable) | ||||
{ | ||||
|
r171 | // Uses delegate to create the qcpplot components according to the variable | ||
r227 | auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); | |||
|
r171 | |||
for (auto createdPlottable : qAsConst(createdPlottables)) { | ||||
r219 | impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable}); | |||
|
r171 | } | ||
r219 | ||||
r276 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); | |||
r111 | } | |||
|
r250 | void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept | ||
{ | ||||
|
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(); | ||||
|
r250 | } | ||
|
r192 | void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor) | ||
r111 | { | |||
|
r193 | if (visitor) { | ||
visitor->visit(this); | ||||
} | ||||
|
r204 | else { | ||
qCCritical(LOG_VisualizationGraphWidget()) | ||||
<< tr("Can't visit widget : the visitor is null"); | ||||
} | ||||
r111 | } | |||
|
r194 | bool VisualizationGraphWidget::canDrop(const Variable &variable) const | ||
{ | ||||
/// @todo : for the moment, a graph can always accomodate a variable | ||||
Q_UNUSED(variable); | ||||
return true; | ||||
} | ||||
r112 | QString VisualizationGraphWidget::name() const | |||
r111 | { | |||
|
r246 | return ui->graphNameLabel->text(); | ||
r111 | } | |||
|
r249 | void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept | ||
r111 | { | |||
|
r249 | QMenu graphMenu{}; | ||
|
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); }); | ||||
|
r182 | } | ||
|
r249 | |||
if (!graphMenu.isEmpty()) { | ||||
graphMenu.exec(mapToGlobal(pos)); | ||||
|
r182 | } | ||
r111 | } | |||
|
r166 | |||
|
r211 | void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2) | ||
{ | ||||
r219 | ||||
qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged"); | ||||
for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); | ||||
it != impl->m_VariableToPlotMultiMap.cend(); ++it) { | ||||
r240 | ||||
r219 | auto variable = it->first; | |||
r240 | auto dateTime = SqpDateTime{t2.lower, t2.upper}; | |||
r219 | ||||
if (!variable->contains(dateTime)) { | ||||
r240 | ||||
r241 | auto variableDateTimeWithTolerance = dateTime; | |||
r240 | if (variable->intersect(dateTime)) { | |||
auto variableDateTime = variable->dateTime(); | ||||
if (variableDateTime.m_TStart < dateTime.m_TStart) { | ||||
r243 | ||||
auto diffEndToKeepDelta = dateTime.m_TEnd - variableDateTime.m_TEnd; | ||||
dateTime.m_TStart = variableDateTime.m_TStart + diffEndToKeepDelta; | ||||
// Tolerance have to be added to the right | ||||
r241 | // add 10% tolerance for right (end) side | |||
auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); | ||||
variableDateTimeWithTolerance.m_TEnd += tolerance; | ||||
r240 | } | |||
if (variableDateTime.m_TEnd > dateTime.m_TEnd) { | ||||
r259 | auto diffStartToKeepDelta = variableDateTime.m_TStart - dateTime.m_TStart; | |||
r243 | dateTime.m_TEnd = variableDateTime.m_TEnd - diffStartToKeepDelta; | |||
// Tolerance have to be added to the left | ||||
r241 | // add 10% tolerance for left (start) side | |||
auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); | ||||
variableDateTimeWithTolerance.m_TStart -= tolerance; | ||||
r240 | } | |||
} | ||||
else { | ||||
// add 10% tolerance for each side | ||||
auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); | ||||
r241 | variableDateTimeWithTolerance.m_TStart -= tolerance; | |||
variableDateTimeWithTolerance.m_TEnd += tolerance; | ||||
r240 | } | |||
r241 | variable->setDateTime(dateTime); | |||
// CHangement detected, we need to ask controller to request data loading | ||||
r276 | emit requestDataLoading(variable, variableDateTimeWithTolerance); | |||
r219 | } | |||
|
r211 | } | ||
} | ||||
|
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 | ||||
|
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 | ||||
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 | ||||
r219 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); | |||
it != impl->m_VariableToPlotMultiMap.cend(); ++it) { | ||||
auto variable = it->first; | ||||
r227 | VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second, | |||
variable->dataSeries(), variable->dateTime()); | ||||
r219 | } | |||
} | ||||
void VisualizationGraphWidget::updateDisplay(std::shared_ptr<Variable> variable) | ||||
{ | ||||
auto abstractPlotableItPair = impl->m_VariableToPlotMultiMap.equal_range(variable); | ||||
auto abstractPlotableVect = QVector<QCPAbstractPlottable *>{}; | ||||
for (auto it = abstractPlotableItPair.first; it != abstractPlotableItPair.second; ++it) { | ||||
abstractPlotableVect.push_back(it->second); | ||||
} | ||||
r227 | VisualizationGraphHelper::updateData(abstractPlotableVect, variable->dataSeries(), | |||
variable->dateTime()); | ||||
r219 | } | |||