Skip to content

Commit

Permalink
Add group method to QgsSettings
Browse files Browse the repository at this point in the history
Is present in the QSettings API, but missing from QgsSettings
  • Loading branch information
nyalldawson committed Dec 4, 2018
1 parent a5bad2d commit ff786f2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions python/core/auto_generated/qgssettings.sip.in
Expand Up @@ -142,6 +142,18 @@ are based on the group. By default, no group is set.
%Docstring
Resets the group to what it was before the corresponding beginGroup() call.
%End

QString group() const;
%Docstring
Returns the current group.

.. seealso:: :py:func:`beginGroup`

.. seealso:: :py:func:`endGroup`

.. versionadded:: 3.6
%End

QStringList allKeys() const;
%Docstring
Returns a list of all keys, including subkeys, that can be read using the QSettings object.
Expand Down
4 changes: 4 additions & 0 deletions src/core/qgssettings.cpp
Expand Up @@ -102,6 +102,10 @@ void QgsSettings::endGroup()
}
}

QString QgsSettings::group() const
{
return mUserSettings->group();
}

QStringList QgsSettings::allKeys() const
{
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgssettings.h
Expand Up @@ -154,6 +154,15 @@ class CORE_EXPORT QgsSettings : public QObject
void beginGroup( const QString &prefix, QgsSettings::Section section = QgsSettings::NoSection );
//! Resets the group to what it was before the corresponding beginGroup() call.
void endGroup();

/**
* Returns the current group.
* \see beginGroup()
* \see endGroup()
* \since QGIS 3.6
*/
QString group() const;

//! Returns a list of all keys, including subkeys, that can be read using the QSettings object.
QStringList allKeys() const;
//! Returns a list of all top-level keys that can be read using the QSettings object.
Expand Down
8 changes: 8 additions & 0 deletions tests/src/python/test_qgssettings.py
Expand Up @@ -181,6 +181,7 @@ def test_groups(self):
self.addToDefaults('testqgissettings/name', 'qgisrocks')

self.settings.beginGroup('testqgissettings')
self.assertEqual(self.settings.group(), 'testqgissettings')
self.assertEqual(['names'], self.settings.childGroups())

self.settings.setValue('surnames/name1', 'qgisrocks-1')
Expand All @@ -189,11 +190,14 @@ def test_groups(self):
self.settings.setValue('names/name1', 'qgisrocks-1')
self.assertEqual('qgisrocks-1', self.settings.value('names/name1'))
self.settings.endGroup()
self.assertEqual(self.settings.group(), '')
self.settings.beginGroup('testqgissettings/names')
self.assertEqual(self.settings.group(), 'testqgissettings/names')
self.settings.setValue('name4', 'qgisrocks-4')
keys = sorted(self.settings.childKeys())
self.assertEqual(keys, ['name1', 'name2', 'name3', 'name4'])
self.settings.endGroup()
self.assertEqual(self.settings.group(), '')
self.assertEqual('qgisrocks-1', self.settings.value('testqgissettings/names/name1'))
self.assertEqual('qgisrocks-4', self.settings.value('testqgissettings/names/name4'))

Expand All @@ -205,9 +209,11 @@ def test_global_groups(self):
self.addToDefaults('testqgissettings/foo/last', 'rocks')

self.settings.beginGroup('testqgissettings')
self.assertEqual(self.settings.group(), 'testqgissettings')
self.assertEqual(['foo'], self.settings.childGroups())
self.assertEqual(['foo'], self.settings.globalChildGroups())
self.settings.endGroup()
self.assertEqual(self.settings.group(), '')

self.settings.setValue('testqgissettings/bar/first', 'qgis')
self.settings.setValue('testqgissettings/bar/last', 'rocks')
Expand All @@ -227,6 +233,7 @@ def test_global_groups(self):
def test_group_section(self):
# Test group by using Section
self.settings.beginGroup('firstgroup', section=QgsSettings.Core)
self.assertEqual(self.settings.group(), 'core/firstgroup')
self.assertEqual([], self.settings.childGroups())
self.settings.setValue('key', 'value')
self.settings.setValue('key2/subkey1', 'subvalue1')
Expand All @@ -237,6 +244,7 @@ def test_group_section(self):
self.assertEqual(['key', 'key3'], self.settings.childKeys())
self.assertEqual(['key2'], self.settings.childGroups())
self.settings.endGroup()
self.assertEqual(self.settings.group(), '')
# Set value by writing the group manually
self.settings.setValue('firstgroup/key4', 'value4', section=QgsSettings.Core)
# Checking the value that have been set
Expand Down

0 comments on commit ff786f2

Please sign in to comment.