VisualizationGraphWidget.cpp
797 lines
| 28.1 KiB
| text/x-c
|
CppLexer
r95 | #include "Visualization/VisualizationGraphWidget.h" | |||
Alexandre Leroux
|
r207 | #include "Visualization/IVisualizationWidgetVisitor.h" | ||
r1003 | #include "Visualization/VisualizationCursorItem.h" | |||
Alexandre Leroux
|
r582 | #include "Visualization/VisualizationDefs.h" | ||
r243 | #include "Visualization/VisualizationGraphHelper.h" | |||
Alexandre Leroux
|
r480 | #include "Visualization/VisualizationGraphRenderingDelegate.h" | ||
r1084 | #include "Visualization/VisualizationSelectionZoneItem.h" | |||
r842 | #include "Visualization/VisualizationZoneWidget.h" | |||
r58 | #include "ui_VisualizationGraphWidget.h" | |||
r850 | #include <Common/MimeTypesDef.h> | |||
r235 | #include <Data/ArrayData.h> | |||
#include <Data/IDataSeries.h> | ||||
Alexandre Leroux
|
r1063 | #include <Data/SpectrogramSeries.h> | ||
r890 | #include <DragAndDrop/DragDropHelper.h> | |||
Alexandre Leroux
|
r470 | #include <Settings/SqpSettingsDefs.h> | ||
r235 | #include <SqpApplication.h> | |||
r884 | #include <Time/TimeController.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 { | ||
r1087 | /// Key pressed to enable drag&drop in all modes | |||
const auto DRAG_DROP_MODIFIER = Qt::AltModifier; | ||||
Alexandre Leroux
|
r179 | /// Key pressed to enable zoom on horizontal axis | ||
r1001 | const auto HORIZONTAL_ZOOM_MODIFIER = Qt::ControlModifier; | |||
Alexandre Leroux
|
r179 | |||
/// Key pressed to enable zoom on vertical axis | ||||
r1001 | const auto VERTICAL_ZOOM_MODIFIER = Qt::ShiftModifier; | |||
/// Speed of a step of a wheel event for a pan, in percentage of the axis range | ||||
const auto PAN_SPEED = 5; | ||||
/// Key pressed to enable a calibration pan | ||||
const auto VERTICAL_PAN_MODIFIER = Qt::AltModifier; | ||||
Alexandre Leroux
|
r179 | |||
r1002 | /// Minimum size for the zoom box, in percentage of the axis range | |||
const auto ZOOM_BOX_MIN_SIZE = 0.8; | ||||
r1003 | /// Format of the dates appearing in the label of a cursor | |||
const auto CURSOR_LABELS_DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd\nhh:mm:ss:zzz"); | ||||
Alexandre Leroux
|
r179 | |||
} // 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
|
r1061 | void updateData(PlottablesMap &plottables, std::shared_ptr<IDataSeries> dataSeries, | ||
const SqpRange &range) | ||||
{ | ||||
VisualizationGraphHelper::updateData(plottables, dataSeries, range); | ||||
// Prevents that data has changed to update rendering | ||||
m_RenderingDelegate->onPlotUpdated(); | ||||
} | ||||
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 | /// Delegate used to attach rendering features to the plot | ||
std::unique_ptr<VisualizationGraphRenderingDelegate> m_RenderingDelegate; | ||||
r1002 | ||||
r1084 | QCPItemRect *m_DrawingZoomRect = nullptr; | |||
r1086 | QStack<QPair<QCPRange, QCPRange> > m_ZoomStack; | |||
r1084 | ||||
r1003 | std::unique_ptr<VisualizationCursorItem> m_HorizontalCursor = nullptr; | |||
std::unique_ptr<VisualizationCursorItem> m_VerticalCursor = nullptr; | ||||
r1002 | ||||
r1084 | VisualizationSelectionZoneItem *m_DrawingZone = nullptr; | |||
VisualizationSelectionZoneItem *m_HoveredZone = nullptr; | ||||
QVector<VisualizationSelectionZoneItem *> m_SelectionZones; | ||||
r1002 | ||||
void startDrawingRect(const QPoint &pos, QCustomPlot &plot) | ||||
{ | ||||
removeDrawingRect(plot); | ||||
auto axisPos = posToAxisPos(pos, plot); | ||||
r1084 | m_DrawingZoomRect = new QCPItemRect{&plot}; | |||
QPen p; | ||||
p.setWidth(2); | ||||
m_DrawingZoomRect->setPen(p); | ||||
r1002 | ||||
r1084 | m_DrawingZoomRect->topLeft->setCoords(axisPos); | |||
m_DrawingZoomRect->bottomRight->setCoords(axisPos); | ||||
r1002 | } | |||
void removeDrawingRect(QCustomPlot &plot) | ||||
{ | ||||
r1084 | if (m_DrawingZoomRect) { | |||
plot.removeItem(m_DrawingZoomRect); // the item is deleted by QCustomPlot | ||||
m_DrawingZoomRect = nullptr; | ||||
r1002 | plot.replot(QCustomPlot::rpQueuedReplot); | |||
} | ||||
} | ||||
r1084 | void startDrawingZone(const QPoint &pos, QCustomPlot &plot) | |||
{ | ||||
endDrawingZone(plot); | ||||
auto axisPos = posToAxisPos(pos, plot); | ||||
m_DrawingZone = new VisualizationSelectionZoneItem{&plot}; | ||||
m_DrawingZone->setRange(axisPos.x(), axisPos.x()); | ||||
m_DrawingZone->setEditionEnabled(false); | ||||
} | ||||
void endDrawingZone(QCustomPlot &plot) | ||||
{ | ||||
if (m_DrawingZone) { | ||||
auto drawingZoneRange = m_DrawingZone->range(); | ||||
if (qAbs(drawingZoneRange.m_TEnd - drawingZoneRange.m_TStart) > 0) { | ||||
m_DrawingZone->setEditionEnabled(true); | ||||
m_SelectionZones.append(m_DrawingZone); | ||||
} | ||||
else { | ||||
r1085 | plot.removeItem(m_DrawingZone); // the item is deleted by QCustomPlot | |||
r1084 | } | |||
plot.replot(QCustomPlot::rpQueuedReplot); | ||||
m_DrawingZone = nullptr; | ||||
} | ||||
} | ||||
void setSelectionZonesEditionEnabled(bool value) | ||||
{ | ||||
for (auto s : m_SelectionZones) { | ||||
s->setEditionEnabled(value); | ||||
} | ||||
} | ||||
r1087 | VisualizationSelectionZoneItem *selectionZoneAt(const QPoint &pos, | |||
const QCustomPlot &plot) const | ||||
{ | ||||
VisualizationSelectionZoneItem *selectionZoneItemUnderCursor = nullptr; | ||||
auto minDistanceToZone = -1; | ||||
for (auto zone : m_SelectionZones) { | ||||
auto distanceToZone = zone->selectTest(pos, false); | ||||
if ((minDistanceToZone < 0 || distanceToZone <= minDistanceToZone) | ||||
&& distanceToZone >= 0 && distanceToZone < plot.selectionTolerance()) { | ||||
selectionZoneItemUnderCursor = zone; | ||||
} | ||||
} | ||||
return selectionZoneItemUnderCursor; | ||||
} | ||||
r1002 | QPointF posToAxisPos(const QPoint &pos, QCustomPlot &plot) const | |||
{ | ||||
auto axisX = plot.axisRect()->axis(QCPAxis::atBottom); | ||||
auto axisY = plot.axisRect()->axis(QCPAxis::atLeft); | ||||
return QPointF{axisX->pixelToCoord(pos.x()), axisY->pixelToCoord(pos.y())}; | ||||
} | ||||
r1003 | ||||
bool pointIsInAxisRect(const QPointF &axisPoint, QCustomPlot &plot) const | ||||
{ | ||||
auto axisX = plot.axisRect()->axis(QCPAxis::atBottom); | ||||
auto axisY = plot.axisRect()->axis(QCPAxis::atLeft); | ||||
return axisX->range().contains(axisPoint.x()) && axisY->range().contains(axisPoint.y()); | ||||
} | ||||
r118 | }; | |||
Alexandre Leroux
|
r205 | VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent) | ||
r842 | : 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 : | ||
r1084 | // - zoom is enabled | |||
Alexandre Leroux
|
r179 | // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation | ||
r1001 | ui->widget->setInteractions(QCP::iRangeZoom | QCP::iSelectItems); | |||
r1085 | ui->widget->axisRect()->setRangeDrag(Qt::Horizontal | Qt::Vertical); | |||
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); | ||||
r1003 | // Init the cursors | |||
impl->m_HorizontalCursor = std::make_unique<VisualizationCursorItem>(&plot()); | ||||
impl->m_HorizontalCursor->setOrientation(Qt::Horizontal); | ||||
impl->m_VerticalCursor = std::make_unique<VisualizationCursorItem>(&plot()); | ||||
impl->m_VerticalCursor->setOrientation(Qt::Vertical); | ||||
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); | ||
Alexandre Leroux
|
r1043 | connect(ui->widget, &QCustomPlot::mouseDoubleClick, this, | ||
&VisualizationGraphWidget::onMouseDoubleClick); | ||||
r848 | 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); | ||||
Thibaud Rabillard
|
r1006 | |||
#ifdef Q_OS_MAC | ||||
plot().setPlottingHint(QCP::phFastPolylines, true); | ||||
#endif | ||||
r58 | } | |||
Alexandre Leroux
|
r227 | |||
r58 | VisualizationGraphWidget::~VisualizationGraphWidget() | |||
{ | ||||
delete ui; | ||||
} | ||||
r118 | ||||
r842 | VisualizationZoneWidget *VisualizationGraphWidget::parentZoneWidget() const noexcept | |||
{ | ||||
auto parent = parentWidget(); | ||||
r849 | while (parent != nullptr && !qobject_cast<VisualizationZoneWidget *>(parent)) { | |||
r842 | parent = parent->parentWidget(); | |||
r849 | } | |||
r842 | ||||
r847 | return qobject_cast<VisualizationZoneWidget *>(parent); | |||
r842 | } | |||
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); | ||
Alexandre Leroux
|
r729 | |||
if (auto dataSeries = variable->dataSeries()) { | ||||
Alexandre Leroux
|
r917 | // Set axes properties according to the units of the data series | ||
impl->m_RenderingDelegate->setAxesProperties(dataSeries); | ||||
Alexandre Leroux
|
r920 | |||
// Sets rendering properties for the new plottables | ||||
// Warning: this method must be called after setAxesProperties(), as it can access to some | ||||
// axes properties that have to be initialized | ||||
impl->m_RenderingDelegate->setPlottablesProperties(dataSeries, createdPlottables); | ||||
Alexandre Leroux
|
r729 | } | ||
Alexandre Leroux
|
r917 | |||
impl->m_VariableToPlotMultiMap.insert({variable, std::move(createdPlottables)}); | ||||
Alexandre Leroux
|
r729 | |||
r298 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); | |||
r540 | ||||
r548 | this->enableAcquisition(false); | |||
this->setGraphRange(range); | ||||
this->enableAcquisition(true); | ||||
r314 | ||||
r816 | 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 | } | ||
r847 | QList<std::shared_ptr<Variable> > VisualizationGraphWidget::variables() const | |||
r842 | { | |||
r847 | auto variables = QList<std::shared_ptr<Variable> >{}; | |||
for (auto it = std::cbegin(impl->m_VariableToPlotMultiMap); | ||||
it != std::cend(impl->m_VariableToPlotMultiMap); ++it) { | ||||
r842 | variables << it->first; | |||
} | ||||
return variables; | ||||
} | ||||
Alexandre Leroux
|
r903 | void VisualizationGraphWidget::setYRange(std::shared_ptr<Variable> variable) | ||
r548 | { | |||
Alexandre Leroux
|
r903 | if (!variable) { | ||
qCCritical(LOG_VisualizationGraphWidget()) << "Can't set y-axis range: variable is null"; | ||||
return; | ||||
} | ||||
VisualizationGraphHelper::setYAxisRange(variable, *ui->widget); | ||||
r548 | } | |||
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 | } | |||
r1086 | void VisualizationGraphWidget::undoZoom() | |||
{ | ||||
auto zoom = impl->m_ZoomStack.pop(); | ||||
auto axisX = plot().axisRect()->axis(QCPAxis::atBottom); | ||||
auto axisY = plot().axisRect()->axis(QCPAxis::atLeft); | ||||
axisX->setRange(zoom.first); | ||||
axisY->setRange(zoom.second); | ||||
plot().replot(QCustomPlot::rpQueuedReplot); | ||||
} | ||||
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 | ||
{ | ||||
Alexandre Leroux
|
r1063 | auto isSpectrogram = [](const auto &variable) { | ||
return std::dynamic_pointer_cast<SpectrogramSeries>(variable.dataSeries()) != nullptr; | ||||
}; | ||||
// - A spectrogram series can't be dropped on graph with existing plottables | ||||
// - No data series can be dropped on graph with existing spectrogram series | ||||
return isSpectrogram(variable) | ||||
? impl->m_VariableToPlotMultiMap.empty() | ||||
: std::none_of( | ||||
impl->m_VariableToPlotMultiMap.cbegin(), impl->m_VariableToPlotMultiMap.cend(), | ||||
[isSpectrogram](const auto &entry) { return isSpectrogram(*entry.first); }); | ||||
Alexandre Leroux
|
r209 | } | ||
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 | } | |||
r1087 | QMimeData *VisualizationGraphWidget::mimeData(const QPoint &position) const | |||
r842 | { | |||
r849 | auto mimeData = new QMimeData; | |||
r842 | ||||
r1087 | auto selectionZoneItemUnderCursor = impl->selectionZoneAt(position, plot()); | |||
if (sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::SelectionZones | ||||
&& selectionZoneItemUnderCursor) { | ||||
mimeData->setData(MIME_TYPE_TIME_RANGE, TimeController::mimeDataForTimeRange( | ||||
selectionZoneItemUnderCursor->range())); | ||||
mimeData->setData(MIME_TYPE_SELECTION_ZONE, TimeController::mimeDataForTimeRange( | ||||
selectionZoneItemUnderCursor->range())); | ||||
} | ||||
else { | ||||
mimeData->setData(MIME_TYPE_GRAPH, QByteArray{}); | ||||
auto timeRangeData = TimeController::mimeDataForTimeRange(graphRange()); | ||||
mimeData->setData(MIME_TYPE_TIME_RANGE, timeRangeData); | ||||
} | ||||
r884 | ||||
r847 | return mimeData; | |||
r842 | } | |||
r1087 | QPixmap VisualizationGraphWidget::customDragPixmap(const QPoint &dragPosition) | |||
{ | ||||
auto selectionZoneItemUnderCursor = impl->selectionZoneAt(dragPosition, plot()); | ||||
if (sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::SelectionZones | ||||
&& selectionZoneItemUnderCursor) { | ||||
auto zoneTopLeft = selectionZoneItemUnderCursor->topLeft->pixelPosition(); | ||||
auto zoneBottomRight = selectionZoneItemUnderCursor->bottomRight->pixelPosition(); | ||||
auto zoneSize = QSizeF{qAbs(zoneBottomRight.x() - zoneTopLeft.x()), | ||||
qAbs(zoneBottomRight.y() - zoneTopLeft.y())} | ||||
.toSize(); | ||||
auto pixmap = QPixmap(zoneSize); | ||||
render(&pixmap, QPoint(), QRegion{QRect{zoneTopLeft.toPoint(), zoneSize}}); | ||||
return pixmap; | ||||
} | ||||
return QPixmap(); | ||||
} | ||||
r842 | bool VisualizationGraphWidget::isDragAllowed() const | |||
{ | ||||
return true; | ||||
} | ||||
r879 | void VisualizationGraphWidget::highlightForMerge(bool highlighted) | |||
{ | ||||
if (highlighted) { | ||||
plot().setBackground(QBrush(QColor("#BBD5EE"))); | ||||
} | ||||
else { | ||||
plot().setBackground(QBrush(Qt::white)); | ||||
} | ||||
plot().update(); | ||||
} | ||||
r1003 | void VisualizationGraphWidget::addVerticalCursor(double time) | |||
{ | ||||
impl->m_VerticalCursor->setPosition(time); | ||||
impl->m_VerticalCursor->setVisible(true); | ||||
auto text | ||||
= DateUtils::dateTime(time).toString(CURSOR_LABELS_DATETIME_FORMAT).replace(' ', '\n'); | ||||
impl->m_VerticalCursor->setLabelText(text); | ||||
} | ||||
void VisualizationGraphWidget::addVerticalCursorAtViewportPosition(double position) | ||||
{ | ||||
impl->m_VerticalCursor->setAbsolutePosition(position); | ||||
impl->m_VerticalCursor->setVisible(true); | ||||
auto axis = plot().axisRect()->axis(QCPAxis::atBottom); | ||||
auto text | ||||
= DateUtils::dateTime(axis->pixelToCoord(position)).toString(CURSOR_LABELS_DATETIME_FORMAT); | ||||
impl->m_VerticalCursor->setLabelText(text); | ||||
} | ||||
void VisualizationGraphWidget::removeVerticalCursor() | ||||
{ | ||||
impl->m_VerticalCursor->setVisible(false); | ||||
plot().replot(QCustomPlot::rpQueuedReplot); | ||||
} | ||||
void VisualizationGraphWidget::addHorizontalCursor(double value) | ||||
{ | ||||
impl->m_HorizontalCursor->setPosition(value); | ||||
impl->m_HorizontalCursor->setVisible(true); | ||||
impl->m_HorizontalCursor->setLabelText(QString::number(value)); | ||||
} | ||||
void VisualizationGraphWidget::addHorizontalCursorAtViewportPosition(double position) | ||||
{ | ||||
impl->m_HorizontalCursor->setAbsolutePosition(position); | ||||
impl->m_HorizontalCursor->setVisible(true); | ||||
auto axis = plot().axisRect()->axis(QCPAxis::atLeft); | ||||
impl->m_HorizontalCursor->setLabelText(QString::number(axis->pixelToCoord(position))); | ||||
} | ||||
void VisualizationGraphWidget::removeHorizontalCursor() | ||||
{ | ||||
impl->m_HorizontalCursor->setVisible(false); | ||||
plot().replot(QCustomPlot::rpQueuedReplot); | ||||
} | ||||
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); | ||||
r1003 | ||||
if (auto parentZone = parentZoneWidget()) { | ||||
parentZone->notifyMouseLeaveGraph(this); | ||||
} | ||||
else { | ||||
qCWarning(LOG_VisualizationGraphWidget()) << "leaveEvent: No parent zone widget"; | ||||
} | ||||
r1084 | ||||
if (impl->m_HoveredZone) { | ||||
impl->m_HoveredZone->setHovered(false); | ||||
impl->m_HoveredZone = nullptr; | ||||
} | ||||
Alexandre Leroux
|
r728 | } | ||
r1087 | QCustomPlot &VisualizationGraphWidget::plot() const noexcept | |||
Alexandre Leroux
|
r725 | { | ||
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 | |||
r1086 | if (!impl->m_ZoomStack.isEmpty()) { | |||
if (!graphMenu.isEmpty()) { | ||||
graphMenu.addSeparator(); | ||||
} | ||||
graphMenu.addAction(tr("Undo Zoom"), [this]() { undoZoom(); }); | ||||
} | ||||
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 | { | ||
r848 | 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 | } | |||
r816 | 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 | } | ||
r1003 | ||||
auto pos = mapFromGlobal(QCursor::pos()); | ||||
auto axisPos = impl->posToAxisPos(pos, plot()); | ||||
if (auto parentZone = parentZoneWidget()) { | ||||
if (impl->pointIsInAxisRect(axisPos, plot())) { | ||||
parentZone->notifyMouseMoveInGraph(pos, axisPos, this); | ||||
} | ||||
else { | ||||
parentZone->notifyMouseLeaveGraph(this); | ||||
} | ||||
} | ||||
else { | ||||
qCWarning(LOG_VisualizationGraphWidget()) << "onMouseMove: No parent zone widget"; | ||||
} | ||||
Alexandre Leroux
|
r227 | } | ||
Alexandre Leroux
|
r1043 | void VisualizationGraphWidget::onMouseDoubleClick(QMouseEvent *event) noexcept | ||
{ | ||||
impl->m_RenderingDelegate->onMouseDoubleClick(event); | ||||
} | ||||
Alexandre Leroux
|
r481 | void VisualizationGraphWidget::onMouseMove(QMouseEvent *event) noexcept | ||
{ | ||||
// Handles plot rendering when mouse is moving | ||||
impl->m_RenderingDelegate->onMouseMove(event); | ||||
r842 | ||||
r1003 | auto axisPos = impl->posToAxisPos(event->pos(), plot()); | |||
r1084 | // Zoom box and zone drawing | |||
if (impl->m_DrawingZoomRect) { | ||||
impl->m_DrawingZoomRect->bottomRight->setCoords(axisPos); | ||||
} | ||||
else if (impl->m_DrawingZone) { | ||||
impl->m_DrawingZone->setEnd(axisPos.x()); | ||||
r1002 | } | |||
r1084 | // Cursor | |||
r1003 | if (auto parentZone = parentZoneWidget()) { | |||
if (impl->pointIsInAxisRect(axisPos, plot())) { | ||||
parentZone->notifyMouseMoveInGraph(event->pos(), axisPos, this); | ||||
} | ||||
else { | ||||
parentZone->notifyMouseLeaveGraph(this); | ||||
} | ||||
} | ||||
else { | ||||
qCWarning(LOG_VisualizationGraphWidget()) << "onMouseMove: No parent zone widget"; | ||||
} | ||||
r1084 | // Search for the selection zone under the mouse | |||
r1087 | auto selectionZoneItemUnderCursor = impl->selectionZoneAt(event->pos(), plot()); | |||
r1084 | if (selectionZoneItemUnderCursor && !impl->m_DrawingZone | |||
&& sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::SelectionZones) { | ||||
// Sets the appropriate cursor shape | ||||
auto cursorShape = selectionZoneItemUnderCursor->curshorShapeForPosition(event->pos()); | ||||
setCursor(cursorShape); | ||||
// Manages the hovered zone | ||||
if (selectionZoneItemUnderCursor != impl->m_HoveredZone) { | ||||
if (impl->m_HoveredZone) { | ||||
impl->m_HoveredZone->setHovered(false); | ||||
} | ||||
selectionZoneItemUnderCursor->setHovered(true); | ||||
impl->m_HoveredZone = selectionZoneItemUnderCursor; | ||||
plot().replot(QCustomPlot::rpQueuedReplot); | ||||
} | ||||
} | ||||
else { | ||||
// There is no zone under the mouse or the interaction mode is not "selection zones" | ||||
if (impl->m_HoveredZone) { | ||||
impl->m_HoveredZone->setHovered(false); | ||||
impl->m_HoveredZone = nullptr; | ||||
} | ||||
setCursor(Qt::ArrowCursor); | ||||
} | ||||
r842 | VisualizationDragWidget::mouseMoveEvent(event); | |||
Alexandre Leroux
|
r481 | } | ||
Alexandre Leroux
|
r179 | void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept | ||
{ | ||||
r1001 | auto value = event->angleDelta().x() + event->angleDelta().y(); | |||
if (value != 0) { | ||||
auto direction = value > 0 ? 1.0 : -1.0; | ||||
auto isZoomX = event->modifiers().testFlag(HORIZONTAL_ZOOM_MODIFIER); | ||||
auto isZoomY = event->modifiers().testFlag(VERTICAL_ZOOM_MODIFIER); | ||||
impl->m_IsCalibration = event->modifiers().testFlag(VERTICAL_PAN_MODIFIER); | ||||
auto zoomOrientations = QFlags<Qt::Orientation>{}; | ||||
zoomOrientations.setFlag(Qt::Horizontal, isZoomX); | ||||
zoomOrientations.setFlag(Qt::Vertical, isZoomY); | ||||
ui->widget->axisRect()->setRangeZoom(zoomOrientations); | ||||
Alexandre Leroux
|
r179 | |||
r1001 | if (!isZoomX && !isZoomY) { | |||
auto axis = plot().axisRect()->axis(QCPAxis::atBottom); | ||||
auto diff = direction * (axis->range().size() * (PAN_SPEED / 100.0)); | ||||
Alexandre Leroux
|
r179 | |||
r1001 | axis->setRange(axis->range() + diff); | |||
if (plot().noAntialiasingOnDrag()) { | ||||
plot().setNotAntialiasedElements(QCP::aeAll); | ||||
} | ||||
plot().replot(QCustomPlot::rpQueuedReplot); | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r179 | } | ||
r235 | ||||
r445 | void VisualizationGraphWidget::onMousePress(QMouseEvent *event) noexcept | |||
{ | ||||
r1087 | bool isDragDropClick = event->modifiers().testFlag(DRAG_DROP_MODIFIER); | |||
if (!isDragDropClick) { | ||||
if (sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::ZoomBox) { | ||||
// Starts a zoom box | ||||
impl->startDrawingRect(event->pos(), plot()); | ||||
} | ||||
else if (sqpApp->plotsInteractionMode() | ||||
== SqpApplication::PlotsInteractionMode::SelectionZones | ||||
&& impl->m_DrawingZone == nullptr) { | ||||
// Starts a new selection zone | ||||
auto zoneAtPos = impl->selectionZoneAt(event->pos(), plot()); | ||||
if (!zoneAtPos) { | ||||
impl->startDrawingZone(event->pos(), plot()); | ||||
} | ||||
r1084 | } | |||
} | ||||
r1085 | else if (sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::None) { | |||
plot().setInteraction(QCP::iRangeDrag, true); | ||||
} | ||||
r1084 | ||||
// Allows mouse panning only in default mode | ||||
plot().setInteraction(QCP::iRangeDrag, sqpApp->plotsInteractionMode() | ||||
r1087 | == SqpApplication::PlotsInteractionMode::None | |||
&& !isDragDropClick); | ||||
r1084 | ||||
r1087 | // Allows zone edition only in selection zone mode without ALT pressed (ALT is for drag&drop) | |||
impl->setSelectionZonesEditionEnabled( | ||||
sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::SelectionZones | ||||
&& !isDragDropClick); | ||||
r842 | ||||
VisualizationDragWidget::mousePressEvent(event); | ||||
r445 | } | |||
void VisualizationGraphWidget::onMouseRelease(QMouseEvent *event) noexcept | ||||
{ | ||||
r1084 | if (impl->m_DrawingZoomRect) { | |||
r1002 | ||||
auto axisX = plot().axisRect()->axis(QCPAxis::atBottom); | ||||
auto axisY = plot().axisRect()->axis(QCPAxis::atLeft); | ||||
r1084 | auto newAxisXRange = QCPRange{impl->m_DrawingZoomRect->topLeft->coords().x(), | |||
impl->m_DrawingZoomRect->bottomRight->coords().x()}; | ||||
r1002 | ||||
r1084 | auto newAxisYRange = QCPRange{impl->m_DrawingZoomRect->topLeft->coords().y(), | |||
impl->m_DrawingZoomRect->bottomRight->coords().y()}; | ||||
r1002 | ||||
impl->removeDrawingRect(plot()); | ||||
if (newAxisXRange.size() > axisX->range().size() * (ZOOM_BOX_MIN_SIZE / 100.0) | ||||
&& newAxisYRange.size() > axisY->range().size() * (ZOOM_BOX_MIN_SIZE / 100.0)) { | ||||
r1086 | impl->m_ZoomStack.push(qMakePair(axisX->range(), axisY->range())); | |||
r1002 | axisX->setRange(newAxisXRange); | |||
axisY->setRange(newAxisYRange); | ||||
plot().replot(QCustomPlot::rpQueuedReplot); | ||||
} | ||||
} | ||||
r1084 | impl->endDrawingZone(plot()); | |||
r445 | 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
|
r1061 | impl->updateData(variableEntry.second, variable->dataSeries(), 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
|
r1061 | impl->updateData(it->second, variable->dataSeries(), range); | ||
r571 | } | |||
} | ||||