Files
ResInsight/ApplicationLibCode/UserInterface/RiuQwtPlotWheelZoomer.cpp
T

117 lines
4.5 KiB
C++
Raw Normal View History

2017-03-27 16:45:00 +02:00
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2017- Statoil ASA
//
2017-03-27 16:45:00 +02:00
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
2017-03-27 16:45:00 +02:00
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
2017-03-27 16:45:00 +02:00
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiuQwtPlotWheelZoomer.h"
2022-03-11 13:24:01 +01:00
#include "caf.h"
2022-03-18 13:16:07 +01:00
#include "qwt_interval.h"
2017-03-27 16:45:00 +02:00
#include "qwt_plot.h"
#include "qwt_scale_div.h"
2022-03-18 13:16:07 +01:00
#include "qwt_scale_map.h"
2021-02-22 17:12:50 +01:00
2017-03-27 16:45:00 +02:00
#include <QEvent>
#include <QWheelEvent>
2021-02-22 17:12:50 +01:00
#include <algorithm>
#define RIU_LOGARITHMIC_MINIMUM 1.0e-15
#define RIU_SCROLLWHEEL_ZOOMFACTOR 1.1
#define RIU_SCROLLWHEEL_PANFACTOR 0.1
2017-03-27 16:45:00 +02:00
//--------------------------------------------------------------------------------------------------
///
2017-03-27 16:45:00 +02:00
//--------------------------------------------------------------------------------------------------
RiuQwtPlotWheelZoomer::RiuQwtPlotWheelZoomer( QwtPlot* plot )
: QObject( plot )
, m_plot( plot )
2017-03-27 16:45:00 +02:00
{
plot->canvas()->installEventFilter( this );
2017-03-27 16:45:00 +02:00
}
//--------------------------------------------------------------------------------------------------
///
2017-03-27 16:45:00 +02:00
//--------------------------------------------------------------------------------------------------
void RiuQwtPlotWheelZoomer::zoomOnAxis( QwtPlot* plot, QwtAxis::Position axis, double zoomFactor, int eventPos )
2017-03-27 16:45:00 +02:00
{
QwtScaleMap scaleMap = plot->canvasMap( axis );
double zoomCenter = scaleMap.invTransform( eventPos );
double newMin = zoomCenter - zoomFactor * ( zoomCenter - scaleMap.s1() );
double newMax = zoomCenter + zoomFactor * ( -zoomCenter + scaleMap.s2() );
// the QwtScaleDiv::interval yields the current axis range
// The following thus doesn't limit the zoom to the min/max data but
// Stops the zoom from changing too much in one step
QwtInterval axisRange = plot->axisScaleDiv( axis ).interval();
if ( axisIsLogarithmic( axis ) )
{
// Handle inverted axes as well by not assuming maxValue > minValue
2020-02-12 11:43:15 +01:00
double minValue = std::max( RIU_LOGARITHMIC_MINIMUM, 0.1 * std::min( axisRange.minValue(), axisRange.maxValue() ) );
2023-02-26 10:48:40 +01:00
double maxValue = std::max( RIU_LOGARITHMIC_MINIMUM, 10.0 * std::max( axisRange.minValue(), axisRange.maxValue() ) );
newMin = std::clamp( newMin, minValue, maxValue );
newMax = std::clamp( newMax, minValue, maxValue );
}
plot->setAxisScale( axis, newMin, newMax );
2017-03-27 16:45:00 +02:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiuQwtPlotWheelZoomer::axisIsLogarithmic( QwtAxis::Position axis ) const
{
auto it = m_axesAreLogarithmic.find( axis );
return it != m_axesAreLogarithmic.end() ? it->second : false;
}
2017-03-27 16:45:00 +02:00
//--------------------------------------------------------------------------------------------------
///
2017-03-27 16:45:00 +02:00
//--------------------------------------------------------------------------------------------------
bool RiuQwtPlotWheelZoomer::eventFilter( QObject* watched, QEvent* event )
2017-03-27 16:45:00 +02:00
{
QWheelEvent* wheelEvent = dynamic_cast<QWheelEvent*>( event );
2017-03-27 16:45:00 +02:00
if ( wheelEvent )
{
double zoomFactor = 1.0 / RIU_SCROLLWHEEL_ZOOMFACTOR;
2022-03-11 13:24:01 +01:00
if ( wheelEvent->angleDelta().y() > 0 )
2017-03-27 16:45:00 +02:00
{
2017-03-27 18:22:09 +02:00
zoomFactor = RIU_SCROLLWHEEL_ZOOMFACTOR;
}
2017-03-27 16:45:00 +02:00
2022-03-11 13:24:01 +01:00
auto position = caf::position( wheelEvent );
zoomOnAxis( m_plot, QwtAxis::XBottom, zoomFactor, position.x() );
zoomOnAxis( m_plot, QwtAxis::XTop, zoomFactor, position.x() );
zoomOnAxis( m_plot, QwtAxis::YLeft, zoomFactor, position.y() );
zoomOnAxis( m_plot, QwtAxis::YRight, zoomFactor, position.y() );
2017-03-27 16:45:00 +02:00
2017-03-27 18:22:09 +02:00
m_plot->replot();
emit zoomUpdated();
2017-03-27 16:45:00 +02:00
}
2017-03-27 18:22:09 +02:00
2017-03-27 16:45:00 +02:00
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuQwtPlotWheelZoomer::setAxisIsLogarithmic( QwtAxis::Position axis, bool logarithmic )
{
m_axesAreLogarithmic[axis] = logarithmic;
}