Skip to content

Commit

Permalink
Python implementation for QgsSettingsEntryEnum/Flag and fixed docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
domi4484 committed Apr 7, 2021
1 parent 4e959f9 commit 3c5f9b1
Show file tree
Hide file tree
Showing 13 changed files with 349 additions and 72 deletions.
5 changes: 5 additions & 0 deletions python/core/__init__.py.in
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion python/core/additions/qgssettings.py
Expand Up @@ -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)
Expand Down
200 changes: 200 additions & 0 deletions python/core/additions/qgssettingsentry.py
@@ -0,0 +1,200 @@
# -*- 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 PyQgsSettingsEntryEnum(QgsSettingsEntryBase):
""" 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.
"""

defaultValueStr = str()
self.__metaEnum = metaEnumFromValue(defaultValue)
if self.__metaEnum is None or not self.__metaEnum.isValid():
QgsLogger.debug("Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration.")
else:
defaultValueStr = self.__metaEnum.valueToKey(defaultValue)
self.__enumClass = defaultValue.__class__

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

def value(self, dynamicKeyPart=str()):
""" Get settings value.
:param self: the QgsSettingsEntryEnum object
:param dynamicKeyPart: argument specifies the dynamic part of the settings key.
"""

return QgsSettings().enumValue(self.key(dynamicKeyPart),
self.defaultValue(),
self.section())

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

if self.__metaEnum is None or not self.__metaEnum.isValid():
QgsLogger.debug("Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration.")
return -1

defaultValueString = self.defaultValueAsVariant()
(defaultValue, ok) = self.__metaEnum.keyToValue(defaultValueString)
if not ok:
QgsLogger.debug("Invalid enum key '{0}'.".format(self.defaultValueAsVariant()))
return -1

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

def setValue(self, value, dynamicKeyPart=str()):
""" Set settings value.
:param self: the QgsSettingsEntryEnum 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 probably misses Q_ENUM or Q_FLAG declaration.")
return False

enumKey = str()
enumKey = self.__metaEnum.valueToKey(value)
if not enumKey:
QgsLogger.debug("Invalid enum value '{0}'.".format(value))
return False

super().setValue(enumKey, dynamicKeyPart)
return True

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

return self.Enum


class PyQgsSettingsEntryFlag(QgsSettingsEntryBase):
""" 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).
"""

defaultValueStr = str()
self.__metaEnum = metaEnumFromValue(defaultValue)
if self.__metaEnum is None or not self.__metaEnum.isValid():
QgsLogger.debug("Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration.")
else:
defaultValueStr = self.__metaEnum.valueToKeys(defaultValue)
self.__flagClass = defaultValue.__class__

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

def value(self, dynamicKeyPart=str()):
""" Get settings value.
:param self: the QgsSettingsEntryFlag object
:param dynamicKeyPart: argument specifies the dynamic part of the settings key.
"""

return QgsSettings().flagValue(self.key(dynamicKeyPart),
self.defaultValue(),
self.section())

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

if self.__metaEnum is None or not self.__metaEnum.isValid():
QgsLogger.debug("Invalid metaenum. Flag probably misses Q_ENUM or Q_FLAG declaration.")
return -1

defaultValueString = self.defaultValueAsVariant()
(defaultValue, ok) = self.__metaEnum.keysToValue(defaultValueString)
if not ok:
QgsLogger.debug("Invalid flag keys '{0}'.".format(self.defaultValueAsVariant()))
return -1

# cast to the flag class
defaultValue = self.__flagClass(defaultValue)
return defaultValue

def setValue(self, value, dynamicKeyPart=str()):
""" Set settings value.
:param self: the QgsSettingsEntryFlag 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. Flag probably misses Q_ENUM or Q_FLAG declaration.")
return False

flagKeys = str()
flagKeys = self.__metaEnum.valueToKeys(value)
if not flagKeys:
QgsLogger.debug("Invalid flag value '{0}'.".format(value))
return False

super().setValue(flagKeys, dynamicKeyPart)
return True

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

return self.Flag
1 change: 0 additions & 1 deletion python/core/auto_generated/layout/qgslayout.sip.in
Expand Up @@ -614,7 +614,6 @@ should be canceled.
.. versionadded:: 3.10
%End


struct Settings
{
const QgsSettingsEntryStringList searchPathForTemplates;
Expand Down
24 changes: 12 additions & 12 deletions python/core/auto_generated/settings/qgssettingsentry.sip.in
Expand Up @@ -165,7 +165,7 @@ The ``description`` argument specifies a description for the settings entry.
virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;

%Docstring
\copydoc :py:class:`QgsSettingsEntry`.setValue
\copydoc :py:class:`QgsSettingsEntryBase`.setValue
%End

QVariant value( const QString &dynamicKeyPart = QString() ) const;
Expand All @@ -182,7 +182,7 @@ Get settings default value.

virtual SettingsType settingsType() const;
%Docstring
\copydoc :py:class:`QgsSettingsEntry`.settingsType
\copydoc :py:class:`QgsSettingsEntryBase`.settingsType
%End
};

Expand Down Expand Up @@ -218,7 +218,7 @@ The ``description`` argument specifies a description for the settings entry.
virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;

%Docstring
\copydoc :py:class:`QgsSettingsEntry`.setValue
\copydoc :py:class:`QgsSettingsEntryBase`.setValue
%End

QString value( const QString &dynamicKeyPart = QString() ) const;
Expand All @@ -235,7 +235,7 @@ Get settings default value.

virtual SettingsType settingsType() const;
%Docstring
\copydoc :py:class:`QgsSettingsEntry`.settingsType
\copydoc :py:class:`QgsSettingsEntryBase`.settingsType
%End

void setMinLength( int minLength );
Expand Down Expand Up @@ -296,7 +296,7 @@ The ``description`` argument specifies a description for the settings entry.
virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;

%Docstring
\copydoc :py:class:`QgsSettingsEntry`.setValue
\copydoc :py:class:`QgsSettingsEntryBase`.setValue
%End

QStringList value( const QString &dynamicKeyPart = QString() ) const;
Expand All @@ -313,7 +313,7 @@ Get settings default value.

virtual SettingsType settingsType() const;
%Docstring
\copydoc :py:class:`QgsSettingsEntry`.settingsType
\copydoc :py:class:`QgsSettingsEntryBase`.settingsType
%End

};
Expand Down Expand Up @@ -350,7 +350,7 @@ The ``description`` argument specifies a description for the settings entry.
virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;

%Docstring
\copydoc :py:class:`QgsSettingsEntry`.setValue
\copydoc :py:class:`QgsSettingsEntryBase`.setValue
%End

bool value( const QString &dynamicKeyPart = QString() ) const;
Expand All @@ -367,7 +367,7 @@ Get settings default value.

virtual SettingsType settingsType() const;
%Docstring
\copydoc :py:class:`QgsSettingsEntry`.settingsType
\copydoc :py:class:`QgsSettingsEntryBase`.settingsType
%End
};

Expand Down Expand Up @@ -405,7 +405,7 @@ The ``description`` argument specifies a description for the settings entry.
virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;

%Docstring
\copydoc :py:class:`QgsSettingsEntry`.setValue
\copydoc :py:class:`QgsSettingsEntryBase`.setValue
%End

qlonglong value( const QString &dynamicKeyPart = QString() ) const;
Expand All @@ -422,7 +422,7 @@ Get settings default value.

virtual SettingsType settingsType() const;
%Docstring
\copydoc :py:class:`QgsSettingsEntry`.settingsType
\copydoc :py:class:`QgsSettingsEntryBase`.settingsType
%End

void setMinValue( qlonglong minValue );
Expand Down Expand Up @@ -483,7 +483,7 @@ The ``description`` argument specifies a description for the settings entry.
virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;

%Docstring
\copydoc :py:class:`QgsSettingsEntry`.setValue
\copydoc :py:class:`QgsSettingsEntryBase`.setValue
%End

double value( const QString &dynamicKeyPart = QString() ) const;
Expand All @@ -500,7 +500,7 @@ Get settings default value.

virtual SettingsType settingsType() const;
%Docstring
\copydoc :py:class:`QgsSettingsEntry`.settingsType
\copydoc :py:class:`QgsSettingsEntryBase`.settingsType
%End

void setMinValue( double minValue );
Expand Down
Expand Up @@ -15,7 +15,8 @@
class QgsSettingsRegistryCore
{
%Docstring(signature="appended")

:py:class:`QgsSettingsRegistryCore` is used for settings introspection and collects all
:py:class:`QgsSettingsEntry` instances of core.

.. versionadded:: 3.20
%End
Expand All @@ -26,7 +27,11 @@ class QgsSettingsRegistryCore
public:

QgsSettingsRegistryCore();
~QgsSettingsRegistryCore();
%Docstring
Constructor for QgsSettingsRegistryCore.
%End

virtual ~QgsSettingsRegistryCore();

};

Expand Down

0 comments on commit 3c5f9b1

Please sign in to comment.