Loading...
Searching...
No Matches
qwt_plot_zoomer.cpp
/******************************************************************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_plot_zoomer.h"
#include "qwt_plot.h"
#include "qwt_scale_div.h"
#include "qwt_scale_map.h"
#include "qwt_interval.h"
#include "qwt_picker_machine.h"
#include <qstack.h>
static QwtInterval qwtExpandedZoomInterval( double v1, double v2,
double minRange, const QwtTransform* transform )
{
double min = v1;
double max = v2;
if ( max - min < minRange )
{
min = 0.5 * ( min + max - minRange );
max = min + minRange;
if ( transform )
{
// f.e the logarithmic scale doesn't allow values
// outside [QwtLogTransform::LogMin/QwtLogTransform::LogMax]
double minBounded = transform->bounded( min );
double maxBounded = transform->bounded( max );
if ( minBounded != min )
{
maxBounded = transform->bounded( minBounded + minRange );
}
else if ( maxBounded != max )
{
minBounded = transform->bounded( maxBounded - minRange );
}
min = minBounded;
max = maxBounded;
}
}
return QwtInterval( min, max );
}
static QRectF qwtExpandedZoomRect( const QRectF& zoomRect, const QSizeF& minSize,
const QwtTransform* transformX, const QwtTransform* transformY )
{
QRectF r = zoomRect;
if ( minSize.width() > r.width() )
{
const QwtInterval intv = qwtExpandedZoomInterval(
r.left(), r.right(), minSize.width(), transformX );
r.setLeft( intv.minValue() );
r.setRight( intv.maxValue() );
}
if ( minSize.height() > r.height() )
{
const QwtInterval intv = qwtExpandedZoomInterval(
zoomRect.top(), zoomRect.bottom(), minSize.height(), transformY );
r.setTop( intv.minValue() );
r.setBottom( intv.maxValue() );
}
return r;
}
class QwtPlotZoomer::PrivateData
{
public:
uint zoomRectIndex;
QStack< QRectF > zoomStack;
int maxStackDepth;
};
QwtPlotZoomer::QwtPlotZoomer( QWidget* canvas, bool doReplot )
: QwtPlotPicker( canvas )
{
if ( canvas )
init( doReplot );
}
QwtPlotZoomer::QwtPlotZoomer( QwtAxisId xAxisId, QwtAxisId yAxisId,
QWidget* canvas, bool doReplot )
: QwtPlotPicker( xAxisId, yAxisId, canvas )
{
if ( canvas )
init( doReplot );
}
void QwtPlotZoomer::init( bool doReplot )
{
m_data = new PrivateData;
m_data->maxStackDepth = -1;
setTrackerMode( ActiveOnly );
setRubberBand( RectRubberBand );
setStateMachine( new QwtPickerDragRectMachine() );
if ( doReplot && plot() )
plot()->replot();
setZoomBase( scaleRect() );
}
QwtPlotZoomer::~QwtPlotZoomer()
{
delete m_data;
}
void QwtPlotZoomer::setMaxStackDepth( int depth )
{
m_data->maxStackDepth = depth;
if ( depth >= 0 )
{
// unzoom if the current depth is below m_data->maxStackDepth
const int zoomOut =
m_data->zoomStack.count() - 1 - depth; // -1 for the zoom base
if ( zoomOut > 0 )
{
zoom( -zoomOut );
for ( int i = m_data->zoomStack.count() - 1;
i > int( m_data->zoomRectIndex ); i-- )
{
( void )m_data->zoomStack.pop(); // remove trailing rects
}
}
}
}
int QwtPlotZoomer::maxStackDepth() const
{
return m_data->maxStackDepth;
}
const QStack< QRectF >& QwtPlotZoomer::zoomStack() const
{
return m_data->zoomStack;
}
QRectF QwtPlotZoomer::zoomBase() const
{
return m_data->zoomStack[0];
}
void QwtPlotZoomer::setZoomBase( bool doReplot )
{
QwtPlot* plt = plot();
if ( plt == NULL )
return;
if ( doReplot )
plt->replot();
m_data->zoomStack.clear();
m_data->zoomStack.push( scaleRect() );
m_data->zoomRectIndex = 0;
rescale();
}
void QwtPlotZoomer::setZoomBase( const QRectF& base )
{
const QwtPlot* plt = plot();
if ( !plt )
return;
const QRectF sRect = scaleRect();
const QRectF bRect = base | sRect;
m_data->zoomStack.clear();
m_data->zoomStack.push( bRect );
m_data->zoomRectIndex = 0;
if ( base != sRect )
{
m_data->zoomStack.push( sRect );
m_data->zoomRectIndex++;
}
rescale();
}
QRectF QwtPlotZoomer::zoomRect() const
{
return m_data->zoomStack[m_data->zoomRectIndex];
}
uint QwtPlotZoomer::zoomRectIndex() const
{
return m_data->zoomRectIndex;
}
void QwtPlotZoomer::zoom( const QRectF& rect )
{
if ( m_data->maxStackDepth >= 0 &&
int( m_data->zoomRectIndex ) >= m_data->maxStackDepth )
{
return;
}
const QRectF zoomRect = rect.normalized();
if ( zoomRect != m_data->zoomStack[m_data->zoomRectIndex] )
{
for ( uint i = m_data->zoomStack.count() - 1;
i > m_data->zoomRectIndex; i-- )
{
( void )m_data->zoomStack.pop();
}
m_data->zoomStack.push( zoomRect );
m_data->zoomRectIndex++;
rescale();
Q_EMIT zoomed( zoomRect );
}
}
void QwtPlotZoomer::zoom( int offset )
{
int newIndex;
if ( offset == 0 )
{
newIndex = 0;
}
else
{
newIndex = m_data->zoomRectIndex + offset;
newIndex = qBound( 0, newIndex, m_data->zoomStack.count() - 1 );
}
if ( newIndex != static_cast< int >( m_data->zoomRectIndex ) )
{
m_data->zoomRectIndex = newIndex;
rescale();
Q_EMIT zoomed( zoomRect() );
}
}
void QwtPlotZoomer::setZoomStack(
const QStack< QRectF >& zoomStack, int zoomRectIndex )
{
if ( zoomStack.isEmpty() )
return;
if ( m_data->maxStackDepth >= 0 &&
zoomStack.count() > m_data->maxStackDepth )
{
return;
}
if ( zoomRectIndex < 0 || zoomRectIndex > zoomStack.count() )
zoomRectIndex = zoomStack.count() - 1;
const bool doRescale = zoomStack[zoomRectIndex] != zoomRect();
m_data->zoomStack = zoomStack;
m_data->zoomRectIndex = uint( zoomRectIndex );
if ( doRescale )
{
rescale();
Q_EMIT zoomed( zoomRect() );
}
}
void QwtPlotZoomer::rescale()
{
QwtPlot* plt = plot();
if ( !plt )
return;
const QRectF& rect = m_data->zoomStack[m_data->zoomRectIndex];
if ( rect != scaleRect() )
{
const bool doReplot = plt->autoReplot();
plt->setAutoReplot( false );
double x1 = rect.left();
double x2 = rect.right();
if ( !plt->axisScaleDiv( xAxis() ).isIncreasing() )
qSwap( x1, x2 );
plt->setAxisScale( xAxis(), x1, x2 );
double y1 = rect.top();
double y2 = rect.bottom();
if ( !plt->axisScaleDiv( yAxis() ).isIncreasing() )
qSwap( y1, y2 );
plt->setAxisScale( yAxis(), y1, y2 );
plt->setAutoReplot( doReplot );
plt->replot();
}
}
void QwtPlotZoomer::setAxes( QwtAxisId xAxisId, QwtAxisId yAxisId )
{
if ( xAxisId != QwtPlotPicker::xAxis() || yAxisId != QwtPlotPicker::yAxis() )
{
QwtPlotPicker::setAxes( xAxisId, yAxisId );
setZoomBase( scaleRect() );
}
}
void QwtPlotZoomer::widgetMouseReleaseEvent( QMouseEvent* me )
{
if ( mouseMatch( MouseSelect2, me ) )
zoom( 0 );
else if ( mouseMatch( MouseSelect3, me ) )
zoom( -1 );
else if ( mouseMatch( MouseSelect6, me ) )
zoom( +1 );
else
QwtPlotPicker::widgetMouseReleaseEvent( me );
}
void QwtPlotZoomer::widgetKeyPressEvent( QKeyEvent* ke )
{
if ( !isActive() )
{
if ( keyMatch( KeyUndo, ke ) )
zoom( -1 );
else if ( keyMatch( KeyRedo, ke ) )
zoom( +1 );
else if ( keyMatch( KeyHome, ke ) )
zoom( 0 );
}
QwtPlotPicker::widgetKeyPressEvent( ke );
}
void QwtPlotZoomer::moveBy( double dx, double dy )
{
const QRectF& rect = m_data->zoomStack[m_data->zoomRectIndex];
moveTo( QPointF( rect.left() + dx, rect.top() + dy ) );
}
void QwtPlotZoomer::moveTo( const QPointF& pos )
{
double x = pos.x();
double y = pos.y();
if ( x < zoomBase().left() )
x = zoomBase().left();
if ( x > zoomBase().right() - zoomRect().width() )
x = zoomBase().right() - zoomRect().width();
if ( y < zoomBase().top() )
y = zoomBase().top();
if ( y > zoomBase().bottom() - zoomRect().height() )
y = zoomBase().bottom() - zoomRect().height();
if ( x != zoomRect().left() || y != zoomRect().top() )
{
m_data->zoomStack[m_data->zoomRectIndex].moveTo( x, y );
rescale();
}
}
bool QwtPlotZoomer::accept( QPolygon& pa ) const
{
if ( pa.count() < 2 )
return false;
QRect rect = QRect( pa.first(), pa.last() );
rect = rect.normalized();
const int minSize = 2;
if ( rect.width() < minSize && rect.height() < minSize )
return false;
const int minZoomSize = 11;
const QPoint center = rect.center();
rect.setSize( rect.size().expandedTo( QSize( minZoomSize, minZoomSize ) ) );
rect.moveCenter( center );
pa.resize( 2 );
pa[0] = rect.topLeft();
pa[1] = rect.bottomRight();
return true;
}
QSizeF QwtPlotZoomer::minZoomSize() const
{
return QSizeF( m_data->zoomStack[0].width() / 10e4,
m_data->zoomStack[0].height() / 10e4 );
}
void QwtPlotZoomer::begin()
{
if ( m_data->maxStackDepth >= 0 )
{
if ( m_data->zoomRectIndex >= uint( m_data->maxStackDepth ) )
return;
}
const QSizeF minSize = minZoomSize();
if ( minSize.isValid() )
{
const QSizeF sz =
m_data->zoomStack[m_data->zoomRectIndex].size() * 0.9999;
if ( minSize.width() >= sz.width() &&
minSize.height() >= sz.height() )
{
return;
}
}
QwtPlotPicker::begin();
}
bool QwtPlotZoomer::end( bool ok )
{
ok = QwtPlotPicker::end( ok );
if ( !ok )
return false;
QwtPlot* plot = QwtPlotZoomer::plot();
if ( !plot )
return false;
const QPolygon& pa = selection();
if ( pa.count() < 2 )
return false;
QRect rect = QRect( pa.first(), pa.last() );
rect = rect.normalized();
const QwtScaleMap xMap = plot->canvasMap( xAxis() );
const QwtScaleMap yMap = plot->canvasMap( yAxis() );
QRectF zoomRect = QwtScaleMap::invTransform( xMap, yMap, rect ).normalized();
zoomRect = qwtExpandedZoomRect( zoomRect, minZoomSize(),
xMap.transformation(), yMap.transformation() );
zoom( zoomRect );
return true;
}
#include "moc_qwt_plot_zoomer.cpp"
QStack< QRectF >
QwtEventPattern::mouseMatch
bool mouseMatch(MousePatternCode, const QMouseEvent *) const
Compare a mouse event with an event pattern.
Definition qwt_event_pattern.cpp:188
QwtEventPattern::keyMatch
bool keyMatch(KeyPatternCode, const QKeyEvent *) const
Compare a key event with an event pattern.
Definition qwt_event_pattern.cpp:234
QwtEventPattern::KeyRedo
@ KeyRedo
Qt::Key_Plus.
Definition qwt_event_pattern.h:136
QwtEventPattern::KeyHome
@ KeyHome
Qt::Key_Escape.
Definition qwt_event_pattern.h:142
QwtEventPattern::KeyUndo
@ KeyUndo
Qt::Key_Minus.
Definition qwt_event_pattern.h:139
QwtEventPattern::MouseSelect3
@ MouseSelect3
Definition qwt_event_pattern.h:72
QwtEventPattern::MouseSelect2
@ MouseSelect2
Definition qwt_event_pattern.h:63
QwtEventPattern::MouseSelect6
@ MouseSelect6
Definition qwt_event_pattern.h:99
QwtInterval
A class representing an interval.
Definition qwt_interval.h:23
QwtInterval::minValue
double minValue() const
Definition qwt_interval.h:192
QwtInterval::maxValue
double maxValue() const
Definition qwt_interval.h:198
QwtPickerDragRectMachine
A state machine for rectangle selections.
Definition qwt_picker_machine.h:162
QwtPicker::ActiveOnly
@ ActiveOnly
Display only when the selection is active.
Definition qwt_picker.h:170
QwtPicker::begin
virtual void begin()
Definition qwt_picker.cpp:1258
QwtPicker::setStateMachine
void setStateMachine(QwtPickerMachine *)
Definition qwt_picker.cpp:230
QwtPicker::setRubberBand
void setRubberBand(RubberBand)
Definition qwt_picker.cpp:290
QwtPicker::setTrackerMode
void setTrackerMode(DisplayMode)
Set the display mode of the tracker.
Definition qwt_picker.cpp:320
QwtPicker::isActive
bool isActive() const
Definition qwt_picker.cpp:1412
QwtPicker::RectRubberBand
@ RectRubberBand
A rectangle ( only for QwtPickerMachine::RectSelection )
Definition qwt_picker.h:142
QwtPicker::widgetMouseReleaseEvent
virtual void widgetMouseReleaseEvent(QMouseEvent *)
Definition qwt_picker.cpp:1069
QwtPicker::widgetKeyPressEvent
virtual void widgetKeyPressEvent(QKeyEvent *)
Definition qwt_picker.cpp:1131
QwtPicker::selection
QPolygon selection() const
Definition qwt_picker.cpp:792
QwtPlot
A 2-D plotting widget.
Definition qwt_plot.h:79
QwtPlot::setAxisScale
void setAxisScale(QwtAxisId, double min, double max, double stepSize=0)
Disable autoscaling and specify a fixed scale for a selected axis.
Definition qwt_plot_axis.cpp:477
QwtPlot::autoReplot
bool autoReplot() const
Definition qwt_plot.cpp:319
QwtPlot::replot
virtual void replot()
Redraw the plot.
Definition qwt_plot.cpp:545
QwtPlot::setAutoReplot
void setAutoReplot(bool=true)
Set or reset the autoReplot option.
Definition qwt_plot.cpp:310
QwtPlot::canvasMap
virtual QwtScaleMap canvasMap(QwtAxisId) const
Definition qwt_plot.cpp:800
QwtPlot::axisScaleDiv
const QwtScaleDiv & axisScaleDiv(QwtAxisId) const
Return the scale division of a specified axis.
Definition qwt_plot_axis.cpp:283
QwtPlotPicker
QwtPlotPicker provides selections on a plot canvas.
Definition qwt_plot_picker.h:34
QwtPlotPicker::end
virtual bool end(bool ok=true) override
Definition qwt_plot_picker.cpp:280
QwtPlotPicker::yAxis
QwtAxisId yAxis() const
Return y axis.
Definition qwt_plot_picker.cpp:192
QwtPlotPicker::xAxis
QwtAxisId xAxis() const
Return x axis.
Definition qwt_plot_picker.cpp:186
QwtPlotPicker::setAxes
virtual void setAxes(QwtAxisId xAxisId, QwtAxisId yAxisId)
Definition qwt_plot_picker.cpp:172
QwtPlotPicker::scaleRect
QRectF scaleRect() const
Definition qwt_plot_picker.cpp:149
QwtPlotPicker::plot
QwtPlot * plot()
Definition qwt_plot_picker.cpp:126
QwtPlotPicker::canvas
QWidget * canvas()
Definition qwt_plot_picker.cpp:114
QwtPlotZoomer::widgetKeyPressEvent
virtual void widgetKeyPressEvent(QKeyEvent *) override
Definition qwt_plot_zoomer.cpp:497
QwtPlotZoomer::moveBy
void moveBy(double dx, double dy)
Definition qwt_plot_zoomer.cpp:520
QwtPlotZoomer::zoomRect
QRectF zoomRect() const
Definition qwt_plot_zoomer.cpp:287
QwtPlotZoomer::rescale
virtual void rescale()
Definition qwt_plot_zoomer.cpp:416
QwtPlotZoomer::zoomStack
const QStack< QRectF > & zoomStack() const
Definition qwt_plot_zoomer.cpp:212
QwtPlotZoomer::QwtPlotZoomer
QwtPlotZoomer(QWidget *, bool doReplot=true)
Create a zoomer for a plot canvas.
Definition qwt_plot_zoomer.cpp:109
QwtPlotZoomer::setMaxStackDepth
void setMaxStackDepth(int)
Limit the number of recursive zoom operations to depth.
Definition qwt_plot_zoomer.cpp:174
QwtPlotZoomer::moveTo
virtual void moveTo(const QPointF &)
Definition qwt_plot_zoomer.cpp:534
QwtPlotZoomer::widgetMouseReleaseEvent
virtual void widgetMouseReleaseEvent(QMouseEvent *) override
Definition qwt_plot_zoomer.cpp:474
QwtPlotZoomer::setZoomBase
virtual void setZoomBase(bool doReplot=true)
Definition qwt_plot_zoomer.cpp:235
QwtPlotZoomer::begin
virtual void begin() override
Definition qwt_plot_zoomer.cpp:609
QwtPlotZoomer::zoomBase
QRectF zoomBase() const
Definition qwt_plot_zoomer.cpp:221
QwtPlotZoomer::setAxes
virtual void setAxes(QwtAxisId xAxis, QwtAxisId yAxis) override
Definition qwt_plot_zoomer.cpp:455
QwtPlotZoomer::zoomRectIndex
uint zoomRectIndex() const
Definition qwt_plot_zoomer.cpp:295
QwtPlotZoomer::maxStackDepth
int maxStackDepth() const
Definition qwt_plot_zoomer.cpp:201
QwtPlotZoomer::accept
virtual bool accept(QPolygon &) const override
Check and correct a selected rectangle.
Definition qwt_plot_zoomer.cpp:567
QwtPlotZoomer::end
virtual bool end(bool ok=true) override
Definition qwt_plot_zoomer.cpp:643
QwtPlotZoomer::minZoomSize
virtual QSizeF minZoomSize() const
Limit zooming by a minimum rectangle.
Definition qwt_plot_zoomer.cpp:597
QwtPlotZoomer::zoomed
void zoomed(const QRectF &rect)
QwtPlotZoomer::setZoomStack
void setZoomStack(const QStack< QRectF > &, int zoomRectIndex=-1)
Assign a zoom stack.
Definition qwt_plot_zoomer.cpp:383
QwtPlotZoomer::zoom
virtual void zoom(const QRectF &)
Zoom in.
Definition qwt_plot_zoomer.cpp:310
QwtScaleMap
A scale map.
Definition qwt_scale_map.h:27
QwtScaleMap::transformation
const QwtTransform * transformation() const
Get the transformation.
Definition qwt_scale_map.cpp:88
QwtScaleMap::invTransform
double invTransform(double p) const
Definition qwt_scale_map.h:154
QwtTransform
A transformation between coordinate systems.
Definition qwt_transform.h:36
QwtTransform::bounded
virtual double bounded(double value) const
Definition qwt_transform.cpp:33