Skip to content
Permalink
Browse files
Added api to add child settings registries to settings registry
  • Loading branch information
domi4484 committed Apr 18, 2021
1 parent 6e1ef0e commit 68ee4442ca1803dd0fa27017c024896bb0440591
@@ -31,14 +31,31 @@ Constructor for QgsSettingsRegistry.

virtual ~QgsSettingsRegistry();

const QgsSettingsEntryBase *getSettingsEntry( const QString &key );
void addSettingsEntry( const QgsSettingsEntryBase *settingsEntry );
%Docstring
Add ``settingsEntry`` to the register.
%End

QList<const QgsSettingsEntryBase *> getChildSettingsEntries() const;
%Docstring
Returns the list of registered :py:class:`QgsSettingsEntryBase`.
%End

const QgsSettingsEntryBase *getSettingsEntry( const QString &key, bool searchChildRegistries = true ) const;
%Docstring
Returns the :py:class:`QgsSettingsEntry` with the given ``key`` or None if not found.

The ``searchChildRegistries`` parameter specifies if child registries should be included in the search
%End

void addSettingsEntry( const QgsSettingsEntryBase *settingsEntry );
void addChildSettingsRegistry( const QgsSettingsRegistry *settingsRegistry );
%Docstring
Add ``settingsEntry`` to the register.
Add a child ``settingsRegistry`` to the register.
%End

QList<const QgsSettingsRegistry *> getChildSettingsRegistries() const;
%Docstring
Returns the list of registered child QgsSettingsRegistry.
%End

};
@@ -27,33 +27,78 @@

QgsSettingsRegistry::QgsSettingsRegistry()
: mSettingsEntriesMap()
, mDynamicSettingsEntriesMap()
, mSettingsRegistryChildList()
{
}

QgsSettingsRegistry::~QgsSettingsRegistry()
{
}

const QgsSettingsEntryBase *QgsSettingsRegistry::getSettingsEntry( const QString &key )
void QgsSettingsRegistry::addSettingsEntry( const QgsSettingsEntryBase *settingsEntry )
{
if ( mSettingsEntriesMap.contains( key ) )
return mSettingsEntriesMap.value( key );
if ( settingsEntry == nullptr )
{
QgsLogger::warning( QStringLiteral( "Trying to register a nullptr settings entry." ) );
return;
}

const QMap<QString, const QgsSettingsEntryBase *> dynamicSettingsEntriesMap = mDynamicSettingsEntriesMap;
for ( const QgsSettingsEntryBase *settingsEntry : dynamicSettingsEntriesMap )
if ( mSettingsEntriesMap.contains( settingsEntry->definitionKey() ) )
{
QgsLogger::warning( QStringLiteral( "Settings with key '%1' is already registered." ).arg( settingsEntry->definitionKey() ) );
return;
}

mSettingsEntriesMap.insert( settingsEntry->definitionKey(), settingsEntry );
}

QList<const QgsSettingsEntryBase *> QgsSettingsRegistry::getChildSettingsEntries() const
{
return mSettingsEntriesMap.values();
}

const QgsSettingsEntryBase *QgsSettingsRegistry::getSettingsEntry( const QString &key, bool searchChildRegistries ) const
{
// Search in this registry
const QMap<QString, const QgsSettingsEntryBase *> settingsEntriesMap = mSettingsEntriesMap;
for ( const QgsSettingsEntryBase *settingsEntry : settingsEntriesMap )
{
if ( settingsEntry->checkKey( key ) )
return settingsEntry;
}

// Search in child registries
if ( searchChildRegistries )
{
for ( const QgsSettingsRegistry *settingsRegistry : mSettingsRegistryChildList )
{
const QgsSettingsEntryBase *settingsEntry = settingsRegistry->getSettingsEntry( key, true );
if ( settingsEntry != nullptr )
return settingsEntry;
}
}

return nullptr;
}

void QgsSettingsRegistry::addSettingsEntry( const QgsSettingsEntryBase *settingsEntry )
void QgsSettingsRegistry::addChildSettingsRegistry( const QgsSettingsRegistry *settingsRegistry )
{
if ( settingsRegistry == nullptr )
{
QgsLogger::warning( QStringLiteral( "Trying to register a nullptr child settings registry." ) );
return;
}

if ( mSettingsRegistryChildList.contains( settingsRegistry ) )
{
QgsLogger::warning( QStringLiteral( "Child register is already registered." ) );
return;
}

mSettingsRegistryChildList.append( settingsRegistry );
}

QList<const QgsSettingsRegistry *> QgsSettingsRegistry::getChildSettingsRegistries() const
{
if ( settingsEntry->hasDynamicKey() )
mDynamicSettingsEntriesMap.insert( settingsEntry->definitionKey(), settingsEntry );
else
mSettingsEntriesMap.insert( settingsEntry->key(), settingsEntry );
return mSettingsRegistryChildList;
}
@@ -45,20 +45,38 @@ class CORE_EXPORT QgsSettingsRegistry
*/
virtual ~QgsSettingsRegistry();

/**
* Add \a settingsEntry to the register.
*/
void addSettingsEntry( const QgsSettingsEntryBase *settingsEntry );

/**
* Returns the list of registered QgsSettingsEntryBase.
*/
QList<const QgsSettingsEntryBase *> getChildSettingsEntries() const;

/**
* Returns the QgsSettingsEntry with the given \a key or nullptr if not found.
*
* The \a searchChildRegistries parameter specifies if child registries should be included in the search
*/
const QgsSettingsEntryBase *getSettingsEntry( const QString &key );
const QgsSettingsEntryBase *getSettingsEntry( const QString &key, bool searchChildRegistries = true ) const;

/**
* Add \a settingsEntry to the register.
* Add a child \a settingsRegistry to the register.
*/
void addSettingsEntry( const QgsSettingsEntryBase *settingsEntry );
void addChildSettingsRegistry( const QgsSettingsRegistry *settingsRegistry );

/**
* Returns the list of registered child QgsSettingsRegistry.
*/
QList<const QgsSettingsRegistry *> getChildSettingsRegistries() const;

private:

QMap<QString, const QgsSettingsEntryBase *> mSettingsEntriesMap;
QMap<QString, const QgsSettingsEntryBase *> mDynamicSettingsEntriesMap;

QList<const QgsSettingsRegistry *> mSettingsRegistryChildList;

};

@@ -32,6 +32,7 @@ class TestQgsSettingsRegistry : public QObject
private slots:
void getSettingsEntries();
void getSettingsEntriesWithDynamicKeys();
void childRegistry();
};

void TestQgsSettingsRegistry::getSettingsEntries()
@@ -44,6 +45,7 @@ void TestQgsSettingsRegistry::getSettingsEntries()
QString settingsEntryInexisting( "/qgis/testing/settingsEntryInexisting" );

QgsSettingsRegistry settingsRegistry;
settingsRegistry.addSettingsEntry( nullptr ); // should not crash
settingsRegistry.addSettingsEntry( &settingsEntryBool );
settingsRegistry.addSettingsEntry( &settingsEntryInteger );

@@ -77,5 +79,27 @@ void TestQgsSettingsRegistry::getSettingsEntriesWithDynamicKeys()
QCOMPARE( settingsRegistry.getSettingsEntry( settingsEntryInexisting ), nullptr );
}

void TestQgsSettingsRegistry::childRegistry()
{
QString settingsEntryBoolKey( "/qgis/testing/settingsEntryBool" );
QgsSettingsEntryBool settingsEntryBool( settingsEntryBoolKey, QgsSettings::NoSection, false );
QString settingsEntryIntegerKey( "/qgis/testing/settingsEntryInteger" );
QgsSettingsEntryBool settingsEntryInteger( settingsEntryIntegerKey, QgsSettings::NoSection, 123 );

QgsSettingsRegistry settingsRegistryChild;
settingsRegistryChild.addSettingsEntry( &settingsEntryInteger );

QgsSettingsRegistry settingsRegistry;
settingsRegistry.addSettingsEntry( &settingsEntryBool );
settingsRegistry.addChildSettingsRegistry( nullptr ); // should not crash
settingsRegistry.addChildSettingsRegistry( &settingsRegistryChild );

// Search only in parent
QCOMPARE( settingsRegistry.getSettingsEntry( settingsEntryIntegerKey, false ), nullptr );

// Search including child registries
QCOMPARE( settingsRegistry.getSettingsEntry( settingsEntryIntegerKey, true ), &settingsEntryInteger );
}

QGSTEST_MAIN( TestQgsSettingsRegistry )
#include "testqgssettingsregistry.moc"
@@ -341,6 +341,7 @@ ADD_PYTHON_TEST(PyQgsDBManagerSpatialite test_db_manager_spatialite.py)
ADD_PYTHON_TEST(PyQgsFileDownloader test_qgsfiledownloader.py)
ADD_PYTHON_TEST(PyQgsSettings test_qgssettings.py)
ADD_PYTHON_TEST(PyQgsSettingsEntry test_qgssettingsentry.py)
ADD_PYTHON_TEST(PyQgsSettingsRegistry test_qgssettingsregistry.py)
ADD_PYTHON_TEST(PyQgsZipUtils test_qgsziputils.py)
ADD_PYTHON_TEST(PyQgsSourceSelectProvider test_qgssourceselectprovider.py)
ADD_PYTHON_TEST(PyQgsAuthManagerProxy test_authmanager_proxy.py)
@@ -10,12 +10,8 @@
(at your option) any later version.
"""

import os
import tempfile
from qgis.core import QgsSettings, QgsSettingsEntryBase, QgsSettingsEntryVariant, QgsSettingsEntryString, QgsSettingsEntryStringList, QgsSettingsEntryBool, QgsSettingsEntryInteger, QgsSettingsEntryDouble, QgsSettingsEntryEnum, QgsSettingsEntryFlag, QgsUnitTypes, QgsMapLayerProxyModel
from qgis.testing import start_app, unittest
from qgis.PyQt.QtCore import QSettings, QVariant, QMetaEnum
from pathlib import Path

__author__ = 'Damiano Lombardi'
__date__ = '02/04/2021'
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
"""
Test the PyQgsSettingsRegistry classes
Run with: ctest -V -R PyQgsSettingsRegistry
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""

from qgis.core import QgsSettingsRegistry, QgsSettingsEntryBase, QgsSettingsEntryInteger, QgsApplication
from qgis.testing import start_app, unittest

__author__ = 'Damiano Lombardi'
__date__ = '18/04/2021'
__copyright__ = 'Copyright 2021, The QGIS Project'


start_app()


class PyQgsSettingsRegistry(unittest.TestCase):

cnt = 0

def setUp(self):
self.pluginName = "UnitTestSettingsRegistry"

def tearDown(self):
pass

def test_settings_registry(self):

settingsEntryKey = "settingsRegistry/integerValue"
settingsEntry = QgsSettingsEntryInteger(settingsEntryKey, self.pluginName, 123)

settingsRegistry = QgsSettingsRegistry()
settingsRegistry.addSettingsEntry(settingsEntry)

# check get settings entry
self.assertEqual(settingsRegistry.getSettingsEntry(settingsEntry.key(), False), settingsEntry)

# add registry to core registry
QgsApplication.settingsRegistryCore().addChildSettingsRegistry(settingsRegistry)

self.assertEqual(QgsApplication.settingsRegistryCore().getSettingsEntry(settingsEntry.key(), True), settingsEntry)


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

0 comments on commit 68ee444

Please sign in to comment.