Skip to content

Commit

Permalink
method to return location (global vs local) of a setting key
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Jan 16, 2023
1 parent 2a67d6c commit b440604
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 34 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_additions/qgis.py
Expand Up @@ -2215,6 +2215,13 @@
# --
Qgis.CoordinateDisplayType.baseClass = Qgis
# monkey patching scoped based enum
Qgis.SettingsLocation.Any.__doc__ = "From any lcoation"
Qgis.SettingsLocation.Global.__doc__ = "Global settings only"
Qgis.SettingsLocation.Local.__doc__ = "Local settings only"
Qgis.SettingsLocation.__doc__ = 'Settings Location\n\n.. versionadded:: 3.30\n\n' + '* ``Any``: ' + Qgis.SettingsLocation.Any.__doc__ + '\n' + '* ``Global``: ' + Qgis.SettingsLocation.Global.__doc__ + '\n' + '* ``Local``: ' + Qgis.SettingsLocation.Local.__doc__
# --
Qgis.SettingsLocation.baseClass = Qgis
# monkey patching scoped based enum
Qgis.ScriptLanguage.Css.__doc__ = "CSS"
Qgis.ScriptLanguage.QgisExpression.__doc__ = "QGIS expressions"
Qgis.ScriptLanguage.Html.__doc__ = "HTML"
Expand Down
7 changes: 7 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Expand Up @@ -1465,6 +1465,13 @@ The development version
CustomCrs,
};

enum class SettingsLocation
{
Any,
Global,
Local,
};

enum class ScriptLanguage
{
Css,
Expand Down
14 changes: 13 additions & 1 deletion python/core/auto_generated/settings/qgssettings.sip.in
Expand Up @@ -191,7 +191,7 @@ Returns a list of all keys, including subkeys, that can be read using the QSetti
%Docstring
Returns a list of all top-level keys that can be read using the QSettings object.
%End
QStringList childGroups() const;
QStringList childGroups( Qgis::SettingsLocation location = Qgis::SettingsLocation::Any ) const;
%Docstring
Returns a list of all key top-level groups that contain keys that can be read using the QSettings object.
%End
Expand Down Expand Up @@ -230,6 +230,17 @@ Closes the array that was started using :py:func:`~QgsSettings.beginReadArray` o
%Docstring
Sets the current array index to i. Calls to functions such as :py:func:`~QgsSettings.setValue`, :py:func:`~QgsSettings.value`,
:py:func:`~QgsSettings.remove`, and :py:func:`~QgsSettings.contains` will operate on the array entry at that index.
%End

Qgis::SettingsLocation location( const QString &key ) const;
%Docstring
Returns the location of the setting if it exists at the given ``key``

.. note::

it will return Qgis.SettingsLocation.Any if the key doesn't exist

.. versionadded:: 3.30
%End

void setValue( const QString &key, const QVariant &value, QgsSettings::Section section = QgsSettings::NoSection );
Expand Down Expand Up @@ -263,6 +274,7 @@ An optional Section argument can be used to get a value from a specific Section.
%End



bool contains( const QString &key, QgsSettings::Section section = QgsSettings::NoSection ) const;
%Docstring
Returns ``True`` if there exists a setting called key; returns ``False`` otherwise.
Expand Down
18 changes: 14 additions & 4 deletions python/core/auto_generated/settings/qgssettingsentry.sip.in
Expand Up @@ -203,6 +203,17 @@ The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key.
Returns ``True`` if the settings is contained in the underlying QSettings.

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

Qgis::SettingsLocation location( const QStringList &dynamicKeyPartList ) const;
%Docstring
Returns the location of the setting if it exists

.. note::

it will return Qgis.SettingsLocation.Any if the key doesn't exist

.. versionadded:: 3.30
%End

void remove( const QString &dynamicKeyPart = QString() ) const;
Expand Down Expand Up @@ -419,18 +430,17 @@ The ``options`` arguments specifies the options for the settings entry.
%Docstring
Returns settings value.

The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key.
The ``defaultValueOverride`` argument if valid is used instead of the normal default value.
\arg dynamicKeyPart specifies the dynamic part of the settings key.
%End

T value( const QStringList &dynamicKeyPartList ) const;
%Docstring
Returns settings value.

The ``dynamicKeyPartList`` argument specifies the list of dynamic parts of the settings key.
The ``defaultValueOverride`` argument if valid is used instead of the normal default value.
\arg dynamicKeyPartList specifies the list of dynamic parts of the settings key.
%End


T valueWithDefaultOverride( const T &defaultValueOverride, const QString &dynamicKeyPart = QString() ) const;
%Docstring
Returns the settings value with a ``defaultValueOverride`` and with an optional ``dynamicKeyPart``
Expand Down
Expand Up @@ -180,7 +180,13 @@ the named list element is used to store a group of settings under a dynamicly na
public:
virtual ~QgsSettingsTreeNamedListElement();

const QStringList items( const QStringList &parentsNamedItems = QStringList() ) const throw( QgsSettingsException );
QStringList items( const QStringList &parentsNamedItems = QStringList() ) const throw( QgsSettingsException );
%Docstring
Returns the list of items
\arg parentsNamedItems the list of named items in the parent named list (if any)
%End

QStringList items( Qgis::SettingsLocation location, const QStringList &parentsNamedItems = QStringList() ) const throw( QgsSettingsException );
%Docstring
Returns the list of items
\arg parentsNamedItems the list of named items in the parent named list (if any)
Expand Down
13 changes: 13 additions & 0 deletions src/core/qgis.h
Expand Up @@ -2527,6 +2527,19 @@ class CORE_EXPORT Qgis
};
Q_ENUM( CoordinateDisplayType )

/**
* Settings Location
*
* \since QGIS 3.30
*/
enum class SettingsLocation : int
{
Any, //!< From any lcoation
Global, //!< Global settings only
Local, //!< Local settings only
};
Q_ENUM( SettingsLocation )

/**
* Scripting languages.
*
Expand Down
43 changes: 31 additions & 12 deletions src/core/settings/qgssettings.cpp
Expand Up @@ -158,24 +158,32 @@ QStringList QgsSettings::childKeys() const
return keys;
}

QStringList QgsSettings::childGroups() const
QStringList QgsSettings::childGroups( Qgis::SettingsLocation location ) const
{
QStringList keys = mUserSettings->childGroups();
if ( mGlobalSettings )
switch ( location )
{
const QStringList constChildGroups = mGlobalSettings->childGroups();
std::copy_if( constChildGroups.constBegin(), constChildGroups.constEnd(), std::back_inserter( keys ), [&keys]( const QString & key ) {return !keys.contains( key );} );
case Qgis::SettingsLocation::Any:
{
QStringList keys = mUserSettings->childGroups();
if ( mGlobalSettings )
{
const QStringList constChildGroups = mGlobalSettings->childGroups();
std::copy_if( constChildGroups.constBegin(), constChildGroups.constEnd(), std::back_inserter( keys ), [&keys]( const QString & key ) {return !keys.contains( key );} );
}
return keys;
}

case Qgis::SettingsLocation::Local:
return mUserSettings->childGroups();

case Qgis::SettingsLocation::Global:
return mGlobalSettings ? mGlobalSettings->childGroups() : QStringList();
}
return keys;

}
QStringList QgsSettings::globalChildGroups() const
{
QStringList keys;
if ( mGlobalSettings )
{
keys = mGlobalSettings->childGroups();
}
return keys;
return childGroups( Qgis::SettingsLocation::Global );
}

QString QgsSettings::globalSettingsPath()
Expand Down Expand Up @@ -301,6 +309,17 @@ void QgsSettings::setArrayIndex( int i )
}
}

Qgis::SettingsLocation QgsSettings::location( const QString &key ) const
{
if ( mGlobalSettings && mGlobalSettings->contains( key ) )
return Qgis::SettingsLocation::Global;

if ( mUserSettings->contains( key ) )
return Qgis::SettingsLocation::Local;

return Qgis::SettingsLocation::Any;
}

void QgsSettings::setValue( const QString &key, const QVariant &value, const QgsSettings::Section section )
{
// TODO: add valueChanged signal
Expand Down
10 changes: 9 additions & 1 deletion src/core/settings/qgssettings.h
Expand Up @@ -233,7 +233,7 @@ class CORE_EXPORT QgsSettings : public QObject
//! Returns a list of all top-level keys that can be read using the QSettings object.
QStringList childKeys() const;
//! Returns a list of all key top-level groups that contain keys that can be read using the QSettings object.
QStringList childGroups() const;
QStringList childGroups( Qgis::SettingsLocation location = Qgis::SettingsLocation::Any ) const;
//! Returns a list of all key top-level groups (same as childGroups) but only for groups defined in global settings.
QStringList globalChildGroups() const;
//! Returns the path to the Global Settings QSettings storage file
Expand All @@ -258,6 +258,13 @@ class CORE_EXPORT QgsSettings : public QObject
*/
void setArrayIndex( int i );

/**
* Returns the location of the setting if it exists at the given \a key
* \note it will return Qgis::SettingsLocation::Any if the key doesn't exist
* \since QGIS 3.30
*/
Qgis::SettingsLocation location( const QString &key ) const;

/**
* Sets the value of setting key to value. If the key already exists, the previous value is overwritten.
* An optional Section argument can be used to set a value to a specific Section.
Expand Down Expand Up @@ -293,6 +300,7 @@ class CORE_EXPORT QgsSettings : public QObject
% End
#endif


#ifndef SIP_RUN

/**
Expand Down
5 changes: 5 additions & 0 deletions src/core/settings/qgssettingsentry.cpp
Expand Up @@ -207,6 +207,11 @@ bool QgsSettingsEntryBase::exists( const QStringList &dynamicKeyPartList ) const
return QgsSettings().contains( key( dynamicKeyPartList ) );
}

Qgis::SettingsLocation QgsSettingsEntryBase::location( const QStringList &dynamicKeyPartList ) const
{
return QgsSettings().location( key( dynamicKeyPartList ) );
}

void QgsSettingsEntryBase::remove( const QString &dynamicKeyPart ) const
{
QgsSettings().remove( key( dynamicKeyPart ) );
Expand Down
14 changes: 10 additions & 4 deletions src/core/settings/qgssettingsentry.h
Expand Up @@ -234,6 +234,13 @@ class CORE_EXPORT QgsSettingsEntryBase
*/
bool exists( const QStringList &dynamicKeyPartList ) const;

/**
* Returns the location of the setting if it exists
* \note it will return Qgis::SettingsLocation::Any if the key doesn't exist
* \since QGIS 3.30
*/
Qgis::SettingsLocation location( const QStringList &dynamicKeyPartList ) const;

/**
* Removes the settings from the underlying QSettings.
*
Expand Down Expand Up @@ -436,19 +443,18 @@ class QgsSettingsEntryByReference : public QgsSettingsEntryBase
/**
* Returns settings value.
*
* The \a dynamicKeyPart argument specifies the dynamic part of the settings key.
* The \a defaultValueOverride argument if valid is used instead of the normal default value.
* \arg dynamicKeyPart specifies the dynamic part of the settings key.
*/
T value( const QString &dynamicKeyPart = QString() ) const { return this->convertFromVariant( valueAsVariant( dynamicKeyPart ) );}

/**
* Returns settings value.
*
* The \a dynamicKeyPartList argument specifies the list of dynamic parts of the settings key.
* The \a defaultValueOverride argument if valid is used instead of the normal default value.
* \arg dynamicKeyPartList specifies the list of dynamic parts of the settings key.
*/
T value( const QStringList &dynamicKeyPartList ) const { return this->convertFromVariant( valueAsVariant( dynamicKeyPartList ) );}


//! Returns the settings value with a \a defaultValueOverride and with an optional \a dynamicKeyPart
T valueWithDefaultOverride( const T &defaultValueOverride, const QString &dynamicKeyPart = QString() ) const { return this->convertFromVariant( valueAsVariantWithDefaultOverride( defaultValueOverride, dynamicKeyPart ) );}

Expand Down
26 changes: 16 additions & 10 deletions src/core/settings/qgssettingstreeelement.cpp
Expand Up @@ -146,38 +146,44 @@ QgsSettingsTreeNamedListElement::~QgsSettingsTreeNamedListElement()
delete mSelectedItemSetting;
}

const QStringList QgsSettingsTreeNamedListElement::items( const QStringList &parentItems ) const

QStringList QgsSettingsTreeNamedListElement::items( const QStringList &parentNamedItems ) const
{
return items( Qgis::SettingsLocation::Any, parentNamedItems );
}

QStringList QgsSettingsTreeNamedListElement::items( Qgis::SettingsLocation location, const QStringList &parentNamedItems ) const
{
if ( namedElementsCount() - 1 != parentItems.count() )
throw QgsSettingsException( QObject::tr( "The number of given parent named items (%1) doesn't match with the number of named items in the key (%2)." ).arg( parentItems.count(), namedElementsCount() ) );
if ( namedElementsCount() - 1 != parentNamedItems.count() )
throw QgsSettingsException( QObject::tr( "The number of given parent named items (%1) doesn't match with the number of named items in the key (%2)." ).arg( parentNamedItems.count(), namedElementsCount() ) );

QgsSettings settings;
switch ( parentItems.count() )
switch ( parentNamedItems.count() )
{
case 0:
settings.beginGroup( completeKey() );
break;
case 1:
settings.beginGroup( completeKey().arg( parentItems[0] ) );
settings.beginGroup( completeKey().arg( parentNamedItems[0] ) );
break;
case 2:
settings.beginGroup( completeKey().arg( parentItems[0], parentItems[1] ) );
settings.beginGroup( completeKey().arg( parentNamedItems[0], parentNamedItems[1] ) );
break;
case 3:
settings.beginGroup( completeKey().arg( parentItems[0], parentItems[1], parentItems[2] ) );
settings.beginGroup( completeKey().arg( parentNamedItems[0], parentNamedItems[1], parentNamedItems[2] ) );
break;
case 4:
settings.beginGroup( completeKey().arg( parentItems[0], parentItems[1], parentItems[2], parentItems[3] ) );
settings.beginGroup( completeKey().arg( parentNamedItems[0], parentNamedItems[1], parentNamedItems[2], parentNamedItems[3] ) );
break;
case 5:
settings.beginGroup( completeKey().arg( parentItems[0], parentItems[1], parentItems[2], parentItems[3], parentItems[4] ) );
settings.beginGroup( completeKey().arg( parentNamedItems[0], parentNamedItems[1], parentNamedItems[2], parentNamedItems[3], parentNamedItems[4] ) );
break;
default:
throw QgsSettingsException( QObject::tr( "Current implementation of QgsSettingsTreeNamedListElement::items doesn't handle more than 5 parent named items" ) );
break;
}

return settings.childGroups();
return settings.childGroups( location );
}

void QgsSettingsTreeNamedListElement::setSelectedItem( const QString &item, const QStringList &parentsNamedItems )
Expand Down
9 changes: 8 additions & 1 deletion src/core/settings/qgssettingstreeelement.h
Expand Up @@ -18,6 +18,7 @@

#include <QObject>

#include "qgis.h"
#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgssettingsregistry.h"
Expand Down Expand Up @@ -185,7 +186,13 @@ class CORE_EXPORT QgsSettingsTreeNamedListElement : public QgsSettingsTreeElemen
* Returns the list of items
* \arg parentsNamedItems the list of named items in the parent named list (if any)
*/
const QStringList items( const QStringList &parentsNamedItems = QStringList() ) const SIP_THROW( QgsSettingsException );
QStringList items( const QStringList &parentsNamedItems = QStringList() ) const SIP_THROW( QgsSettingsException );

/**
* Returns the list of items
* \arg parentsNamedItems the list of named items in the parent named list (if any)
*/
QStringList items( Qgis::SettingsLocation location, const QStringList &parentsNamedItems = QStringList() ) const SIP_THROW( QgsSettingsException );


/**
Expand Down

0 comments on commit b440604

Please sign in to comment.