Skip to content
Permalink
Browse files

Add layout zoom tool

  • Loading branch information
nyalldawson committed Jul 11, 2017
1 parent 867bdb6 commit 41b98aa23c11dd2a2fd1aafbabe50f887deed04e
@@ -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
@@ -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 *
************************************************************************/
@@ -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
@@ -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
@@ -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 );
}
@@ -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.
You can’t perform that action at this time.