Skip to content

Commit

Permalink
QgsVectorLayer code cleaning use enums instead of int
Browse files Browse the repository at this point in the history
It was one of the TODOs for QGIS 3
  • Loading branch information
elpaso committed Nov 23, 2017
1 parent d1cf7e6 commit def85fa
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 170 deletions.
5 changes: 4 additions & 1 deletion python/core/geometry/qgsgeometry.sip
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ class QgsGeometry
Success,
NothingHappened,
InvalidBaseGeometry,
InvalidInput,
InvalidInputGeometryType,
SelectionIsEmpty,
SelectionIsGreaterThanOne,
GeometryEngineError,
LayerNotEditable,
AddPartSelectedGeometryNotFound,
AddPartNotMultiGeometry,
AddRingNotClosed,
Expand Down
54 changes: 40 additions & 14 deletions python/core/qgsvectorlayer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -1043,32 +1043,50 @@ Return the provider type for this layer
:rtype: bool
%End

int addRing( const QVector<QgsPointXY> &ring, QgsFeatureId *featureId = 0 );
QgsGeometry::OperationResult addRing( const QVector<QgsPointXY> &ring, QgsFeatureId *featureId = 0 );
%Docstring
:rtype: int
Adds a ring to polygon/multipolygon features
\param ring ring to add
\param featureId if specified, feature ID for feature ring was added to will be stored in this parameter
:return: QgsGeometry.OperationResult
:rtype: QgsGeometry.OperationResult
%End

int addRing( QgsCurve *ring /Transfer/, QgsFeatureId *featureId = 0 ) /PyName=addCurvedRing/;
QgsGeometry::OperationResult addRing( QgsCurve *ring /Transfer/, QgsFeatureId *featureId = 0 ) /PyName=addCurvedRing/;
%Docstring
:rtype: int
Adds a ring to polygon/multipolygon features (takes ownership)
\param ring ring to add
\param featureId if specified, feature ID for feature ring was added to will be stored in this parameter
:return: QgsGeometry.OperationResult
.. note::

available in Python as addCurvedRing
:rtype: QgsGeometry.OperationResult
%End

int addPart( const QList<QgsPointXY> &ring );
QgsGeometry::OperationResult addPart( const QList<QgsPointXY> &ring );
%Docstring
:rtype: int
Adds a new part polygon to a multipart feature
:return: QgsGeometry.OperationResult
:rtype: QgsGeometry.OperationResult
%End

int addPart( const QgsPointSequence &ring ) /PyName=addPartV2/;
QgsGeometry::OperationResult addPart( const QgsPointSequence &ring ) /PyName=addPartV2/;
%Docstring
:rtype: int
Adds a new part polygon to a multipart feature
:return: QgsGeometry.OperationResult
.. note::

available in Python bindings as addPartV2
:rtype: QgsGeometry.OperationResult
%End

int addPart( QgsCurve *ring /Transfer/ ) /PyName=addCurvedPart/;
QgsGeometry::OperationResult addPart( QgsCurve *ring /Transfer/ ) /PyName=addCurvedPart/;
%Docstring
.. note::

available in Python as addCurvedPart
:rtype: int
:rtype: QgsGeometry.OperationResult
%End

int translateFeature( QgsFeatureId featureId, double dx, double dy );
Expand All @@ -1081,14 +1099,22 @@ Return the provider type for this layer
:rtype: int
%End

int splitParts( const QVector<QgsPointXY> &splitLine, bool topologicalEditing = false );
QgsGeometry::OperationResult splitParts( const QVector<QgsPointXY> &splitLine, bool topologicalEditing = false );
%Docstring
:rtype: int
Splits parts cut by the given line
\param splitLine line that splits the layer features
\param topologicalEditing true if topological editing is enabled
:return: QgsGeometry.OperationResult
:rtype: QgsGeometry.OperationResult
%End

int splitFeatures( const QVector<QgsPointXY> &splitLine, bool topologicalEditing = false );
QgsGeometry::OperationResult splitFeatures( const QVector<QgsPointXY> &splitLine, bool topologicalEditing = false );
%Docstring
:rtype: int
Splits features cut by the given line
\param splitLine line that splits the layer features
\param topologicalEditing true if topological editing is enabled
:return: QgsGeometry.OperationResult
:rtype: QgsGeometry.OperationResult
%End

int addTopologicalPoints( const QgsGeometry &geom );
Expand Down
18 changes: 10 additions & 8 deletions src/app/qgsmaptoolfillring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,30 @@ void QgsMapToolFillRing::cadCanvasReleaseEvent( QgsMapMouseEvent *e )

QVector< QgsPointXY > pointList = points();

int addRingReturnCode = vlayer->addRing( pointList, &fid );
if ( addRingReturnCode != 0 )
QgsGeometry::OperationResult addRingReturnCode = vlayer->addRing( pointList, &fid );

// AP: this is all dead code:
//todo: open message box to communicate errors
if ( addRingReturnCode != QgsGeometry::OperationResult::Success )
{
QString errorMessage;
//todo: open message box to communicate errors
if ( addRingReturnCode == 1 )
if ( addRingReturnCode == QgsGeometry::OperationResult::InvalidInputGeometryType )
{
errorMessage = tr( "a problem with geometry type occurred" );
}
else if ( addRingReturnCode == 2 )
else if ( addRingReturnCode == QgsGeometry::OperationResult::AddRingNotClosed )
{
errorMessage = tr( "the inserted Ring is not closed" );
}
else if ( addRingReturnCode == 3 )
else if ( addRingReturnCode == QgsGeometry::OperationResult::AddRingNotValid )
{
errorMessage = tr( "the inserted Ring is not a valid geometry" );
}
else if ( addRingReturnCode == 4 )
else if ( addRingReturnCode == QgsGeometry::OperationResult::AddRingCrossesExistingRings )
{
errorMessage = tr( "the inserted Ring crosses existing rings" );
}
else if ( addRingReturnCode == 5 )
else if ( addRingReturnCode == QgsGeometry::OperationResult::AddRingNotInExistingFeature )
{
errorMessage = tr( "the inserted Ring is not contained in a feature" );
}
Expand Down
10 changes: 5 additions & 5 deletions src/app/qgsmaptoolsplitfeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,33 +93,33 @@ void QgsMapToolSplitFeatures::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
//bring up dialog if a split was not possible (polygon) or only done once (line)
int topologicalEditing = QgsProject::instance()->topologicalEditing();
vlayer->beginEditCommand( tr( "Features split" ) );
int returnCode = vlayer->splitFeatures( points(), topologicalEditing );
QgsGeometry::OperationResult returnCode = vlayer->splitFeatures( points(), topologicalEditing );
vlayer->endEditCommand();
if ( returnCode == 4 )
if ( returnCode == QgsGeometry::OperationResult::NothingHappened )
{
QgisApp::instance()->messageBar()->pushMessage(
tr( "No features were split" ),
tr( "If there are selected features, the split tool only applies to those. If you would like to split all features under the split line, clear the selection." ),
QgsMessageBar::WARNING,
QgisApp::instance()->messageTimeout() );
}
else if ( returnCode == 3 )
else if ( returnCode == QgsGeometry::OperationResult::GeometryEngineError )
{
QgisApp::instance()->messageBar()->pushMessage(
tr( "No feature split done" ),
tr( "Cut edges detected. Make sure the line splits features into multiple parts." ),
QgsMessageBar::WARNING,
QgisApp::instance()->messageTimeout() );
}
else if ( returnCode == 7 )
else if ( returnCode == QgsGeometry::OperationResult::InvalidBaseGeometry )
{
QgisApp::instance()->messageBar()->pushMessage(
tr( "No feature split done" ),
tr( "The geometry is invalid. Please repair before trying to split it." ),
QgsMessageBar::WARNING,
QgisApp::instance()->messageTimeout() );
}
else if ( returnCode != 0 )
else if ( returnCode != QgsGeometry::OperationResult::Success )
{
//several intersections but only one split (most likely line)
QgisApp::instance()->messageBar()->pushMessage(
Expand Down
10 changes: 5 additions & 5 deletions src/app/qgsmaptoolsplitparts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,33 +91,33 @@ void QgsMapToolSplitParts::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
//bring up dialog if a split was not possible (polygon) or only done once (line)
bool topologicalEditing = QgsProject::instance()->topologicalEditing();
vlayer->beginEditCommand( tr( "Parts split" ) );
int returnCode = vlayer->splitParts( points(), topologicalEditing );
QgsGeometry::OperationResult returnCode = vlayer->splitParts( points(), topologicalEditing );
vlayer->endEditCommand();
if ( returnCode == 4 )
if ( returnCode == QgsGeometry::OperationResult::NothingHappened )
{
QgisApp::instance()->messageBar()->pushMessage(
tr( "No parts were split" ),
tr( "If there are selected parts, the split tool only applies to those. If you would like to split all parts under the split line, clear the selection." ),
QgsMessageBar::WARNING,
QgisApp::instance()->messageTimeout() );
}
else if ( returnCode == 3 )
else if ( returnCode == QgsGeometry::OperationResult::GeometryEngineError )
{
QgisApp::instance()->messageBar()->pushMessage(
tr( "No part split done" ),
tr( "Cut edges detected. Make sure the line splits parts into multiple parts." ),
QgsMessageBar::WARNING,
QgisApp::instance()->messageTimeout() );
}
else if ( returnCode == 7 )
else if ( returnCode == QgsGeometry::OperationResult::InvalidBaseGeometry )
{
QgisApp::instance()->messageBar()->pushMessage(
tr( "No part split done" ),
tr( "The geometry is invalid. Please repair before trying to split it." ),
QgsMessageBar::WARNING,
QgisApp::instance()->messageTimeout() );
}
else if ( returnCode != 0 )
else if ( returnCode != QgsGeometry::OperationResult::Success )
{
//several intersections but only one split (most likely line)
QgisApp::instance()->messageBar()->pushMessage(
Expand Down
6 changes: 3 additions & 3 deletions src/core/geometry/qgsgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ QgsGeometry::OperationResult QgsGeometry::addRing( QgsCurve *ring )
std::unique_ptr< QgsCurve > r( ring );
if ( !d->geometry )
{
return InvalidInput;
return InvalidInputGeometryType;
}

detach();
Expand Down Expand Up @@ -816,7 +816,7 @@ QgsGeometry::OperationResult QgsGeometry::splitGeometry( const QVector<QgsPointX
case QgsGeometryEngine::InvalidBaseGeometry:
return QgsGeometry::InvalidBaseGeometry;
case QgsGeometryEngine::InvalidInput:
return QgsGeometry::InvalidInput;
return QgsGeometry::InvalidInputGeometryType;
case QgsGeometryEngine::SplitCannotSplitPoint:
return QgsGeometry::SplitCannotSplitPoint;
case QgsGeometryEngine::NothingHappened:
Expand Down Expand Up @@ -857,7 +857,7 @@ QgsGeometry::OperationResult QgsGeometry::reshapeGeometry( const QgsLineString &
case QgsGeometryEngine::InvalidBaseGeometry:
return InvalidBaseGeometry;
case QgsGeometryEngine::InvalidInput:
return InvalidInput;
return InvalidInputGeometryType;
case QgsGeometryEngine::SplitCannotSplitPoint: // should not happen
return GeometryEngineError;
case QgsGeometryEngine::NothingHappened:
Expand Down
5 changes: 4 additions & 1 deletion src/core/geometry/qgsgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ class CORE_EXPORT QgsGeometry
Success = 0, //!< Operation succeeded
NothingHappened = 1000, //!< Nothing happened, without any error
InvalidBaseGeometry, //!< The base geometry on which the operation is done is invalid or empty
InvalidInput, //!< The input geometry (ring, part, split line, etc.) has not the correct geometry type
InvalidInputGeometryType, //!< The input geometry (ring, part, split line, etc.) has not the correct geometry type
SelectionIsEmpty, //!< No features were selected
SelectionIsGreaterThanOne, //!< More than one features were selected
GeometryEngineError, //!< Geometry engine misses a method implemented or an error occurred in the geometry engine
LayerNotEditable, //!< Cannot edit layer
/* Add part issues */
AddPartSelectedGeometryNotFound, //!< The selected geometry cannot be found
AddPartNotMultiGeometry, //!< The source geometry is not multi
Expand Down
12 changes: 6 additions & 6 deletions src/core/geometry/qgsgeometryeditutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ QgsGeometry::OperationResult QgsGeometryEditUtils::addRing( QgsAbstractGeometry
{
if ( !ring )
{
return QgsGeometry::InvalidInput;
return QgsGeometry::InvalidInputGeometryType;
}

QVector< QgsCurvePolygon * > polygonList;
Expand All @@ -50,7 +50,7 @@ QgsGeometry::OperationResult QgsGeometryEditUtils::addRing( QgsAbstractGeometry
}
else
{
return QgsGeometry::InvalidInput; //not polygon / multipolygon;
return QgsGeometry::InvalidInputGeometryType; //not polygon / multipolygon;
}

//ring must be closed
Expand Down Expand Up @@ -104,7 +104,7 @@ QgsGeometry::OperationResult QgsGeometryEditUtils::addPart( QgsAbstractGeometry

if ( !part )
{
return QgsGeometry::InvalidInput;
return QgsGeometry::InvalidInputGeometryType;
}

//multitype?
Expand Down Expand Up @@ -155,19 +155,19 @@ QgsGeometry::OperationResult QgsGeometryEditUtils::addPart( QgsAbstractGeometry
{
while ( geomCollection->numGeometries() > n )
geomCollection->removeGeometry( n );
return QgsGeometry::InvalidInput;
return QgsGeometry::InvalidInputGeometryType;
}
}
else
{
return QgsGeometry::InvalidInput;
return QgsGeometry::InvalidInputGeometryType;
}
}
else
{
added = geomCollection->addGeometry( part.release() );
}
return added ? QgsGeometry::Success : QgsGeometry::InvalidInput;
return added ? QgsGeometry::Success : QgsGeometry::InvalidInputGeometryType;
}

bool QgsGeometryEditUtils::deleteRing( QgsAbstractGeometry *geom, int ringNum, int partNum )
Expand Down
Loading

0 comments on commit def85fa

Please sign in to comment.