Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings registry (QEP 124) part two settings introspection #42860

Merged
merged 13 commits into from
Apr 26, 2021
Merged
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgssettings.h *
* src/core/settings/qgssettings.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
Expand Down Expand Up @@ -279,7 +279,7 @@ Removes all entries in the user settings
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgssettings.h *
* src/core/settings/qgssettings.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
14 changes: 14 additions & 0 deletions python/core/auto_generated/settings/qgssettingsentry.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ 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;
%Docstring
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 :py:func:`~QgsSettingsEntryBase.key`.
%End

bool hasDynamicKey() const;
Expand Down
69 changes: 69 additions & 0 deletions python/core/auto_generated/settings/qgssettingsregistry.sip.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/settings/qgssettingsregistry.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/





class QgsSettingsRegistry
{
%Docstring(signature="appended")
:py:class:`QgsSettingsRegistry` is used for settings introspection and collects a
list of child :py:class:`QgsSettingsRegistry` and a list of child :py:class:`QgsSettingsRegistry`

.. versionadded:: 3.20
%End

%TypeHeaderCode
#include "qgssettingsregistry.h"
%End
public:

QgsSettingsRegistry();
%Docstring
Constructor for QgsSettingsRegistry.
%End

virtual ~QgsSettingsRegistry();

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 addChildSettingsRegistry( const QgsSettingsRegistry *settingsRegistry );
%Docstring
Add a child ``settingsRegistry`` to the register.
%End

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

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/settings/qgssettingsregistry.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@






class QgsSettingsRegistryCore
class QgsSettingsRegistryCore : QgsSettingsRegistry
{
%Docstring(signature="appended")
:py:class:`QgsSettingsRegistryCore` is used for settings introspection and collects all
Expand Down
3 changes: 2 additions & 1 deletion python/core/core_auto.sip
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@
%Include auto_generated/qgsruntimeprofiler.sip
%Include auto_generated/qgsscalecalculator.sip
%Include auto_generated/qgsscaleutils.sip
%Include auto_generated/qgssettings.sip
%Include auto_generated/qgssimplifymethod.sip
%Include auto_generated/qgssnappingconfig.sip
%Include auto_generated/qgssnappingutils.sip
Expand Down Expand Up @@ -602,7 +601,9 @@
%Include auto_generated/textrenderer/qgstextrenderer.sip
%Include auto_generated/textrenderer/qgstextrendererutils.sip
%Include auto_generated/textrenderer/qgstextshadowsettings.sip
%Include auto_generated/settings/qgssettings.sip
%Include auto_generated/settings/qgssettingsentry.sip
%Include auto_generated/settings/qgssettingsregistry.sip
%Include auto_generated/settings/qgssettingsregistrycore.sip
%Include auto_generated/validity/qgsabstractvaliditycheck.sip
%Include auto_generated/validity/qgsvaliditycheckcontext.sip
Expand Down
16 changes: 13 additions & 3 deletions src/app/qgssettingstree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#include "qgsvariantdelegate.h"
#include "qgslogger.h"
#include "qgssettings.h"
#include "qgssettingsentry.h"
#include "qgssettingsregistrycore.h"
#include "qgsapplication.h"

#include <QMenu>
Expand Down Expand Up @@ -319,7 +321,17 @@ QTreeWidgetItem *QgsSettingsTree::createItem( const QString &text,
item->setFlags( item->flags() | Qt::ItemIsEditable );

item->setData( 0, TypeRole, isGroup ? Group : Setting );
item->setData( 0, PathRole, mSettings->group().isEmpty() ? text : mSettings->group() + '/' + text );

QString completeSettingsPath = mSettings->group().isEmpty() ? text : mSettings->group() + '/' + text;
item->setData( 0, PathRole, completeSettingsPath );

// If settings registered add description
if ( !isGroup )
{
const QgsSettingsEntryBase *settingsEntry = QgsApplication::settingsRegistryCore()->getSettingsEntry( completeSettingsPath, true );
if ( settingsEntry != nullptr )
item->setText( 3, settingsEntry->description() );
}

QString key = itemKey( item );
QgsDebugMsgLevel( key, 4 );
Expand All @@ -332,8 +344,6 @@ QTreeWidgetItem *QgsSettingsTree::createItem( const QString &text,
item->setToolTip( 2, values.at( 1 ) );
}

// if ( settingsMap.contains(

return item;
}

Expand Down
6 changes: 4 additions & 2 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,6 @@ set(QGIS_CORE_SRCS
qgsvirtuallayerdefinitionutils.cpp
qgsmapthemecollection.cpp
qgsxmlutils.cpp
qgssettings.cpp
qgsarchive.cpp
qgstestutils.cpp
qgsziputils.cpp
Expand Down Expand Up @@ -717,7 +716,9 @@ set(QGIS_CORE_SRCS
geocms/geonode/qgsgeonodeconnection.cpp
geocms/geonode/qgsgeonoderequest.cpp

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

validity/qgsabstractvaliditycheck.cpp
Expand Down Expand Up @@ -1035,7 +1036,6 @@ set(QGIS_CORE_HDRS
qgsruntimeprofiler.h
qgsscalecalculator.h
qgsscaleutils.h
qgssettings.h
qgsshapegenerator.h
qgssimplifymethod.h
qgssnappingconfig.h
Expand Down Expand Up @@ -1561,7 +1561,9 @@ set(QGIS_CORE_HDRS
textrenderer/qgstextrendererutils.h
textrenderer/qgstextshadowsettings.h

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

validity/qgsabstractvaliditycheck.h
Expand Down
File renamed without changes.
File renamed without changes.
52 changes: 52 additions & 0 deletions src/core/settings/qgssettingsentry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,58 @@ QString QgsSettingsEntryBase::key( const QStringList &dynamicKeyPartList ) const
return completeKey;
}

bool QgsSettingsEntryBase::checkKey( const QString &key ) const
{
// Key to check
QString completeKeyToCheck = key;

QString settingsPrefix = QgsSettings().prefixedKey( "", section() );
settingsPrefix.chop( 1 );
if ( !completeKeyToCheck.startsWith( settingsPrefix ) )
{
if ( !mPluginName.isEmpty()
&& !completeKeyToCheck.startsWith( mPluginName ) )
{
if ( !completeKeyToCheck.startsWith( "/" ) )
completeKeyToCheck.prepend( "/" );
completeKeyToCheck.prepend( mPluginName );
}

if ( !completeKeyToCheck.startsWith( "/" ) )
completeKeyToCheck.prepend( "/" );
completeKeyToCheck.prepend( settingsPrefix );
}

// Prefixed settings key
QString prefixedSettingsKey = definitionKey();
if ( !prefixedSettingsKey.startsWith( settingsPrefix ) )
{
if ( !prefixedSettingsKey.startsWith( "/" ) )
prefixedSettingsKey.prepend( "/" );
prefixedSettingsKey.prepend( settingsPrefix );
}

if ( !hasDynamicKey() )
return completeKeyToCheck == prefixedSettingsKey;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably use QString::compare method for a string comparison.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pblottiere Thank you for the review! Why is QString::compare preferable over the == operator in this case? Compare return an integer with more information (greater/smaller/equal) and all i want is to know if the string are equal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case, it doesn't seem to be significant
https://forum.qt.io/topic/52155/compare-2-qstring-use-or-qstring-compare


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

QString QgsSettingsEntryBase::definitionKey() const
{
QString completeKey = mKey;
if ( !mPluginName.isEmpty() )
{
if ( !completeKey.startsWith( "/" ) )
completeKey.prepend( "/" );
completeKey.prepend( mPluginName );
}

return completeKey;
}

bool QgsSettingsEntryBase::hasDynamicKey() const
{
static const QRegularExpression regularExpression( "%\\d+" );
Expand Down
14 changes: 14 additions & 0 deletions src/core/settings/qgssettingsentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ 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
* 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.
*/
Expand Down
104 changes: 104 additions & 0 deletions src/core/settings/qgssettingsregistry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/***************************************************************************
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()
, mSettingsRegistryChildList()
{
}

QgsSettingsRegistry::~QgsSettingsRegistry()
{
}

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

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::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
{
return mSettingsRegistryChildList;
}
Loading