Skip to content
Permalink
Browse files

Clear ongoing drawing on map canvas when closing layer's edit session (

…fix #36292)
  • Loading branch information
gacarrillor authored and nyalldawson committed Jun 11, 2020
1 parent f3d473c commit 4af5597e7618ed28ca7aba55be957d1a4eb62429
Showing with 56 additions and 0 deletions.
  1. +1 −0 python/gui/auto_generated/qgsmaptooledit.sip.in
  2. +41 −0 src/gui/qgsmaptooledit.cpp
  3. +14 −0 src/gui/qgsmaptooledit.h
@@ -93,6 +93,7 @@ Display a timed message bar noting the active layer is not vector.
%Docstring
Display a timed message bar noting the active vector layer is not editable.
%End

};

/************************************************************************
@@ -27,6 +27,11 @@
QgsMapToolEdit::QgsMapToolEdit( QgsMapCanvas *canvas )
: QgsMapTool( canvas )
{
if ( mCanvas->project() )
{
connect( mCanvas->project(), &QgsProject::layersAdded, this, &QgsMapToolEdit::connectLayers );
connectLayers( mCanvas->project()->mapLayers().values() ); // Connect existing layers
}
}

double QgsMapToolEdit::defaultZValue() const
@@ -171,3 +176,39 @@ void QgsMapToolEdit::notifyNotEditableLayer()
{
emit messageEmitted( tr( "Layer not editable" ) );
}

void QgsMapToolEdit::connectLayers( const QList<QgsMapLayer *> &layers )
{
for ( QgsMapLayer *layer : layers )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( vlayer )
{
connect( vlayer, &QgsVectorLayer::editingStopped, this, &QgsMapToolEdit::cleanCanvas );
}
}
}

void QgsMapToolEdit::cleanCanvas()
{
if ( editableVectorLayers().isEmpty() )
{
clean();
}
}

QList<QgsVectorLayer *> QgsMapToolEdit::editableVectorLayers()
{
QList<QgsVectorLayer *> editableLayers;
if ( mCanvas->project() )
{
const auto layers = mCanvas->project()->mapLayers().values();
for ( QgsMapLayer *layer : layers )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( vlayer && vlayer->isEditable() && vlayer->isSpatial() )
editableLayers << vlayer;
}
}
return editableLayers;
}
@@ -44,6 +44,16 @@ class GUI_EXPORT QgsMapToolEdit: public QgsMapTool
*/
double defaultZValue() const;

private slots:
//! Vector layers' editingStopped SIGNAL will eventually trigger a clean
void connectLayers( const QList<QgsMapLayer *> &layers );

/**
* Makes sure rubber bands are removed if there
* is no editable layer left in the project
*/
void cleanCanvas();

protected:

//! Returns stroke color for rubber bands (from global settings)
@@ -93,6 +103,10 @@ class GUI_EXPORT QgsMapToolEdit: public QgsMapTool
void notifyNotVectorLayer();
//! Display a timed message bar noting the active vector layer is not editable.
void notifyNotEditableLayer();

private:
//! Returns a list of layers filtered to just editable spatial vector layers
QList<QgsVectorLayer *> editableVectorLayers();
};

#endif

0 comments on commit 4af5597

Please sign in to comment.
You can’t perform that action at this time.