QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgslayoutviewtoolpan.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutviewtoolpan.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 
16 #include "qgslayoutviewtoolpan.h"
18 #include "qgslayoutview.h"
19 #include <QScrollBar>
20 
22  : QgsLayoutViewTool( view, tr( "Pan" ) )
23 {
24  setCursor( Qt::OpenHandCursor );
25 }
26 
28 {
29  mMousePressStartPos = event->pos();
30 
31  if ( event->button() != Qt::LeftButton )
32  {
33  event->ignore();
34  return;
35  }
36 
37  mIsPanning = true;
38  mLastMousePos = event->pos();
39  view()->setCursor( Qt::ClosedHandCursor );
40 }
41 
43 {
44  if ( !mIsPanning )
45  {
46  event->ignore();
47  return;
48  }
49 
50  view()->horizontalScrollBar()->setValue( view()->horizontalScrollBar()->value() - ( event->x() - mLastMousePos.x() ) );
51  view()->verticalScrollBar()->setValue( view()->verticalScrollBar()->value() - ( event->y() - mLastMousePos.y() ) );
52  mLastMousePos = event->pos();
53  view()->viewChanged();
54 }
55 
57 {
58  bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
59 
60  if ( event->button() == Qt::MiddleButton && clickOnly )
61  {
62  //middle mouse button click = recenter on point
63 
64  //get current visible part of scene
65  QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
66  QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
67  QPointF scenePoint = event->layoutPoint();
68  visibleRect.scale( 1, scenePoint.x(), scenePoint.y() );
69  QRectF boundsRect = visibleRect.toRectF();
70 
71  //zoom view to fit desired bounds
72  view()->fitInView( boundsRect, Qt::KeepAspectRatio );
74  view()->viewChanged();
75  return;
76  }
77 
78  if ( !mIsPanning || event->button() != Qt::LeftButton )
79  {
81  view()->viewChanged();
82  event->ignore();
83  return;
84  }
85 
86  mIsPanning = false;
87  view()->setCursor( Qt::OpenHandCursor );
88 }
89 
91 {
92  mIsPanning = false;
94 }
qgslayoutviewtoolpan.h
QgsLayoutViewToolPan::deactivate
void deactivate() override
Called when tool is deactivated.
Definition: qgslayoutviewtoolpan.cpp:90
qgslayoutview.h
QgsRectangle::toRectF
QRectF toRectF() const
Returns a QRectF with same coordinates as the rectangle.
Definition: qgsrectangle.h:468
qgslayoutviewmouseevent.h
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsLayoutViewTool::deactivate
virtual void deactivate()
Called when tool is deactivated.
Definition: qgslayoutviewtool.cpp:130
QgsLayoutView::emitZoomLevelChanged
void emitZoomLevelChanged()
Emits the zoomLevelChanged() signal.
Definition: qgslayoutview.cpp:586
QgsRectangle::scale
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
Definition: qgsrectangle.h:235
QgsLayoutViewToolPan::layoutPressEvent
void layoutPressEvent(QgsLayoutViewMouseEvent *event) override
Mouse press event for overriding.
Definition: qgslayoutviewtoolpan.cpp:27
QgsLayoutViewToolPan::layoutReleaseEvent
void layoutReleaseEvent(QgsLayoutViewMouseEvent *event) override
Mouse release event for overriding.
Definition: qgslayoutviewtoolpan.cpp:56
QgsLayoutViewTool::view
QgsLayoutView * view() const
Returns the view associated with the tool.
Definition: qgslayoutviewtool.cpp:38
QgsLayoutViewToolPan::layoutMoveEvent
void layoutMoveEvent(QgsLayoutViewMouseEvent *event) override
Mouse move event for overriding.
Definition: qgslayoutviewtoolpan.cpp:42
QgsLayoutView::viewChanged
void viewChanged()
Updates associated rulers and other widgets after view extent or zoom has changed.
Definition: qgslayoutview.cpp:1171
QgsLayoutViewTool::isClickAndDrag
bool isClickAndDrag(QPoint startViewPoint, QPoint endViewPoint) const
Returns true if a mouse press/release operation which started at startViewPoint and ended at endViewP...
Definition: qgslayoutviewtool.cpp:31
QgsLayoutViewTool
Abstract base class for all layout view tools.
Definition: qgslayoutviewtool.h:47
QgsLayoutViewToolPan::QgsLayoutViewToolPan
QgsLayoutViewToolPan(QgsLayoutView *view)
Constructor for QgsLayoutViewToolPan.
Definition: qgslayoutviewtoolpan.cpp:21
QgsLayoutView
A graphical widget to display and interact with QgsLayouts.
Definition: qgslayoutview.h:50
QgsLayoutViewTool::setCursor
void setCursor(const QCursor &cursor)
Sets a user defined cursor for use when the tool is active.
Definition: qgslayoutviewtool.cpp:115
QgsLayoutViewMouseEvent
A QgsLayoutViewMouseEvent is the result of a user interaction with the mouse on a QgsLayoutView.
Definition: qgslayoutviewmouseevent.h:36