Skip to content

Commit 41b98aa

Browse files
committedJul 11, 2017
Add layout zoom tool
1 parent 867bdb6 commit 41b98aa

File tree

5 files changed

+226
-0
lines changed

5 files changed

+226
-0
lines changed
 

‎python/gui/gui_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@
282282
%Include layout/qgslayoutviewtool.sip
283283
%Include layout/qgslayoutviewtooladditem.sip
284284
%Include layout/qgslayoutviewtoolpan.sip
285+
%Include layout/qgslayoutviewtoolzoom.sip
285286
%Include locator/qgslocator.sip
286287
%Include locator/qgslocatorfilter.sip
287288
%Include locator/qgslocatorwidget.sip
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/gui/layout/qgslayoutviewtoolzoom.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
class QgsLayoutViewToolZoom : QgsLayoutViewTool
12+
{
13+
%Docstring
14+
Layout view tool for zooming into and out of the layout.
15+
.. versionadded:: 3.0
16+
%End
17+
18+
%TypeHeaderCode
19+
#include "qgslayoutviewtoolzoom.h"
20+
%End
21+
public:
22+
23+
QgsLayoutViewToolZoom( QgsLayoutView *view );
24+
%Docstring
25+
Constructor for QgsLayoutViewToolZoom.
26+
%End
27+
28+
virtual void layoutPressEvent( QgsLayoutViewMouseEvent *event );
29+
30+
virtual void layoutMoveEvent( QgsLayoutViewMouseEvent *event );
31+
32+
virtual void layoutReleaseEvent( QgsLayoutViewMouseEvent *event );
33+
34+
virtual void deactivate();
35+
36+
37+
};
38+
39+
/************************************************************************
40+
* This file has been generated automatically from *
41+
* *
42+
* src/gui/layout/qgslayoutviewtoolzoom.h *
43+
* *
44+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
45+
************************************************************************/

‎src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ SET(QGIS_GUI_SRCS
165165
layout/qgslayoutviewtool.cpp
166166
layout/qgslayoutviewtooladditem.cpp
167167
layout/qgslayoutviewtoolpan.cpp
168+
layout/qgslayoutviewtoolzoom.cpp
168169

169170
locator/qgslocator.cpp
170171
locator/qgslocatorfilter.cpp
@@ -632,6 +633,7 @@ SET(QGIS_GUI_MOC_HDRS
632633
layout/qgslayoutviewtool.h
633634
layout/qgslayoutviewtooladditem.h
634635
layout/qgslayoutviewtoolpan.h
636+
layout/qgslayoutviewtoolzoom.h
635637

636638
locator/qgslocator.h
637639
locator/qgslocatorfilter.h
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/***************************************************************************
2+
qgslayoutviewtoolzoom.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 "qgslayoutviewtoolzoom.h"
17+
#include "qgslayoutviewmouseevent.h"
18+
#include "qgslayoutview.h"
19+
#include "qgslayoutviewrubberband.h"
20+
#include "qgsrectangle.h"
21+
#include "qgscursors.h"
22+
#include <QScrollBar>
23+
24+
QgsLayoutViewToolZoom::QgsLayoutViewToolZoom( QgsLayoutView *view )
25+
: QgsLayoutViewTool( view, tr( "Pan" ) )
26+
{
27+
QPixmap zoomQPixmap = QPixmap( ( const char ** )( zoom_in ) );
28+
setCursor( QCursor( zoomQPixmap, 7, 7 ) );
29+
30+
mRubberBand.reset( new QgsLayoutViewRectangularRubberBand( view ) );
31+
mRubberBand->setBrush( QBrush( QColor( 70, 50, 255, 25 ) ) );
32+
mRubberBand->setPen( QPen( QBrush( QColor( 70, 50, 255, 100 ) ), 0 ) );
33+
}
34+
35+
void QgsLayoutViewToolZoom::layoutPressEvent( QgsLayoutViewMouseEvent *event )
36+
{
37+
if ( event->button() != Qt::LeftButton )
38+
{
39+
return;
40+
}
41+
42+
mMousePressStartPos = event->pos();
43+
if ( event->modifiers() & Qt::AltModifier )
44+
{
45+
//zoom out action, so zoom out and recenter on clicked point
46+
double scaleFactor = 2;
47+
//get current visible part of scene
48+
QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
49+
QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
50+
51+
visibleRect.scale( scaleFactor, event->layoutPoint().x(), event->layoutPoint().y() );
52+
QRectF boundsRect = visibleRect.toRectF();
53+
54+
//zoom view to fit desired bounds
55+
view()->fitInView( boundsRect, Qt::KeepAspectRatio );
56+
}
57+
else
58+
{
59+
//zoom in action
60+
startMarqueeZoom( event->layoutPoint() );
61+
}
62+
}
63+
64+
void QgsLayoutViewToolZoom::layoutMoveEvent( QgsLayoutViewMouseEvent *event )
65+
{
66+
if ( !mMarqueeZoom )
67+
return;
68+
69+
mRubberBand->update( event->layoutPoint(), 0 );
70+
}
71+
72+
void QgsLayoutViewToolZoom::layoutReleaseEvent( QgsLayoutViewMouseEvent *event )
73+
{
74+
if ( !mMarqueeZoom || event->button() != Qt::LeftButton )
75+
{
76+
return;
77+
}
78+
79+
mMarqueeZoom = false;
80+
QRectF newBoundsRect = mRubberBand->finish( event->layoutPoint() );
81+
82+
// click? or click-and-drag?
83+
if ( !isClickAndDrag( mMousePressStartPos, event->pos() ) )
84+
{
85+
//just a click, so zoom to clicked point and recenter
86+
double scaleFactor = 0.5;
87+
//get current visible part of scene
88+
QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
89+
QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
90+
91+
visibleRect.scale( scaleFactor, event->layoutPoint().x(), event->layoutPoint().y() );
92+
newBoundsRect = visibleRect.toRectF();
93+
}
94+
95+
//zoom view to fit desired bounds
96+
view()->fitInView( newBoundsRect, Qt::KeepAspectRatio );
97+
}
98+
99+
void QgsLayoutViewToolZoom::deactivate()
100+
{
101+
if ( mMarqueeZoom )
102+
{
103+
mMarqueeZoom = false;
104+
mRubberBand->finish();
105+
}
106+
QgsLayoutViewTool::deactivate();
107+
}
108+
109+
void QgsLayoutViewToolZoom::startMarqueeZoom( QPointF scenePoint )
110+
{
111+
mMarqueeZoom = true;
112+
113+
mRubberBandStartPos = scenePoint;
114+
mRubberBand->start( scenePoint, 0 );
115+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/***************************************************************************
2+
qgslayoutviewtoolzoom.h
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+
#ifndef QGSLAYOUTVIEWTOOLZOOM_H
17+
#define QGSLAYOUTVIEWTOOLZOOM_H
18+
19+
#include "qgis.h"
20+
#include "qgis_gui.h"
21+
#include "qgslayoutviewtool.h"
22+
#include "qgslayoutviewrubberband.h"
23+
#include <memory>
24+
25+
/**
26+
* \ingroup gui
27+
* Layout view tool for zooming into and out of the layout.
28+
* \since QGIS 3.0
29+
*/
30+
class GUI_EXPORT QgsLayoutViewToolZoom : public QgsLayoutViewTool
31+
{
32+
33+
Q_OBJECT
34+
35+
public:
36+
37+
/**
38+
* Constructor for QgsLayoutViewToolZoom.
39+
*/
40+
QgsLayoutViewToolZoom( QgsLayoutView *view );
41+
42+
void layoutPressEvent( QgsLayoutViewMouseEvent *event ) override;
43+
void layoutMoveEvent( QgsLayoutViewMouseEvent *event ) override;
44+
void layoutReleaseEvent( QgsLayoutViewMouseEvent *event ) override;
45+
void deactivate() override;
46+
47+
private:
48+
49+
//! Start position for mouse press
50+
QPoint mMousePressStartPos;
51+
52+
bool mMarqueeZoom = false;
53+
54+
QPointF mRubberBandStartPos;
55+
56+
//! Rubber band item
57+
std::unique_ptr< QgsLayoutViewRectangularRubberBand > mRubberBand;
58+
59+
void startMarqueeZoom( QPointF scenePoint );
60+
61+
};
62+
63+
#endif // QGSLAYOUTVIEWTOOLZOOM_H

0 commit comments

Comments
 (0)
Please sign in to comment.