Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #8247 from 3nids/maptoolzoom
fixes to the map tool zoom
  • Loading branch information
3nids committed Oct 21, 2018
2 parents fcf85a1 + f17b1a5 commit 7e3b5f4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 16 deletions.
7 changes: 7 additions & 0 deletions python/gui/auto_generated/qgsmaptool.sip.in
Expand Up @@ -116,6 +116,13 @@ the previously used toolbutton to pop out. *
QAction *action();
%Docstring
Returns associated action with map tool or NULL if no action is associated
%End

bool isActive() const;
%Docstring
Returns if the current map tool active on the map canvas

.. versionadded:: 3.4
%End

void setButton( QAbstractButton *button );
Expand Down
6 changes: 6 additions & 0 deletions python/gui/auto_generated/qgsmaptoolzoom.sip.in
Expand Up @@ -34,13 +34,19 @@ constructor

virtual void canvasReleaseEvent( QgsMapMouseEvent *e );

virtual void keyPressEvent( QKeyEvent *e );

virtual void keyReleaseEvent( QKeyEvent *e );

virtual void deactivate();


protected:





};

/************************************************************************
Expand Down
7 changes: 7 additions & 0 deletions src/gui/qgsmaptool.cpp
Expand Up @@ -130,6 +130,11 @@ QAction *QgsMapTool::action()
return mAction;
}

bool QgsMapTool::isActive() const
{
return mCanvas && mCanvas->mapTool() == this;
}

void QgsMapTool::setButton( QAbstractButton *button )
{
mButton = button;
Expand All @@ -143,6 +148,8 @@ QAbstractButton *QgsMapTool::button()
void QgsMapTool::setCursor( const QCursor &cursor )
{
mCursor = cursor;
if ( isActive() )
mCanvas->setCursor( mCursor );
}


Expand Down
6 changes: 6 additions & 0 deletions src/gui/qgsmaptool.h
Expand Up @@ -137,6 +137,12 @@ class GUI_EXPORT QgsMapTool : public QObject
//! Returns associated action with map tool or NULL if no action is associated
QAction *action();

/**
* Returns if the current map tool active on the map canvas
* \since QGIS 3.4
*/
bool isActive() const;

/**
* Use this to associate a button to this maptool. It has the same meaning
* as setAction() function except it works with a button instead of an QAction. */
Expand Down
55 changes: 39 additions & 16 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 Down Expand Up @@ -84,21 +85,20 @@ 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() ) )
bool tooShort = ( mZoomRect.topLeft() - mZoomRect.bottomRight() ).manhattanLength() < mMinPixelZoom;
if ( !mDragging || tooShort )
{
mDragging = false;
delete mRubberBand;
mRubberBand = nullptr;
}

if ( mDragging )
// change to zoom in/out by the default multiple
mCanvas->zoomWithCenter( e->x(), e->y(), !mZoomOut );
}
else
{
mDragging = false;
delete mRubberBand;
Expand All @@ -122,15 +122,10 @@ 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();
}
else // not dragging
{
// change to zoom in/out by the default multiple
mCanvas->zoomWithCenter( e->x(), e->y(), !zoomOut );
}
}

void QgsMapToolZoom::deactivate()
Expand All @@ -140,3 +135,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();
}
}
12 changes: 12 additions & 0 deletions src/gui/qgsmaptoolzoom.h
Expand Up @@ -40,19 +40,31 @@ 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:
//! stores actual zoom rect
QRect mZoomRect;
// minimum pixel size of diagonal of the zoom rectangle
int mMinPixelZoom = 20;

//! 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;

private:
void updateCursor();
};

#endif

0 comments on commit 7e3b5f4

Please sign in to comment.