Skip to content

Commit f209105

Browse files
committed
Avoid more needless project dirtying
1 parent 98cef32 commit f209105

6 files changed

+75
-6
lines changed

python/core/auto_generated/qgscoordinatetransformcontext.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Copy constructor
6363
%End
6464

6565

66+
bool operator==( const QgsCoordinateTransformContext &rhs ) const;
6667

6768
void clear();
6869
%Docstring

src/core/qgscoordinatetransformcontext.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ QgsCoordinateTransformContext &QgsCoordinateTransformContext::operator=( const Q
3636
return *this;
3737
}
3838

39+
bool QgsCoordinateTransformContext::operator==( const QgsCoordinateTransformContext &rhs ) const
40+
{
41+
if ( d == rhs.d )
42+
return true;
43+
44+
d->mLock.lockForRead();
45+
rhs.d->mLock.lockForRead();
46+
bool equal = d->mSourceDestDatumTransforms == rhs.d->mSourceDestDatumTransforms;
47+
d->mLock.unlock();
48+
rhs.d->mLock.unlock();
49+
return equal;
50+
}
51+
3952
void QgsCoordinateTransformContext::clear()
4053
{
4154
d.detach();

src/core/qgscoordinatetransformcontext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class CORE_EXPORT QgsCoordinateTransformContext
8080
*/
8181
QgsCoordinateTransformContext &operator=( const QgsCoordinateTransformContext &rhs ) SIP_SKIP;
8282

83+
bool operator==( const QgsCoordinateTransformContext &rhs ) const ;
8384

8485
/**
8586
* Clears all stored transform information from the context.

src/core/qgsproject.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,13 @@ QgsProjectProperty *addKey_( const QString &scope,
278278
return nullptr;
279279
}
280280

281+
/**
282+
* Remove a given key
283+
284+
\param scope scope of key
285+
\param key key name
286+
\param rootProperty is the property from which to start adding
287+
*/
281288

282289
void removeKey_( const QString &scope,
283290
const QString &key,
@@ -614,6 +621,9 @@ QgsCoordinateReferenceSystem QgsProject::crs() const
614621

615622
void QgsProject::setCrs( const QgsCoordinateReferenceSystem &crs )
616623
{
624+
if ( crs == mCrs )
625+
return;
626+
617627
mCrs = crs;
618628
writeEntry( QStringLiteral( "SpatialRefSys" ), QStringLiteral( "/ProjectionsEnabled" ), crs.isValid() ? 1 : 0 );
619629
setDirty( true );
@@ -631,7 +641,6 @@ QString QgsProject::ellipsoid() const
631641
void QgsProject::setEllipsoid( const QString &ellipsoid )
632642
{
633643
writeEntry( QStringLiteral( "Measure" ), QStringLiteral( "/Ellipsoid" ), ellipsoid );
634-
setDirty( true );
635644
emit ellipsoidChanged( ellipsoid );
636645
}
637646

@@ -642,6 +651,9 @@ QgsCoordinateTransformContext QgsProject::transformContext() const
642651

643652
void QgsProject::setTransformContext( const QgsCoordinateTransformContext &context )
644653
{
654+
if ( context == mTransformContext )
655+
return;
656+
645657
mTransformContext = context;
646658
emit transformContextChanged();
647659
}
@@ -834,7 +846,7 @@ void QgsProject::setSnappingConfig( const QgsSnappingConfig &snappingConfig )
834846
return;
835847

836848
mSnappingConfig = snappingConfig;
837-
setDirty();
849+
setDirty( true );
838850
emit snappingConfigChanged( mSnappingConfig );
839851
}
840852

@@ -2084,9 +2096,11 @@ bool QgsProject::readBoolEntry( const QString &scope, const QString &key, bool d
20842096

20852097
bool QgsProject::removeEntry( const QString &scope, const QString &key )
20862098
{
2087-
removeKey_( scope, key, mProperties );
2088-
2089-
setDirty( true );
2099+
if ( findKey_( scope, key, mProperties ) )
2100+
{
2101+
removeKey_( scope, key, mProperties );
2102+
setDirty( true );
2103+
}
20902104

20912105
return !findKey_( scope, key, mProperties );
20922106
}
@@ -2357,6 +2371,9 @@ bool QgsProject::evaluateDefaultValues() const
23572371

23582372
void QgsProject::setEvaluateDefaultValues( bool evaluateDefaultValues )
23592373
{
2374+
if ( evaluateDefaultValues == mEvaluateDefaultValues )
2375+
return;
2376+
23602377
const QMap<QString, QgsMapLayer *> layers = mapLayers();
23612378
QMap<QString, QgsMapLayer *>::const_iterator layerIt = layers.constBegin();
23622379
for ( ; layerIt != layers.constEnd(); ++layerIt )
@@ -2848,6 +2865,9 @@ const QgsProjectMetadata &QgsProject::metadata() const
28482865

28492866
void QgsProject::setMetadata( const QgsProjectMetadata &metadata )
28502867
{
2868+
if ( metadata == mMetadata )
2869+
return;
2870+
28512871
mMetadata = metadata;
28522872
emit metadataChanged();
28532873

tests/src/python/test_qgscoordinatetransformcontext.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,19 @@ def testReadWriteSettings(self):
408408
{('EPSG:4204', 'EPSG:4326'): QgsDatumTransform.TransformPair(source_id_1, dest_id_1),
409409
('EPSG:4205', 'EPSG:4326'): QgsDatumTransform.TransformPair(source_id_2, dest_id_2)})
410410

411+
def testEqualOperator(self):
412+
context1 = QgsCoordinateTransformContext()
413+
context2 = QgsCoordinateTransformContext()
414+
self.assertTrue(context1 == context2)
415+
416+
context1.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem('EPSG:3111'),
417+
QgsCoordinateReferenceSystem('EPSG:4283'), 1, 2)
418+
self.assertFalse(context1 == context2)
419+
420+
context2.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem('EPSG:3111'),
421+
QgsCoordinateReferenceSystem('EPSG:4283'), 1, 2)
422+
self.assertTrue(context1 == context2)
423+
411424

412425
if __name__ == '__main__':
413426
unittest.main()

tests/src/python/test_qgsproject.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ def testWriteEntry(self):
11211121
self.assertTrue(ok)
11221122
self.assertEqual(q, query)
11231123

1124-
def testWriteEntryDirtying(self):
1124+
def testDirtying(self):
11251125

11261126
project = QgsProject()
11271127

@@ -1140,6 +1140,27 @@ def testWriteEntryDirtying(self):
11401140
self.assertTrue(project.writeEntry('myscope', 'myentry', False))
11411141
self.assertTrue(project.isDirty())
11421142

1143+
# removing an existing entry should dirty the project
1144+
project.setDirty(False)
1145+
self.assertTrue(project.removeEntry('myscope', 'myentry'))
1146+
self.assertTrue(project.isDirty())
1147+
1148+
# removing a non-existing entry should _not_ dirty the project
1149+
project.setDirty(False)
1150+
self.assertTrue(project.removeEntry('myscope', 'myentry'))
1151+
self.assertFalse(project.isDirty())
1152+
1153+
# setting a project CRS with a new value should dirty the project
1154+
project.setCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
1155+
project.setDirty(False)
1156+
project.setCrs(QgsCoordinateReferenceSystem('EPSG:3148'))
1157+
self.assertTrue(project.isDirty())
1158+
1159+
# setting a project CRS with the same project CRS should not dirty the project
1160+
project.setDirty(False)
1161+
project.setCrs(QgsCoordinateReferenceSystem('EPSG:3148'))
1162+
self.assertFalse(project.isDirty())
1163+
11431164

11441165
if __name__ == '__main__':
11451166
unittest.main()

0 commit comments

Comments
 (0)