QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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
17#include "moc_qgslayoutviewtoolpan.cpp"
19#include "qgslayoutview.h"
20#include <QScrollBar>
21
23 : QgsLayoutViewTool( view, tr( "Pan" ) )
24{
25 setCursor( Qt::OpenHandCursor );
26}
27
29{
30 mMousePressStartPos = event->pos();
31
32 if ( event->button() != Qt::LeftButton )
33 {
34 event->ignore();
35 return;
36 }
37
38 mIsPanning = true;
39 mLastMousePos = event->pos();
40 view()->viewport()->setCursor( Qt::ClosedHandCursor );
41}
42
44{
45 if ( !mIsPanning )
46 {
47 event->ignore();
48 return;
49 }
50
51 view()->horizontalScrollBar()->setValue( view()->horizontalScrollBar()->value() - ( event->x() - mLastMousePos.x() ) );
52 view()->verticalScrollBar()->setValue( view()->verticalScrollBar()->value() - ( event->y() - mLastMousePos.y() ) );
53 mLastMousePos = event->pos();
54 view()->viewChanged();
55}
56
58{
59 const bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
60
61 if ( event->button() == Qt::MiddleButton && clickOnly )
62 {
63 //middle mouse button click = recenter on point
64
65 //get current visible part of scene
66 const QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
67 QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
68 const QPointF scenePoint = event->layoutPoint();
69 visibleRect.scale( 1, scenePoint.x(), scenePoint.y() );
70 const QRectF boundsRect = visibleRect.toRectF();
71
72 //zoom view to fit desired bounds
73 view()->fitInView( boundsRect, Qt::KeepAspectRatio );
75 view()->viewChanged();
76 return;
77 }
78
79 if ( !mIsPanning || event->button() != Qt::LeftButton )
80 {
82 view()->viewChanged();
83 event->ignore();
84 return;
85 }
86
87 mIsPanning = false;
88 view()->viewport()->setCursor( Qt::OpenHandCursor );
89}
90
92{
93 mIsPanning = false;
95}
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.
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.
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.