QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsmodelviewtoolpan.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmodelviewtoolpan.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
16#include "qgsmodelviewtoolpan.h"
17
20
21#include <QScrollBar>
22
23#include "moc_qgsmodelviewtoolpan.cpp"
24
26 : QgsModelViewTool( view, tr( "Pan" ) )
27{
28 setCursor( Qt::OpenHandCursor );
29}
30
32{
33 mMousePressStartPos = event->pos();
34
35 if ( event->button() != Qt::LeftButton )
36 {
37 event->ignore();
38 return;
39 }
40
41 mIsPanning = true;
42 mLastMousePos = event->pos();
43 view()->viewport()->setCursor( Qt::ClosedHandCursor );
44}
45
47{
48 if ( !mIsPanning )
49 {
50 event->ignore();
51 return;
52 }
53
54 view()->horizontalScrollBar()->setValue( view()->horizontalScrollBar()->value() - ( event->x() - mLastMousePos.x() ) );
55 view()->verticalScrollBar()->setValue( view()->verticalScrollBar()->value() - ( event->y() - mLastMousePos.y() ) );
56 mLastMousePos = event->pos();
57}
58
60{
61 const bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
62
63 if ( event->button() == Qt::MiddleButton && clickOnly )
64 {
65 //middle mouse button click = recenter on point
66
67 //get current visible part of scene
68 const QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
69 QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
70 const QPointF scenePoint = event->modelPoint();
71 visibleRect.scale( 1, scenePoint.x(), scenePoint.y() );
72 const QRectF boundsRect = visibleRect.toRectF();
73
74 //zoom view to fit desired bounds
75 view()->fitInView( boundsRect, Qt::KeepAspectRatio );
76 return;
77 }
78
79 if ( !mIsPanning || event->button() != Qt::LeftButton )
80 {
81 event->ignore();
82 return;
83 }
84
85 mIsPanning = false;
86 view()->viewport()->setCursor( Qt::OpenHandCursor );
87}
88
90{
91 mIsPanning = false;
93}
A mouse event which is the result of a user interaction with a QgsModelGraphicsView.
void modelMoveEvent(QgsModelViewMouseEvent *event) override
Mouse move event for overriding.
void deactivate() override
Called when tool is deactivated.
void modelPressEvent(QgsModelViewMouseEvent *event) override
Mouse press event for overriding.
QgsModelViewToolPan(QgsModelGraphicsView *view)
Constructor for QgsModelViewToolPan.
void modelReleaseEvent(QgsModelViewMouseEvent *event) override
Mouse release 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.