Skip to content
Permalink
Browse files

Add group method to QgsSettings

Is present in the QSettings API, but missing from QgsSettings
  • Loading branch information
nyalldawson committed Dec 4, 2018
1 parent a5bad2d commit ff786f2223c65b58fbb290434c7a628e9215321f
@@ -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.
@@ -102,6 +102,10 @@ void QgsSettings::endGroup()
}
}

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

QStringList QgsSettings::allKeys() const
{
@@ -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.
@@ -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')
@@ -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'))

@@ -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')
@@ -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')
@@ -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

0 comments on commit ff786f2

Please sign in to comment.
You can’t perform that action at this time.