QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 canvas()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ? QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) : QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
101 }
102 event->ignore();
103}
104
105void QgsPlotToolZoom::keyReleaseEvent( QKeyEvent *event )
106{
107 //respond to changes in the alt key status and update cursor accordingly
108 if ( !event->isAutoRepeat() )
109 {
110 canvas()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ? QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) : QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
111 }
112 event->ignore();
113}
114
116{
117 if ( mMarqueeZoom )
118 {
119 mMarqueeZoom = false;
120 mRubberBand->finish();
121 }
123}
124
125QPointF QgsPlotToolZoom::constrainStartPoint( QPointF scenePoint ) const
126{
127 return scenePoint;
128}
129
130QPointF QgsPlotToolZoom::constrainMovePoint( QPointF scenePoint ) const
131{
132 return scenePoint;
133}
134
135QRectF QgsPlotToolZoom::constrainBounds( const QRectF &sceneBounds ) const
136{
137 return sceneBounds;
138}
139
140void QgsPlotToolZoom::zoomOutClickOn( QPointF scenePoint )
141{
142 //just a click, so zoom to clicked point and recenter
143 canvas()->centerPlotOn( scenePoint.x(), scenePoint.y() );
144 canvas()->scalePlot( 0.5 );
145}
146
147void QgsPlotToolZoom::zoomInClickOn( QPointF scenePoint )
148{
149 //just a click, so zoom to clicked point and recenter
150 canvas()->centerPlotOn( scenePoint.x(), scenePoint.y() );
151 canvas()->scalePlot( 2.0 );
152}
153
154void QgsPlotToolZoom::startMarqueeZoom( QPointF scenePoint )
155{
156 mMarqueeZoom = true;
157
158 mRubberBandStartPos = scenePoint;
159 mRubberBand->start( constrainStartPoint( scenePoint ), Qt::KeyboardModifiers() );
160}
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...