QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgslayoutviewtoolzoom.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutviewtoolzoom.cpp
3  -------------------------
4  Date : July 2017
5  Copyright : (C) 2017 Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgslayoutviewtoolzoom.h"
18 #include "qgslayoutview.h"
20 #include "qgsrectangle.h"
21 #include <QScrollBar>
22 
24  : QgsLayoutViewTool( view, tr( "Pan" ) )
25 {
26  setCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
27  mRubberBand.reset( new QgsLayoutViewRectangularRubberBand( view ) );
28  mRubberBand->setBrush( QBrush( QColor( 70, 50, 255, 25 ) ) );
29  mRubberBand->setPen( QPen( QBrush( QColor( 70, 50, 255, 100 ) ), 0 ) );
30 }
31 
33 {
34  if ( event->button() != Qt::LeftButton )
35  {
36  if ( mMarqueeZoom )
37  {
38  mMarqueeZoom = false;
39  mRubberBand->finish();
40  }
41  event->ignore();
42  return;
43  }
44 
45  mMousePressStartPos = event->pos();
46  if ( event->modifiers() & Qt::AltModifier )
47  {
48  //zoom out action, so zoom out and recenter on clicked point
49  const double scaleFactor = 2;
50  //get current visible part of scene
51  const QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
52  QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
53 
54  visibleRect.scale( scaleFactor, event->layoutPoint().x(), event->layoutPoint().y() );
55  const QRectF boundsRect = visibleRect.toRectF();
56 
57  //zoom view to fit desired bounds
58  view()->fitInView( boundsRect, Qt::KeepAspectRatio );
60  view()->viewChanged();
61  }
62  else
63  {
64  //zoom in action
65  startMarqueeZoom( event->layoutPoint() );
66  }
67 }
68 
70 {
71  if ( !mMarqueeZoom )
72  {
73  event->ignore();
74  return;
75  }
76 
77  mRubberBand->update( event->layoutPoint(), Qt::KeyboardModifiers() );
78 }
79 
81 {
82  if ( !mMarqueeZoom || event->button() != Qt::LeftButton )
83  {
84  event->ignore();
85  return;
86  }
87 
88  mMarqueeZoom = false;
89  QRectF newBoundsRect = mRubberBand->finish( event->layoutPoint() );
90 
91  // click? or click-and-drag?
92  if ( !isClickAndDrag( mMousePressStartPos, event->pos() ) )
93  {
94  //just a click, so zoom to clicked point and recenter
95  const double scaleFactor = 0.5;
96  //get current visible part of scene
97  const QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
98  QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
99 
100  visibleRect.scale( scaleFactor, event->layoutPoint().x(), event->layoutPoint().y() );
101  newBoundsRect = visibleRect.toRectF();
102  }
103 
104  //zoom view to fit desired bounds
105  view()->fitInView( newBoundsRect, Qt::KeepAspectRatio );
107  view()->viewChanged();
108 }
109 
110 void QgsLayoutViewToolZoom::keyPressEvent( QKeyEvent *event )
111 {
112  //respond to changes in the alt key status and update cursor accordingly
113  if ( !event->isAutoRepeat() )
114  {
115 
116  view()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ?
117  QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) :
118  QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
119  }
120  event->ignore();
121 }
122 
124 {
125  //respond to changes in the alt key status and update cursor accordingly
126  if ( !event->isAutoRepeat() )
127  {
128 
129  view()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ?
130  QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) :
131  QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
132  }
133  event->ignore();
134 }
135 
137 {
138  if ( mMarqueeZoom )
139  {
140  mMarqueeZoom = false;
141  mRubberBand->finish();
142  }
144 }
145 
146 void QgsLayoutViewToolZoom::startMarqueeZoom( QPointF scenePoint )
147 {
148  mMarqueeZoom = true;
149 
150  mRubberBandStartPos = scenePoint;
151  mRubberBand->start( scenePoint, Qt::KeyboardModifiers() );
152 }
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
A QgsLayoutViewMouseEvent is the result of a user interaction with the mouse on a QgsLayoutView.
QPointF layoutPoint() const
Returns the event point location in layout coordinates.
QgsLayoutViewRectangularRubberBand is rectangular rubber band for use within QgsLayoutView widgets.
void deactivate() override
Called when tool is deactivated.
void layoutReleaseEvent(QgsLayoutViewMouseEvent *event) override
Mouse release event for overriding.
void layoutPressEvent(QgsLayoutViewMouseEvent *event) override
Mouse press event for overriding.
QgsLayoutViewToolZoom(QgsLayoutView *view)
Constructor for QgsLayoutViewToolZoom.
void layoutMoveEvent(QgsLayoutViewMouseEvent *event) override
Mouse move event for overriding.
void keyReleaseEvent(QKeyEvent *event) override
Key release event for overriding.
void keyPressEvent(QKeyEvent *event) override
Key press event for overriding.
bool mMarqueeZoom
Will be true will marquee zoom operation is in progress.
Abstract base class for all layout view tools.
void setCursor(const QCursor &cursor)
Sets a user defined cursor for use when the tool is active.
QgsLayoutView * view() const
Returns the view associated with the tool.
virtual void deactivate()
Called when tool is deactivated.
bool isClickAndDrag(QPoint startViewPoint, QPoint endViewPoint) const
Returns true if a mouse press/release operation which started at startViewPoint and ended at endViewP...
A graphical widget to display and interact with QgsLayouts.
Definition: qgslayoutview.h:50
void viewChanged()
Updates associated rulers and other widgets after view extent or zoom has changed.
void emitZoomLevelChanged()
Emits the zoomLevelChanged() signal.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
Definition: qgsrectangle.h:256
QRectF toRectF() const
Returns a QRectF with same coordinates as the rectangle.
Definition: qgsrectangle.h:500