Skip to content
Permalink
Browse files
testqgssettingsregistry
  • Loading branch information
domi4484 committed Apr 16, 2021
1 parent 2087a7f commit 6e1ef0e988a91f1945b58f1db123ec9fe3989aa1
@@ -82,6 +82,13 @@ The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key.
Get settings entry key.

The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key.
%End

bool checkKey( const QString &key ) const;
%Docstring
Returns true if the provided key match the settings entry

The ``key`` to check
%End

QString definitionKey() const;
@@ -83,14 +83,24 @@ QString QgsSettingsEntryBase::key( const QStringList &dynamicKeyPartList ) const
return completeKey;
}

bool QgsSettingsEntryBase::checkKey( const QString &key ) const
{
if ( !hasDynamicKey() )
return key == QgsSettingsEntryBase::key();

QRegularExpression regularExpression( definitionKey().replace( QRegularExpression( "%\\d+" ), ".*" ) );
QRegularExpressionMatch regularExpresisonMatch = regularExpression.match( key );
return regularExpresisonMatch.hasMatch();
}

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

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

/**
* Returns true if the provided key match the settings entry
*
* The \a key to check
*/
bool checkKey( const QString &key ) const;

/**
* Returns settings entry defining key.
* For dynamic settings it return the key with the placeholder for dynamic part
@@ -43,9 +43,7 @@ const QgsSettingsEntryBase *QgsSettingsRegistry::getSettingsEntry( const QString
const QMap<QString, const QgsSettingsEntryBase *> dynamicSettingsEntriesMap = mDynamicSettingsEntriesMap;
for ( const QgsSettingsEntryBase *settingsEntry : dynamicSettingsEntriesMap )
{
QRegularExpression regularExpression( settingsEntry->key( ".*" ) );
QRegularExpressionMatch regularExpresisonMatch = regularExpression.match( key );
if ( regularExpresisonMatch.hasMatch() )
if ( settingsEntry->checkKey( key ) )
return settingsEntry;
}

@@ -54,21 +54,26 @@ void TestQgsSettingsRegistry::getSettingsEntries()

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

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

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

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

0 comments on commit 6e1ef0e

Please sign in to comment.