Skip to content

Commit

Permalink
[project] Avoid needlessly dirtying when written value does not change
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Nov 14, 2018
1 parent cdc3cd1 commit 3b892c9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 15 deletions.
61 changes: 46 additions & 15 deletions src/core/qgsproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,13 @@ QgsProjectProperty *findKey_( const QString &scope,
\param key key name
\param rootProperty is the property from which to start adding
\param value the value associated with the key
\param propertiesModified the parameter will be set to true if the written entry modifies pre-existing properties
*/
QgsProjectProperty *addKey_( const QString &scope,
const QString &key,
QgsProjectPropertyKey *rootProperty,
const QVariant &value )
const QVariant &value,
bool &propertiesModified )
{
QStringList keySequence = makeKeyTokens_( scope, key );

Expand All @@ -210,6 +212,7 @@ QgsProjectProperty *addKey_( const QString &scope,
QgsProjectProperty *nextProperty; // link to next property down hierarchy
QgsProjectPropertyKey *newPropertyKey = nullptr;

propertiesModified = false;
while ( ! keySequence.isEmpty() )
{
// if the current head of the sequence list matches the property name,
Expand All @@ -223,14 +226,24 @@ QgsProjectProperty *addKey_( const QString &scope,
// name to store the value
if ( 1 == keySequence.count() )
{
currentProperty->setValue( keySequence.front(), value );
QgsProjectProperty *property = currentProperty->find( keySequence.front() );
if ( !property || property->value() != value )
{
currentProperty->setValue( keySequence.front(), value );
propertiesModified = true;
}

return currentProperty;
}
// we're at the top element if popping the keySequence element
// will leave it empty; in that case, just add the key
else if ( keySequence.isEmpty() )
{
currentProperty->setValue( value );
if ( currentProperty->value() != value )
{
currentProperty->setValue( value );
propertiesModified = true;
}

return currentProperty;
}
Expand Down Expand Up @@ -263,7 +276,6 @@ QgsProjectProperty *addKey_( const QString &scope,
}

return nullptr;

}


Expand Down Expand Up @@ -325,7 +337,6 @@ void removeKey_( const QString &scope,
return;
}
}

}

QgsProject::QgsProject( QObject *parent )
Expand Down Expand Up @@ -1932,37 +1943,57 @@ bool QgsProject::writeProjectFile( const QString &filename )

bool QgsProject::writeEntry( const QString &scope, QString const &key, bool value )
{
setDirty( true );
bool propertiesModified;
bool success = addKey_( scope, key, &mProperties, value, propertiesModified );

if ( propertiesModified )
setDirty( true );

return addKey_( scope, key, &mProperties, value );
return success;
}

bool QgsProject::writeEntry( const QString &scope, const QString &key, double value )
{
setDirty( true );
bool propertiesModified;
bool success = addKey_( scope, key, &mProperties, value, propertiesModified );

if ( propertiesModified )
setDirty( true );

return addKey_( scope, key, &mProperties, value );
return success;
}

bool QgsProject::writeEntry( const QString &scope, QString const &key, int value )
{
setDirty( true );
bool propertiesModified;
bool success = addKey_( scope, key, &mProperties, value, propertiesModified );

if ( propertiesModified )
setDirty( true );

return addKey_( scope, key, &mProperties, value );
return success;
}

bool QgsProject::writeEntry( const QString &scope, const QString &key, const QString &value )
{
setDirty( true );
bool propertiesModified;
bool success = addKey_( scope, key, &mProperties, value, propertiesModified );

if ( propertiesModified )
setDirty( true );

return addKey_( scope, key, &mProperties, value );
return success;
}

bool QgsProject::writeEntry( const QString &scope, const QString &key, const QStringList &value )
{
setDirty( true );
bool propertiesModified;
bool success = addKey_( scope, key, &mProperties, value, propertiesModified );

if ( propertiesModified )
setDirty( true );

return addKey_( scope, key, &mProperties, value );
return success;
}

QStringList QgsProject::readListEntry( const QString &scope,
Expand Down
19 changes: 19 additions & 0 deletions tests/src/python/test_qgsproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,25 @@ def testWriteEntry(self):
self.assertTrue(ok)
self.assertEqual(q, query)

def testWriteEntryDirtying(self):

project = QgsProject()

# writing a new entry should dirty the project
project.setDirty(False)
self.assertTrue(project.writeEntry('myscope', 'myentry', True))
self.assertTrue(project.isDirty())

# over-writing a pre-existing entry with the same value should _not_ dirty the project
project.setDirty(False)
self.assertTrue(project.writeEntry('myscope', 'myentry', True))
self.assertFalse(project.isDirty())

# over-writing a pre-existing entry with a different value should dirty the project
project.setDirty(False)
self.assertTrue(project.writeEntry('myscope', 'myentry', False))
self.assertTrue(project.isDirty())


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

0 comments on commit 3b892c9

Please sign in to comment.