QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
18#include <memory>
19
20#include "qgslayoutview.h"
23#include "qgsrectangle.h"
24
25#include <QScrollBar>
26
27#include "moc_qgslayoutviewtoolzoom.cpp"
28
30 : QgsLayoutViewTool( view, tr( "Pan" ) )
31{
33 mRubberBand = std::make_unique<QgsLayoutViewRectangularRubberBand>( view );
34 mRubberBand->setBrush( QBrush( QColor( 70, 50, 255, 25 ) ) );
35 mRubberBand->setPen( QPen( QBrush( QColor( 70, 50, 255, 100 ) ), 0 ) );
36}
37
39{
40 if ( event->button() != Qt::LeftButton )
41 {
42 if ( mMarqueeZoom )
43 {
44 mMarqueeZoom = false;
45 mRubberBand->finish();
46 }
47 event->ignore();
48 return;
49 }
50
51 mMousePressStartPos = event->pos();
52 if ( event->modifiers() & Qt::AltModifier )
53 {
54 //zoom out action, so zoom out and recenter on clicked point
55 const double scaleFactor = 2;
56 //get current visible part of scene
57 const QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
58 QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
59
60 visibleRect.scale( scaleFactor, event->layoutPoint().x(), event->layoutPoint().y() );
61 const QRectF boundsRect = visibleRect.toRectF();
62
63 //zoom view to fit desired bounds
64 view()->fitInView( boundsRect, Qt::KeepAspectRatio );
66 view()->viewChanged();
67 }
68 else
69 {
70 //zoom in action
71 startMarqueeZoom( event->layoutPoint() );
72 }
73}
74
76{
77 if ( !mMarqueeZoom )
78 {
79 event->ignore();
80 return;
81 }
82
83 mRubberBand->update( event->layoutPoint(), Qt::KeyboardModifiers() );
84}
85
87{
88 if ( !mMarqueeZoom || event->button() != Qt::LeftButton )
89 {
90 event->ignore();
91 return;
92 }
93
94 mMarqueeZoom = false;
95 QRectF newBoundsRect = mRubberBand->finish( event->layoutPoint() );
96
97 // click? or click-and-drag?
98 if ( !isClickAndDrag( mMousePressStartPos, event->pos() ) )
99 {
100 //just a click, so zoom to clicked point and recenter
101 const double scaleFactor = 0.5;
102 //get current visible part of scene
103 const QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
104 QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
105
106 visibleRect.scale( scaleFactor, event->layoutPoint().x(), event->layoutPoint().y() );
107 newBoundsRect = visibleRect.toRectF();
108 }
109
110 //zoom view to fit desired bounds
111 view()->fitInView( newBoundsRect, Qt::KeepAspectRatio );
113 view()->viewChanged();
114}
115
117{
118 //respond to changes in the alt key status and update cursor accordingly
119 if ( !event->isAutoRepeat() )
120 {
121 view()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ? QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) : QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
122 }
123 event->ignore();
124}
125
127{
128 //respond to changes in the alt key status and update cursor accordingly
129 if ( !event->isAutoRepeat() )
130 {
131 view()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ? QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) : 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
146void 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.
@ 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.
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.
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...
QgsLayoutViewTool(QgsLayoutView *view, const QString &name)
Constructor for QgsLayoutViewTool, taking a layout view and tool name as parameters.
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.