From 752c4071b9d7b7b921461309312d0b184cc279ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l=20Hagen?= Date: Fri, 4 Sep 2015 15:57:34 +0200 Subject: [PATCH] (#412) Improved scroll wheel zooming (zoom around mouse Y position) Moved scroll wheel handling to well log trace plot viewer, using event filter for the plot canvas to get the mouse Y position, which is transformed to the corresponding plot depth value and used as center for zooming. --- .../ProjectDataModel/RimWellLogPlot.cpp | 9 +-- .../ProjectDataModel/RimWellLogPlot.h | 2 +- .../ProjectDataModel/RimWellLogPlotTrace.cpp | 2 +- .../UserInterface/RiuWellLogPlot.cpp | 39 +---------- .../UserInterface/RiuWellLogPlot.h | 4 -- .../UserInterface/RiuWellLogTracePlot.cpp | 65 ++++++++++++++++++- .../UserInterface/RiuWellLogTracePlot.h | 13 +++- 7 files changed, 80 insertions(+), 54 deletions(-) diff --git a/ApplicationCode/ProjectDataModel/RimWellLogPlot.cpp b/ApplicationCode/ProjectDataModel/RimWellLogPlot.cpp index 63ecde53d6..71a170a501 100644 --- a/ApplicationCode/ProjectDataModel/RimWellLogPlot.cpp +++ b/ApplicationCode/ProjectDataModel/RimWellLogPlot.cpp @@ -151,13 +151,10 @@ RiuWellLogPlot* RimWellLogPlot::viewer() //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void RimWellLogPlot::zoomDepth(double zoomFactor) +void RimWellLogPlot::zoomDepth(double zoomFactor, double zoomCenter) { - double center = (m_maximumVisibleDepth + m_minimumVisibleDepth)/2; - double newHalfDepth = zoomFactor*(m_maximumVisibleDepth - m_minimumVisibleDepth)/2; - - double newMinimum = center - newHalfDepth; - double newMaximum = center + newHalfDepth; + double newMinimum = zoomCenter - (zoomCenter - m_minimumVisibleDepth)*zoomFactor; + double newMaximum = zoomCenter + (m_maximumVisibleDepth - zoomCenter)*zoomFactor; setVisibleDepthRange(newMinimum, newMaximum); } diff --git a/ApplicationCode/ProjectDataModel/RimWellLogPlot.h b/ApplicationCode/ProjectDataModel/RimWellLogPlot.h index 5afd02e4cc..9b62aeb058 100644 --- a/ApplicationCode/ProjectDataModel/RimWellLogPlot.h +++ b/ApplicationCode/ProjectDataModel/RimWellLogPlot.h @@ -47,7 +47,7 @@ public: RiuWellLogPlot* viewer(); - void zoomDepth(double zoomFactor); + void zoomDepth(double zoomFactor, double zoomCenter); void panDepth(double panFactor); void setVisibleDepthRange(double minimumDepth, double maximumDepth); diff --git a/ApplicationCode/ProjectDataModel/RimWellLogPlotTrace.cpp b/ApplicationCode/ProjectDataModel/RimWellLogPlotTrace.cpp index f311c3cc5b..4e399523fd 100644 --- a/ApplicationCode/ProjectDataModel/RimWellLogPlotTrace.cpp +++ b/ApplicationCode/ProjectDataModel/RimWellLogPlotTrace.cpp @@ -171,7 +171,7 @@ void RimWellLogPlotTrace::recreateViewer() { CVF_ASSERT(m_viewer == NULL); - m_viewer = new RiuWellLogTracePlot; + m_viewer = new RiuWellLogTracePlot(this); for (size_t cIdx = 0; cIdx < curves.size(); ++cIdx) { curves[cIdx]->setPlot(this->m_viewer); diff --git a/ApplicationCode/UserInterface/RiuWellLogPlot.cpp b/ApplicationCode/UserInterface/RiuWellLogPlot.cpp index 62935320eb..eec9b0c991 100644 --- a/ApplicationCode/UserInterface/RiuWellLogPlot.cpp +++ b/ApplicationCode/UserInterface/RiuWellLogPlot.cpp @@ -27,14 +27,10 @@ #include "cvfAssert.h" #include -#include #include #include -#define RIU_SCROLLWHEEL_ZOOMFACTOR 1.1 -#define RIU_SCROLLWHEEL_PANFACTOR 0.1 - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -68,11 +64,9 @@ RiuWellLogPlot::~RiuWellLogPlot() //-------------------------------------------------------------------------------------------------- void RiuWellLogPlot::insertTracePlot(RiuWellLogTracePlot* tracePlot) { - // Insert the plot to the left of the scroll bar m_layout->insertWidget(m_layout->count() - 1, tracePlot); - m_tracePlots.append(tracePlot); - + m_tracePlots.append(tracePlot); } //-------------------------------------------------------------------------------------------------- @@ -88,7 +82,6 @@ void RiuWellLogPlot::setDepthRange(double minDepth, double maxDepth) updateScrollBar(minDepth, maxDepth); } - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -108,36 +101,6 @@ void RiuWellLogPlot::updateScrollBar(double minDepth, double maxDepth) m_scrollBar->setVisible(true); } -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void RiuWellLogPlot::wheelEvent(QWheelEvent* event) -{ - if (!m_plotDefinition) - { - QWidget::wheelEvent(event); - return; - } - - if (event->modifiers() & Qt::ControlModifier) - { - if (event->delta() > 0) - { - m_plotDefinition->zoomDepth(RIU_SCROLLWHEEL_ZOOMFACTOR); - } - else - { - m_plotDefinition->zoomDepth(1.0/RIU_SCROLLWHEEL_ZOOMFACTOR); - } - } - else - { - m_plotDefinition->panDepth(event->delta() < 0 ? RIU_SCROLLWHEEL_PANFACTOR : -RIU_SCROLLWHEEL_PANFACTOR); - } - - event->accept(); -} - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationCode/UserInterface/RiuWellLogPlot.h b/ApplicationCode/UserInterface/RiuWellLogPlot.h index 9572ed4061..e743c3e6af 100644 --- a/ApplicationCode/UserInterface/RiuWellLogPlot.h +++ b/ApplicationCode/UserInterface/RiuWellLogPlot.h @@ -26,7 +26,6 @@ class RimWellLogPlot; class RiuWellLogTracePlot; class QHBoxLayout; -class QWheelEvent; class QScrollBar; //================================================================================================== @@ -46,9 +45,6 @@ public: void setDepthRange(double minDepth, double maxDepth); -protected: - void wheelEvent(QWheelEvent* event); - private: void updateScrollBar(double minDepth, double maxDepth); diff --git a/ApplicationCode/UserInterface/RiuWellLogTracePlot.cpp b/ApplicationCode/UserInterface/RiuWellLogTracePlot.cpp index 7be909fe56..3497f62817 100644 --- a/ApplicationCode/UserInterface/RiuWellLogTracePlot.cpp +++ b/ApplicationCode/UserInterface/RiuWellLogTracePlot.cpp @@ -19,17 +19,28 @@ #include "RiuWellLogTracePlot.h" +#include "RimWellLogPlot.h" +#include "RimWellLogPlotTrace.h" + #include "qwt_plot_grid.h" #include "qwt_legend.h" #include "qwt_scale_engine.h" #include "qwt_plot_layout.h" +#include + +#define RIU_SCROLLWHEEL_ZOOMFACTOR 1.1 +#define RIU_SCROLLWHEEL_PANFACTOR 0.1 + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -RiuWellLogTracePlot::RiuWellLogTracePlot(QWidget* parent) +RiuWellLogTracePlot::RiuWellLogTracePlot(RimWellLogPlotTrace* plotTraceDefinition, QWidget* parent) : QwtPlot(parent) { + Q_ASSERT(plotTraceDefinition); + m_plotTraceDefinition = plotTraceDefinition; + m_grid = new QwtPlotGrid(); m_grid->attach(this); @@ -66,6 +77,9 @@ void RiuWellLogTracePlot::setDefaults() canvasFrame->setFrameShape(QFrame::NoFrame); } + canvas()->setMouseTracking(true); + canvas()->installEventFilter(this); + QPen gridPen(Qt::SolidLine); gridPen.setColor(Qt::lightGray); m_grid->setPen(gridPen); @@ -97,3 +111,52 @@ void RiuWellLogTracePlot::setDepthRange(double minDepth, double maxDepth) setAxisScale(QwtPlot::yLeft, maxDepth, minDepth); replot(); } + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool RiuWellLogTracePlot::eventFilter(QObject* watched, QEvent* event) +{ + if (watched == canvas()) + { + QWheelEvent* wheelEvent = dynamic_cast(event); + if (wheelEvent) + { + if (!m_plotTraceDefinition) + { + return QwtPlot::eventFilter(watched, event); + } + + RimWellLogPlot* plotDefinition; + m_plotTraceDefinition->firstAnchestorOrThisOfType(plotDefinition); + if (!plotDefinition) + { + return QwtPlot::eventFilter(watched, event); + } + + if (wheelEvent->modifiers() & Qt::ControlModifier) + { + QwtScaleMap scaleMap = canvasMap(QwtPlot::yLeft); + double zoomCenter = scaleMap.invTransform(wheelEvent->pos().y()); + + if (wheelEvent->delta() > 0) + { + plotDefinition->zoomDepth(RIU_SCROLLWHEEL_ZOOMFACTOR, zoomCenter); + } + else + { + plotDefinition->zoomDepth(1.0/RIU_SCROLLWHEEL_ZOOMFACTOR, zoomCenter); + } + } + else + { + plotDefinition->panDepth(wheelEvent->delta() < 0 ? RIU_SCROLLWHEEL_PANFACTOR : -RIU_SCROLLWHEEL_PANFACTOR); + } + + event->accept(); + return true; + } + } + + return QwtPlot::eventFilter(watched, event); +} diff --git a/ApplicationCode/UserInterface/RiuWellLogTracePlot.h b/ApplicationCode/UserInterface/RiuWellLogTracePlot.h index 8666c79b3d..ac9e63be57 100644 --- a/ApplicationCode/UserInterface/RiuWellLogTracePlot.h +++ b/ApplicationCode/UserInterface/RiuWellLogTracePlot.h @@ -21,9 +21,12 @@ #include "qwt_plot.h" +class RimWellLogPlotTrace; class QwtPlotGrid; class QwtLegend; +class QEvent; + //================================================================================================== // // RiuWellLogTracePlot @@ -34,16 +37,20 @@ class RiuWellLogTracePlot : public QwtPlot Q_OBJECT public: - RiuWellLogTracePlot(QWidget* parent = NULL); + RiuWellLogTracePlot(RimWellLogPlotTrace* plotTraceDefinition, QWidget* parent = NULL); virtual ~RiuWellLogTracePlot(); void setDepthRange(double minDepth, double maxDepth); +protected: + virtual bool eventFilter(QObject* watched, QEvent* event); + private: void setDefaults(); private: - QwtPlotGrid* m_grid; - QwtLegend* m_legend; + RimWellLogPlotTrace* m_plotTraceDefinition; + QwtPlotGrid* m_grid; + QwtLegend* m_legend; };