QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsmodelviewtoolzoom.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmodelviewtoolzoom.cpp
3 -------------------------
4 Date : March 2020
5 Copyright : (C) 2020 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_qgsmodelviewtoolzoom.cpp"
21#include "qgsrectangle.h"
22#include "qgsapplication.h"
23#include <QScrollBar>
24
25QgsModelViewToolZoom::QgsModelViewToolZoom( QgsModelGraphicsView *view )
26 : QgsModelViewTool( view, tr( "Pan" ) )
27{
29 mRubberBand.reset( new QgsModelViewRectangularRubberBand( view ) );
30 mRubberBand->setBrush( QBrush( QColor( 70, 50, 255, 25 ) ) );
31 mRubberBand->setPen( QPen( QBrush( QColor( 70, 50, 255, 100 ) ), 0 ) );
32}
33
35{
36 if ( event->button() != Qt::LeftButton )
37 {
38 event->ignore();
39 return;
40 }
41
42 mMousePressStartPos = event->pos();
43 if ( event->modifiers() & Qt::AltModifier )
44 {
45 //zoom out action, so zoom out and recenter on clicked point
46 const double scaleFactor = 2;
47 //get current visible part of scene
48 const QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
49 QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
50
51 visibleRect.scale( scaleFactor, event->modelPoint().x(), event->modelPoint().y() );
52 const QRectF boundsRect = visibleRect.toRectF();
53
54 //zoom view to fit desired bounds
55 view()->fitInView( boundsRect, Qt::KeepAspectRatio );
56 }
57 else
58 {
59 //zoom in action
60 startMarqueeZoom( event->modelPoint() );
61 }
62}
63
65{
66 if ( !mMarqueeZoom )
67 {
68 event->ignore();
69 return;
70 }
71
72 mRubberBand->update( event->modelPoint(), 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->modelPoint() );
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 const double scaleFactor = 0.5;
91 //get current visible part of scene
92 const QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
93 QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
94
95 visibleRect.scale( scaleFactor, event->modelPoint().x(), event->modelPoint().y() );
96 newBoundsRect = visibleRect.toRectF();
97 }
98
99 //zoom view to fit desired bounds
100 view()->fitInView( newBoundsRect, Qt::KeepAspectRatio );
101}
102
104{
105 //respond to changes in the alt key status and update cursor accordingly
106 if ( !event->isAutoRepeat() )
107 {
108 view()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ? QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) : QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
109 }
110 event->ignore();
111}
112
114{
115 //respond to changes in the alt key status and update cursor accordingly
116 if ( !event->isAutoRepeat() )
117 {
118 view()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ? QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) : QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
119 }
120 event->ignore();
121}
122
124{
125 if ( mMarqueeZoom )
126 {
127 mMarqueeZoom = false;
128 mRubberBand->finish();
129 }
131}
132
133void QgsModelViewToolZoom::startMarqueeZoom( QPointF scenePoint )
134{
135 mMarqueeZoom = true;
136
137 mRubberBandStartPos = scenePoint;
138 mRubberBand->start( scenePoint, Qt::KeyboardModifiers() );
139}
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
@ ZoomOut
Zoom out.
A QgsModelViewMouseEvent is the result of a user interaction with the mouse on a QgsModelGraphicsView...
QPointF modelPoint() const
Returns the event point location in model coordinates.
QgsModelViewRectangularRubberBand is rectangular rubber band for use within QgsModelGraphicsView widg...
void deactivate() override
Called when tool is deactivated.
void keyReleaseEvent(QKeyEvent *event) override
Key release event for overriding.
void modelPressEvent(QgsModelViewMouseEvent *event) override
Mouse press 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 modelReleaseEvent(QgsModelViewMouseEvent *event) override
Mouse release event for overriding.
QgsModelViewToolZoom(QgsModelGraphicsView *view)
Constructor for QgsModelViewToolZoom.
void modelMoveEvent(QgsModelViewMouseEvent *event) override
Mouse move event for overriding.
Abstract base class for all model designer view tools.
QgsModelGraphicsView * view() const
Returns the view associated with the tool.
bool isClickAndDrag(QPoint startViewPoint, QPoint endViewPoint) const
Returns true if a mouse press/release operation which started at startViewPoint and ended at endViewP...
void setCursor(const QCursor &cursor)
Sets a user defined cursor for use when the tool is active.
virtual void deactivate()
Called when tool is deactivated.
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.