QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  event->ignore();
37  return;
38  }
39 
40  mMousePressStartPos = event->pos();
41  if ( event->modifiers() & Qt::AltModifier )
42  {
43  //zoom out action, so zoom out and recenter on clicked point
44  double scaleFactor = 2;
45  //get current visible part of scene
46  QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
47  QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
48 
49  visibleRect.scale( scaleFactor, event->layoutPoint().x(), event->layoutPoint().y() );
50  QRectF boundsRect = visibleRect.toRectF();
51 
52  //zoom view to fit desired bounds
53  view()->fitInView( boundsRect, Qt::KeepAspectRatio );
55  view()->viewChanged();
56  }
57  else
58  {
59  //zoom in action
60  startMarqueeZoom( event->layoutPoint() );
61  }
62 }
63 
65 {
66  if ( !mMarqueeZoom )
67  {
68  event->ignore();
69  return;
70  }
71 
72  mRubberBand->update( event->layoutPoint(), Qt::KeyboardModifiers() );
73 }
74 
76 {
77  if ( !mMarqueeZoom || event->button() != Qt::LeftButton )
78  {
79  event->ignore();
80  return;
81  }
82 
83  mMarqueeZoom = false;
84  QRectF newBoundsRect = mRubberBand->finish( event->layoutPoint() );
85 
86  // click? or click-and-drag?
87  if ( !isClickAndDrag( mMousePressStartPos, event->pos() ) )
88  {
89  //just a click, so zoom to clicked point and recenter
90  double scaleFactor = 0.5;
91  //get current visible part of scene
92  QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
93  QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
94 
95  visibleRect.scale( scaleFactor, event->layoutPoint().x(), event->layoutPoint().y() );
96  newBoundsRect = visibleRect.toRectF();
97  }
98 
99  //zoom view to fit desired bounds
100  view()->fitInView( newBoundsRect, Qt::KeepAspectRatio );
102  view()->viewChanged();
103 }
104 
105 void QgsLayoutViewToolZoom::keyPressEvent( QKeyEvent *event )
106 {
107  //respond to changes in the alt key status and update cursor accordingly
108  if ( !event->isAutoRepeat() )
109  {
110 
111  view()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ?
112  QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) :
113  QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
114  }
115  event->ignore();
116 }
117 
119 {
120  //respond to changes in the alt key status and update cursor accordingly
121  if ( !event->isAutoRepeat() )
122  {
123 
124  view()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ?
125  QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) :
126  QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
127  }
128  event->ignore();
129 }
130 
132 {
133  if ( mMarqueeZoom )
134  {
135  mMarqueeZoom = false;
136  mRubberBand->finish();
137  }
139 }
140 
141 void QgsLayoutViewToolZoom::startMarqueeZoom( QPointF scenePoint )
142 {
143  mMarqueeZoom = true;
144 
145  mRubberBandStartPos = scenePoint;
146  mRubberBand->start( scenePoint, Qt::KeyboardModifiers() );
147 }
qgsrectangle.h
QgsApplication::getThemeCursor
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
Definition: qgsapplication.cpp:656
qgslayoutview.h
QgsRectangle::toRectF
QRectF toRectF() const
Returns a QRectF with same coordinates as the rectangle.
Definition: qgsrectangle.h:468
QgsLayoutViewRectangularRubberBand
QgsLayoutViewRectangularRubberBand is rectangular rubber band for use within QgsLayoutView widgets.
Definition: qgslayoutviewrubberband.h:160
QgsLayoutViewToolZoom::layoutReleaseEvent
void layoutReleaseEvent(QgsLayoutViewMouseEvent *event) override
Mouse release event for overriding.
Definition: qgslayoutviewtoolzoom.cpp:75
qgslayoutviewmouseevent.h
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsLayoutViewTool::deactivate
virtual void deactivate()
Called when tool is deactivated.
Definition: qgslayoutviewtool.cpp:130
QgsLayoutView::emitZoomLevelChanged
void emitZoomLevelChanged()
Emits the zoomLevelChanged() signal.
Definition: qgslayoutview.cpp:586
QgsRectangle::scale
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
Definition: qgsrectangle.h:235
qgslayoutviewrubberband.h
QgsLayoutViewToolZoom::mMarqueeZoom
bool mMarqueeZoom
Will be true will marquee zoom operation is in progress.
Definition: qgslayoutviewtoolzoom.h:52
QgsLayoutViewTool::view
QgsLayoutView * view() const
Returns the view associated with the tool.
Definition: qgslayoutviewtool.cpp:38
QgsLayoutView::viewChanged
void viewChanged()
Updates associated rulers and other widgets after view extent or zoom has changed.
Definition: qgslayoutview.cpp:1171
QgsLayoutViewToolZoom::deactivate
void deactivate() override
Called when tool is deactivated.
Definition: qgslayoutviewtoolzoom.cpp:131
QgsLayoutViewTool::isClickAndDrag
bool isClickAndDrag(QPoint startViewPoint, QPoint endViewPoint) const
Returns true if a mouse press/release operation which started at startViewPoint and ended at endViewP...
Definition: qgslayoutviewtool.cpp:31
QgsLayoutViewTool
Abstract base class for all layout view tools.
Definition: qgslayoutviewtool.h:47
QgsLayoutViewToolZoom::keyReleaseEvent
void keyReleaseEvent(QKeyEvent *event) override
Key release event for overriding.
Definition: qgslayoutviewtoolzoom.cpp:118
QgsLayoutViewToolZoom::keyPressEvent
void keyPressEvent(QKeyEvent *event) override
Key press event for overriding.
Definition: qgslayoutviewtoolzoom.cpp:105
QgsLayoutViewToolZoom::layoutPressEvent
void layoutPressEvent(QgsLayoutViewMouseEvent *event) override
Mouse press event for overriding.
Definition: qgslayoutviewtoolzoom.cpp:32
qgslayoutviewtoolzoom.h
QgsLayoutView
A graphical widget to display and interact with QgsLayouts.
Definition: qgslayoutview.h:50
QgsLayoutViewMouseEvent::layoutPoint
QPointF layoutPoint() const
Returns the event point location in layout coordinates.
Definition: qgslayoutviewmouseevent.cpp:44
QgsLayoutViewToolZoom::QgsLayoutViewToolZoom
QgsLayoutViewToolZoom(QgsLayoutView *view)
Constructor for QgsLayoutViewToolZoom.
Definition: qgslayoutviewtoolzoom.cpp:23
QgsLayoutViewTool::setCursor
void setCursor(const QCursor &cursor)
Sets a user defined cursor for use when the tool is active.
Definition: qgslayoutviewtool.cpp:115
QgsLayoutViewMouseEvent
A QgsLayoutViewMouseEvent is the result of a user interaction with the mouse on a QgsLayoutView.
Definition: qgslayoutviewmouseevent.h:36
QgsLayoutViewToolZoom::layoutMoveEvent
void layoutMoveEvent(QgsLayoutViewMouseEvent *event) override
Mouse move event for overriding.
Definition: qgslayoutviewtoolzoom.cpp:64