Skip to content
Permalink
Browse files

Zoom tool: do not draw rect when zooming out, update cursor when pres…

…sing alt
  • Loading branch information
3nids committed Oct 19, 2018
1 parent 7731674 commit 5fd6f6870184e074030cdf3b26a4a35beca942cf
Showing with 64 additions and 12 deletions.
  1. +10 −0 python/gui/auto_generated/qgsmaptoolzoom.sip.in
  2. +45 −12 src/gui/qgsmaptoolzoom.cpp
  3. +9 −0 src/gui/qgsmaptoolzoom.h
@@ -34,13 +34,23 @@ constructor

virtual void canvasReleaseEvent( QgsMapMouseEvent *e );

virtual void keyPressEvent( QKeyEvent *e );

virtual void keyReleaseEvent( QKeyEvent *e );

virtual void deactivate();


protected:





void updateCursor();
%Docstring
Flag to indicate a map canvas drag operation is taking place
%End
};

/************************************************************************
@@ -31,13 +31,14 @@
QgsMapToolZoom::QgsMapToolZoom( QgsMapCanvas *canvas, bool zoomOut )
: QgsMapTool( canvas )
, mZoomOut( zoomOut )
, mNativeZoomOut( zoomOut )
, mDragging( false )
, mZoomOutCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) )
, mZoomInCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn ) )

{
mToolName = tr( "Zoom" );
// set the cursor
mCursor = zoomOut ? QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomOut ) :
QgsApplication::getThemeCursor( QgsApplication::Cursor::ZoomIn );
updateCursor();
}

QgsMapToolZoom::~QgsMapToolZoom()
@@ -64,8 +65,15 @@ void QgsMapToolZoom::canvasMoveEvent( QgsMapMouseEvent *e )
mZoomRect.setBottomRight( e->pos() );
if ( mRubberBand )
{
mRubberBand->setToCanvasRectangle( mZoomRect );
mRubberBand->show();
if ( mZoomOut )
{
mRubberBand->hide();
}
else
{
mRubberBand->setToCanvasRectangle( mZoomRect );
mRubberBand->show();
}
}
}

@@ -84,21 +92,18 @@ void QgsMapToolZoom::canvasReleaseEvent( QgsMapMouseEvent *e )
if ( e->button() != Qt::LeftButton )
return;

bool zoomOut = mZoomOut;
if ( e->modifiers() & Qt::AltModifier )
zoomOut = !zoomOut;

// We are not really dragging in this case. This is sometimes caused by
// a pen based computer reporting a press, move, and release, all the
// one point.
if ( !mDragging || ( mZoomRect.topLeft() - mZoomRect.bottomRight() ).manhattanLength() < mMinPixelZoom )
bool tooShort = ( mZoomRect.topLeft() - mZoomRect.bottomRight() ).manhattanLength() < mMinPixelZoom;
if ( !mDragging || tooShort || mZoomOut )
{
mDragging = false;
delete mRubberBand;
mRubberBand = nullptr;

// change to zoom in/out by the default multiple
mCanvas->zoomWithCenter( e->x(), e->y(), !zoomOut );
mCanvas->zoomWithCenter( e->x(), e->y(), !mZoomOut );
}
else
{
@@ -124,7 +129,7 @@ void QgsMapToolZoom::canvasReleaseEvent( QgsMapMouseEvent *e )
const QgsMapToPixel *m2p = mCanvas->getCoordinateTransform();
QgsPointXY c = m2p->toMapCoordinates( mZoomRect.center() );

mCanvas->zoomByFactor( zoomOut ? 1.0 / sf : sf, &c );
mCanvas->zoomByFactor( mZoomOut ? 1.0 / sf : sf, &c );

mCanvas->refresh();
}
@@ -137,3 +142,31 @@ void QgsMapToolZoom::deactivate()

QgsMapTool::deactivate();
}

void QgsMapToolZoom::updateCursor()
{
setCursor( mZoomOut ? mZoomOutCursor : mZoomInCursor );
}

void QgsMapToolZoom::keyPressEvent( QKeyEvent *e )
{
if ( e->key() == Qt::Key_Alt )
{
mZoomOut = !mZoomOut;
updateCursor();
}
}

void QgsMapToolZoom::keyReleaseEvent( QKeyEvent *e )
{
// key press events are not caught wile the mouse is pressed
// so we can't mess if we are already dragging
// we need to go back to native state, as it cannot be determine
// (since the press event is not detected while mouse is pressed)

if ( e->key() == Qt::Key_Alt )
{
mZoomOut = mNativeZoomOut;
updateCursor();
}
}
@@ -40,6 +40,8 @@ class GUI_EXPORT QgsMapToolZoom : public QgsMapTool
void canvasMoveEvent( QgsMapMouseEvent *e ) override;
void canvasPressEvent( QgsMapMouseEvent *e ) override;
void canvasReleaseEvent( QgsMapMouseEvent *e ) override;
void keyPressEvent( QKeyEvent *e ) override;
void keyReleaseEvent( QKeyEvent *e ) override;
void deactivate() override;

protected:
@@ -50,11 +52,18 @@ class GUI_EXPORT QgsMapToolZoom : public QgsMapTool

//! indicates whether we're zooming in or out
bool mZoomOut;
//! native tool
bool mNativeZoomOut;

//! Flag to indicate a map canvas drag operation is taking place
bool mDragging;

QgsRubberBand *mRubberBand = nullptr;

QCursor mZoomOutCursor;
QCursor mZoomInCursor;

void updateCursor();
};

#endif

0 comments on commit 5fd6f68

Please sign in to comment.
You can’t perform that action at this time.