Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
qwt_plot_zoomer.cpp
1/******************************************************************************
2 * Qwt Widget Library
3 * Copyright (C) 1997 Josef Wilgen
4 * Copyright (C) 2002 Uwe Rathmann
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the Qwt License, Version 1.0
8 *****************************************************************************/
9
10#include "qwt_plot_zoomer.h"
11#include "qwt_plot.h"
12#include "qwt_scale_div.h"
13#include "qwt_scale_map.h"
14#include "qwt_interval.h"
15#include "qwt_picker_machine.h"
16
17#include <qstack.h>
18
19static QwtInterval qwtExpandedZoomInterval( double v1, double v2,
20 double minRange, const QwtTransform* transform )
21{
22 double min = v1;
23 double max = v2;
24
25 if ( max - min < minRange )
26 {
27 min = 0.5 * ( min + max - minRange );
28 max = min + minRange;
29
30 if ( transform )
31 {
32 // f.e the logarithmic scale doesn't allow values
33 // outside [QwtLogTransform::LogMin/QwtLogTransform::LogMax]
34
35 double minBounded = transform->bounded( min );
36 double maxBounded = transform->bounded( max );
37
38 if ( minBounded != min )
39 {
40 maxBounded = transform->bounded( minBounded + minRange );
41 }
42 else if ( maxBounded != max )
43 {
44 minBounded = transform->bounded( maxBounded - minRange );
45 }
46
47 min = minBounded;
48 max = maxBounded;
49 }
50 }
51
52 return QwtInterval( min, max );
53}
54
55static QRectF qwtExpandedZoomRect( const QRectF& zoomRect, const QSizeF& minSize,
56 const QwtTransform* transformX, const QwtTransform* transformY )
57{
58 QRectF r = zoomRect;
59
60 if ( minSize.width() > r.width() )
61 {
62 const QwtInterval intv = qwtExpandedZoomInterval(
63 r.left(), r.right(), minSize.width(), transformX );
64
65 r.setLeft( intv.minValue() );
66 r.setRight( intv.maxValue() );
67 }
68
69 if ( minSize.height() > r.height() )
70 {
71 const QwtInterval intv = qwtExpandedZoomInterval(
72 zoomRect.top(), zoomRect.bottom(), minSize.height(), transformY );
73
74 r.setTop( intv.minValue() );
75 r.setBottom( intv.maxValue() );
76 }
77
78 return r;
79}
80
81class QwtPlotZoomer::PrivateData
82{
83 public:
84 uint zoomRectIndex;
85 QStack< QRectF > zoomStack;
86
87 int maxStackDepth;
88};
89
109QwtPlotZoomer::QwtPlotZoomer( QWidget* canvas, bool doReplot )
110 : QwtPlotPicker( canvas )
111{
112 if ( canvas )
113 init( doReplot );
114}
115
133QwtPlotZoomer::QwtPlotZoomer( QwtAxisId xAxisId, QwtAxisId yAxisId,
134 QWidget* canvas, bool doReplot )
135 : QwtPlotPicker( xAxisId, yAxisId, canvas )
136{
137 if ( canvas )
138 init( doReplot );
139}
140
142void QwtPlotZoomer::init( bool doReplot )
143{
144 m_data = new PrivateData;
145
146 m_data->maxStackDepth = -1;
147
151
152 if ( doReplot && plot() )
153 plot()->replot();
154
156}
157
158QwtPlotZoomer::~QwtPlotZoomer()
159{
160 delete m_data;
161}
162
175{
176 m_data->maxStackDepth = depth;
177
178 if ( depth >= 0 )
179 {
180 // unzoom if the current depth is below m_data->maxStackDepth
181
182 const int zoomOut =
183 m_data->zoomStack.count() - 1 - depth; // -1 for the zoom base
184
185 if ( zoomOut > 0 )
186 {
187 zoom( -zoomOut );
188 for ( int i = m_data->zoomStack.count() - 1;
189 i > int( m_data->zoomRectIndex ); i-- )
190 {
191 ( void )m_data->zoomStack.pop(); // remove trailing rects
192 }
193 }
194 }
195}
196
202{
203 return m_data->maxStackDepth;
204}
205
213{
214 return m_data->zoomStack;
215}
216
222{
223 return m_data->zoomStack[0];
224}
225
235void QwtPlotZoomer::setZoomBase( bool doReplot )
236{
237 QwtPlot* plt = plot();
238 if ( plt == NULL )
239 return;
240
241 if ( doReplot )
242 plt->replot();
243
244 m_data->zoomStack.clear();
245 m_data->zoomStack.push( scaleRect() );
246 m_data->zoomRectIndex = 0;
247
248 rescale();
249}
250
261void QwtPlotZoomer::setZoomBase( const QRectF& base )
262{
263 const QwtPlot* plt = plot();
264 if ( !plt )
265 return;
266
267 const QRectF sRect = scaleRect();
268 const QRectF bRect = base | sRect;
269
270 m_data->zoomStack.clear();
271 m_data->zoomStack.push( bRect );
272 m_data->zoomRectIndex = 0;
273
274 if ( base != sRect )
275 {
276 m_data->zoomStack.push( sRect );
277 m_data->zoomRectIndex++;
278 }
279
280 rescale();
281}
282
288{
289 return m_data->zoomStack[m_data->zoomRectIndex];
290}
291
296{
297 return m_data->zoomRectIndex;
298}
299
310void QwtPlotZoomer::zoom( const QRectF& rect )
311{
312 if ( m_data->maxStackDepth >= 0 &&
313 int( m_data->zoomRectIndex ) >= m_data->maxStackDepth )
314 {
315 return;
316 }
317
318 const QRectF zoomRect = rect.normalized();
319 if ( zoomRect != m_data->zoomStack[m_data->zoomRectIndex] )
320 {
321 for ( uint i = m_data->zoomStack.count() - 1;
322 i > m_data->zoomRectIndex; i-- )
323 {
324 ( void )m_data->zoomStack.pop();
325 }
326
327 m_data->zoomStack.push( zoomRect );
328 m_data->zoomRectIndex++;
329
330 rescale();
331
332 Q_EMIT zoomed( zoomRect );
333 }
334}
335
347void QwtPlotZoomer::zoom( int offset )
348{
349 int newIndex;
350
351 if ( offset == 0 )
352 {
353 newIndex = 0;
354 }
355 else
356 {
357 newIndex = m_data->zoomRectIndex + offset;
358 newIndex = qBound( 0, newIndex, m_data->zoomStack.count() - 1 );
359 }
360
361 if ( newIndex != static_cast< int >( m_data->zoomRectIndex ) )
362 {
363 m_data->zoomRectIndex = newIndex;
364 rescale();
365 Q_EMIT zoomed( zoomRect() );
366 }
367}
368
384 const QStack< QRectF >& zoomStack, int zoomRectIndex )
385{
386 if ( zoomStack.isEmpty() )
387 return;
388
389 if ( m_data->maxStackDepth >= 0 &&
390 zoomStack.count() > m_data->maxStackDepth )
391 {
392 return;
393 }
394
395 if ( zoomRectIndex < 0 || zoomRectIndex > zoomStack.count() )
396 zoomRectIndex = zoomStack.count() - 1;
397
398 const bool doRescale = zoomStack[zoomRectIndex] != zoomRect();
399
400 m_data->zoomStack = zoomStack;
401 m_data->zoomRectIndex = uint( zoomRectIndex );
402
403 if ( doRescale )
404 {
405 rescale();
406 Q_EMIT zoomed( zoomRect() );
407 }
408}
409
417{
418 QwtPlot* plt = plot();
419 if ( !plt )
420 return;
421
422 const QRectF& rect = m_data->zoomStack[m_data->zoomRectIndex];
423 if ( rect != scaleRect() )
424 {
425 const bool doReplot = plt->autoReplot();
426 plt->setAutoReplot( false );
427
428 double x1 = rect.left();
429 double x2 = rect.right();
430 if ( !plt->axisScaleDiv( xAxis() ).isIncreasing() )
431 qSwap( x1, x2 );
432
433 plt->setAxisScale( xAxis(), x1, x2 );
434
435 double y1 = rect.top();
436 double y2 = rect.bottom();
437 if ( !plt->axisScaleDiv( yAxis() ).isIncreasing() )
438 qSwap( y1, y2 );
439
440 plt->setAxisScale( yAxis(), y1, y2 );
441
442 plt->setAutoReplot( doReplot );
443
444 plt->replot();
445 }
446}
447
455void QwtPlotZoomer::setAxes( QwtAxisId xAxisId, QwtAxisId yAxisId )
456{
457 if ( xAxisId != QwtPlotPicker::xAxis() || yAxisId != QwtPlotPicker::yAxis() )
458 {
459 QwtPlotPicker::setAxes( xAxisId, yAxisId );
461 }
462}
463
475{
476 if ( mouseMatch( MouseSelect2, me ) )
477 zoom( 0 );
478 else if ( mouseMatch( MouseSelect3, me ) )
479 zoom( -1 );
480 else if ( mouseMatch( MouseSelect6, me ) )
481 zoom( +1 );
482 else
484}
485
498{
499 if ( !isActive() )
500 {
501 if ( keyMatch( KeyUndo, ke ) )
502 zoom( -1 );
503 else if ( keyMatch( KeyRedo, ke ) )
504 zoom( +1 );
505 else if ( keyMatch( KeyHome, ke ) )
506 zoom( 0 );
507 }
508
510}
511
520void QwtPlotZoomer::moveBy( double dx, double dy )
521{
522 const QRectF& rect = m_data->zoomStack[m_data->zoomRectIndex];
523 moveTo( QPointF( rect.left() + dx, rect.top() + dy ) );
524}
525
534void QwtPlotZoomer::moveTo( const QPointF& pos )
535{
536 double x = pos.x();
537 double y = pos.y();
538
539 if ( x < zoomBase().left() )
540 x = zoomBase().left();
541 if ( x > zoomBase().right() - zoomRect().width() )
542 x = zoomBase().right() - zoomRect().width();
543
544 if ( y < zoomBase().top() )
545 y = zoomBase().top();
546 if ( y > zoomBase().bottom() - zoomRect().height() )
547 y = zoomBase().bottom() - zoomRect().height();
548
549 if ( x != zoomRect().left() || y != zoomRect().top() )
550 {
551 m_data->zoomStack[m_data->zoomRectIndex].moveTo( x, y );
552 rescale();
553 }
554}
555
567bool QwtPlotZoomer::accept( QPolygon& pa ) const
568{
569 if ( pa.count() < 2 )
570 return false;
571
572 QRect rect = QRect( pa.first(), pa.last() );
573 rect = rect.normalized();
574
575 const int minSize = 2;
576 if ( rect.width() < minSize && rect.height() < minSize )
577 return false;
578
579 const int minZoomSize = 11;
580
581 const QPoint center = rect.center();
582 rect.setSize( rect.size().expandedTo( QSize( minZoomSize, minZoomSize ) ) );
583 rect.moveCenter( center );
584
585 pa.resize( 2 );
586 pa[0] = rect.topLeft();
587 pa[1] = rect.bottomRight();
588
589 return true;
590}
591
598{
599 return QSizeF( m_data->zoomStack[0].width() / 10e4,
600 m_data->zoomStack[0].height() / 10e4 );
601}
602
610{
611 if ( m_data->maxStackDepth >= 0 )
612 {
613 if ( m_data->zoomRectIndex >= uint( m_data->maxStackDepth ) )
614 return;
615 }
616
617 const QSizeF minSize = minZoomSize();
618 if ( minSize.isValid() )
619 {
620 const QSizeF sz =
621 m_data->zoomStack[m_data->zoomRectIndex].size() * 0.9999;
622
623 if ( minSize.width() >= sz.width() &&
624 minSize.height() >= sz.height() )
625 {
626 return;
627 }
628 }
629
631}
632
643bool QwtPlotZoomer::end( bool ok )
644{
645 ok = QwtPlotPicker::end( ok );
646 if ( !ok )
647 return false;
648
650 if ( !plot )
651 return false;
652
653 const QPolygon& pa = selection();
654 if ( pa.count() < 2 )
655 return false;
656
657 QRect rect = QRect( pa.first(), pa.last() );
658 rect = rect.normalized();
659
660 const QwtScaleMap xMap = plot->canvasMap( xAxis() );
661 const QwtScaleMap yMap = plot->canvasMap( yAxis() );
662
663 QRectF zoomRect = QwtScaleMap::invTransform( xMap, yMap, rect ).normalized();
664
665 zoomRect = qwtExpandedZoomRect( zoomRect, minZoomSize(),
666 xMap.transformation(), yMap.transformation() );
667
668 zoom( zoomRect );
669
670 return true;
671}
672
673#include "moc_qwt_plot_zoomer.cpp"
bool mouseMatch(MousePatternCode, const QMouseEvent *) const
Compare a mouse event with an event pattern.
bool keyMatch(KeyPatternCode, const QKeyEvent *) const
Compare a key event with an event pattern.
@ KeyRedo
Qt::Key_Plus.
@ KeyHome
Qt::Key_Escape.
@ KeyUndo
Qt::Key_Minus.
A class representing an interval.
double minValue() const
double maxValue() const
A state machine for rectangle selections.
@ ActiveOnly
Display only when the selection is active.
Definition qwt_picker.h:170
virtual void begin()
void setStateMachine(QwtPickerMachine *)
void setRubberBand(RubberBand)
void setTrackerMode(DisplayMode)
Set the display mode of the tracker.
bool isActive() const
@ RectRubberBand
A rectangle ( only for QwtPickerMachine::RectSelection )
Definition qwt_picker.h:142
virtual void widgetMouseReleaseEvent(QMouseEvent *)
virtual void widgetKeyPressEvent(QKeyEvent *)
QPolygon selection() const
A 2-D plotting widget.
Definition qwt_plot.h:79
void setAxisScale(QwtAxisId, double min, double max, double stepSize=0)
Disable autoscaling and specify a fixed scale for a selected axis.
bool autoReplot() const
Definition qwt_plot.cpp:319
virtual void replot()
Redraw the plot.
Definition qwt_plot.cpp:545
void setAutoReplot(bool=true)
Set or reset the autoReplot option.
Definition qwt_plot.cpp:310
virtual QwtScaleMap canvasMap(QwtAxisId) const
Definition qwt_plot.cpp:800
const QwtScaleDiv & axisScaleDiv(QwtAxisId) const
Return the scale division of a specified axis.
QwtPlotPicker provides selections on a plot canvas.
virtual bool end(bool ok=true) override
QwtAxisId yAxis() const
Return y axis.
QwtAxisId xAxis() const
Return x axis.
virtual void setAxes(QwtAxisId xAxisId, QwtAxisId yAxisId)
QRectF scaleRect() const
virtual void widgetKeyPressEvent(QKeyEvent *) override
void moveBy(double dx, double dy)
QRectF zoomRect() const
virtual void rescale()
const QStack< QRectF > & zoomStack() const
QwtPlotZoomer(QWidget *, bool doReplot=true)
Create a zoomer for a plot canvas.
void setMaxStackDepth(int)
Limit the number of recursive zoom operations to depth.
virtual void moveTo(const QPointF &)
virtual void widgetMouseReleaseEvent(QMouseEvent *) override
virtual void setZoomBase(bool doReplot=true)
virtual void begin() override
QRectF zoomBase() const
virtual void setAxes(QwtAxisId xAxis, QwtAxisId yAxis) override
uint zoomRectIndex() const
int maxStackDepth() const
virtual bool accept(QPolygon &) const override
Check and correct a selected rectangle.
virtual bool end(bool ok=true) override
virtual QSizeF minZoomSize() const
Limit zooming by a minimum rectangle.
void zoomed(const QRectF &rect)
void setZoomStack(const QStack< QRectF > &, int zoomRectIndex=-1)
Assign a zoom stack.
virtual void zoom(const QRectF &)
Zoom in.
A scale map.
const QwtTransform * transformation() const
Get the transformation.
double invTransform(double p) const
A transformation between coordinate systems.
virtual double bounded(double value) const