QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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
117 view()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ?
120 }
121 event->ignore();
122}
123
125{
126 //respond to changes in the alt key status and update cursor accordingly
127 if ( !event->isAutoRepeat() )
128 {
129
130 view()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ?
133 }
134 event->ignore();
135}
136
138{
139 if ( mMarqueeZoom )
140 {
141 mMarqueeZoom = false;
142 mRubberBand->finish();
143 }
145}
146
147void QgsLayoutViewToolZoom::startMarqueeZoom( QPointF scenePoint )
148{
149 mMarqueeZoom = true;
150
151 mRubberBandStartPos = scenePoint;
152 mRubberBand->start( scenePoint, Qt::KeyboardModifiers() );
153}
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
@ ZoomOut
Zoom out.
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.
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.