Skip to content
Permalink
Browse files

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 7e3b5f40b895c261d54cede3c37f27ef8bf57041
@@ -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 );
@@ -34,13 +34,19 @@ constructor

virtual void canvasReleaseEvent( QgsMapMouseEvent *e );

virtual void keyPressEvent( QKeyEvent *e );

virtual void keyReleaseEvent( QKeyEvent *e );

virtual void deactivate();


protected:





};

/************************************************************************
@@ -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;
@@ -143,6 +148,8 @@ QAbstractButton *QgsMapTool::button()
void QgsMapTool::setCursor( const QCursor &cursor )
{
mCursor = cursor;
if ( isActive() )
mCanvas->setCursor( mCursor );
}


@@ -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. */
@@ -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()
@@ -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;
@@ -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()
@@ -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();
}
}
@@ -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.
You can’t perform that action at this time.