QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
18#include <memory>
19
20#include "qgsapplication.h"
24#include "qgsrectangle.h"
25
26#include <QScrollBar>
27
28#include "moc_qgsmodelviewtoolzoom.cpp"
29
31 : QgsModelViewTool( view, tr( "Pan" ) )
32{
34 mRubberBand = std::make_unique<QgsModelViewRectangularRubberBand>( view );
35 mRubberBand->setBrush( QBrush( QColor( 70, 50, 255, 25 ) ) );
36 mRubberBand->setPen( QPen( QBrush( QColor( 70, 50, 255, 100 ) ), 0 ) );
37}
38
40{
41 if ( event->button() != Qt::LeftButton )
42 {
43 event->ignore();
44 return;
45 }
46
47 mMousePressStartPos = event->pos();
48 if ( event->modifiers() & Qt::AltModifier )
49 {
50 //zoom out action, so zoom out and recenter on clicked point
51 const double scaleFactor = 2;
52 //get current visible part of scene
53 const QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
54 QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
55
56 visibleRect.scale( scaleFactor, event->modelPoint().x(), event->modelPoint().y() );
57 const QRectF boundsRect = visibleRect.toRectF();
58
59 //zoom view to fit desired bounds
60 view()->fitInView( boundsRect, Qt::KeepAspectRatio );
61 }
62 else
63 {
64 //zoom in action
65 startMarqueeZoom( event->modelPoint() );
66 }
67}
68
70{
71 if ( !mMarqueeZoom )
72 {
73 event->ignore();
74 return;
75 }
76
77 mRubberBand->update( event->modelPoint(), Qt::KeyboardModifiers() );
78}
79
81{
82 if ( !mMarqueeZoom || event->button() != Qt::LeftButton )
83 {
84 event->ignore();
85 return;
86 }
87
88 mMarqueeZoom = false;
89 QRectF newBoundsRect = mRubberBand->finish( event->modelPoint() );
90
91 // click? or click-and-drag?
92 if ( !isClickAndDrag( mMousePressStartPos, event->pos() ) )
93 {
94 //just a click, so zoom to clicked point and recenter
95 const double scaleFactor = 0.5;
96 //get current visible part of scene
97 const QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
98 QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
99
100 visibleRect.scale( scaleFactor, event->modelPoint().x(), event->modelPoint().y() );
101 newBoundsRect = visibleRect.toRectF();
102 }
103
104 //zoom view to fit desired bounds
105 view()->fitInView( newBoundsRect, Qt::KeepAspectRatio );
106}
107
109{
110 //respond to changes in the alt key status and update cursor accordingly
111 if ( !event->isAutoRepeat() )
112 {
113 view()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ? QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) : 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 view()->viewport()->setCursor( ( event->modifiers() & Qt::AltModifier ) ? QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) : QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) );
124 }
125 event->ignore();
126}
127
129{
130 if ( mMarqueeZoom )
131 {
132 mMarqueeZoom = false;
133 mRubberBand->finish();
134 }
136}
137
138void QgsModelViewToolZoom::startMarqueeZoom( QPointF scenePoint )
139{
140 mMarqueeZoom = true;
141
142 mRubberBandStartPos = scenePoint;
143 mRubberBand->start( scenePoint, Qt::KeyboardModifiers() );
144}
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 QgsModelGraphicsView.
QPointF modelPoint() const
Returns the event point location in model coordinates.
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.
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.
QgsModelViewTool(QgsModelGraphicsView *view, const QString &name)
Constructor for QgsModelViewTool, taking a model view and tool name as parameters.
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.