Skip to content

Commit

Permalink
Add QgsMapTool::canvasToolTipEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
manisandro authored and nyalldawson committed Jun 18, 2021
1 parent 39226e7 commit 9553f06
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/gui/auto_generated/qgsmapcanvas.sip.in
Expand Up @@ -1303,6 +1303,8 @@ Can be used to extend the context menu.

virtual void dragEnterEvent( QDragEnterEvent *e );

virtual bool viewportEvent( QEvent *event );


void moveCanvasContents( bool reset = false );
%Docstring
Expand Down
8 changes: 8 additions & 0 deletions python/gui/auto_generated/qgsmaptool.sip.in
Expand Up @@ -108,6 +108,14 @@ Key event for overriding. Default implementation does nothing.
virtual bool gestureEvent( QGestureEvent *e );
%Docstring
gesture event for overriding. Default implementation does nothing.
%End

virtual bool canvasToolTipEvent( QHelpEvent *e );
%Docstring
Tooltip event for overriding. Default implementation does nothing.
Returns whether the event was handled by the tool and should not be propagated further.

.. versionadded:: 3.22
%End

void setAction( QAction *action );
Expand Down
9 changes: 9 additions & 0 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -2796,6 +2796,15 @@ void QgsMapCanvas::dragEnterEvent( QDragEnterEvent *event )
}
}

bool QgsMapCanvas::viewportEvent( QEvent *event )
{
if ( event->type() == QEvent::ToolTip && mMapTool && mMapTool->canvasToolTipEvent( qgis::down_cast<QHelpEvent *>( event ) ) )
{
return true;
}
return QGraphicsView::viewportEvent( event );
}

void QgsMapCanvas::mapToolDestroyed()
{
QgsDebugMsgLevel( QStringLiteral( "maptool destroyed" ), 2 );
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -1174,6 +1174,7 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView, public QgsExpressionContex
void resizeEvent( QResizeEvent *e ) override;
void paintEvent( QPaintEvent *e ) override;
void dragEnterEvent( QDragEnterEvent *e ) override;
bool viewportEvent( QEvent *event ) override;

//! called when panning is in action, reset indicates end of panning
void moveCanvasContents( bool reset = false );
Expand Down
6 changes: 6 additions & 0 deletions src/gui/qgsmaptool.cpp
Expand Up @@ -196,6 +196,12 @@ bool QgsMapTool::gestureEvent( QGestureEvent *e )
return true;
}

bool QgsMapTool::canvasToolTipEvent( QHelpEvent *e )
{
Q_UNUSED( e )
return false;
}

QgsMapCanvas *QgsMapTool::canvas() const
{
return mCanvas;
Expand Down
7 changes: 7 additions & 0 deletions src/gui/qgsmaptool.h
Expand Up @@ -133,6 +133,13 @@ class GUI_EXPORT QgsMapTool : public QObject
//! gesture event for overriding. Default implementation does nothing.
virtual bool gestureEvent( QGestureEvent *e );

/**
* Tooltip event for overriding. Default implementation does nothing.
* Returns whether the event was handled by the tool and should not be propagated further.
* \since QGIS 3.22
*/
virtual bool canvasToolTipEvent( QHelpEvent *e );

/**
* Use this to associate a QAction to this maptool. Then when the setMapTool
* method of mapcanvas is called the action state will be set to on.
Expand Down
27 changes: 27 additions & 0 deletions tests/src/gui/testqgsmapcanvas.cpp
Expand Up @@ -42,6 +42,20 @@ class QgsMapToolTest : public QgsMapTool // clazy:exclude=missing-qobject-macro
public:
QgsMapToolTest( QgsMapCanvas *canvas ) : QgsMapTool( canvas ) {}

bool canvasToolTipEvent( QHelpEvent *e ) override
{
Q_UNUSED( e );
mGotTooltipEvent = true;
return true;
}
bool gotTooltipEvent() const
{
return mGotTooltipEvent;
}

private:
bool mGotTooltipEvent = false;

};

class TestQgsMapCanvas : public QObject
Expand All @@ -64,6 +78,7 @@ class TestQgsMapCanvas : public QObject
void testShiftZoom();
void testDragDrop();
void testZoomResolutions();
void testTooltipEvent();

private:
QgsMapCanvas *mCanvas = nullptr;
Expand Down Expand Up @@ -538,5 +553,17 @@ void TestQgsMapCanvas::testZoomResolutions()
QCOMPARE( mCanvas->zoomResolutions(), resolutions );
}

void TestQgsMapCanvas::testTooltipEvent()
{
QgsMapToolTest mapTool( mCanvas );
mCanvas->setMapTool( &mapTool );

QHelpEvent helpEvent( QEvent::ToolTip, QPoint( 10, 10 ), QPoint( 10, 10 ) );

QApplication::sendEvent( mCanvas->viewport(), &helpEvent );

QVERIFY( mapTool.gotTooltipEvent() );
}

QGSTEST_MAIN( TestQgsMapCanvas )
#include "testqgsmapcanvas.moc"

0 comments on commit 9553f06

Please sign in to comment.