[api] Add method for PyQGIS QgsMapTool subclasses to set the tool name

Since PyQGIS can't access protected member variables, we need an
explicit setter for this variable.
This commit is contained in:
Nyall Dawson 2021-06-15 11:12:27 +10:00
parent bb82807623
commit 57700196cd
3 changed files with 24 additions and 0 deletions

View File

@ -170,6 +170,8 @@ returns pointer to the tool's map canvas
%Docstring %Docstring
Emit map tool changed with the old tool Emit map tool changed with the old tool
.. seealso:: :py:func:`setToolName`
.. versionadded:: 2.3 .. versionadded:: 2.3
%End %End
@ -305,6 +307,15 @@ Transforms a ``rect`` from map coordinates to ``layer`` coordinates.
Transforms a ``point`` from map coordinates to screen coordinates. Transforms a ``point`` from map coordinates to screen coordinates.
%End %End
void setToolName( const QString &name );
%Docstring
Sets the tool's ``name``.
.. seealso:: :py:func:`toolName`
.. versionadded:: 3.20
%End

View File

@ -76,6 +76,10 @@ QPoint QgsMapTool::toCanvasCoordinates( const QgsPointXY &point ) const
return QPoint( std::round( x ), std::round( y ) ); return QPoint( std::round( x ), std::round( y ) );
} }
void QgsMapTool::setToolName( const QString &name )
{
mToolName = name;
}
void QgsMapTool::activate() void QgsMapTool::activate()
{ {

View File

@ -176,6 +176,7 @@ class GUI_EXPORT QgsMapTool : public QObject
/** /**
* Emit map tool changed with the old tool * Emit map tool changed with the old tool
* \see setToolName()
* \since QGIS 2.3 * \since QGIS 2.3
*/ */
QString toolName() { return mToolName; } QString toolName() { return mToolName; }
@ -284,6 +285,14 @@ class GUI_EXPORT QgsMapTool : public QObject
//! Transforms a \a point from map coordinates to screen coordinates. //! Transforms a \a point from map coordinates to screen coordinates.
QPoint toCanvasCoordinates( const QgsPointXY &point ) const; QPoint toCanvasCoordinates( const QgsPointXY &point ) const;
/**
* Sets the tool's \a name.
*
* \see toolName()
* \since QGIS 3.20
*/
void setToolName( const QString &name );
//! The pointer to the map canvas //! The pointer to the map canvas
QgsMapCanvas *mCanvas = nullptr; QgsMapCanvas *mCanvas = nullptr;