Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
move QgsSettingsEntryGroup to a separate file and deprecate it
  • Loading branch information
3nids committed Jan 16, 2023
1 parent 166ad00 commit 799ef39
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 223 deletions.
59 changes: 0 additions & 59 deletions python/core/auto_generated/settings/qgssettingsentry.sip.in
Expand Up @@ -11,65 +11,6 @@



class QgsSettingsEntryGroup
{
%Docstring(signature="appended")
Creates a group of setting which have a common definition of base key

.. versionadded:: 3.26
%End

%TypeHeaderCode
#include "qgssettingsentry.h"
%End
public:
QgsSettingsEntryGroup( QList<const QgsSettingsEntryBase *> settings );
%Docstring
Constructor
%End
%MethodCode
sipCpp = new QgsSettingsEntryGroup( *a0, false );
sipIsErr = sipCpp->isValid() ? 0 : 1;
if ( sipIsErr )
PyErr_SetString( PyExc_ValueError, QStringLiteral( "Settings do not share the same base definition key for this group. This will lead to unpredictable results." ).toUtf8().constData() );
%End


bool isValid() const;
%Docstring
Returns if the group is valid (if settings share the same base key)
%End

QString baseKey( const QStringList &dynamicKeyPartList = QStringList() ) const;
%Docstring
Returns the base key for the given ``dynamicKeyPartList``
%End

const QList<const QgsSettingsEntryBase *> settings() const;
%Docstring
Returns all the settings
%End

void removeAllSettingsAtBaseKey( const QStringList &dynamicKeyPartList = QStringList() ) const;
%Docstring
Removes all the settings at the base key for the given ``dynamicKeyPartList``
This means it might remove more settings than the ones registered in the group, use with caution
%End

void removeAllChildrenSettings( const QString &dynamicKeyPart = QString() ) const;
%Docstring
Removes all the settings from this group
The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key.
%End

void removeAllChildrenSettings( const QStringList &dynamicKeyPartList ) const;
%Docstring
Removes all the settings from this group
The ``dynamicKeyPartList`` argument specifies the dynamic part of the settings key.
%End

};

class QgsSettingsEntryBase
{
%Docstring(signature="appended")
Expand Down
83 changes: 83 additions & 0 deletions python/core/auto_generated/settings/qgssettingsentrygroup.sip.in
@@ -0,0 +1,83 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/settings/qgssettingsentrygroup.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/






class QgsSettingsEntryGroup /Deprecated/
{
%Docstring(signature="appended")
Creates a group of setting which have a common definition of base key

.. deprecated:: QGIS 3.30
use :py:class:`QgsSettingsTreeElement` instead

.. versionadded:: 3.26
%End

%TypeHeaderCode
#include "qgssettingsentrygroup.h"
%End
public:
QgsSettingsEntryGroup( QList<const QgsSettingsEntryBase *> settings );
%Docstring
Constructor
%End
%MethodCode
sipCpp = new QgsSettingsEntryGroup( *a0, false );
sipIsErr = sipCpp->isValid() ? 0 : 1;
if ( sipIsErr )
PyErr_SetString( PyExc_ValueError, QStringLiteral( "Settings do not share the same base definition key for this group. This will lead to unpredictable results." ).toUtf8().constData() );
%End


bool isValid() const;
%Docstring
Returns if the group is valid (if settings share the same base key)
%End

QString baseKey( const QStringList &dynamicKeyPartList = QStringList() ) const;
%Docstring
Returns the base key for the given ``dynamicKeyPartList``
%End

const QList<const QgsSettingsEntryBase *> settings() const;
%Docstring
Returns all the settings
%End

void removeAllSettingsAtBaseKey( const QStringList &dynamicKeyPartList = QStringList() ) const;
%Docstring
Removes all the settings at the base key for the given ``dynamicKeyPartList``
This means it might remove more settings than the ones registered in the group, use with caution
%End

void removeAllChildrenSettings( const QString &dynamicKeyPart = QString() ) const;
%Docstring
Removes all the settings from this group
The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key.
%End

void removeAllChildrenSettings( const QStringList &dynamicKeyPartList ) const;
%Docstring
Removes all the settings from this group
The ``dynamicKeyPartList`` argument specifies the dynamic part of the settings key.
%End

};


/************************************************************************
* This file has been generated automatically from *
* *
* src/core/settings/qgssettingsentrygroup.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
Expand Up @@ -96,7 +96,6 @@ Returns settings value.
typedef PyObject *( *pyqt5_from_qvariant_by_type )( QVariant &value, PyObject *type );
QVariant value;

// QSettings has an internal mutex so release the GIL to avoid the possibility of deadlocks.
value = sipCpp->value();

pyqt5_from_qvariant_by_type f = ( pyqt5_from_qvariant_by_type ) sipImportSymbol( "pyqt5_from_qvariant_by_type" );
Expand Down
Expand Up @@ -66,11 +66,13 @@ Returns the list of registered child QgsSettingsRegistry.
Adds ``settingsEntry`` to the registry.
%End

void addSettingsEntryGroup( const QgsSettingsEntryGroup *settingsGroup );
void addSettingsEntryGroup( const QgsSettingsEntryGroup *settingsGroup ) /Deprecated/;
%Docstring
Adds a group of setting to the registry

.. versionadded:: 3.26

.. deprecated:: QGIS 3.30
%End

};
Expand Down
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -702,6 +702,7 @@
%Include auto_generated/textrenderer/qgstextshadowsettings.sip
%Include auto_generated/settings/qgssettings.sip
%Include auto_generated/settings/qgssettingsentry.sip
%Include auto_generated/settings/qgssettingsentrygroup.sip
%Include auto_generated/settings/qgssettingsentryimpl.sip
%Include auto_generated/settings/qgssettingsregistry.sip
%Include auto_generated/settings/qgssettingsregistrycore.sip
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -855,6 +855,7 @@ set(QGIS_CORE_SRCS

settings/qgssettings.cpp
settings/qgssettingsentry.cpp
settings/qgssettingsentrygroup.cpp
settings/qgssettingsentryimpl.cpp
settings/qgssettingsregistry.cpp
settings/qgssettingsregistrycore.cpp
Expand Down Expand Up @@ -1856,6 +1857,7 @@ set(QGIS_CORE_HDRS

settings/qgssettings.h
settings/qgssettingsentry.h
settings/qgssettingsentrygroup.h
settings/qgssettingsentryenumflag.h
settings/qgssettingsentryimpl.h
settings/qgssettingsregistry.h
Expand Down
86 changes: 0 additions & 86 deletions src/core/settings/qgssettingsentry.cpp
Expand Up @@ -20,92 +20,6 @@
#include <QDir>



QgsSettingsEntryGroup::QgsSettingsEntryGroup( QList<const QgsSettingsEntryBase *> settings, bool fatalErrorIfInvalid )
: mSettings( settings )
{
for ( const auto *setting : std::as_const( mSettings ) )
{
QString otherBaseKey = setting->definitionKey();
otherBaseKey = otherBaseKey.left( otherBaseKey.lastIndexOf( QLatin1Char( '/' ) ) );
if ( mDefinitionBaseKey.isEmpty() )
{
mDefinitionBaseKey = otherBaseKey;
}
else
{
if ( mDefinitionBaseKey != otherBaseKey )
{
QgsDebugMsg( "Settings do not share the same base definition key for this group. This will lead to unpredictable results." );
if ( fatalErrorIfInvalid )
Q_ASSERT( false );
mIsValid = false;
}
}
}
}

QString QgsSettingsEntryGroup::baseKey( const QStringList &dynamicKeyPartList ) const
{
QString key = mDefinitionBaseKey;

if ( dynamicKeyPartList.isEmpty() )
{
if ( hasDynamicKey() )
QgsDebugMsg( QStringLiteral( "Settings group '%1' have a dynamic key but the dynamic key part was not provided" ).arg( key ) );

return key;
}
else
{
if ( !hasDynamicKey() )
{
QgsDebugMsg( QStringLiteral( "Settings group '%1' don't have a dynamic key, the provided dynamic key part will be ignored" ).arg( key ) );
return key;
}

for ( int i = 0; i < dynamicKeyPartList.size(); i++ )
{
key.replace( QStringLiteral( "%" ).append( QString::number( i + 1 ) ), dynamicKeyPartList.at( i ) );
}
}

return key;
}

void QgsSettingsEntryGroup::removeAllSettingsAtBaseKey( const QStringList &dynamicKeyPartList ) const
{
QString key = baseKey( dynamicKeyPartList );
// https://regex101.com/r/kICr42/1
const thread_local QRegularExpression regularExpression( QStringLiteral( "^(\\/?(qgis\\/?)?)?$" ) );
if ( key.contains( regularExpression ) )
{
QgsDebugMsg( QStringLiteral( "Preventing mass removal of settings at key %1" ).arg( key ) );
return;
}

QgsSettings settings;
settings.remove( key );
}

void QgsSettingsEntryGroup::removeAllChildrenSettings( const QString &dynamicKeyPart ) const
{
removeAllChildrenSettings( QgsSettingsEntryBase::dynamicKeyPartToList( dynamicKeyPart ) );
}

void QgsSettingsEntryGroup::removeAllChildrenSettings( const QStringList &dynamicKeyPartList ) const
{
for ( const auto *setting : mSettings )
setting->remove( dynamicKeyPartList );
}

bool QgsSettingsEntryGroup::hasDynamicKey() const
{
const thread_local QRegularExpression regularExpression( QStringLiteral( "%\\d+" ) );
return mDefinitionBaseKey.contains( regularExpression );
}


QgsSettingsEntryBase::QgsSettingsEntryBase( const QString &key, QgsSettingsTreeElement *parentTreeElement, const QVariant &defaultValue, const QString &description, Qgis::SettingsOptions options )
: mParentTreeElement( parentTreeElement )
, mDefaultValue( defaultValue )
Expand Down
62 changes: 0 additions & 62 deletions src/core/settings/qgssettingsentry.h
Expand Up @@ -25,71 +25,9 @@
#include "qgis_sip.h"
#include "qgssettings.h"

class QgsSettingsEntryBase;
class QgsSettingsTreeElement;


/**
* \ingroup core
* \class QgsSettingsEntryGroup
* \brief Creates a group of setting which have a common definition of base key
*
* \since QGIS 3.26
*/
class CORE_EXPORT QgsSettingsEntryGroup
{
public:
//! Constructor
QgsSettingsEntryGroup( QList<const QgsSettingsEntryBase *> settings )
: QgsSettingsEntryGroup( settings, true )
{}
#ifdef SIP_RUN
% MethodCode
sipCpp = new QgsSettingsEntryGroup( *a0, false );
sipIsErr = sipCpp->isValid() ? 0 : 1;
if ( sipIsErr )
PyErr_SetString( PyExc_ValueError, QStringLiteral( "Settings do not share the same base definition key for this group. This will lead to unpredictable results." ).toUtf8().constData() );
% End
#endif

//! Constructor
QgsSettingsEntryGroup( QList<const QgsSettingsEntryBase *> settings, bool fatalErrorIfInvalid ) SIP_SKIP;

//! Returns if the group is valid (if settings share the same base key)
bool isValid() const {return mIsValid;}

//! Returns the base key for the given \a dynamicKeyPartList
QString baseKey( const QStringList &dynamicKeyPartList = QStringList() ) const;

//! Returns all the settings
const QList<const QgsSettingsEntryBase *> settings() const {return mSettings;}

/**
* Removes all the settings at the base key for the given \a dynamicKeyPartList
* This means it might remove more settings than the ones registered in the group, use with caution
*/
void removeAllSettingsAtBaseKey( const QStringList &dynamicKeyPartList = QStringList() ) const;

/**
* Removes all the settings from this group
* The \a dynamicKeyPart argument specifies the dynamic part of the settings key.
*/
void removeAllChildrenSettings( const QString &dynamicKeyPart = QString() ) const;

/**
* Removes all the settings from this group
* The \a dynamicKeyPartList argument specifies the dynamic part of the settings key.
*/
void removeAllChildrenSettings( const QStringList &dynamicKeyPartList ) const;

private:
bool hasDynamicKey() const;

QList<const QgsSettingsEntryBase *> mSettings;
QString mDefinitionBaseKey;
bool mIsValid = true;
};

/**
* \ingroup core
* \class QgsSettingsEntryBase
Expand Down

0 comments on commit 799ef39

Please sign in to comment.