Skip to content
Permalink
Browse files
Add QgsMapTool::canvasToolTipEvent
  • Loading branch information
manisandro authored and nyalldawson committed Jun 18, 2021
1 parent 39226e7 commit 9553f06700c476afb438b5fd8adff2dd5e5c9e5c
@@ -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
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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;
@@ -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.
@@ -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
@@ -64,6 +78,7 @@ class TestQgsMapCanvas : public QObject
void testShiftZoom();
void testDragDrop();
void testZoomResolutions();
void testTooltipEvent();

private:
QgsMapCanvas *mCanvas = nullptr;
@@ -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.