Skip to content

Commit

Permalink
Handle OperationResults in add part map tool
Browse files Browse the repository at this point in the history
  • Loading branch information
uclaros authored and nyalldawson committed Mar 31, 2021
1 parent 669db38 commit 404d2ff
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
65 changes: 35 additions & 30 deletions src/app/qgsmaptooladdpart.cpp
Expand Up @@ -82,25 +82,13 @@ void QgsMapToolAddPart::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
return;
}

bool isGeometryEmpty = false;
if ( vlayer->selectedFeatureCount() > 0 )
{
// be efficient here - only grab the first selected feature if there's a selection, don't
// fetch all the other features which we don't require.
QgsFeatureIterator selectedFeatures = vlayer->getSelectedFeatures();
QgsFeature firstSelectedFeature;
if ( selectedFeatures.nextFeature( firstSelectedFeature ) )
if ( firstSelectedFeature.geometry().isNull() )
isGeometryEmpty = true;
}

if ( !checkSelection() )
{
stopCapturing();
return;
}

int errorCode = 0;
QgsGeometry::OperationResult errorCode = QgsGeometry::Success;
switch ( mode() )
{
case CapturePoint:
Expand Down Expand Up @@ -191,7 +179,7 @@ void QgsMapToolAddPart::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
case QgsProject::AvoidIntersectionsMode::AllowIntersections:
break;
}
if ( avoidIntersectionsLayers.size() > 0 )
if ( !avoidIntersectionsLayers.isEmpty() )
{
geom->avoidIntersections( avoidIntersectionsLayers );
}
Expand All @@ -217,14 +205,14 @@ void QgsMapToolAddPart::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
break;
default:
Q_ASSERT( !"invalid capture mode" );
errorCode = 6;
errorCode = QgsGeometry::OperationResult::AddPartSelectedGeometryNotFound;
break;
}

QString errorMessage;
switch ( errorCode )
{
case 0:
case QgsGeometry::OperationResult::Success:
{
// remove previous message
emit messageDiscarded();
Expand All @@ -240,37 +228,41 @@ void QgsMapToolAddPart::cadCanvasReleaseEvent( QgsMapMouseEvent *e )

vlayer->triggerRepaint();

if ( ( !isGeometryEmpty ) && QgsWkbTypes::isSingleType( vlayer->wkbType() ) )
{
emit messageEmitted( tr( "Add part: Feature geom is single part and you've added more than one" ), Qgis::Warning );
}

return;
}

case 1:
case QgsGeometry::OperationResult::InvalidInputGeometryType:
errorMessage = tr( "New part's geometry is empty or invalid." );
break;

case QgsGeometry::OperationResult::AddPartNotMultiGeometry:
errorMessage = tr( "Selected feature is not multi part." );
break;

case 2:
case QgsGeometry::OperationResult::AddRingNotValid:
errorMessage = tr( "New part's geometry is not valid." );
break;

case 3:
case QgsGeometry::OperationResult::AddRingCrossesExistingRings:
errorMessage = tr( "New polygon ring not disjoint with existing polygons." );
break;

case 4:
errorMessage = tr( "No feature selected. Please select a feature with the selection tool or in the attribute table" );
case QgsGeometry::OperationResult::SelectionIsEmpty:
errorMessage = tr( "No feature selected. Please select a feature with the selection tool or in the attribute table." );
break;

case 5:
case QgsGeometry::OperationResult::SelectionIsGreaterThanOne:
errorMessage = tr( "Several features are selected. Please select only one feature to which an island should be added." );
break;

case 6:
case QgsGeometry::OperationResult::AddPartSelectedGeometryNotFound:
errorMessage = tr( "Selected geometry could not be found" );
break;

default:
// Should not reach here
// Other OperationResults should not be returned by addPart
errorMessage = tr( "Unexpected OperationResult: %1" ).arg( errorCode );
}

emit messageEmitted( errorMessage, Qgis::Warning );
Expand Down Expand Up @@ -298,11 +290,24 @@ bool QgsMapToolAddPart::checkSelection()
QString selectionErrorMsg;
if ( nSelectedFeatures < 1 )
{
selectionErrorMsg = tr( "No feature selected. Please select a feature with the selection tool or in the attribute table" );
selectionErrorMsg = tr( "No feature selected. Please select a feature with the selection tool or in the attribute table." );
}
else if ( nSelectedFeatures > 1 )
{
selectionErrorMsg = tr( "Several features are selected. Please select only one feature to which an part should be added." );
selectionErrorMsg = tr( "Several features are selected. Please select only one feature to which a part should be added." );
}
else
{
// Only one selected feature
// For single-type layers only allow feaetures without geometry
QgsFeatureIterator selectedFeatures = vlayer->getSelectedFeatures();
QgsFeature selectedFeature;
selectedFeatures.nextFeature( selectedFeature );
if ( QgsWkbTypes::isSingleType( vlayer->wkbType() ) &&
selectedFeature.geometry().constGet() )
{
selectionErrorMsg = tr( "This layer does not support multipart geometries." );
}
}

if ( !selectionErrorMsg.isEmpty() )
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptooladdpart.h
Expand Up @@ -32,6 +32,6 @@ class APP_EXPORT QgsMapToolAddPart : public QgsMapToolCapture
void activate() override;

private:
//! Check if there is any feature selected
//! Check if there is any feature selected and the layer supports adding the part
bool checkSelection();
};

0 comments on commit 404d2ff

Please sign in to comment.