Skip to content

Commit

Permalink
Add layout zoom tool
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 11, 2017
1 parent 867bdb6 commit 41b98aa
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/gui/gui_auto.sip
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@
%Include layout/qgslayoutviewtool.sip
%Include layout/qgslayoutviewtooladditem.sip
%Include layout/qgslayoutviewtoolpan.sip
%Include layout/qgslayoutviewtoolzoom.sip
%Include locator/qgslocator.sip
%Include locator/qgslocatorfilter.sip
%Include locator/qgslocatorwidget.sip
Expand Down
45 changes: 45 additions & 0 deletions python/gui/layout/qgslayoutviewtoolzoom.sip
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutviewtoolzoom.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/



class QgsLayoutViewToolZoom : QgsLayoutViewTool
{
%Docstring
Layout view tool for zooming into and out of the layout.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayoutviewtoolzoom.h"
%End
public:

QgsLayoutViewToolZoom( QgsLayoutView *view );
%Docstring
Constructor for QgsLayoutViewToolZoom.
%End

virtual void layoutPressEvent( QgsLayoutViewMouseEvent *event );

virtual void layoutMoveEvent( QgsLayoutViewMouseEvent *event );

virtual void layoutReleaseEvent( QgsLayoutViewMouseEvent *event );

virtual void deactivate();


};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutviewtoolzoom.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ SET(QGIS_GUI_SRCS
layout/qgslayoutviewtool.cpp
layout/qgslayoutviewtooladditem.cpp
layout/qgslayoutviewtoolpan.cpp
layout/qgslayoutviewtoolzoom.cpp

locator/qgslocator.cpp
locator/qgslocatorfilter.cpp
Expand Down Expand Up @@ -632,6 +633,7 @@ SET(QGIS_GUI_MOC_HDRS
layout/qgslayoutviewtool.h
layout/qgslayoutviewtooladditem.h
layout/qgslayoutviewtoolpan.h
layout/qgslayoutviewtoolzoom.h

locator/qgslocator.h
locator/qgslocatorfilter.h
Expand Down
115 changes: 115 additions & 0 deletions src/gui/layout/qgslayoutviewtoolzoom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/***************************************************************************
qgslayoutviewtoolzoom.cpp
-------------------------
Date : July 2017
Copyright : (C) 2017 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgslayoutviewtoolzoom.h"
#include "qgslayoutviewmouseevent.h"
#include "qgslayoutview.h"
#include "qgslayoutviewrubberband.h"
#include "qgsrectangle.h"
#include "qgscursors.h"
#include <QScrollBar>

QgsLayoutViewToolZoom::QgsLayoutViewToolZoom( QgsLayoutView *view )
: QgsLayoutViewTool( view, tr( "Pan" ) )
{
QPixmap zoomQPixmap = QPixmap( ( const char ** )( zoom_in ) );
setCursor( QCursor( zoomQPixmap, 7, 7 ) );

mRubberBand.reset( new QgsLayoutViewRectangularRubberBand( view ) );
mRubberBand->setBrush( QBrush( QColor( 70, 50, 255, 25 ) ) );
mRubberBand->setPen( QPen( QBrush( QColor( 70, 50, 255, 100 ) ), 0 ) );
}

void QgsLayoutViewToolZoom::layoutPressEvent( QgsLayoutViewMouseEvent *event )
{
if ( event->button() != Qt::LeftButton )
{
return;
}

mMousePressStartPos = event->pos();
if ( event->modifiers() & Qt::AltModifier )
{
//zoom out action, so zoom out and recenter on clicked point
double scaleFactor = 2;
//get current visible part of scene
QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );

visibleRect.scale( scaleFactor, event->layoutPoint().x(), event->layoutPoint().y() );
QRectF boundsRect = visibleRect.toRectF();

//zoom view to fit desired bounds
view()->fitInView( boundsRect, Qt::KeepAspectRatio );
}
else
{
//zoom in action
startMarqueeZoom( event->layoutPoint() );
}
}

void QgsLayoutViewToolZoom::layoutMoveEvent( QgsLayoutViewMouseEvent *event )
{
if ( !mMarqueeZoom )
return;

mRubberBand->update( event->layoutPoint(), 0 );
}

void QgsLayoutViewToolZoom::layoutReleaseEvent( QgsLayoutViewMouseEvent *event )
{
if ( !mMarqueeZoom || event->button() != Qt::LeftButton )
{
return;
}

mMarqueeZoom = false;
QRectF newBoundsRect = mRubberBand->finish( event->layoutPoint() );

// click? or click-and-drag?
if ( !isClickAndDrag( mMousePressStartPos, event->pos() ) )
{
//just a click, so zoom to clicked point and recenter
double scaleFactor = 0.5;
//get current visible part of scene
QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );

visibleRect.scale( scaleFactor, event->layoutPoint().x(), event->layoutPoint().y() );
newBoundsRect = visibleRect.toRectF();
}

//zoom view to fit desired bounds
view()->fitInView( newBoundsRect, Qt::KeepAspectRatio );
}

void QgsLayoutViewToolZoom::deactivate()
{
if ( mMarqueeZoom )
{
mMarqueeZoom = false;
mRubberBand->finish();
}
QgsLayoutViewTool::deactivate();
}

void QgsLayoutViewToolZoom::startMarqueeZoom( QPointF scenePoint )
{
mMarqueeZoom = true;

mRubberBandStartPos = scenePoint;
mRubberBand->start( scenePoint, 0 );
}
63 changes: 63 additions & 0 deletions src/gui/layout/qgslayoutviewtoolzoom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/***************************************************************************
qgslayoutviewtoolzoom.h
-----------------------
Date : July 2017
Copyright : (C) 2017 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSLAYOUTVIEWTOOLZOOM_H
#define QGSLAYOUTVIEWTOOLZOOM_H

#include "qgis.h"
#include "qgis_gui.h"
#include "qgslayoutviewtool.h"
#include "qgslayoutviewrubberband.h"
#include <memory>

/**
* \ingroup gui
* Layout view tool for zooming into and out of the layout.
* \since QGIS 3.0
*/
class GUI_EXPORT QgsLayoutViewToolZoom : public QgsLayoutViewTool
{

Q_OBJECT

public:

/**
* Constructor for QgsLayoutViewToolZoom.
*/
QgsLayoutViewToolZoom( QgsLayoutView *view );

void layoutPressEvent( QgsLayoutViewMouseEvent *event ) override;
void layoutMoveEvent( QgsLayoutViewMouseEvent *event ) override;
void layoutReleaseEvent( QgsLayoutViewMouseEvent *event ) override;
void deactivate() override;

private:

//! Start position for mouse press
QPoint mMousePressStartPos;

bool mMarqueeZoom = false;

QPointF mRubberBandStartPos;

//! Rubber band item
std::unique_ptr< QgsLayoutViewRectangularRubberBand > mRubberBand;

void startMarqueeZoom( QPointF scenePoint );

};

#endif // QGSLAYOUTVIEWTOOLZOOM_H

0 comments on commit 41b98aa

Please sign in to comment.