QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsplottoolzoom.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsplottoolzoom.cpp
3  ---------------
4  begin : March 2022
5  copyright : (C) 2022 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsplottoolzoom.h"
19 #include "qgsapplication.h"
20 #include "qgsplotmouseevent.h"
21 #include "qgsplotcanvas.h"
22 #include "qgsplotrubberband.h"
23 
25  : QgsPlotTool( canvas, tr( "Zoom" ) )
26 {
27  setCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
28  mRubberBand.reset( new QgsPlotRectangularRubberBand( canvas ) );
29  mRubberBand->setBrush( QBrush( QColor( 70, 50, 255, 25 ) ) );
30  mRubberBand->setPen( QPen( QBrush( QColor( 70, 50, 255, 100 ) ), 0 ) );
31 }
32 
34 
36 {
37  if ( event->button() != Qt::LeftButton )
38  {
39  if ( mMarqueeZoom )
40  {
41  mMarqueeZoom = false;
42  mRubberBand->finish();
43  }
44  event->ignore();
45  return;
46  }
47 
48  mMousePressStartPos = event->pos();
49  if ( event->modifiers() & Qt::AltModifier )
50  {
51  zoomOutClickOn( event->pos() );
52  }
53  else
54  {
55  //zoom in action
56  startMarqueeZoom( event->pos() );
57  }
58 }
59 
61 {
62  if ( !mMarqueeZoom )
63  {
64  event->ignore();
65  return;
66  }
67 
68  mRubberBand->update( constrainMovePoint( event->pos() ), Qt::KeyboardModifiers() );
69 }
70 
72 {
73  if ( !mMarqueeZoom || event->button() != Qt::LeftButton )
74  {
75  event->ignore();
76  return;
77  }
78 
79  mMarqueeZoom = false;
80  // click? or click-and-drag?
81  if ( !isClickAndDrag( mMousePressStartPos, event->pos() ) )
82  {
83  zoomInClickOn( event->pos() );
84  }
85  else
86  {
87  QRectF newBoundsRect = mRubberBand->finish( constrainMovePoint( event->pos() ) );
88 
89  //zoom view to fit desired bounds
90  canvas()->zoomToRect( constrainBounds( newBoundsRect ) );
91  }
92 }
93 
94 void QgsPlotToolZoom::keyPressEvent( QKeyEvent *event )
95 {
96  //respond to changes in the alt key status and update cursor accordingly
97  if ( !event->isAutoRepeat() )
98  {
99 
100  canvas()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ?
101  QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) :
102  QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
103  }
104  event->ignore();
105 }
106 
107 void QgsPlotToolZoom::keyReleaseEvent( QKeyEvent *event )
108 {
109  //respond to changes in the alt key status and update cursor accordingly
110  if ( !event->isAutoRepeat() )
111  {
112 
113  canvas()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ?
114  QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) :
115  QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
116  }
117  event->ignore();
118 }
119 
121 {
122  if ( mMarqueeZoom )
123  {
124  mMarqueeZoom = false;
125  mRubberBand->finish();
126  }
128 }
129 
130 QPointF QgsPlotToolZoom::constrainStartPoint( QPointF scenePoint ) const
131 {
132  return scenePoint;
133 }
134 
135 QPointF QgsPlotToolZoom::constrainMovePoint( QPointF scenePoint ) const
136 {
137  return scenePoint;
138 }
139 
140 QRectF QgsPlotToolZoom::constrainBounds( const QRectF &sceneBounds ) const
141 {
142  return sceneBounds;
143 }
144 
145 void QgsPlotToolZoom::zoomOutClickOn( QPointF scenePoint )
146 {
147  //just a click, so zoom to clicked point and recenter
148  canvas()->centerPlotOn( scenePoint.x(), scenePoint.y() );
149  canvas()->scalePlot( 0.5 );
150 }
151 
152 void QgsPlotToolZoom::zoomInClickOn( QPointF scenePoint )
153 {
154  //just a click, so zoom to clicked point and recenter
155  canvas()->centerPlotOn( scenePoint.x(), scenePoint.y() );
156  canvas()->scalePlot( 2.0 );
157 }
158 
159 void QgsPlotToolZoom::startMarqueeZoom( QPointF scenePoint )
160 {
161  mMarqueeZoom = true;
162 
163  mRubberBandStartPos = scenePoint;
164  mRubberBand->start( constrainStartPoint( scenePoint ), Qt::KeyboardModifiers() );
165 }
QgsPlotRectangularRubberBand
QgsPlotRectangularRubberBand is rectangular rubber band for use within QgsPlotCanvas widgets.
Definition: qgsplotrubberband.h:128
QgsPlotCanvas::zoomToRect
virtual void zoomToRect(const QRectF &rect)
Zooms the plot to the specified rect in canvas units.
Definition: qgsplotcanvas.cpp:292
qgsplotmouseevent.h
QgsPlotTool::setCursor
void setCursor(const QCursor &cursor)
Sets a user defined cursor for use when the tool is active.
Definition: qgsplottool.cpp:162
QgsApplication::getThemeCursor
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
Definition: qgsapplication.cpp:762
qgsplotcanvas.h
QgsPlotTool::deactivate
virtual void deactivate()
Called when the tool is being deactivated.
Definition: qgsplottool.cpp:89
QgsPlotToolZoom::mMarqueeZoom
bool mMarqueeZoom
Will be true will marquee zoom operation is in progress.
Definition: qgsplottoolzoom.h:92
QgsPlotToolZoom::constrainBounds
virtual QRectF constrainBounds(const QRectF &sceneBounds) const
Applies constraints to the overall bounds of the rubber band.
Definition: qgsplottoolzoom.cpp:140
QgsPlotToolZoom::plotMoveEvent
void plotMoveEvent(QgsPlotMouseEvent *event) override
Mouse move event for overriding.
Definition: qgsplottoolzoom.cpp:60
QgsPlotCanvas::scalePlot
virtual void scalePlot(double factor)
Scales the plot by a specified scale factor.
Definition: qgsplotcanvas.cpp:287
qgsplottoolzoom.h
QgsPlotCanvas::centerPlotOn
virtual void centerPlotOn(double x, double y)
Centers the plot on the plot point corresponding to x, y in canvas units.
Definition: qgsplotcanvas.cpp:282
qgsapplication.h
QgsPlotToolZoom::zoomInClickOn
virtual void zoomInClickOn(QPointF scenePoint)
Handles a zoom out click on the given point.
Definition: qgsplottoolzoom.cpp:152
QgsPlotToolZoom::constrainStartPoint
virtual QPointF constrainStartPoint(QPointF scenePoint) const
Applies constraints to the start point of the zoom rubber band.
Definition: qgsplottoolzoom.cpp:130
QgsPlotToolZoom::plotReleaseEvent
void plotReleaseEvent(QgsPlotMouseEvent *event) override
Mouse release event for overriding.
Definition: qgsplottoolzoom.cpp:71
QgsPlotToolZoom::keyPressEvent
void keyPressEvent(QKeyEvent *event) override
Key press event for overriding.
Definition: qgsplottoolzoom.cpp:94
QgsPlotToolZoom::QgsPlotToolZoom
QgsPlotToolZoom(QgsPlotCanvas *canvas)
Constructor for QgsPlotToolZoom, with the associated canvas.
Definition: qgsplottoolzoom.cpp:24
QgsPlotToolZoom::constrainMovePoint
virtual QPointF constrainMovePoint(QPointF scenePoint) const
Applies constraints to a move point of the zoom rubber band.
Definition: qgsplottoolzoom.cpp:135
QgsPlotToolZoom::plotPressEvent
void plotPressEvent(QgsPlotMouseEvent *event) override
Mouse press event for overriding.
Definition: qgsplottoolzoom.cpp:35
QgsPlotTool
Abstract base class for all interactive plot tools.
Definition: qgsplottool.h:57
QgsPlotToolZoom::~QgsPlotToolZoom
~QgsPlotToolZoom() override
QgsPlotCanvas
Plot canvas is a class for displaying interactive 2d charts and plots.
Definition: qgsplotcanvas.h:53
QgsPlotToolZoom::zoomOutClickOn
virtual void zoomOutClickOn(QPointF scenePoint)
Handles a zoom out click on the given point.
Definition: qgsplottoolzoom.cpp:145
QgsPlotToolZoom::keyReleaseEvent
void keyReleaseEvent(QKeyEvent *event) override
Key release event for overriding.
Definition: qgsplottoolzoom.cpp:107
QgsPlotToolZoom::deactivate
void deactivate() override
Called when the tool is being deactivated.
Definition: qgsplottoolzoom.cpp:120
QgsPlotMouseEvent
A QgsPlotMouseEvent is the result of a user interaction with the mouse on a QgsPlotCanvas.
Definition: qgsplotmouseevent.h:39
QgsPlotTool::canvas
QgsPlotCanvas * canvas() const
Returns the tool's plot canvas.
Definition: qgsplottool.cpp:147
QgsPlotTool::isClickAndDrag
bool isClickAndDrag(QPoint startViewPoint, QPoint endViewPoint) const
Returns true if a mouse press/release operation which started at startViewPoint and ended at endViewP...
Definition: qgsplottool.cpp:46
qgsplotrubberband.h