QGIS API Documentation 3.43.0-Master (c67cf405802)
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
17#include "moc_qgslayoutviewtoolzoom.cpp"
19#include "qgslayoutview.h"
21#include "qgsrectangle.h"
22#include <QScrollBar>
23
25 : QgsLayoutViewTool( view, tr( "Pan" ) )
26{
28 mRubberBand.reset( new QgsLayoutViewRectangularRubberBand( view ) );
29 mRubberBand->setBrush( QBrush( QColor( 70, 50, 255, 25 ) ) );
30 mRubberBand->setPen( QPen( QBrush( QColor( 70, 50, 255, 100 ) ), 0 ) );
31}
32
34{
35 if ( event->button() != Qt::LeftButton )
36 {
37 if ( mMarqueeZoom )
38 {
39 mMarqueeZoom = false;
40 mRubberBand->finish();
41 }
42 event->ignore();
43 return;
44 }
45
46 mMousePressStartPos = event->pos();
47 if ( event->modifiers() & Qt::AltModifier )
48 {
49 //zoom out action, so zoom out and recenter on clicked point
50 const double scaleFactor = 2;
51 //get current visible part of scene
52 const QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
53 QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
54
55 visibleRect.scale( scaleFactor, event->layoutPoint().x(), event->layoutPoint().y() );
56 const QRectF boundsRect = visibleRect.toRectF();
57
58 //zoom view to fit desired bounds
59 view()->fitInView( boundsRect, Qt::KeepAspectRatio );
61 view()->viewChanged();
62 }
63 else
64 {
65 //zoom in action
66 startMarqueeZoom( event->layoutPoint() );
67 }
68}
69
71{
72 if ( !mMarqueeZoom )
73 {
74 event->ignore();
75 return;
76 }
77
78 mRubberBand->update( event->layoutPoint(), Qt::KeyboardModifiers() );
79}
80
82{
83 if ( !mMarqueeZoom || event->button() != Qt::LeftButton )
84 {
85 event->ignore();
86 return;
87 }
88
89 mMarqueeZoom = false;
90 QRectF newBoundsRect = mRubberBand->finish( event->layoutPoint() );
91
92 // click? or click-and-drag?
93 if ( !isClickAndDrag( mMousePressStartPos, event->pos() ) )
94 {
95 //just a click, so zoom to clicked point and recenter
96 const double scaleFactor = 0.5;
97 //get current visible part of scene
98 const QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
99 QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
100
101 visibleRect.scale( scaleFactor, event->layoutPoint().x(), event->layoutPoint().y() );
102 newBoundsRect = visibleRect.toRectF();
103 }
104
105 //zoom view to fit desired bounds
106 view()->fitInView( newBoundsRect, Qt::KeepAspectRatio );
108 view()->viewChanged();
109}
110
112{
113 //respond to changes in the alt key status and update cursor accordingly
114 if ( !event->isAutoRepeat() )
115 {
116 view()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ? QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) : QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
117 }
118 event->ignore();
119}
120
122{
123 //respond to changes in the alt key status and update cursor accordingly
124 if ( !event->isAutoRepeat() )
125 {
126 view()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ? QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) : 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
141void QgsLayoutViewToolZoom::startMarqueeZoom( QPointF scenePoint )
142{
143 mMarqueeZoom = true;
144
145 mRubberBandStartPos = scenePoint;
146 mRubberBand->start( scenePoint, Qt::KeyboardModifiers() );
147}
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
@ ZoomOut
Zoom out.
A mouse event which is the result of a user interaction with a QgsLayoutView.
QPointF layoutPoint() const
Returns the event point location in layout coordinates.
A 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.
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.
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
QRectF toRectF() const
Returns a QRectF with same coordinates as the rectangle.