Skip to content
Permalink
Browse files
Added QgsSettingsRegistry base class
  • Loading branch information
domi4484 committed Apr 15, 2021
1 parent 432224a commit 65163814ff15f58c46eadb33985360a241baed02

Large diffs are not rendered by default.

@@ -10,7 +10,7 @@



class QgsSettingsRegistryCore
class QgsSettingsRegistryCore : QgsSettingsRegistry
{
%Docstring(signature="appended")
:py:class:`QgsSettingsRegistryCore` is used for settings introspection and collects all
@@ -31,16 +31,6 @@ Constructor for QgsSettingsRegistryCore.

virtual ~QgsSettingsRegistryCore();

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

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

};

/************************************************************************
@@ -718,6 +718,7 @@ set(QGIS_CORE_SRCS
geocms/geonode/qgsgeonoderequest.cpp

settings/qgssettingsentry.cpp
settings/qgssettingsregistry.cpp
settings/qgssettingsregistrycore.cpp

validity/qgsabstractvaliditycheck.cpp
@@ -1562,6 +1563,7 @@ set(QGIS_CORE_HDRS
textrenderer/qgstextshadowsettings.h

settings/qgssettingsentry.h
settings/qgssettingsregistry.h
settings/qgssettingsregistrycore.h

validity/qgsabstractvaliditycheck.h
@@ -80,6 +80,18 @@ QString QgsSettingsEntryBase::key( const QStringList &dynamicKeyPartList ) const
completeKey.replace( QString( "%%1" ).arg( QString::number( i + 1 ) ), dynamicKeyPartList.at( i ) );
}
}
}

QString QgsSettingsEntryBase::definitionKey() const
{
QString completeKey = mKey;
if ( !mPluginName.isEmpty() )
{
if ( completeKey.startsWith( '/' ) )
completeKey.remove( 0, 1 );
completeKey.prepend( mPluginName + "/" );
}

return completeKey;
}

@@ -119,6 +119,13 @@ class CORE_EXPORT QgsSettingsEntryBase
*/
QString key( const QStringList &dynamicKeyPartList ) const;

/**
* Returns settings entry defining key.
* For dynamic settings it return the key with the placeholder for dynamic part
* included. For non-dynamic settings returns the same as key().
*/
QString definitionKey() const;

/**
* Returns true if a part of the settings key is built dynamically.
*/
@@ -0,0 +1,61 @@
/***************************************************************************
qgssettingsregistry.cpp
--------------------------------------
Date : February 2021
Copyright : (C) 2021 by Damiano Lombardi
Email : damiano at opengis dot ch
***************************************************************************
* *
* 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. *
* *
***************************************************************************/

#include "qgssettingsregistry.h"

#include "qgslayout.h"
#include "qgslocator.h"
#include "qgsnetworkaccessmanager.h"
#include "qgsnewsfeedparser.h"
#include "qgsprocessing.h"
#include "qgsapplication.h"
#include "qgsgeometryoptions.h"
#include "qgslocalizeddatapathregistry.h"
#include "qgsmaprendererjob.h"

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

QgsSettingsRegistry::~QgsSettingsRegistry()
{
}

const QgsSettingsEntryBase *QgsSettingsRegistry::getSettingsEntry( const QString &key )
{
if ( mSettingsEntriesMap.contains( key ) )
return mSettingsEntriesMap.value( key );

const QMap<QString, const QgsSettingsEntryBase *> dynamicSettingsEntriesMap = mDynamicSettingsEntriesMap;
for ( const QgsSettingsEntryBase *settingsEntry : dynamicSettingsEntriesMap )
{
QRegularExpression regularExpression( settingsEntry->key( ".*" ) );
QRegularExpressionMatch regularExpresisonMatch = regularExpression.match( key );
if ( regularExpresisonMatch.hasMatch() )
return settingsEntry;
}

return nullptr;
}

void QgsSettingsRegistry::addSettingsEntry( const QgsSettingsEntryBase *settingsEntry )
{
if ( settingsEntry->hasDynamicKey() )
mDynamicSettingsEntriesMap.insert( settingsEntry->definitionKey(), settingsEntry );
else
mSettingsEntriesMap.insert( settingsEntry->key(), settingsEntry );
}
@@ -0,0 +1,65 @@
/***************************************************************************
qgssettingsregistry.h
--------------------------------------
Date : February 2021
Copyright : (C) 2021 by Damiano Lombardi
Email : damiano at opengis dot ch
***************************************************************************
* *
* 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. *
* *
***************************************************************************/


#ifndef QGSSETTINGSREGISTRY_H
#define QGSSETTINGSREGISTRY_H

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgssettingsentry.h"

#include <QMap>

/**
* \ingroup core
* \class QgsSettingsRegistry
* QgsSettingsRegistry is used for settings introspection and collects a
* list of child QgsSettingsRegistry and a list of child QgsSettingsRegistry
*
* \since QGIS 3.20
*/
class CORE_EXPORT QgsSettingsRegistry
{
public:

/**
* Constructor for QgsSettingsRegistry.
*/
QgsSettingsRegistry();

/**
* Destructor for QgsSettingsRegistry.
*/
virtual ~QgsSettingsRegistry();

/**
* Returns the QgsSettingsEntry with the given \a key or nullptr if not found.
*/
const QgsSettingsEntryBase *getSettingsEntry( const QString &key );

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

private:

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

};

#endif // QGSSETTINGSREGISTRY_H
@@ -26,7 +26,7 @@
#include "qgsmaprendererjob.h"

QgsSettingsRegistryCore::QgsSettingsRegistryCore()
: mSettingsEntriesMap()
: QgsSettingsRegistry()
{
addSettingsEntry( &QgsLayout::settingsSearchPathForTemplates );

@@ -62,28 +62,3 @@ QgsSettingsRegistryCore::QgsSettingsRegistryCore()
QgsSettingsRegistryCore::~QgsSettingsRegistryCore()
{
}

const QgsSettingsEntryBase *QgsSettingsRegistryCore::getSettingsEntry( const QString &key )
{
if ( mSettingsEntriesMap.contains( key ) )
return mSettingsEntriesMap.value( key );

const QMap<QString, const QgsSettingsEntryBase *> dynamicSettingsEntriesMap = mDynamicSettingsEntriesMap;
for ( const QgsSettingsEntryBase *settingsEntry : dynamicSettingsEntriesMap )
{
QRegularExpression regularExpression( settingsEntry->key( ".*" ) );
QRegularExpressionMatch regularExpresisonMatch = regularExpression.match( key );
if ( regularExpresisonMatch.hasMatch() )
return settingsEntry;
}

return nullptr;
}

void QgsSettingsRegistryCore::addSettingsEntry( const QgsSettingsEntryBase *settingsEntry )
{
if ( settingsEntry->hasDynamicKey() )
mDynamicSettingsEntriesMap.insert( settingsEntry->key(), settingsEntry );
else
mSettingsEntriesMap.insert( settingsEntry->key(), settingsEntry );
}
@@ -19,7 +19,7 @@

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgssettingsentry.h"
#include "qgssettingsregistry.h"

#include <QMap>

@@ -31,7 +31,7 @@
*
* \since QGIS 3.20
*/
class CORE_EXPORT QgsSettingsRegistryCore
class CORE_EXPORT QgsSettingsRegistryCore : public QgsSettingsRegistry
{
public:

@@ -45,21 +45,6 @@ class CORE_EXPORT QgsSettingsRegistryCore
*/
virtual ~QgsSettingsRegistryCore();

/**
* Returns the QgsSettingsEntry with the given \a key or nullptr if not found.
*/
const QgsSettingsEntryBase *getSettingsEntry( const QString &key );

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

private:

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

};

#endif // QGSSETTINGSREGISTRYCORE_H
@@ -189,6 +189,7 @@ set(TESTS
testqgsruntimeprofiler.cpp
testqgssettings.cpp
testqgssettingsentry.cpp
testqgssettingsregistry.cpp
testqgsshapeburst.cpp
testqgssimplemarker.cpp
testqgssnappingutils.cpp
@@ -23,7 +23,7 @@

/**
* \ingroup UnitTests
* This is a unit test for the operations on curve geometries
* This is a unit test for the enum and flag functions of QgsSettings
*/
class TestQgsSettings : public QObject
{
@@ -24,7 +24,8 @@

/**
* \ingroup UnitTests
* This is a unit test for the operations on curve geometries
* This is a unit test for QgsSettingsEntry classes
* \note Most functions are covered in the python test
*/
class TestQgsSettingsEntry : public QObject
{
@@ -0,0 +1,76 @@
/***************************************************************************
testqgssettingregistry.cpp
--------------------------------------
Date : 08.04.2021
Copyright : (C) 2021 by Damiano Lombardi
Email : damiano@opengis.ch
***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include <QObject>

#include "qgssettingsentry.h"
#include "qgssettingsregistry.h"
#include "qgsunittypes.h"
#include "qgsmaplayerproxymodel.h"
#include "qgstest.h"


/**
* \ingroup UnitTests
* This is a unit test for the QgsSettingsRegistry classes
*/
class TestQgsSettingsRegistry : public QObject
{
Q_OBJECT

private slots:
void getSettingsEntries();
void getSettingsEntriesWithDynamicKeys();
};

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

QString settingsEntryInexisting( "/qgis/testing/settingsEntryInexisting" );

QgsSettingsRegistry settingsRegistry;
settingsRegistry.addSettingsEntry( &settingsEntryBool );
settingsRegistry.addSettingsEntry( &settingsEntryInteger );

QCOMPARE( settingsRegistry.getSettingsEntry( settingsEntryBoolKey ), &settingsEntryBool );
QCOMPARE( settingsRegistry.getSettingsEntry( settingsEntryIntegerKey ), &settingsEntryInteger );
QCOMPARE( settingsRegistry.getSettingsEntry( settingsEntryInexisting ), nullptr );
}

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

QString settingsEntryInexisting( "/qgis/testing/settingsEntryInexisting%" );

QgsSettingsRegistry settingsRegistry;
settingsRegistry.addSettingsEntry( &settingsEntryBool );
settingsRegistry.addSettingsEntry( &settingsEntryInteger );

QCOMPARE( settingsRegistry.getSettingsEntry( settingsEntryBoolKey ), &settingsEntryBool );
QCOMPARE( settingsRegistry.getSettingsEntry( settingsEntryBoolKey.replace( "%", "1st" ) ), &settingsEntryBool );
QCOMPARE( settingsRegistry.getSettingsEntry( settingsEntryIntegerKey.replace( "%", "Second" ) ), &settingsEntryInteger );
QCOMPARE( settingsRegistry.getSettingsEntry( settingsEntryIntegerKey ), &settingsEntryInteger );
QCOMPARE( settingsRegistry.getSettingsEntry( settingsEntryInexisting ), nullptr );
}

QGSTEST_MAIN( TestQgsSettingsRegistry )
#include "testqgssettingsregistry.moc"

0 comments on commit 6516381

Please sign in to comment.