Skip to content

Commit 9050cc1

Browse files
committed
Add == and != operator to QgsFeature and add QML bindings
1 parent 6ae2daa commit 9050cc1

File tree

5 files changed

+147
-6
lines changed

5 files changed

+147
-6
lines changed

python/core/qgsfeature.sip

+8-2
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,19 @@ class QgsFeature
239239
* @returns feature ID
240240
* @see setFeatureId
241241
*/
242-
qint64 id() const;
242+
QgsFeatureId id() const;
243243

244244
/** Sets the feature ID for this feature.
245245
* @param id feature id
246246
* @see id
247247
*/
248-
void setFeatureId( qint64 id );
248+
void setFeatureId( QgsFeatureId id );
249+
250+
/** Sets the feature ID for this feature.
251+
* @param id feature id
252+
* @see id
253+
*/
254+
void setId( QgsFeatureId id );
249255

250256
/** Returns the feature's attributes.
251257
* @link attributes @endlink method.

src/core/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ SET(QGIS_CORE_MOC_HDRS
454454
qgsdataitem.h
455455
qgsdataprovider.h
456456
qgsdbfilterproxymodel.h
457+
qgsfeature.h
457458
qgsfeedback.h
458459
qgsfield.h
459460
qgsgeometryvalidator.h
@@ -642,7 +643,6 @@ SET(QGIS_CORE_HDRS
642643
qgsexpressioncontext.h
643644
qgsexpressioncontextgenerator.h
644645
qgsexpressionfieldbuffer.h
645-
qgsfeature.h
646646
qgsfeature_p.h
647647
qgsfeatureiterator.h
648648
qgsfeaturerequest.h

src/core/qgsfeature.cpp

+37-2
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,37 @@ QgsFeature::QgsFeature( const QgsFields &fields, QgsFeatureId id )
4141
initAttributes( d->fields.count() );
4242
}
4343

44-
QgsFeature::QgsFeature( QgsFeature const & rhs )
44+
QgsFeature::QgsFeature( const QgsFeature& rhs )
4545
: d( rhs.d )
4646
{
4747
}
4848

49-
QgsFeature & QgsFeature::operator=( QgsFeature const & rhs )
49+
QgsFeature & QgsFeature::operator=( const QgsFeature & rhs )
5050
{
5151
d = rhs.d;
5252
return *this;
5353
}
5454

55+
bool QgsFeature::operator ==( const QgsFeature& other ) const
56+
{
57+
if ( d == other.d )
58+
return true;
59+
60+
if ( d->fid == other.d->fid
61+
&& d->valid == other.d->valid
62+
&& d->fields == other.d->fields
63+
&& d->attributes == other.d->attributes
64+
&& d->geometry.equals( other.d->geometry ) )
65+
return true;
66+
67+
return false;
68+
}
69+
70+
bool QgsFeature::operator!=( const QgsFeature& other ) const
71+
{
72+
return !( *this == other );
73+
}
74+
5575
QgsFeature::~QgsFeature()
5676
{
5777
}
@@ -93,6 +113,21 @@ void QgsFeature::setFeatureId( QgsFeatureId id )
93113
d->fid = id;
94114
}
95115

116+
/***************************************************************************
117+
* This class is considered CRITICAL and any change MUST be accompanied with
118+
* full unit tests in testqgsfeature.cpp.
119+
* See details in QEP #17
120+
****************************************************************************/
121+
122+
void QgsFeature::setId( QgsFeatureId id )
123+
{
124+
if ( id == d->fid )
125+
return;
126+
127+
d.detach();
128+
d->fid = id;
129+
}
130+
96131
QgsAttributes QgsFeature::attributes() const
97132
{
98133
return d->attributes;

src/core/qgsfeature.h

+25-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ email : sherman at mrcc.com
2424
#include <QSet>
2525
#include <QExplicitlySharedDataPointer>
2626

27+
#include "qgsfield.h"
28+
2729
class QgsGeometry;
2830
class QgsRectangle;
2931
class QgsFeature;
30-
class QgsFields;
3132
class QgsFeaturePrivate;
3233

3334
// feature id class (currently 64 bit)
@@ -123,6 +124,12 @@ class QgsField;
123124
*/
124125
class CORE_EXPORT QgsFeature
125126
{
127+
Q_GADGET
128+
129+
Q_PROPERTY( QgsFeatureId id READ id WRITE setId )
130+
Q_PROPERTY( QgsAttributes attributes READ attributes WRITE setAttributes )
131+
Q_PROPERTY( QgsFields fields READ fields WRITE setFields )
132+
126133
public:
127134

128135
/** Constructor for QgsFeature
@@ -144,6 +151,17 @@ class CORE_EXPORT QgsFeature
144151
*/
145152
QgsFeature& operator=( const QgsFeature& rhs );
146153

154+
/**
155+
* Compares two features
156+
*/
157+
bool operator==( const QgsFeature& other ) const;
158+
159+
/**
160+
* Compares two features
161+
*/
162+
bool operator!=( const QgsFeature& other ) const;
163+
164+
147165
//! Destructor
148166
virtual ~QgsFeature();
149167

@@ -159,6 +177,12 @@ class CORE_EXPORT QgsFeature
159177
*/
160178
void setFeatureId( QgsFeatureId id );
161179

180+
/** Sets the feature ID for this feature.
181+
* @param id feature id
182+
* @see id
183+
*/
184+
void setId( QgsFeatureId id );
185+
162186
/** Returns the feature's attributes.
163187
* @link attributes @endlink method.
164188
* @returns list of feature's attributes

tests/src/core/testqgsfeature.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class TestQgsFeature: public QObject
4141
void geometry();
4242
void asVariant(); //test conversion to and from a QVariant
4343
void fields();
44+
void equality();
4445
void attributeUsingField();
4546
void dataStream();
4647

@@ -359,6 +360,81 @@ void TestQgsFeature::fields()
359360
QCOMPARE( copy.fieldNameIndex( "field2" ), 1 );
360361
}
361362

363+
void TestQgsFeature::equality()
364+
{
365+
366+
QgsFeature feature;
367+
feature.setFields( mFields, true );
368+
feature.setAttribute( 0, QString( "attr1" ) );
369+
feature.setAttribute( 1, QString( "attr2" ) );
370+
feature.setAttribute( 2, QString( "attr3" ) );
371+
feature.setValid( true );
372+
feature.setId( 1 );
373+
feature.setGeometry( QgsGeometry( new QgsPointV2( 1, 2 ) ) );
374+
375+
QgsFeature feature2 = feature;
376+
QVERIFY( feature == feature2 );
377+
378+
feature2.setAttribute( 0, "attr1" );
379+
QVERIFY( feature == feature2 );
380+
381+
feature2.setAttribute( 1, 1 );
382+
QVERIFY( feature != feature2 );
383+
384+
QgsFeature feature3;
385+
feature3.setFields( mFields, true );
386+
feature3.setAttribute( 0, QString( "attr1" ) );
387+
feature3.setAttribute( 1, QString( "attr2" ) );
388+
feature3.setAttribute( 2, QString( "attr3" ) );
389+
feature3.setValid( true );
390+
feature3.setId( 1 );
391+
feature3.setGeometry( QgsGeometry( new QgsPointV2( 1, 2 ) ) );
392+
QVERIFY( feature == feature3 );
393+
394+
QgsFeature feature4;
395+
feature4.setFields( mFields, true );
396+
feature4.setAttribute( 0, 1 );
397+
feature4.setAttribute( 1, 2 );
398+
feature4.setAttribute( 2, 3 );
399+
feature4.setValid( true );
400+
feature4.setId( 1 );
401+
feature4.setGeometry( QgsGeometry( new QgsPointV2( 1, 2 ) ) );
402+
QVERIFY( feature != feature4 );
403+
404+
QgsFeature feature5;
405+
feature5.setFields( mFields, true );
406+
feature5.setAttribute( 0, QString( "attr1" ) );
407+
feature5.setAttribute( 1, QString( "attr2" ) );
408+
feature5.setAttribute( 2, QString( "attr3" ) );
409+
feature5.setValid( false );
410+
feature5.setId( 1 );
411+
feature5.setGeometry( QgsGeometry( new QgsPointV2( 1, 2 ) ) );
412+
413+
QVERIFY( feature != feature5 );
414+
415+
QgsFeature feature6;
416+
feature6.setFields( mFields, true );
417+
feature6.setAttribute( 0, QString( "attr1" ) );
418+
feature6.setAttribute( 1, QString( "attr2" ) );
419+
feature6.setAttribute( 2, QString( "attr3" ) );
420+
feature6.setValid( true );
421+
feature6.setId( 2 );
422+
feature6.setGeometry( QgsGeometry( new QgsPointV2( 1, 2 ) ) );
423+
424+
QVERIFY( feature != feature6 );
425+
426+
QgsFeature feature7;
427+
feature7.setFields( mFields, true );
428+
feature7.setAttribute( 0, QString( "attr1" ) );
429+
feature7.setAttribute( 1, QString( "attr2" ) );
430+
feature7.setAttribute( 2, QString( "attr3" ) );
431+
feature7.setValid( true );
432+
feature7.setId( 1 );
433+
feature7.setGeometry( QgsGeometry( new QgsPointV2( 1, 3 ) ) );
434+
435+
QVERIFY( feature != feature7 );
436+
}
437+
362438
void TestQgsFeature::attributeUsingField()
363439
{
364440
QgsFeature feature;

0 commit comments

Comments
 (0)