From 41b98aa23c11dd2a2fd1aafbabe50f887deed04e Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Wed, 5 Jul 2017 18:01:16 +1000 Subject: [PATCH] Add layout zoom tool --- python/gui/gui_auto.sip | 1 + python/gui/layout/qgslayoutviewtoolzoom.sip | 45 ++++++++ src/gui/CMakeLists.txt | 2 + src/gui/layout/qgslayoutviewtoolzoom.cpp | 115 ++++++++++++++++++++ src/gui/layout/qgslayoutviewtoolzoom.h | 63 +++++++++++ 5 files changed, 226 insertions(+) create mode 100644 python/gui/layout/qgslayoutviewtoolzoom.sip create mode 100644 src/gui/layout/qgslayoutviewtoolzoom.cpp create mode 100644 src/gui/layout/qgslayoutviewtoolzoom.h diff --git a/python/gui/gui_auto.sip b/python/gui/gui_auto.sip index 8bb275bcd23..3cf57ef3593 100644 --- a/python/gui/gui_auto.sip +++ b/python/gui/gui_auto.sip @@ -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 diff --git a/python/gui/layout/qgslayoutviewtoolzoom.sip b/python/gui/layout/qgslayoutviewtoolzoom.sip new file mode 100644 index 00000000000..896419d9de5 --- /dev/null +++ b/python/gui/layout/qgslayoutviewtoolzoom.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 * + ************************************************************************/ diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index febcb183704..f066dea94cb 100755 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -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 diff --git a/src/gui/layout/qgslayoutviewtoolzoom.cpp b/src/gui/layout/qgslayoutviewtoolzoom.cpp new file mode 100644 index 00000000000..312277b173e --- /dev/null +++ b/src/gui/layout/qgslayoutviewtoolzoom.cpp @@ -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 + +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 ); +} diff --git a/src/gui/layout/qgslayoutviewtoolzoom.h b/src/gui/layout/qgslayoutviewtoolzoom.h new file mode 100644 index 00000000000..f1b59184b8c --- /dev/null +++ b/src/gui/layout/qgslayoutviewtoolzoom.h @@ -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 + +/** + * \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