From 4b445d4313d1c65725a148e0c1652ba06047b283 2017-07-25 15:40:43 From: Alexandre Leroux Date: 2017-07-25 15:40:43 Subject: [PATCH] Limits requests on mouse wheels/pans --- diff --git a/gui/include/Visualization/VisualizationGraphWidget.h b/gui/include/Visualization/VisualizationGraphWidget.h index 1f31ed2..bbda9f9 100644 --- a/gui/include/Visualization/VisualizationGraphWidget.h +++ b/gui/include/Visualization/VisualizationGraphWidget.h @@ -67,7 +67,7 @@ private slots: void onGraphMenuRequested(const QPoint &pos) noexcept; /// Rescale the X axe to range parameter - void onRangeChanged(const QCPRange &t1, const QCPRange &t2); + void processRangeChange(); /// Slot called when a mouse wheel was made, to perform some processing before the zoom is done void onMouseWheel(QWheelEvent *event) noexcept; diff --git a/gui/src/Visualization/VisualizationGraphWidget.cpp b/gui/src/Visualization/VisualizationGraphWidget.cpp index bf903ae..8dbce13 100644 --- a/gui/src/Visualization/VisualizationGraphWidget.cpp +++ b/gui/src/Visualization/VisualizationGraphWidget.cpp @@ -15,6 +15,9 @@ Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget") namespace { +/// Timeout from which the mouse wheel is considered completed (in ms) +const auto MOUSE_WHEEL_TIMEOUT = 250; + /// Key pressed to enable zoom on horizontal axis const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier; @@ -25,7 +28,11 @@ const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier; struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate { - explicit VisualizationGraphWidgetPrivate() : m_DoSynchronize{true}, m_IsCalibration{false} {} + explicit VisualizationGraphWidgetPrivate() + : m_DoSynchronize{true}, m_IsCalibration{false}, m_RefRange{}, m_MouseWheelTimer{} + { + m_MouseWheelTimer.setSingleShot(true); + } // Return the operation when range changed @@ -36,6 +43,8 @@ struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate { bool m_DoSynchronize; bool m_IsCalibration; + QCPRange m_RefRange; + QTimer m_MouseWheelTimer; }; VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent) @@ -61,9 +70,10 @@ VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget connect(ui->widget, &QCustomPlot::mouseRelease, this, &VisualizationGraphWidget::onMouseRelease); connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel); - connect(ui->widget->xAxis, static_cast( - &QCPAxis::rangeChanged), - this, &VisualizationGraphWidget::onRangeChanged, Qt::DirectConnection); + + // Connects mouse wheel timer + connect(&impl->m_MouseWheelTimer, &QTimer::timeout, this, + &VisualizationGraphWidget::processRangeChange); // Activates menu when right clicking on the graph ui->widget->setContextMenuPolicy(Qt::CustomContextMenu); @@ -218,14 +228,17 @@ void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept } } -void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2) +void VisualizationGraphWidget::processRangeChange() { - qCInfo(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged") + qCInfo(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::processRangeChange") << QThread::currentThread()->objectName(); - auto dateTimeRange = SqpDateTime{t1.lower, t1.upper}; + auto oldRange = impl->m_RefRange; + auto newRange = ui->widget->xAxis->range(); - auto zoomType = impl->getZoomType(t1, t2); + auto dateTimeRange = SqpDateTime{newRange.lower, newRange.upper}; + + auto zoomType = impl->getZoomType(newRange, impl->m_RefRange); for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); it != impl->m_VariableToPlotMultiMap.cend(); ++it) { @@ -310,16 +323,20 @@ void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange } if (impl->m_DoSynchronize && !impl->m_IsCalibration) { - auto oldDateTime = SqpDateTime{t2.lower, t2.upper}; + auto oldDateTime = SqpDateTime{oldRange.lower, oldRange.upper}; qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: VisualizationGraphWidget::Synchronize notify !!") << QThread::currentThread()->objectName(); emit synchronize(dateTimeRange, oldDateTime, zoomType); } + + impl->m_RefRange = newRange; } void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept { + impl->m_MouseWheelTimer.start(MOUSE_WHEEL_TIMEOUT); + auto zoomOrientations = QFlags{}; // Lambda that enables a zoom orientation if the key modifier related to this orientation @@ -338,12 +355,14 @@ void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept void VisualizationGraphWidget::onMousePress(QMouseEvent *event) noexcept { + impl->m_RefRange = ui->widget->xAxis->range(); impl->m_IsCalibration = event->modifiers().testFlag(Qt::ControlModifier); } void VisualizationGraphWidget::onMouseRelease(QMouseEvent *event) noexcept { impl->m_IsCalibration = false; + processRangeChange(); } void VisualizationGraphWidget::onDataCacheVariableUpdated()