QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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 "moc_qgsplottoolzoom.cpp"
20#include "qgsapplication.h"
21#include "qgsplotmouseevent.h"
22#include "qgsplotcanvas.h"
23#include "qgsplotrubberband.h"
24
26 : QgsPlotTool( canvas, tr( "Zoom" ) )
27{
29 mRubberBand.reset( new QgsPlotRectangularRubberBand( canvas ) );
30 mRubberBand->setBrush( QBrush( QColor( 70, 50, 255, 25 ) ) );
31 mRubberBand->setPen( QPen( QBrush( QColor( 70, 50, 255, 100 ) ), 0 ) );
32}
33
35
37{
38 if ( event->button() != Qt::LeftButton )
39 {
40 if ( mMarqueeZoom )
41 {
42 mMarqueeZoom = false;
43 mRubberBand->finish();
44 }
45 event->ignore();
46 return;
47 }
48
49 mMousePressStartPos = event->pos();
50 if ( event->modifiers() & Qt::AltModifier )
51 {
52 zoomOutClickOn( event->pos() );
53 }
54 else
55 {
56 //zoom in action
57 startMarqueeZoom( event->pos() );
58 }
59}
60
62{
63 if ( !mMarqueeZoom )
64 {
65 event->ignore();
66 return;
67 }
68
69 mRubberBand->update( constrainMovePoint( event->pos() ), Qt::KeyboardModifiers() );
70}
71
73{
74 if ( !mMarqueeZoom || event->button() != Qt::LeftButton )
75 {
76 event->ignore();
77 return;
78 }
79
80 mMarqueeZoom = false;
81 // click? or click-and-drag?
82 if ( !isClickAndDrag( mMousePressStartPos, event->pos() ) )
83 {
84 zoomInClickOn( event->pos() );
85 }
86 else
87 {
88 QRectF newBoundsRect = mRubberBand->finish( constrainMovePoint( event->pos() ) );
89
90 //zoom view to fit desired bounds
91 canvas()->zoomToRect( constrainBounds( newBoundsRect ) );
92 }
93}
94
95void QgsPlotToolZoom::keyPressEvent( QKeyEvent *event )
96{
97 //respond to changes in the alt key status and update cursor accordingly
98 if ( !event->isAutoRepeat() )
99 {
100
101 canvas()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ?
104 }
105 event->ignore();
106}
107
108void QgsPlotToolZoom::keyReleaseEvent( QKeyEvent *event )
109{
110 //respond to changes in the alt key status and update cursor accordingly
111 if ( !event->isAutoRepeat() )
112 {
113
114 canvas()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ?
117 }
118 event->ignore();
119}
120
122{
123 if ( mMarqueeZoom )
124 {
125 mMarqueeZoom = false;
126 mRubberBand->finish();
127 }
129}
130
131QPointF QgsPlotToolZoom::constrainStartPoint( QPointF scenePoint ) const
132{
133 return scenePoint;
134}
135
136QPointF QgsPlotToolZoom::constrainMovePoint( QPointF scenePoint ) const
137{
138 return scenePoint;
139}
140
141QRectF QgsPlotToolZoom::constrainBounds( const QRectF &sceneBounds ) const
142{
143 return sceneBounds;
144}
145
146void QgsPlotToolZoom::zoomOutClickOn( QPointF scenePoint )
147{
148 //just a click, so zoom to clicked point and recenter
149 canvas()->centerPlotOn( scenePoint.x(), scenePoint.y() );
150 canvas()->scalePlot( 0.5 );
151}
152
153void QgsPlotToolZoom::zoomInClickOn( QPointF scenePoint )
154{
155 //just a click, so zoom to clicked point and recenter
156 canvas()->centerPlotOn( scenePoint.x(), scenePoint.y() );
157 canvas()->scalePlot( 2.0 );
158}
159
160void QgsPlotToolZoom::startMarqueeZoom( QPointF scenePoint )
161{
162 mMarqueeZoom = true;
163
164 mRubberBandStartPos = scenePoint;
165 mRubberBand->start( constrainStartPoint( scenePoint ), Qt::KeyboardModifiers() );
166}
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
@ ZoomOut
Zoom out.
Plot canvas is a class for displaying interactive 2d charts and plots.
virtual void zoomToRect(const QRectF &rect)
Zooms the plot to the specified rect in canvas units.
virtual void scalePlot(double factor)
Scales the plot by a specified scale factor.
virtual void centerPlotOn(double x, double y)
Centers the plot on the plot point corresponding to x, y in canvas units.
A QgsPlotMouseEvent is the result of a user interaction with the mouse on a QgsPlotCanvas.
QgsPlotRectangularRubberBand is rectangular rubber band for use within QgsPlotCanvas widgets.
void plotPressEvent(QgsPlotMouseEvent *event) override
Mouse press event for overriding.
void keyPressEvent(QKeyEvent *event) override
Key press event for overriding.
virtual QRectF constrainBounds(const QRectF &sceneBounds) const
Applies constraints to the overall bounds of the rubber band.
~QgsPlotToolZoom() override
QPointF mRubberBandStartPos
Start position for drag, in scene coordinates.
virtual QPointF constrainStartPoint(QPointF scenePoint) const
Applies constraints to the start point of the zoom rubber band.
void plotReleaseEvent(QgsPlotMouseEvent *event) override
Mouse release event for overriding.
QPoint mMousePressStartPos
Start position for mouse drag.
void plotMoveEvent(QgsPlotMouseEvent *event) override
Mouse move event for overriding.
void keyReleaseEvent(QKeyEvent *event) override
Key release event for overriding.
virtual void zoomInClickOn(QPointF scenePoint)
Handles a zoom out click on the given point.
bool mMarqueeZoom
Will be true will marquee zoom operation is in progress.
void deactivate() override
Called when the tool is being deactivated.
virtual QPointF constrainMovePoint(QPointF scenePoint) const
Applies constraints to a move point of the zoom rubber band.
virtual void zoomOutClickOn(QPointF scenePoint)
Handles a zoom out click on the given point.
QgsPlotToolZoom(QgsPlotCanvas *canvas)
Constructor for QgsPlotToolZoom, with the associated canvas.
Abstract base class for all interactive plot tools.
Definition qgsplottool.h:58
QgsPlotCanvas * canvas() const
Returns the tool's plot canvas.
virtual void deactivate()
Called when the tool is being deactivated.
void setCursor(const QCursor &cursor)
Sets a user defined cursor for use when the tool is active.
bool isClickAndDrag(QPoint startViewPoint, QPoint endViewPoint) const
Returns true if a mouse press/release operation which started at startViewPoint and ended at endViewP...