(#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.
This commit is contained in:
Pål Hagen 2015-09-04 15:57:34 +02:00
parent 8d57bbe77b
commit 752c4071b9
7 changed files with 80 additions and 54 deletions

View File

@ -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 newMinimum = zoomCenter - (zoomCenter - m_minimumVisibleDepth)*zoomFactor;
double newHalfDepth = zoomFactor*(m_maximumVisibleDepth - m_minimumVisibleDepth)/2; double newMaximum = zoomCenter + (m_maximumVisibleDepth - zoomCenter)*zoomFactor;
double newMinimum = center - newHalfDepth;
double newMaximum = center + newHalfDepth;
setVisibleDepthRange(newMinimum, newMaximum); setVisibleDepthRange(newMinimum, newMaximum);
} }

View File

@ -47,7 +47,7 @@ public:
RiuWellLogPlot* viewer(); RiuWellLogPlot* viewer();
void zoomDepth(double zoomFactor); void zoomDepth(double zoomFactor, double zoomCenter);
void panDepth(double panFactor); void panDepth(double panFactor);
void setVisibleDepthRange(double minimumDepth, double maximumDepth); void setVisibleDepthRange(double minimumDepth, double maximumDepth);

View File

@ -171,7 +171,7 @@ void RimWellLogPlotTrace::recreateViewer()
{ {
CVF_ASSERT(m_viewer == NULL); CVF_ASSERT(m_viewer == NULL);
m_viewer = new RiuWellLogTracePlot; m_viewer = new RiuWellLogTracePlot(this);
for (size_t cIdx = 0; cIdx < curves.size(); ++cIdx) for (size_t cIdx = 0; cIdx < curves.size(); ++cIdx)
{ {
curves[cIdx]->setPlot(this->m_viewer); curves[cIdx]->setPlot(this->m_viewer);

View File

@ -27,14 +27,10 @@
#include "cvfAssert.h" #include "cvfAssert.h"
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QWheelEvent>
#include <QScrollBar> #include <QScrollBar>
#include <math.h> #include <math.h>
#define RIU_SCROLLWHEEL_ZOOMFACTOR 1.1
#define RIU_SCROLLWHEEL_PANFACTOR 0.1
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -68,11 +64,9 @@ RiuWellLogPlot::~RiuWellLogPlot()
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
void RiuWellLogPlot::insertTracePlot(RiuWellLogTracePlot* tracePlot) void RiuWellLogPlot::insertTracePlot(RiuWellLogTracePlot* tracePlot)
{ {
// Insert the plot to the left of the scroll bar // Insert the plot to the left of the scroll bar
m_layout->insertWidget(m_layout->count() - 1, tracePlot); 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); updateScrollBar(minDepth, maxDepth);
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -108,36 +101,6 @@ void RiuWellLogPlot::updateScrollBar(double minDepth, double maxDepth)
m_scrollBar->setVisible(true); 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();
}
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------

View File

@ -26,7 +26,6 @@ class RimWellLogPlot;
class RiuWellLogTracePlot; class RiuWellLogTracePlot;
class QHBoxLayout; class QHBoxLayout;
class QWheelEvent;
class QScrollBar; class QScrollBar;
//================================================================================================== //==================================================================================================
@ -46,9 +45,6 @@ public:
void setDepthRange(double minDepth, double maxDepth); void setDepthRange(double minDepth, double maxDepth);
protected:
void wheelEvent(QWheelEvent* event);
private: private:
void updateScrollBar(double minDepth, double maxDepth); void updateScrollBar(double minDepth, double maxDepth);

View File

@ -19,17 +19,28 @@
#include "RiuWellLogTracePlot.h" #include "RiuWellLogTracePlot.h"
#include "RimWellLogPlot.h"
#include "RimWellLogPlotTrace.h"
#include "qwt_plot_grid.h" #include "qwt_plot_grid.h"
#include "qwt_legend.h" #include "qwt_legend.h"
#include "qwt_scale_engine.h" #include "qwt_scale_engine.h"
#include "qwt_plot_layout.h" #include "qwt_plot_layout.h"
#include <QWheelEvent>
#define RIU_SCROLLWHEEL_ZOOMFACTOR 1.1
#define RIU_SCROLLWHEEL_PANFACTOR 0.1
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
RiuWellLogTracePlot::RiuWellLogTracePlot(QWidget* parent) RiuWellLogTracePlot::RiuWellLogTracePlot(RimWellLogPlotTrace* plotTraceDefinition, QWidget* parent)
: QwtPlot(parent) : QwtPlot(parent)
{ {
Q_ASSERT(plotTraceDefinition);
m_plotTraceDefinition = plotTraceDefinition;
m_grid = new QwtPlotGrid(); m_grid = new QwtPlotGrid();
m_grid->attach(this); m_grid->attach(this);
@ -66,6 +77,9 @@ void RiuWellLogTracePlot::setDefaults()
canvasFrame->setFrameShape(QFrame::NoFrame); canvasFrame->setFrameShape(QFrame::NoFrame);
} }
canvas()->setMouseTracking(true);
canvas()->installEventFilter(this);
QPen gridPen(Qt::SolidLine); QPen gridPen(Qt::SolidLine);
gridPen.setColor(Qt::lightGray); gridPen.setColor(Qt::lightGray);
m_grid->setPen(gridPen); m_grid->setPen(gridPen);
@ -97,3 +111,52 @@ void RiuWellLogTracePlot::setDepthRange(double minDepth, double maxDepth)
setAxisScale(QwtPlot::yLeft, maxDepth, minDepth); setAxisScale(QwtPlot::yLeft, maxDepth, minDepth);
replot(); replot();
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiuWellLogTracePlot::eventFilter(QObject* watched, QEvent* event)
{
if (watched == canvas())
{
QWheelEvent* wheelEvent = dynamic_cast<QWheelEvent*>(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);
}

View File

@ -21,9 +21,12 @@
#include "qwt_plot.h" #include "qwt_plot.h"
class RimWellLogPlotTrace;
class QwtPlotGrid; class QwtPlotGrid;
class QwtLegend; class QwtLegend;
class QEvent;
//================================================================================================== //==================================================================================================
// //
// RiuWellLogTracePlot // RiuWellLogTracePlot
@ -34,16 +37,20 @@ class RiuWellLogTracePlot : public QwtPlot
Q_OBJECT Q_OBJECT
public: public:
RiuWellLogTracePlot(QWidget* parent = NULL); RiuWellLogTracePlot(RimWellLogPlotTrace* plotTraceDefinition, QWidget* parent = NULL);
virtual ~RiuWellLogTracePlot(); virtual ~RiuWellLogTracePlot();
void setDepthRange(double minDepth, double maxDepth); void setDepthRange(double minDepth, double maxDepth);
protected:
virtual bool eventFilter(QObject* watched, QEvent* event);
private: private:
void setDefaults(); void setDefaults();
private: private:
QwtPlotGrid* m_grid; RimWellLogPlotTrace* m_plotTraceDefinition;
QwtLegend* m_legend; QwtPlotGrid* m_grid;
QwtLegend* m_legend;
}; };