Skip to content

Commit def85fa

Browse files
committedNov 23, 2017
QgsVectorLayer code cleaning use enums instead of int
It was one of the TODOs for QGIS 3
1 parent d1cf7e6 commit def85fa

12 files changed

+164
-170
lines changed
 

‎python/core/geometry/qgsgeometry.sip

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ class QgsGeometry
5252
Success,
5353
NothingHappened,
5454
InvalidBaseGeometry,
55-
InvalidInput,
55+
InvalidInputGeometryType,
56+
SelectionIsEmpty,
57+
SelectionIsGreaterThanOne,
5658
GeometryEngineError,
59+
LayerNotEditable,
5760
AddPartSelectedGeometryNotFound,
5861
AddPartNotMultiGeometry,
5962
AddRingNotClosed,

‎python/core/qgsvectorlayer.sip

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,32 +1043,50 @@ Return the provider type for this layer
10431043
:rtype: bool
10441044
%End
10451045

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

1051-
int addRing( QgsCurve *ring /Transfer/, QgsFeatureId *featureId = 0 ) /PyName=addCurvedRing/;
1055+
QgsGeometry::OperationResult addRing( QgsCurve *ring /Transfer/, QgsFeatureId *featureId = 0 ) /PyName=addCurvedRing/;
10521056
%Docstring
1053-
:rtype: int
1057+
Adds a ring to polygon/multipolygon features (takes ownership)
1058+
\param ring ring to add
1059+
\param featureId if specified, feature ID for feature ring was added to will be stored in this parameter
1060+
:return: QgsGeometry.OperationResult
1061+
.. note::
1062+
1063+
available in Python as addCurvedRing
1064+
:rtype: QgsGeometry.OperationResult
10541065
%End
10551066

1056-
int addPart( const QList<QgsPointXY> &ring );
1067+
QgsGeometry::OperationResult addPart( const QList<QgsPointXY> &ring );
10571068
%Docstring
1058-
:rtype: int
1069+
Adds a new part polygon to a multipart feature
1070+
:return: QgsGeometry.OperationResult
1071+
:rtype: QgsGeometry.OperationResult
10591072
%End
10601073

1061-
int addPart( const QgsPointSequence &ring ) /PyName=addPartV2/;
1074+
QgsGeometry::OperationResult addPart( const QgsPointSequence &ring ) /PyName=addPartV2/;
10621075
%Docstring
1063-
:rtype: int
1076+
Adds a new part polygon to a multipart feature
1077+
:return: QgsGeometry.OperationResult
1078+
.. note::
1079+
1080+
available in Python bindings as addPartV2
1081+
:rtype: QgsGeometry.OperationResult
10641082
%End
10651083

1066-
int addPart( QgsCurve *ring /Transfer/ ) /PyName=addCurvedPart/;
1084+
QgsGeometry::OperationResult addPart( QgsCurve *ring /Transfer/ ) /PyName=addCurvedPart/;
10671085
%Docstring
10681086
.. note::
10691087

10701088
available in Python as addCurvedPart
1071-
:rtype: int
1089+
:rtype: QgsGeometry.OperationResult
10721090
%End
10731091

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

1084-
int splitParts( const QVector<QgsPointXY> &splitLine, bool topologicalEditing = false );
1102+
QgsGeometry::OperationResult splitParts( const QVector<QgsPointXY> &splitLine, bool topologicalEditing = false );
10851103
%Docstring
1086-
:rtype: int
1104+
Splits parts cut by the given line
1105+
\param splitLine line that splits the layer features
1106+
\param topologicalEditing true if topological editing is enabled
1107+
:return: QgsGeometry.OperationResult
1108+
:rtype: QgsGeometry.OperationResult
10871109
%End
10881110

1089-
int splitFeatures( const QVector<QgsPointXY> &splitLine, bool topologicalEditing = false );
1111+
QgsGeometry::OperationResult splitFeatures( const QVector<QgsPointXY> &splitLine, bool topologicalEditing = false );
10901112
%Docstring
1091-
:rtype: int
1113+
Splits features cut by the given line
1114+
\param splitLine line that splits the layer features
1115+
\param topologicalEditing true if topological editing is enabled
1116+
:return: QgsGeometry.OperationResult
1117+
:rtype: QgsGeometry.OperationResult
10921118
%End
10931119

10941120
int addTopologicalPoints( const QgsGeometry &geom );

‎src/app/qgsmaptoolfillring.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,28 +90,30 @@ void QgsMapToolFillRing::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
9090

9191
QVector< QgsPointXY > pointList = points();
9292

93-
int addRingReturnCode = vlayer->addRing( pointList, &fid );
94-
if ( addRingReturnCode != 0 )
93+
QgsGeometry::OperationResult addRingReturnCode = vlayer->addRing( pointList, &fid );
94+
95+
// AP: this is all dead code:
96+
//todo: open message box to communicate errors
97+
if ( addRingReturnCode != QgsGeometry::OperationResult::Success )
9598
{
9699
QString errorMessage;
97-
//todo: open message box to communicate errors
98-
if ( addRingReturnCode == 1 )
100+
if ( addRingReturnCode == QgsGeometry::OperationResult::InvalidInputGeometryType )
99101
{
100102
errorMessage = tr( "a problem with geometry type occurred" );
101103
}
102-
else if ( addRingReturnCode == 2 )
104+
else if ( addRingReturnCode == QgsGeometry::OperationResult::AddRingNotClosed )
103105
{
104106
errorMessage = tr( "the inserted Ring is not closed" );
105107
}
106-
else if ( addRingReturnCode == 3 )
108+
else if ( addRingReturnCode == QgsGeometry::OperationResult::AddRingNotValid )
107109
{
108110
errorMessage = tr( "the inserted Ring is not a valid geometry" );
109111
}
110-
else if ( addRingReturnCode == 4 )
112+
else if ( addRingReturnCode == QgsGeometry::OperationResult::AddRingCrossesExistingRings )
111113
{
112114
errorMessage = tr( "the inserted Ring crosses existing rings" );
113115
}
114-
else if ( addRingReturnCode == 5 )
116+
else if ( addRingReturnCode == QgsGeometry::OperationResult::AddRingNotInExistingFeature )
115117
{
116118
errorMessage = tr( "the inserted Ring is not contained in a feature" );
117119
}

‎src/app/qgsmaptoolsplitfeatures.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,33 +93,33 @@ void QgsMapToolSplitFeatures::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
9393
//bring up dialog if a split was not possible (polygon) or only done once (line)
9494
int topologicalEditing = QgsProject::instance()->topologicalEditing();
9595
vlayer->beginEditCommand( tr( "Features split" ) );
96-
int returnCode = vlayer->splitFeatures( points(), topologicalEditing );
96+
QgsGeometry::OperationResult returnCode = vlayer->splitFeatures( points(), topologicalEditing );
9797
vlayer->endEditCommand();
98-
if ( returnCode == 4 )
98+
if ( returnCode == QgsGeometry::OperationResult::NothingHappened )
9999
{
100100
QgisApp::instance()->messageBar()->pushMessage(
101101
tr( "No features were split" ),
102102
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." ),
103103
QgsMessageBar::WARNING,
104104
QgisApp::instance()->messageTimeout() );
105105
}
106-
else if ( returnCode == 3 )
106+
else if ( returnCode == QgsGeometry::OperationResult::GeometryEngineError )
107107
{
108108
QgisApp::instance()->messageBar()->pushMessage(
109109
tr( "No feature split done" ),
110110
tr( "Cut edges detected. Make sure the line splits features into multiple parts." ),
111111
QgsMessageBar::WARNING,
112112
QgisApp::instance()->messageTimeout() );
113113
}
114-
else if ( returnCode == 7 )
114+
else if ( returnCode == QgsGeometry::OperationResult::InvalidBaseGeometry )
115115
{
116116
QgisApp::instance()->messageBar()->pushMessage(
117117
tr( "No feature split done" ),
118118
tr( "The geometry is invalid. Please repair before trying to split it." ),
119119
QgsMessageBar::WARNING,
120120
QgisApp::instance()->messageTimeout() );
121121
}
122-
else if ( returnCode != 0 )
122+
else if ( returnCode != QgsGeometry::OperationResult::Success )
123123
{
124124
//several intersections but only one split (most likely line)
125125
QgisApp::instance()->messageBar()->pushMessage(

‎src/app/qgsmaptoolsplitparts.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,33 +91,33 @@ void QgsMapToolSplitParts::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
9191
//bring up dialog if a split was not possible (polygon) or only done once (line)
9292
bool topologicalEditing = QgsProject::instance()->topologicalEditing();
9393
vlayer->beginEditCommand( tr( "Parts split" ) );
94-
int returnCode = vlayer->splitParts( points(), topologicalEditing );
94+
QgsGeometry::OperationResult returnCode = vlayer->splitParts( points(), topologicalEditing );
9595
vlayer->endEditCommand();
96-
if ( returnCode == 4 )
96+
if ( returnCode == QgsGeometry::OperationResult::NothingHappened )
9797
{
9898
QgisApp::instance()->messageBar()->pushMessage(
9999
tr( "No parts were split" ),
100100
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." ),
101101
QgsMessageBar::WARNING,
102102
QgisApp::instance()->messageTimeout() );
103103
}
104-
else if ( returnCode == 3 )
104+
else if ( returnCode == QgsGeometry::OperationResult::GeometryEngineError )
105105
{
106106
QgisApp::instance()->messageBar()->pushMessage(
107107
tr( "No part split done" ),
108108
tr( "Cut edges detected. Make sure the line splits parts into multiple parts." ),
109109
QgsMessageBar::WARNING,
110110
QgisApp::instance()->messageTimeout() );
111111
}
112-
else if ( returnCode == 7 )
112+
else if ( returnCode == QgsGeometry::OperationResult::InvalidBaseGeometry )
113113
{
114114
QgisApp::instance()->messageBar()->pushMessage(
115115
tr( "No part split done" ),
116116
tr( "The geometry is invalid. Please repair before trying to split it." ),
117117
QgsMessageBar::WARNING,
118118
QgisApp::instance()->messageTimeout() );
119119
}
120-
else if ( returnCode != 0 )
120+
else if ( returnCode != QgsGeometry::OperationResult::Success )
121121
{
122122
//several intersections but only one split (most likely line)
123123
QgisApp::instance()->messageBar()->pushMessage(

‎src/core/geometry/qgsgeometry.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ QgsGeometry::OperationResult QgsGeometry::addRing( QgsCurve *ring )
625625
std::unique_ptr< QgsCurve > r( ring );
626626
if ( !d->geometry )
627627
{
628-
return InvalidInput;
628+
return InvalidInputGeometryType;
629629
}
630630

631631
detach();
@@ -816,7 +816,7 @@ QgsGeometry::OperationResult QgsGeometry::splitGeometry( const QVector<QgsPointX
816816
case QgsGeometryEngine::InvalidBaseGeometry:
817817
return QgsGeometry::InvalidBaseGeometry;
818818
case QgsGeometryEngine::InvalidInput:
819-
return QgsGeometry::InvalidInput;
819+
return QgsGeometry::InvalidInputGeometryType;
820820
case QgsGeometryEngine::SplitCannotSplitPoint:
821821
return QgsGeometry::SplitCannotSplitPoint;
822822
case QgsGeometryEngine::NothingHappened:
@@ -857,7 +857,7 @@ QgsGeometry::OperationResult QgsGeometry::reshapeGeometry( const QgsLineString &
857857
case QgsGeometryEngine::InvalidBaseGeometry:
858858
return InvalidBaseGeometry;
859859
case QgsGeometryEngine::InvalidInput:
860-
return InvalidInput;
860+
return InvalidInputGeometryType;
861861
case QgsGeometryEngine::SplitCannotSplitPoint: // should not happen
862862
return GeometryEngineError;
863863
case QgsGeometryEngine::NothingHappened:

‎src/core/geometry/qgsgeometry.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,11 @@ class CORE_EXPORT QgsGeometry
121121
Success = 0, //!< Operation succeeded
122122
NothingHappened = 1000, //!< Nothing happened, without any error
123123
InvalidBaseGeometry, //!< The base geometry on which the operation is done is invalid or empty
124-
InvalidInput, //!< The input geometry (ring, part, split line, etc.) has not the correct geometry type
124+
InvalidInputGeometryType, //!< The input geometry (ring, part, split line, etc.) has not the correct geometry type
125+
SelectionIsEmpty, //!< No features were selected
126+
SelectionIsGreaterThanOne, //!< More than one features were selected
125127
GeometryEngineError, //!< Geometry engine misses a method implemented or an error occurred in the geometry engine
128+
LayerNotEditable, //!< Cannot edit layer
126129
/* Add part issues */
127130
AddPartSelectedGeometryNotFound, //!< The selected geometry cannot be found
128131
AddPartNotMultiGeometry, //!< The source geometry is not multi

‎src/core/geometry/qgsgeometryeditutils.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ QgsGeometry::OperationResult QgsGeometryEditUtils::addRing( QgsAbstractGeometry
3030
{
3131
if ( !ring )
3232
{
33-
return QgsGeometry::InvalidInput;
33+
return QgsGeometry::InvalidInputGeometryType;
3434
}
3535

3636
QVector< QgsCurvePolygon * > polygonList;
@@ -50,7 +50,7 @@ QgsGeometry::OperationResult QgsGeometryEditUtils::addRing( QgsAbstractGeometry
5050
}
5151
else
5252
{
53-
return QgsGeometry::InvalidInput; //not polygon / multipolygon;
53+
return QgsGeometry::InvalidInputGeometryType; //not polygon / multipolygon;
5454
}
5555

5656
//ring must be closed
@@ -104,7 +104,7 @@ QgsGeometry::OperationResult QgsGeometryEditUtils::addPart( QgsAbstractGeometry
104104

105105
if ( !part )
106106
{
107-
return QgsGeometry::InvalidInput;
107+
return QgsGeometry::InvalidInputGeometryType;
108108
}
109109

110110
//multitype?
@@ -155,19 +155,19 @@ QgsGeometry::OperationResult QgsGeometryEditUtils::addPart( QgsAbstractGeometry
155155
{
156156
while ( geomCollection->numGeometries() > n )
157157
geomCollection->removeGeometry( n );
158-
return QgsGeometry::InvalidInput;
158+
return QgsGeometry::InvalidInputGeometryType;
159159
}
160160
}
161161
else
162162
{
163-
return QgsGeometry::InvalidInput;
163+
return QgsGeometry::InvalidInputGeometryType;
164164
}
165165
}
166166
else
167167
{
168168
added = geomCollection->addGeometry( part.release() );
169169
}
170-
return added ? QgsGeometry::Success : QgsGeometry::InvalidInput;
170+
return added ? QgsGeometry::Success : QgsGeometry::InvalidInputGeometryType;
171171
}
172172

173173
bool QgsGeometryEditUtils::deleteRing( QgsAbstractGeometry *geom, int ringNum, int partNum )

0 commit comments

Comments
 (0)
Please sign in to comment.