Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 5fd6f68
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
10 changes: 10 additions & 0 deletions python/gui/auto_generated/qgsmaptoolzoom.sip.in
Expand Up @@ -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
};

/************************************************************************
Expand Down
57 changes: 45 additions & 12 deletions src/gui/qgsmaptoolzoom.cpp
Expand Up @@ -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()
Expand All @@ -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();
}
}
}

Expand All @@ -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
{
Expand All @@ -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();
}
Expand All @@ -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();
}
}
9 changes: 9 additions & 0 deletions src/gui/qgsmaptoolzoom.h
Expand Up @@ -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:
Expand All @@ -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.