QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
20#include <memory>
21
22#include "qgsapplication.h"
23#include "qgsplotcanvas.h"
24#include "qgsplotmouseevent.h"
25#include "qgsplotrubberband.h"
26
27#include "moc_qgsplottoolzoom.cpp"
28
30 : QgsPlotTool( canvas, tr( "Zoom" ) )
31{
33 mRubberBand = std::make_unique<QgsPlotRectangularRubberBand>( canvas );
34 mRubberBand->setBrush( QBrush( QColor( 70, 50, 255, 25 ) ) );
35 mRubberBand->setPen( QPen( QBrush( QColor( 70, 50, 255, 100 ) ), 0 ) );
36}
37
39
41{
42 if ( event->button() != Qt::LeftButton )
43 {
44 if ( mMarqueeZoom )
45 {
46 mMarqueeZoom = false;
47 mRubberBand->finish();
48 }
49 event->ignore();
50 return;
51 }
52
53 mMousePressStartPos = event->pos();
54 if ( event->modifiers() & Qt::AltModifier )
55 {
56 zoomOutClickOn( event->pos() );
57 }
58 else
59 {
60 //zoom in action
61 startMarqueeZoom( event->pos() );
62 }
63}
64
66{
67 if ( !mMarqueeZoom )
68 {
69 event->ignore();
70 return;
71 }
72
73 mRubberBand->update( constrainMovePoint( event->pos() ), Qt::KeyboardModifiers() );
74}
75
77{
78 if ( !mMarqueeZoom || event->button() != Qt::LeftButton )
79 {
80 event->ignore();
81 return;
82 }
83
84 mMarqueeZoom = false;
85 // click? or click-and-drag?
86 if ( !isClickAndDrag( mMousePressStartPos, event->pos() ) )
87 {
88 zoomInClickOn( event->pos() );
89 }
90 else
91 {
92 QRectF newBoundsRect = mRubberBand->finish( constrainMovePoint( event->pos() ) );
93
94 //zoom view to fit desired bounds
95 canvas()->zoomToRect( constrainBounds( newBoundsRect ) );
96 }
97}
98
99void QgsPlotToolZoom::keyPressEvent( QKeyEvent *event )
100{
101 //respond to changes in the alt key status and update cursor accordingly
102 if ( !event->isAutoRepeat() )
103 {
104 canvas()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ? QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) : QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
105 }
106 event->ignore();
107}
108
109void QgsPlotToolZoom::keyReleaseEvent( QKeyEvent *event )
110{
111 //respond to changes in the alt key status and update cursor accordingly
112 if ( !event->isAutoRepeat() )
113 {
114 canvas()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ? QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) : QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
115 }
116 event->ignore();
117}
118
120{
121 if ( mMarqueeZoom )
122 {
123 mMarqueeZoom = false;
124 mRubberBand->finish();
125 }
127}
128
129QPointF QgsPlotToolZoom::constrainStartPoint( QPointF scenePoint ) const
130{
131 return scenePoint;
132}
133
134QPointF QgsPlotToolZoom::constrainMovePoint( QPointF scenePoint ) const
135{
136 return scenePoint;
137}
138
139QRectF QgsPlotToolZoom::constrainBounds( const QRectF &sceneBounds ) const
140{
141 return sceneBounds;
142}
143
144void QgsPlotToolZoom::zoomOutClickOn( QPointF scenePoint )
145{
146 //just a click, so zoom to clicked point and recenter
147 canvas()->centerPlotOn( scenePoint.x(), scenePoint.y() );
148 canvas()->scalePlot( 0.5 );
149}
150
151void QgsPlotToolZoom::zoomInClickOn( QPointF scenePoint )
152{
153 //just a click, so zoom to clicked point and recenter
154 canvas()->centerPlotOn( scenePoint.x(), scenePoint.y() );
155 canvas()->scalePlot( 2.0 );
156}
157
158void QgsPlotToolZoom::startMarqueeZoom( QPointF scenePoint )
159{
160 mMarqueeZoom = true;
161
162 mRubberBandStartPos = scenePoint;
163 mRubberBand->start( constrainStartPoint( scenePoint ), Qt::KeyboardModifiers() );
164}
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 mouse event which is the result of a user interaction with a QgsPlotCanvas.
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.
QgsPlotCanvas * canvas() const
Returns the tool's plot canvas.
virtual void deactivate()
Called when the tool is being deactivated.
QgsPlotTool(QgsPlotCanvas *canvas, const QString &name)
Constructor takes a plot canvas as a parameter.
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...