Skip to content

Commit

Permalink
Avoid more needless project dirtying
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Nov 23, 2018
1 parent 98cef32 commit f209105
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 6 deletions.
Expand Up @@ -63,6 +63,7 @@ Copy constructor
%End %End




bool operator==( const QgsCoordinateTransformContext &rhs ) const;


void clear(); void clear();
%Docstring %Docstring
Expand Down
13 changes: 13 additions & 0 deletions src/core/qgscoordinatetransformcontext.cpp
Expand Up @@ -36,6 +36,19 @@ QgsCoordinateTransformContext &QgsCoordinateTransformContext::operator=( const Q
return *this; return *this;
} }


bool QgsCoordinateTransformContext::operator==( const QgsCoordinateTransformContext &rhs ) const
{
if ( d == rhs.d )
return true;

d->mLock.lockForRead();
rhs.d->mLock.lockForRead();
bool equal = d->mSourceDestDatumTransforms == rhs.d->mSourceDestDatumTransforms;
d->mLock.unlock();
rhs.d->mLock.unlock();
return equal;
}

void QgsCoordinateTransformContext::clear() void QgsCoordinateTransformContext::clear()
{ {
d.detach(); d.detach();
Expand Down
1 change: 1 addition & 0 deletions src/core/qgscoordinatetransformcontext.h
Expand Up @@ -80,6 +80,7 @@ class CORE_EXPORT QgsCoordinateTransformContext
*/ */
QgsCoordinateTransformContext &operator=( const QgsCoordinateTransformContext &rhs ) SIP_SKIP; QgsCoordinateTransformContext &operator=( const QgsCoordinateTransformContext &rhs ) SIP_SKIP;


bool operator==( const QgsCoordinateTransformContext &rhs ) const ;


/** /**
* Clears all stored transform information from the context. * Clears all stored transform information from the context.
Expand Down
30 changes: 25 additions & 5 deletions src/core/qgsproject.cpp
Expand Up @@ -278,6 +278,13 @@ QgsProjectProperty *addKey_( const QString &scope,
return nullptr; return nullptr;
} }


/**
* Remove a given key
\param scope scope of key
\param key key name
\param rootProperty is the property from which to start adding
*/


void removeKey_( const QString &scope, void removeKey_( const QString &scope,
const QString &key, const QString &key,
Expand Down Expand Up @@ -614,6 +621,9 @@ QgsCoordinateReferenceSystem QgsProject::crs() const


void QgsProject::setCrs( const QgsCoordinateReferenceSystem &crs ) void QgsProject::setCrs( const QgsCoordinateReferenceSystem &crs )
{ {
if ( crs == mCrs )
return;

mCrs = crs; mCrs = crs;
writeEntry( QStringLiteral( "SpatialRefSys" ), QStringLiteral( "/ProjectionsEnabled" ), crs.isValid() ? 1 : 0 ); writeEntry( QStringLiteral( "SpatialRefSys" ), QStringLiteral( "/ProjectionsEnabled" ), crs.isValid() ? 1 : 0 );
setDirty( true ); setDirty( true );
Expand All @@ -631,7 +641,6 @@ QString QgsProject::ellipsoid() const
void QgsProject::setEllipsoid( const QString &ellipsoid ) void QgsProject::setEllipsoid( const QString &ellipsoid )
{ {
writeEntry( QStringLiteral( "Measure" ), QStringLiteral( "/Ellipsoid" ), ellipsoid ); writeEntry( QStringLiteral( "Measure" ), QStringLiteral( "/Ellipsoid" ), ellipsoid );
setDirty( true );
emit ellipsoidChanged( ellipsoid ); emit ellipsoidChanged( ellipsoid );
} }


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


void QgsProject::setTransformContext( const QgsCoordinateTransformContext &context ) void QgsProject::setTransformContext( const QgsCoordinateTransformContext &context )
{ {
if ( context == mTransformContext )
return;

mTransformContext = context; mTransformContext = context;
emit transformContextChanged(); emit transformContextChanged();
} }
Expand Down Expand Up @@ -834,7 +846,7 @@ void QgsProject::setSnappingConfig( const QgsSnappingConfig &snappingConfig )
return; return;


mSnappingConfig = snappingConfig; mSnappingConfig = snappingConfig;
setDirty(); setDirty( true );
emit snappingConfigChanged( mSnappingConfig ); emit snappingConfigChanged( mSnappingConfig );
} }


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


bool QgsProject::removeEntry( const QString &scope, const QString &key ) bool QgsProject::removeEntry( const QString &scope, const QString &key )
{ {
removeKey_( scope, key, mProperties ); if ( findKey_( scope, key, mProperties ) )

{
setDirty( true ); removeKey_( scope, key, mProperties );
setDirty( true );
}


return !findKey_( scope, key, mProperties ); return !findKey_( scope, key, mProperties );
} }
Expand Down Expand Up @@ -2357,6 +2371,9 @@ bool QgsProject::evaluateDefaultValues() const


void QgsProject::setEvaluateDefaultValues( bool evaluateDefaultValues ) void QgsProject::setEvaluateDefaultValues( bool evaluateDefaultValues )
{ {
if ( evaluateDefaultValues == mEvaluateDefaultValues )
return;

const QMap<QString, QgsMapLayer *> layers = mapLayers(); const QMap<QString, QgsMapLayer *> layers = mapLayers();
QMap<QString, QgsMapLayer *>::const_iterator layerIt = layers.constBegin(); QMap<QString, QgsMapLayer *>::const_iterator layerIt = layers.constBegin();
for ( ; layerIt != layers.constEnd(); ++layerIt ) for ( ; layerIt != layers.constEnd(); ++layerIt )
Expand Down Expand Up @@ -2848,6 +2865,9 @@ const QgsProjectMetadata &QgsProject::metadata() const


void QgsProject::setMetadata( const QgsProjectMetadata &metadata ) void QgsProject::setMetadata( const QgsProjectMetadata &metadata )
{ {
if ( metadata == mMetadata )
return;

mMetadata = metadata; mMetadata = metadata;
emit metadataChanged(); emit metadataChanged();


Expand Down
13 changes: 13 additions & 0 deletions tests/src/python/test_qgscoordinatetransformcontext.py
Expand Up @@ -408,6 +408,19 @@ def testReadWriteSettings(self):
{('EPSG:4204', 'EPSG:4326'): QgsDatumTransform.TransformPair(source_id_1, dest_id_1), {('EPSG:4204', 'EPSG:4326'): QgsDatumTransform.TransformPair(source_id_1, dest_id_1),
('EPSG:4205', 'EPSG:4326'): QgsDatumTransform.TransformPair(source_id_2, dest_id_2)}) ('EPSG:4205', 'EPSG:4326'): QgsDatumTransform.TransformPair(source_id_2, dest_id_2)})


def testEqualOperator(self):
context1 = QgsCoordinateTransformContext()
context2 = QgsCoordinateTransformContext()
self.assertTrue(context1 == context2)

context1.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem('EPSG:3111'),
QgsCoordinateReferenceSystem('EPSG:4283'), 1, 2)
self.assertFalse(context1 == context2)

context2.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem('EPSG:3111'),
QgsCoordinateReferenceSystem('EPSG:4283'), 1, 2)
self.assertTrue(context1 == context2)



if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
23 changes: 22 additions & 1 deletion tests/src/python/test_qgsproject.py
Expand Up @@ -1121,7 +1121,7 @@ def testWriteEntry(self):
self.assertTrue(ok) self.assertTrue(ok)
self.assertEqual(q, query) self.assertEqual(q, query)


def testWriteEntryDirtying(self): def testDirtying(self):


project = QgsProject() project = QgsProject()


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


# removing an existing entry should dirty the project
project.setDirty(False)
self.assertTrue(project.removeEntry('myscope', 'myentry'))
self.assertTrue(project.isDirty())

# removing a non-existing entry should _not_ dirty the project
project.setDirty(False)
self.assertTrue(project.removeEntry('myscope', 'myentry'))
self.assertFalse(project.isDirty())

# setting a project CRS with a new value should dirty the project
project.setCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
project.setDirty(False)
project.setCrs(QgsCoordinateReferenceSystem('EPSG:3148'))
self.assertTrue(project.isDirty())

# setting a project CRS with the same project CRS should not dirty the project
project.setDirty(False)
project.setCrs(QgsCoordinateReferenceSystem('EPSG:3148'))
self.assertFalse(project.isDirty())



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

0 comments on commit f209105

Please sign in to comment.