Skip to content

Commit 69f370e

Browse files
authored
Merge pull request #5152 from ismailsunni/add_section
Add section in beginGroup and remove Setting.
2 parents f9f5aaf + 04e5490 commit 69f370e

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

python/core/qgssettings.sip

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class QgsSettings : QObject
130130
%End
131131
~QgsSettings();
132132

133-
void beginGroup( const QString &prefix );
133+
void beginGroup( const QString &prefix, const QgsSettings::Section section = QgsSettings::NoSection );
134134
%Docstring
135135
Appends prefix to the current group.
136136
The current group is automatically prepended to all keys specified to QSettings.
@@ -245,9 +245,9 @@ Returns the path where settings written using this QSettings object are stored.
245245
This function is called automatically from QSettings's destructor and by the event
246246
loop at regular intervals, so you normally don't need to call it yourself.
247247
%End
248-
void remove( const QString &key );
248+
void remove( const QString &key, const QgsSettings::Section section = QgsSettings::NoSection );
249249
%Docstring
250-
Removes the setting key and any sub-settings of key.
250+
Removes the setting key and any sub-settings of key in a section.
251251
%End
252252
QString prefixedKey( const QString &key, const QgsSettings::Section section ) const;
253253
%Docstring

src/core/qgssettings.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@ QgsSettings::~QgsSettings()
8383
}
8484

8585

86-
void QgsSettings::beginGroup( const QString &prefix )
86+
void QgsSettings::beginGroup( const QString &prefix, const QgsSettings::Section section )
8787
{
88-
mUserSettings->beginGroup( sanitizeKey( prefix ) );
88+
QString pKey = prefixedKey( prefix, section );
89+
mUserSettings->beginGroup( pKey );
8990
if ( mGlobalSettings )
9091
{
91-
mGlobalSettings->beginGroup( sanitizeKey( prefix ) );
92+
mGlobalSettings->beginGroup( pKey );
9293
}
9394
}
9495

@@ -191,9 +192,10 @@ void QgsSettings::sync()
191192
return mUserSettings->sync();
192193
}
193194

194-
void QgsSettings::remove( const QString &key )
195+
void QgsSettings::remove( const QString &key, const QgsSettings::Section section )
195196
{
196-
mUserSettings->remove( sanitizeKey( key ) );
197+
QString pKey = prefixedKey( key, section );
198+
mUserSettings->remove( pKey );
197199
}
198200

199201
QString QgsSettings::prefixedKey( const QString &key, const Section section ) const

src/core/qgssettings.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class CORE_EXPORT QgsSettings : public QObject
141141
* In addition, query functions such as childGroups(), childKeys(), and allKeys()
142142
* are based on the group. By default, no group is set.
143143
*/
144-
void beginGroup( const QString &prefix );
144+
void beginGroup( const QString &prefix, const QgsSettings::Section section = QgsSettings::NoSection );
145145
//! Resets the group to what it was before the corresponding beginGroup() call.
146146
void endGroup();
147147
//! Returns a list of all keys, including subkeys, that can be read using the QSettings object.
@@ -218,8 +218,8 @@ class CORE_EXPORT QgsSettings : public QObject
218218
* loop at regular intervals, so you normally don't need to call it yourself.
219219
*/
220220
void sync();
221-
//! Removes the setting key and any sub-settings of key.
222-
void remove( const QString &key );
221+
//! Removes the setting key and any sub-settings of key in a section.
222+
void remove( const QString &key, const QgsSettings::Section section = QgsSettings::NoSection );
223223
//! Return the sanitized and prefixed key
224224
QString prefixedKey( const QString &key, const QgsSettings::Section section ) const;
225225
//! Removes all entries in the user settings

tests/src/python/test_qgssettings.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,30 @@ def test_global_groups(self):
224224
self.assertEqual([], self.settings.globalChildGroups())
225225
self.settings.endGroup()
226226

227+
def test_group_section(self):
228+
# Test group by using Section
229+
self.settings.beginGroup('firstgroup', section=QgsSettings.Core)
230+
self.assertEqual([], self.settings.childGroups())
231+
self.settings.setValue('key', 'value')
232+
self.settings.setValue('key2/subkey1', 'subvalue1')
233+
self.settings.setValue('key2/subkey2', 'subvalue2')
234+
self.settings.setValue('key3', 'value3')
235+
236+
self.assertEqual(['key', 'key2/subkey1', 'key2/subkey2', 'key3'], self.settings.allKeys())
237+
self.assertEqual(['key', 'key3'], self.settings.childKeys())
238+
self.assertEqual(['key2'], self.settings.childGroups())
239+
self.settings.endGroup()
240+
# Set value by writing the group manually
241+
self.settings.setValue('firstgroup/key4', 'value4', section=QgsSettings.Core)
242+
# Checking the value that have been set
243+
self.assertEqual(self.settings.value('firstgroup/key', section=QgsSettings.Core), 'value')
244+
self.assertEqual(self.settings.value('firstgroup/key2/subkey1', section=QgsSettings.Core), 'subvalue1')
245+
self.assertEqual(self.settings.value('firstgroup/key2/subkey2', section=QgsSettings.Core), 'subvalue2')
246+
self.assertEqual(self.settings.value('firstgroup/key3', section=QgsSettings.Core), 'value3')
247+
self.assertEqual(self.settings.value('firstgroup/key4', section=QgsSettings.Core), 'value4')
248+
# Clean up firstgroup
249+
self.settings.remove('firstgroup', section=QgsSettings.Core)
250+
227251
def test_array(self):
228252
self.assertEqual(self.settings.allKeys(), [])
229253
self.addArrayToDefaults('testqgissettings', 'key', ['qgisrocks1', 'qgisrocks2', 'qgisrocks3'])
@@ -361,6 +385,12 @@ def test_remove(self):
361385
self.settings.remove('testQgisSettings/temp')
362386
self.assertEqual(self.settings.value('testqQgisSettings/temp'), None)
363387

388+
# Test remove by using Section
389+
self.settings.setValue('testQgisSettings/tempSection', True, section=QgsSettings.Core)
390+
self.assertEqual(self.settings.value('testQgisSettings/tempSection', section=QgsSettings.Core), True)
391+
self.settings.remove('testQgisSettings/temp', section=QgsSettings.Core)
392+
self.assertEqual(self.settings.value('testqQgisSettings/temp', section=QgsSettings.Core), None)
393+
364394

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

0 commit comments

Comments
 (0)