Make features valid on setAttribute and setGeometry

Implements https://github.com/qgis/qgis3.0_api/issues/75
This commit is contained in:
Matthias Kuhn 2017-03-15 13:14:30 +01:00
parent 921a0c75e3
commit 255eb98be6
3 changed files with 29 additions and 1 deletions

View File

@ -142,12 +142,14 @@ void QgsFeature::setAttributes( const QgsAttributes &attrs )
d.detach();
d->attributes = attrs;
d->valid = true;
}
void QgsFeature::setGeometry( const QgsGeometry &geometry )
{
d.detach();
d->geometry = geometry;
d->valid = true;
}
void QgsFeature::clearGeometry()
@ -220,6 +222,7 @@ bool QgsFeature::setAttribute( int idx, const QVariant &value )
d.detach();
d->attributes[idx] = value;
d->valid = true;
return true;
}
@ -237,6 +240,7 @@ bool QgsFeature::setAttribute( const QString &name, const QVariant &value )
d.detach();
d->attributes[fieldIdx] = value;
d->valid = true;
return true;
}

View File

@ -196,6 +196,7 @@ class CORE_EXPORT QgsFeature
QgsAttributes attributes() const;
/** Sets the feature's attributes.
* The feature will be valid after.
* @param attrs attribute list
* @see setAttribute
* @see attributes
@ -203,6 +204,7 @@ class CORE_EXPORT QgsFeature
void setAttributes( const QgsAttributes &attrs );
/** Set an attribute's value by field index.
* The feature will be valid if it was successful.
* @param field the index of the field to set
* @param attr the value of the attribute
* @return false, if the field index does not exist
@ -251,7 +253,7 @@ class CORE_EXPORT QgsFeature
*/
QgsGeometry geometry() const;
/** Set the feature's geometry.
/** Set the feature's geometry. The feature will be valid after.
* @param geometry new feature geometry
* @see geometry()
* @see clearGeometry()
@ -282,6 +284,7 @@ class CORE_EXPORT QgsFeature
/** Insert a value into attribute. Returns false if attribute name could not be converted to index.
* Field map must be associated using @link setFields @endlink before this method can be used.
* The feature will be valid if it was successful
* @param name The name of the field to set
* @param value The value to set
* @return false if attribute name could not be converted to index (C++ only)

View File

@ -46,6 +46,27 @@ class TestQgsFeature(unittest.TestCase):
myMessage = '\nExpected: %s\nGot: %s' % ("True", myValidValue)
assert myValidValue, myMessage
def test_Validity(self):
f = QgsFeature()
self.assertFalse(f.isValid())
f.setGeometry(QgsGeometry())
self.assertTrue(f.isValid())
f.setValid(False)
self.assertFalse(f.isValid())
fields = QgsFields()
field1 = QgsField('my_field')
fields.append(field1)
field2 = QgsField('my_field2')
fields.append(field2)
f.setFields(fields)
f.setAttribute(0, 0)
self.assertTrue(f.isValid())
f.setValid(False)
self.assertFalse(f.isValid())
f.setValid(False)
self.assertFalse(f.isValid())
def test_Attributes(self):
myPath = os.path.join(unitTestDataPath(), 'lines.shp')
myLayer = QgsVectorLayer(myPath, 'Lines', 'ogr')