Skip to content

Commit

Permalink
testqgssettingsregistry
Browse files Browse the repository at this point in the history
  • Loading branch information
domi4484 committed Apr 16, 2021
1 parent 2087a7f commit 6e1ef0e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/settings/qgssettingsentry.sip.in
Expand Up @@ -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;
Expand Down
16 changes: 13 additions & 3 deletions src/core/settings/qgssettingsentry.cpp
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/core/settings/qgssettingsentry.h
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/core/settings/qgssettingsregistry.cpp
Expand Up @@ -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;
}

Expand Down
15 changes: 10 additions & 5 deletions tests/src/core/testqgssettingsregistry.cpp
Expand Up @@ -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 );
}

Expand Down

0 comments on commit 6e1ef0e

Please sign in to comment.