Skip to content
Permalink
Browse files
Merge pull request #42597 from domi4484/settingsRegistryStaticInline
Settings registry (QEP 124)
  • Loading branch information
3nids committed Apr 15, 2021
2 parents 84c2ab4 + 97f1a64 commit fbb7eaaf3284aa5d08969833ba3095b7d3ffae7e
@@ -34,6 +34,7 @@ from .additions.qgsfeature import mapping_feature
from .additions.qgsfunction import register_function, qgsfunction
from .additions.qgsgeometry import _geometryNonZero, mapping_geometry
from .additions.qgssettings import _qgssettings_enum_value, _qgssettings_set_enum_value, _qgssettings_flag_value
from .additions.qgssettingsentry import PyQgsSettingsEntryEnum, PyQgsSettingsEntryFlag
from .additions.qgstaskwrapper import QgsTaskWrapper
from .additions.readwritecontextentercategory import ReadWriteContextEnterCategory
from .additions.runtimeprofiler import ScopedRuntimeProfileContextManager
@@ -57,6 +58,10 @@ QgsTask.fromFunction = fromFunction
QgsDateTimeRange.__repr__ = datetime_range_repr
QgsDateRange.__repr__ = date_range_repr

# Classes patched
QgsSettingsEntryEnum = PyQgsSettingsEntryEnum
QgsSettingsEntryFlag = PyQgsSettingsEntryFlag

# Classes patched using a derived class
QgsProviderMetadata = PyProviderMetadata

@@ -110,7 +110,7 @@ def _qgssettings_flag_value(self, key, flagDefaultValue, section=QgsSettings.NoS
# this should not happen
raise ValueError("could not get the meta enum for given enum default value (type: {})".format(type(flagDefaultValue)))

str_val = self.value(key, meta_enum.valueToKey(flagDefaultValue), str, section)
str_val = self.value(key, meta_enum.valueToKeys(flagDefaultValue), str, section)
# need a new meta enum as QgsSettings.value is making a copy and leads to seg fault (probably a PyQt issue)
meta_enum_2 = metaEnumFromValue(flagDefaultValue)
(flag_val, ok) = meta_enum_2.keysToValue(str_val)
@@ -0,0 +1,183 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
qgssettingsentry.py
---------------------
Date : April 2021
Copyright : (C) 2021 by Damiano Lombardi
Email : damiano@opengis.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. *
* *
***************************************************************************
"""

from .metaenum import metaEnumFromValue
from qgis.core import QgsSettings, QgsSettingsEntryBase, QgsLogger
import qgis # required to get base class of enums


class _PyQgsSettingsEntryEnumFlag(QgsSettingsEntryBase):
""" class QgsSettingsEntryEnum
ingroup core
An enum settings entry.
since QGIS 3.20
"""

def __init__(self, key, pluginName, defaultValue, description=str()):
""" Constructor for _PyQgsSettingsEntryEnumFlag.
:param self: the QgsSettingsEntryEnum object
:param key: argument specifies the final part of the settings key.
:param pluginName: argument is inserted in the key after the section.
:param defaultValue: argument specifies the default value for the settings entry.
:param description: argument specifies a description for the settings entry.
.. note:: This constructor should not be used. Use PyQgsSettingsEntryEnum
or PyQgsSettingsEntryFlag instead.
"""

defaultValueStr = str()
self.__metaEnum = metaEnumFromValue(defaultValue)
if self.__metaEnum is None or not self.__metaEnum.isValid():
QgsLogger.debug("Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '{0}'".format(self.key()))
else:
if self.settingsType() is self.SettingsType.Enum:
defaultValueStr = self.__metaEnum.valueToKey(defaultValue)
else:
defaultValueStr = self.__metaEnum.valueToKeys(defaultValue)
self.__enumFlagClass = defaultValue.__class__

super().__init__(key, pluginName, defaultValueStr, description)

def value(self, dynamicKeyPart=None, useDefaultValueOverride=False, defaultValueOverride=None):
""" Get settings value.
:param self: the _PyQgsSettingsEntryEnumFlag object
:param dynamicKeyPart: argument specifies the dynamic part of the settings key.
:param useDefaultValueOverride: argument specifies if defaultValueOverride should be used.
:param defaultValueOverride: argument if valid is used instead of the normal default value.
"""

defaultValue = self.defaultValue()
if useDefaultValueOverride:
defaultValue = defaultValueOverride

if self.settingsType() is self.SettingsType.Enum:
return QgsSettings().enumValue(self.key(dynamicKeyPart),
defaultValue,
self.section())
else:
return QgsSettings().flagValue(self.key(dynamicKeyPart),
defaultValue,
self.section())

def defaultValue(self):
""" Get settings default value.
:param self: the _PyQgsSettingsEntryEnumFlag object
"""

if self.__metaEnum is None or not self.__metaEnum.isValid():
QgsLogger.debug("Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '{0}'".format(self.key()))
return -1

defaultValueString = self.defaultValueAsVariant()
if self.settingsType() is self.SettingsType.Enum:
(defaultValue, ok) = self.__metaEnum.keyToValue(defaultValueString)
else:
(defaultValue, ok) = self.__metaEnum.keysToValue(defaultValueString)
if not ok:
QgsLogger.debug("Invalid enum/flag key/s '{0}'.".format(self.defaultValueAsVariant()))
return -1

# cast to the enum class
defaultValue = self.__enumFlagClass(defaultValue)
return defaultValue

def setValue(self, value, dynamicKeyPart=None):
""" Set settings value.
:param self: the _PyQgsSettingsEntryEnumFlag object
:param dynamicKeyPart: argument specifies the dynamic part of the settings key.
"""

if self.__metaEnum is None or not self.__metaEnum.isValid():
QgsLogger.debug("Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '{0}'".format(self.key()))
return False

enumFlagKey = str()
if self.settingsType() is self.SettingsType.Enum:
enumFlagKey = self.__metaEnum.valueToKey(value)
else:
enumFlagKey = self.__metaEnum.valueToKeys(value)
if not enumFlagKey:
QgsLogger.debug("Invalid enum/flag value '{0}'.".format(value))
return False

return super().setVariantValue(enumFlagKey, dynamicKeyPart)


class PyQgsSettingsEntryEnum(_PyQgsSettingsEntryEnumFlag):
""" class QgsSettingsEntryEnum
ingroup core
An enum settings entry.
since QGIS 3.20
"""

def __init__(self, key, pluginName, defaultValue, description=str()):
""" Constructor for QgsSettingsEntryEnum.
:param self: the QgsSettingsEntryEnum object
:param key: argument specifies the final part of the settings key.
:param pluginName: argument is inserted in the key after the section.
:param defaultValue: argument specifies the default value for the settings entry.
:param description: argument specifies a description for the settings entry.
.. note:: The enum needs to be declared with Q_ENUM.
"""

super().__init__(key, pluginName, defaultValue, description)

def settingsType(self):
""" Get the settings entry type.
:param self: the QgsSettingsEntryEnum object
"""

return self.SettingsType.Enum


class PyQgsSettingsEntryFlag(_PyQgsSettingsEntryEnumFlag):
""" class QgsSettingsEntryFlag
ingroup core
A flag settings entry.
since QGIS 3.20
"""

def __init__(self, key, pluginName, defaultValue, description=str()):
""" Constructor for QgsSettingsEntryFlag.
:param self: the QgsSettingsEntryFlag object
:param key: argument specifies the final part of the settings key.
:param pluginName: argument is inserted in the key after the section.
:param defaultValue: argument specifies the default value for the settings entry.
:param description: argument specifies a description for the settings entry.
.. note:: The flag needs to be declared with Q_FLAG (not Q_FLAGS).
"""

super().__init__(key, pluginName, defaultValue, description)

def settingsType(self):
""" Get the settings entry type.
:param self: the QgsSettingsEntryFlag object
"""

return self.SettingsType.Flag
@@ -0,0 +1,12 @@
# The following has been generated automatically from src/core/settings/qgssettingsentry.h
# monkey patching scoped based enum
QgsSettingsEntryBase.SettingsType.Variant.__doc__ = ""
QgsSettingsEntryBase.SettingsType.String.__doc__ = ""
QgsSettingsEntryBase.SettingsType.StringList.__doc__ = ""
QgsSettingsEntryBase.SettingsType.Bool.__doc__ = ""
QgsSettingsEntryBase.SettingsType.Integer.__doc__ = ""
QgsSettingsEntryBase.SettingsType.Double.__doc__ = ""
QgsSettingsEntryBase.SettingsType.Enum.__doc__ = ""
QgsSettingsEntryBase.SettingsType.Flag.__doc__ = ""
QgsSettingsEntryBase.SettingsType.__doc__ = '\n\n' + '* ``Variant``: ' + QgsSettingsEntryBase.SettingsType.Variant.__doc__ + '\n' + '* ``String``: ' + QgsSettingsEntryBase.SettingsType.String.__doc__ + '\n' + '* ``StringList``: ' + QgsSettingsEntryBase.SettingsType.StringList.__doc__ + '\n' + '* ``Bool``: ' + QgsSettingsEntryBase.SettingsType.Bool.__doc__ + '\n' + '* ``Integer``: ' + QgsSettingsEntryBase.SettingsType.Integer.__doc__ + '\n' + '* ``Double``: ' + QgsSettingsEntryBase.SettingsType.Double.__doc__ + '\n' + '* ``Enum``: ' + QgsSettingsEntryBase.SettingsType.Enum.__doc__ + '\n' + '* ``Flag``: ' + QgsSettingsEntryBase.SettingsType.Flag.__doc__
# --
@@ -614,6 +614,8 @@ should be canceled.
.. versionadded:: 3.10
%End

const QgsSettingsEntryStringList settingsSearchPathForTemplates;

public slots:

void refresh();
@@ -141,6 +141,10 @@ This list is updated when preparing the search
.. versionadded:: 3.16
%End

const QgsSettingsEntryBool settingsLocatorFilterEnabled;
const QgsSettingsEntryBool settingsLocatorFilterDefault;
const QgsSettingsEntryString settingsLocatorFilterPrefix;

signals:

void foundResult( const QgsLocatorResult &result );
@@ -657,6 +657,13 @@ Returns the application's task manager, used for managing application
wide background task handling.

.. versionadded:: 3.0
%End

static QgsSettingsRegistryCore *settingsRegistryCore() /KeepReference/;
%Docstring
Returns the application's settings registry, used for managing application settings.

.. versionadded:: 3.20
%End

static QgsColorSchemeRegistry *colorSchemeRegistry() /KeepReference/;

0 comments on commit fbb7eaa

Please sign in to comment.