QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
94void 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
107void 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
130QPointF QgsPlotToolZoom::constrainStartPoint( QPointF scenePoint ) const
131{
132 return scenePoint;
133}
134
135QPointF QgsPlotToolZoom::constrainMovePoint( QPointF scenePoint ) const
136{
137 return scenePoint;
138}
139
140QRectF QgsPlotToolZoom::constrainBounds( const QRectF &sceneBounds ) const
141{
142 return sceneBounds;
143}
144
145void 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
152void 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
159void QgsPlotToolZoom::startMarqueeZoom( QPointF scenePoint )
160{
161 mMarqueeZoom = true;
162
163 mRubberBandStartPos = scenePoint;
164 mRubberBand->start( constrainStartPoint( scenePoint ), Qt::KeyboardModifiers() );
165}
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
Plot canvas is a class for displaying interactive 2d charts and plots.
Definition: qgsplotcanvas.h:54
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.
Definition: qgsplottool.cpp:89
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...
Definition: qgsplottool.cpp:46