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