Skip to content

Commit 8b9e86d

Browse files
committedNov 14, 2018
[project] Avoid needlessly dirtying when written value does not change
1 parent 6b2d44a commit 8b9e86d

File tree

2 files changed

+65
-15
lines changed

2 files changed

+65
-15
lines changed
 

‎src/core/qgsproject.cpp

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,13 @@ QgsProjectProperty *findKey_( const QString &scope,
197197
\param key key name
198198
\param rootProperty is the property from which to start adding
199199
\param value the value associated with the key
200+
\param propertiesModified the parameter will be set to true if the written entry modifies pre-existing properties
200201
*/
201202
QgsProjectProperty *addKey_( const QString &scope,
202203
const QString &key,
203204
QgsProjectPropertyKey *rootProperty,
204-
const QVariant &value )
205+
const QVariant &value,
206+
bool &propertiesModified )
205207
{
206208
QStringList keySequence = makeKeyTokens_( scope, key );
207209

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

215+
propertiesModified = false;
213216
while ( ! keySequence.isEmpty() )
214217
{
215218
// if the current head of the sequence list matches the property name,
@@ -223,14 +226,24 @@ QgsProjectProperty *addKey_( const QString &scope,
223226
// name to store the value
224227
if ( 1 == keySequence.count() )
225228
{
226-
currentProperty->setValue( keySequence.front(), value );
229+
QgsProjectProperty *property = currentProperty->find( keySequence.front() );
230+
if ( !property || property->value() != value )
231+
{
232+
currentProperty->setValue( keySequence.front(), value );
233+
propertiesModified = true;
234+
}
235+
227236
return currentProperty;
228237
}
229238
// we're at the top element if popping the keySequence element
230239
// will leave it empty; in that case, just add the key
231240
else if ( keySequence.isEmpty() )
232241
{
233-
currentProperty->setValue( value );
242+
if ( currentProperty->value() != value )
243+
{
244+
currentProperty->setValue( value );
245+
propertiesModified = true;
246+
}
234247

235248
return currentProperty;
236249
}
@@ -263,7 +276,6 @@ QgsProjectProperty *addKey_( const QString &scope,
263276
}
264277

265278
return nullptr;
266-
267279
}
268280

269281

@@ -325,7 +337,6 @@ void removeKey_( const QString &scope,
325337
return;
326338
}
327339
}
328-
329340
}
330341

331342
QgsProject::QgsProject( QObject *parent )
@@ -1899,37 +1910,57 @@ bool QgsProject::writeProjectFile( const QString &filename )
18991910

19001911
bool QgsProject::writeEntry( const QString &scope, QString const &key, bool value )
19011912
{
1902-
setDirty( true );
1913+
bool propertiesModified;
1914+
bool success = addKey_( scope, key, &mProperties, value, propertiesModified );
1915+
1916+
if ( propertiesModified )
1917+
setDirty( true );
19031918

1904-
return addKey_( scope, key, &mProperties, value );
1919+
return success;
19051920
}
19061921

19071922
bool QgsProject::writeEntry( const QString &scope, const QString &key, double value )
19081923
{
1909-
setDirty( true );
1924+
bool propertiesModified;
1925+
bool success = addKey_( scope, key, &mProperties, value, propertiesModified );
1926+
1927+
if ( propertiesModified )
1928+
setDirty( true );
19101929

1911-
return addKey_( scope, key, &mProperties, value );
1930+
return success;
19121931
}
19131932

19141933
bool QgsProject::writeEntry( const QString &scope, QString const &key, int value )
19151934
{
1916-
setDirty( true );
1935+
bool propertiesModified;
1936+
bool success = addKey_( scope, key, &mProperties, value, propertiesModified );
1937+
1938+
if ( propertiesModified )
1939+
setDirty( true );
19171940

1918-
return addKey_( scope, key, &mProperties, value );
1941+
return success;
19191942
}
19201943

19211944
bool QgsProject::writeEntry( const QString &scope, const QString &key, const QString &value )
19221945
{
1923-
setDirty( true );
1946+
bool propertiesModified;
1947+
bool success = addKey_( scope, key, &mProperties, value, propertiesModified );
1948+
1949+
if ( propertiesModified )
1950+
setDirty( true );
19241951

1925-
return addKey_( scope, key, &mProperties, value );
1952+
return success;
19261953
}
19271954

19281955
bool QgsProject::writeEntry( const QString &scope, const QString &key, const QStringList &value )
19291956
{
1930-
setDirty( true );
1957+
bool propertiesModified;
1958+
bool success = addKey_( scope, key, &mProperties, value, propertiesModified );
1959+
1960+
if ( propertiesModified )
1961+
setDirty( true );
19311962

1932-
return addKey_( scope, key, &mProperties, value );
1963+
return success;
19331964
}
19341965

19351966
QStringList QgsProject::readListEntry( const QString &scope,

‎tests/src/python/test_qgsproject.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,25 @@ def testWriteEntry(self):
11211121
self.assertTrue(ok)
11221122
self.assertEqual(q, query)
11231123

1124+
def testWriteEntryDirtying(self):
1125+
1126+
project = QgsProject()
1127+
1128+
# writing a new entry should dirty the project
1129+
project.setDirty(False)
1130+
self.assertTrue(project.writeEntry('myscope', 'myentry', True))
1131+
self.assertTrue(project.isDirty())
1132+
1133+
# over-writing a pre-existing entry with the same value should _not_ dirty the project
1134+
project.setDirty(False)
1135+
self.assertTrue(project.writeEntry('myscope', 'myentry', True))
1136+
self.assertFalse(project.isDirty())
1137+
1138+
# over-writing a pre-existing entry with a different value should dirty the project
1139+
project.setDirty(False)
1140+
self.assertTrue(project.writeEntry('myscope', 'myentry', False))
1141+
self.assertTrue(project.isDirty())
1142+
11241143

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

0 commit comments

Comments
 (0)
Please sign in to comment.