Skip to content

Commit

Permalink
Add easy way for QgsMapToolAdvancedDigitizing to show snapping indicator
Browse files Browse the repository at this point in the history
Instead of requiring all subclasses to manually implement this logic,
add QgsMapToolAdvancedDigitizing::setUseSnappingIndicator so that
the base class takes care of this for us
  • Loading branch information
nyalldawson committed Jul 16, 2024
1 parent ba2ff0c commit cd9b2dc
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Creates an advanced digitizing maptool
:param canvas: The map canvas on which the tool works
:param cadDockWidget: The cad dock widget which will be used to adjust mouse events
%End
~QgsMapToolAdvancedDigitizing();

virtual void canvasPressEvent( QgsMapMouseEvent *e );

Expand Down Expand Up @@ -90,6 +91,9 @@ may keep advanced digitizing allowed all the time.

If ``True`` is returned, that does not mean that advanced digitizing is actually active,
because it is up to the user to enable/disable it when it is allowed.

The default is that advanced digitizing is allowed.

\sa :py:func:`~QgsMapToolAdvancedDigitizing.setAdvancedDigitizingAllowed`
%End

Expand All @@ -99,7 +103,21 @@ Returns whether mouse events (press/move/release) should automatically try to sn
(according to the snapping configuration of map canvas) before passing the mouse coordinates
to the tool. This may be desirable default behavior for some map tools, but not for other map tools.
It is therefore possible to configure the behavior by the map tool.

The default is that auto snapping is enabled.

\sa :py:func:`~QgsMapToolAdvancedDigitizing.isAutoSnapEnabled`
%End

bool useSnappingIndicator() const;
%Docstring
Returns whether the snapping indicator should automatically be used.

The default is that a snap indicator is not used.

.. seealso:: :py:func:`setUseSnappingIndicator`

.. versionadded:: 3.40
%End

protected:
Expand All @@ -108,16 +126,33 @@ It is therefore possible to configure the behavior by the map tool.
%Docstring
Sets whether functionality of advanced digitizing dock widget is currently allowed.
This method is protected because it should be a decision of the map tool and not from elsewhere.

The default is that advanced digitizing is allowed.

\sa :py:func:`~QgsMapToolAdvancedDigitizing.isAdvancedDigitizingAllowed`
%End

void setAutoSnapEnabled( bool enabled );
%Docstring
Sets whether mouse events (press/move/release) should automatically try to snap mouse position
This method is protected because it should be a decision of the map tool and not from elsewhere.

The default is that auto snapping is enabled.

\sa :py:func:`~QgsMapToolAdvancedDigitizing.isAutoSnapEnabled`
%End

void setUseSnappingIndicator( bool enabled );
%Docstring
Sets whether a snapping indicator should automatically be used.

The default is that a snap indicator is not used.

.. seealso:: :py:func:`useSnappingIndicator`

.. versionadded:: 3.40
%End



public:
Expand Down
35 changes: 35 additions & 0 deletions python/gui/auto_generated/qgsmaptooladvanceddigitizing.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Creates an advanced digitizing maptool
:param canvas: The map canvas on which the tool works
:param cadDockWidget: The cad dock widget which will be used to adjust mouse events
%End
~QgsMapToolAdvancedDigitizing();

virtual void canvasPressEvent( QgsMapMouseEvent *e );

Expand Down Expand Up @@ -90,6 +91,9 @@ may keep advanced digitizing allowed all the time.

If ``True`` is returned, that does not mean that advanced digitizing is actually active,
because it is up to the user to enable/disable it when it is allowed.

The default is that advanced digitizing is allowed.

\sa :py:func:`~QgsMapToolAdvancedDigitizing.setAdvancedDigitizingAllowed`
%End

Expand All @@ -99,7 +103,21 @@ Returns whether mouse events (press/move/release) should automatically try to sn
(according to the snapping configuration of map canvas) before passing the mouse coordinates
to the tool. This may be desirable default behavior for some map tools, but not for other map tools.
It is therefore possible to configure the behavior by the map tool.

The default is that auto snapping is enabled.

\sa :py:func:`~QgsMapToolAdvancedDigitizing.isAutoSnapEnabled`
%End

bool useSnappingIndicator() const;
%Docstring
Returns whether the snapping indicator should automatically be used.

The default is that a snap indicator is not used.

.. seealso:: :py:func:`setUseSnappingIndicator`

.. versionadded:: 3.40
%End

protected:
Expand All @@ -108,16 +126,33 @@ It is therefore possible to configure the behavior by the map tool.
%Docstring
Sets whether functionality of advanced digitizing dock widget is currently allowed.
This method is protected because it should be a decision of the map tool and not from elsewhere.

The default is that advanced digitizing is allowed.

\sa :py:func:`~QgsMapToolAdvancedDigitizing.isAdvancedDigitizingAllowed`
%End

void setAutoSnapEnabled( bool enabled );
%Docstring
Sets whether mouse events (press/move/release) should automatically try to snap mouse position
This method is protected because it should be a decision of the map tool and not from elsewhere.

The default is that auto snapping is enabled.

\sa :py:func:`~QgsMapToolAdvancedDigitizing.isAutoSnapEnabled`
%End

void setUseSnappingIndicator( bool enabled );
%Docstring
Sets whether a snapping indicator should automatically be used.

The default is that a snap indicator is not used.

.. seealso:: :py:func:`useSnappingIndicator`

.. versionadded:: 3.40
%End



public:
Expand Down
27 changes: 27 additions & 0 deletions src/gui/qgsmaptooladvanceddigitizing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ QgsMapToolAdvancedDigitizing::QgsMapToolAdvancedDigitizing( QgsMapCanvas *canvas
connect( canvas, &QgsMapCanvas::currentLayerChanged, this, &QgsMapToolAdvancedDigitizing::onCurrentLayerChanged );
}

QgsMapToolAdvancedDigitizing::~QgsMapToolAdvancedDigitizing() = default;

void QgsMapToolAdvancedDigitizing::canvasPressEvent( QgsMapMouseEvent *e )
{
if ( isAdvancedDigitizingAllowed() && mCadDockWidget->cadEnabled() )
Expand Down Expand Up @@ -115,6 +117,11 @@ void QgsMapToolAdvancedDigitizing::canvasMoveEvent( QgsMapMouseEvent *e )
mSnapToGridCanvasItem->setPoint( e->mapPoint() );
}

if ( mSnapIndicator )
{
mSnapIndicator->setMatch( e->mapPointMatch() );
}

cadCanvasMoveEvent( e );
}

Expand All @@ -140,13 +147,33 @@ void QgsMapToolAdvancedDigitizing::deactivate()
mCadDockWidget->disable();
delete mSnapToGridCanvasItem;
mSnapToGridCanvasItem = nullptr;

if ( mSnapIndicator )
mSnapIndicator->setMatch( QgsPointLocator::Match() );
}

QgsMapLayer *QgsMapToolAdvancedDigitizing::layer() const
{
return canvas()->currentLayer();
}

bool QgsMapToolAdvancedDigitizing::useSnappingIndicator() const
{
return static_cast< bool >( mSnapIndicator.get() );
}

void QgsMapToolAdvancedDigitizing::setUseSnappingIndicator( bool enabled )
{
if ( enabled && !mSnapIndicator )
{
mSnapIndicator = std::make_unique< QgsSnapIndicator >( mCanvas );
}
else if ( !enabled && mSnapIndicator )
{
mSnapIndicator.reset();
}
}

void QgsMapToolAdvancedDigitizing::cadPointChanged( const QgsPointXY &point )
{
Q_UNUSED( point )
Expand Down
37 changes: 37 additions & 0 deletions src/gui/qgsmaptooladvanceddigitizing.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

#include "qgsmaptooledit.h"
#include "qgis_gui.h"
#include <memory>

class QgsMapMouseEvent;
class QgsAdvancedDigitizingDockWidget;
class QgsSnapToGridCanvasItem;
class QgsSnapIndicator;

/**
* \ingroup gui
Expand All @@ -44,6 +46,7 @@ class GUI_EXPORT QgsMapToolAdvancedDigitizing : public QgsMapToolEdit
* \param cadDockWidget The cad dock widget which will be used to adjust mouse events
*/
explicit QgsMapToolAdvancedDigitizing( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *cadDockWidget );
~QgsMapToolAdvancedDigitizing() override;

//! Catch the mouse press event, filters it, transforms it to map coordinates and send it to virtual method
void canvasPressEvent( QgsMapMouseEvent *e ) override;
Expand Down Expand Up @@ -83,6 +86,9 @@ class GUI_EXPORT QgsMapToolAdvancedDigitizing : public QgsMapToolEdit
*
* If TRUE is returned, that does not mean that advanced digitizing is actually active,
* because it is up to the user to enable/disable it when it is allowed.
*
* The default is that advanced digitizing is allowed.
*
* \sa setAdvancedDigitizingAllowed()
*/
bool isAdvancedDigitizingAllowed() const { return mAdvancedDigitizingAllowed; }
Expand All @@ -92,26 +98,55 @@ class GUI_EXPORT QgsMapToolAdvancedDigitizing : public QgsMapToolEdit
* (according to the snapping configuration of map canvas) before passing the mouse coordinates
* to the tool. This may be desirable default behavior for some map tools, but not for other map tools.
* It is therefore possible to configure the behavior by the map tool.
*
* The default is that auto snapping is enabled.
*
* \sa isAutoSnapEnabled()
*/
bool isAutoSnapEnabled() const { return mAutoSnapEnabled; }

/**
* Returns whether the snapping indicator should automatically be used.
*
* The default is that a snap indicator is not used.
*
* \see setUseSnappingIndicator()
* \since QGIS 3.40
*/
bool useSnappingIndicator() const;

protected:

/**
* Sets whether functionality of advanced digitizing dock widget is currently allowed.
* This method is protected because it should be a decision of the map tool and not from elsewhere.
*
* The default is that advanced digitizing is allowed.
*
* \sa isAdvancedDigitizingAllowed()
*/
void setAdvancedDigitizingAllowed( bool allowed ) { mAdvancedDigitizingAllowed = allowed; }

/**
* Sets whether mouse events (press/move/release) should automatically try to snap mouse position
* This method is protected because it should be a decision of the map tool and not from elsewhere.
*
* The default is that auto snapping is enabled.
*
* \sa isAutoSnapEnabled()
*/
void setAutoSnapEnabled( bool enabled ) { mAutoSnapEnabled = enabled; }

/**
* Sets whether a snapping indicator should automatically be used.
*
* The default is that a snap indicator is not used.
*
* \see useSnappingIndicator()
* \since QGIS 3.40
*/
void setUseSnappingIndicator( bool enabled );


QgsAdvancedDigitizingDockWidget *mCadDockWidget = nullptr;

Expand Down Expand Up @@ -188,6 +223,8 @@ class GUI_EXPORT QgsMapToolAdvancedDigitizing : public QgsMapToolEdit
//! Whether to snap to grid before passing coordinates to cadCanvas*Event()
bool mSnapToLayerGridEnabled = true;
QgsSnapToGridCanvasItem *mSnapToGridCanvasItem = nullptr;

std::unique_ptr<QgsSnapIndicator> mSnapIndicator;
};

#endif // QGSMAPTOOLADVANCEDDIGITIZE_H

0 comments on commit cd9b2dc

Please sign in to comment.