From 255eb98be61d2a44f8e2f9546887172323563f5d Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Wed, 15 Mar 2017 13:14:30 +0100 Subject: [PATCH] Make features valid on setAttribute and setGeometry Implements https://github.com/qgis/qgis3.0_api/issues/75 --- src/core/qgsfeature.cpp | 4 ++++ src/core/qgsfeature.h | 5 ++++- tests/src/python/test_qgsfeature.py | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/core/qgsfeature.cpp b/src/core/qgsfeature.cpp index 127f65dd70f..d3734e6c9d8 100644 --- a/src/core/qgsfeature.cpp +++ b/src/core/qgsfeature.cpp @@ -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; } diff --git a/src/core/qgsfeature.h b/src/core/qgsfeature.h index d0a6af1e7fa..5522c1656e5 100644 --- a/src/core/qgsfeature.h +++ b/src/core/qgsfeature.h @@ -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) diff --git a/tests/src/python/test_qgsfeature.py b/tests/src/python/test_qgsfeature.py index 97fe21804e9..bf99895d116 100644 --- a/tests/src/python/test_qgsfeature.py +++ b/tests/src/python/test_qgsfeature.py @@ -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')