Skip to content

Commit 255eb98

Browse files
committedMar 18, 2017
Make features valid on setAttribute and setGeometry
Implements qgis/qgis4.0_api#75
1 parent 921a0c7 commit 255eb98

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed
 

‎src/core/qgsfeature.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,14 @@ void QgsFeature::setAttributes( const QgsAttributes &attrs )
142142

143143
d.detach();
144144
d->attributes = attrs;
145+
d->valid = true;
145146
}
146147

147148
void QgsFeature::setGeometry( const QgsGeometry &geometry )
148149
{
149150
d.detach();
150151
d->geometry = geometry;
152+
d->valid = true;
151153
}
152154

153155
void QgsFeature::clearGeometry()
@@ -220,6 +222,7 @@ bool QgsFeature::setAttribute( int idx, const QVariant &value )
220222

221223
d.detach();
222224
d->attributes[idx] = value;
225+
d->valid = true;
223226
return true;
224227
}
225228

@@ -237,6 +240,7 @@ bool QgsFeature::setAttribute( const QString &name, const QVariant &value )
237240

238241
d.detach();
239242
d->attributes[fieldIdx] = value;
243+
d->valid = true;
240244
return true;
241245
}
242246

‎src/core/qgsfeature.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,15 @@ class CORE_EXPORT QgsFeature
196196
QgsAttributes attributes() const;
197197

198198
/** Sets the feature's attributes.
199+
* The feature will be valid after.
199200
* @param attrs attribute list
200201
* @see setAttribute
201202
* @see attributes
202203
*/
203204
void setAttributes( const QgsAttributes &attrs );
204205

205206
/** Set an attribute's value by field index.
207+
* The feature will be valid if it was successful.
206208
* @param field the index of the field to set
207209
* @param attr the value of the attribute
208210
* @return false, if the field index does not exist
@@ -251,7 +253,7 @@ class CORE_EXPORT QgsFeature
251253
*/
252254
QgsGeometry geometry() const;
253255

254-
/** Set the feature's geometry.
256+
/** Set the feature's geometry. The feature will be valid after.
255257
* @param geometry new feature geometry
256258
* @see geometry()
257259
* @see clearGeometry()
@@ -282,6 +284,7 @@ class CORE_EXPORT QgsFeature
282284

283285
/** Insert a value into attribute. Returns false if attribute name could not be converted to index.
284286
* Field map must be associated using @link setFields @endlink before this method can be used.
287+
* The feature will be valid if it was successful
285288
* @param name The name of the field to set
286289
* @param value The value to set
287290
* @return false if attribute name could not be converted to index (C++ only)

‎tests/src/python/test_qgsfeature.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ def test_ValidFeature(self):
4646
myMessage = '\nExpected: %s\nGot: %s' % ("True", myValidValue)
4747
assert myValidValue, myMessage
4848

49+
def test_Validity(self):
50+
f = QgsFeature()
51+
self.assertFalse(f.isValid())
52+
f.setGeometry(QgsGeometry())
53+
self.assertTrue(f.isValid())
54+
f.setValid(False)
55+
self.assertFalse(f.isValid())
56+
fields = QgsFields()
57+
field1 = QgsField('my_field')
58+
fields.append(field1)
59+
field2 = QgsField('my_field2')
60+
fields.append(field2)
61+
f.setFields(fields)
62+
f.setAttribute(0, 0)
63+
self.assertTrue(f.isValid())
64+
f.setValid(False)
65+
self.assertFalse(f.isValid())
66+
67+
f.setValid(False)
68+
self.assertFalse(f.isValid())
69+
4970
def test_Attributes(self):
5071
myPath = os.path.join(unitTestDataPath(), 'lines.shp')
5172
myLayer = QgsVectorLayer(myPath, 'Lines', 'ogr')

0 commit comments

Comments
 (0)
Please sign in to comment.