QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 }
A QgsLayoutViewMouseEvent is the result of a user interaction with the mouse on a QgsLayoutView.
void layoutPressEvent(QgsLayoutViewMouseEvent *event) override
Mouse press event for overriding.
void deactivate() override
Called when tool is deactivated.
void layoutReleaseEvent(QgsLayoutViewMouseEvent *event) override
Mouse release event for overriding.
QgsLayoutViewToolPan(QgsLayoutView *view)
Constructor for QgsLayoutViewToolPan.
void layoutMoveEvent(QgsLayoutViewMouseEvent *event) override
Mouse move event for overriding.
Abstract base class for all layout view tools.
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...
A graphical widget to display and interact with QgsLayouts.
Definition: qgslayoutview.h:50
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.
Definition: qgsrectangle.h:42
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
Definition: qgsrectangle.h:256
QRectF toRectF() const
Returns a QRectF with same coordinates as the rectangle.
Definition: qgsrectangle.h:500