Qwt User's Guide: qwt_plot_zoomer.cpp Source File

Qwt User's Guide 6.2.0

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
19 static 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
55 static 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
81 class QwtPlotZoomer::PrivateData
82 {
83 public:
84 uint zoomRectIndex;
85 QStack< QRectF > zoomStack;
86
87 int maxStackDepth;
88 };
89
109 QwtPlotZoomer::QwtPlotZoomer( QWidget* canvas, bool doReplot )
110 : QwtPlotPicker( canvas )
111 {
112 if ( canvas )
113 init( doReplot );
114 }
115
133 QwtPlotZoomer::QwtPlotZoomer( QwtAxisId xAxisId, QwtAxisId yAxisId,
134 QWidget* canvas, bool doReplot )
135 : QwtPlotPicker( xAxisId, yAxisId, canvas )
136 {
137 if ( canvas )
138 init( doReplot );
139 }
140
142 void QwtPlotZoomer::init( bool doReplot )
143 {
144 m_data = new PrivateData;
145
146 m_data->maxStackDepth = -1;
147
148 setTrackerMode( ActiveOnly );
149 setRubberBand( RectRubberBand );
150 setStateMachine( new QwtPickerDragRectMachine() );
151
152 if ( doReplot && plot() )
153 plot()->replot();
154
155 setZoomBase( scaleRect() );
156 }
157
158 QwtPlotZoomer::~QwtPlotZoomer()
159 {
160 delete m_data;
161 }
162
174 void QwtPlotZoomer::setMaxStackDepth( int depth )
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
201 int QwtPlotZoomer::maxStackDepth() const
202 {
203 return m_data->maxStackDepth;
204 }
205
212 const QStack< QRectF >& QwtPlotZoomer::zoomStack() const
213 {
214 return m_data->zoomStack;
215 }
216
221 QRectF QwtPlotZoomer::zoomBase() const
222 {
223 return m_data->zoomStack[0];
224 }
225
235 void 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
261 void 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
287 QRectF QwtPlotZoomer::zoomRect() const
288 {
289 return m_data->zoomStack[m_data->zoomRectIndex];
290 }
291
295 uint QwtPlotZoomer::zoomRectIndex() const
296 {
297 return m_data->zoomRectIndex;
298 }
299
310 void 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
347 void 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
383 void QwtPlotZoomer::setZoomStack(
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
416 void QwtPlotZoomer::rescale()
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
455 void QwtPlotZoomer::setAxes( QwtAxisId xAxisId, QwtAxisId yAxisId )
456 {
457 if ( xAxisId != QwtPlotPicker::xAxis() || yAxisId != QwtPlotPicker::yAxis() )
458 {
459 QwtPlotPicker::setAxes( xAxisId, yAxisId );
460 setZoomBase( scaleRect() );
461 }
462 }
463
474 void QwtPlotZoomer::widgetMouseReleaseEvent( QMouseEvent* me )
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
483 QwtPlotPicker::widgetMouseReleaseEvent( me );
484 }
485
497 void QwtPlotZoomer::widgetKeyPressEvent( QKeyEvent* ke )
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
509 QwtPlotPicker::widgetKeyPressEvent( ke );
510 }
511
520 void 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
534 void 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
567 bool 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
597 QSizeF QwtPlotZoomer::minZoomSize() const
598 {
599 return QSizeF( m_data->zoomStack[0].width() / 10e4,
600 m_data->zoomStack[0].height() / 10e4 );
601 }
602
609 void QwtPlotZoomer::begin()
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
630 QwtPlotPicker::begin();
631 }
632
643 bool QwtPlotZoomer::end( bool ok )
644 {
645 ok = QwtPlotPicker::end( ok );
646 if ( !ok )
647 return false;
648
649 QwtPlot* plot = QwtPlotZoomer::plot();
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 #if QWT_MOC_INCLUDE
674 #include "moc_qwt_plot_zoomer.cpp"
675 #endif
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:1239
QwtPicker::setStateMachine
void setStateMachine(QwtPickerMachine *)
Definition: qwt_picker.cpp:229
QwtPicker::setRubberBand
void setRubberBand(RubberBand)
Definition: qwt_picker.cpp:289
QwtPicker::setTrackerMode
void setTrackerMode(DisplayMode)
Set the display mode of the tracker.
Definition: qwt_picker.cpp:319
QwtPicker::isActive
bool isActive() const
Definition: qwt_picker.cpp:1393
QwtPicker::RectRubberBand
@ RectRubberBand
A rectangle ( only for QwtPickerMachine::RectSelection )
Definition: qwt_picker.h:142
QwtPicker::widgetMouseReleaseEvent
virtual void widgetMouseReleaseEvent(QMouseEvent *)
Definition: qwt_picker.cpp:1050
QwtPicker::widgetKeyPressEvent
virtual void widgetKeyPressEvent(QKeyEvent *)
Definition: qwt_picker.cpp:1112
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:473
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