Skip to content
Permalink
Browse files
Removed Settings struct / unified Enum/Flag logic in one base class
  • Loading branch information
domi4484 committed Apr 14, 2021
1 parent be04888 commit 9762206731590faba536070ed74859133fde38b8
@@ -22,7 +22,7 @@
import qgis # required to get base class of enums


class PyQgsSettingsEntryEnum(QgsSettingsEntryBase):
class _PyQgsSettingsEntryEnumFlag(QgsSettingsEntryBase):
""" class QgsSettingsEntryEnum
ingroup core
An enum settings entry.
@@ -44,10 +44,13 @@ def __init__(self, key, pluginName, defaultValue, description=str()):
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.")
QgsLogger.debug("Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '{0}'".format(self.key()))
else:
defaultValueStr = self.__metaEnum.valueToKey(defaultValue)
self.__enumClass = defaultValue.__class__
if self.settingsType() is self.Enum:
defaultValueStr = self.__metaEnum.valueToKey(defaultValue)
else:
defaultValueStr = self.__metaEnum.valueToKeys(defaultValue)
self.__enumFlagClass = defaultValue.__class__

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

@@ -69,38 +72,76 @@ def defaultValue(self):
"""

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

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

# cast to the enum class
defaultValue = self.__enumClass(defaultValue)
defaultValue = self.__enumFlagClass(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.")
QgsLogger.debug("Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '{0}'".format(self.key()))
return False

enumKey = str()
enumKey = self.__metaEnum.valueToKey(value)
if not enumKey:
QgsLogger.debug("Invalid enum value '{0}'.".format(value))
enumFlagKey = str()
if self.settingsType() is self.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

super().setValue(enumKey, dynamicKeyPart)
return True
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 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 settingsType(self):
""" Get the settings entry type.
@@ -111,7 +152,7 @@ def settingsType(self):
return self.Enum


class PyQgsSettingsEntryFlag(QgsSettingsEntryBase):
class PyQgsSettingsEntryFlag(_PyQgsSettingsEntryEnumFlag):
""" class QgsSettingsEntryFlag
ingroup core
A flag settings entry.
@@ -130,15 +171,7 @@ def __init__(self, key, pluginName, defaultValue, description=str()):
.. 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)
super().__init__(key, pluginName, defaultValue, description)

def value(self, dynamicKeyPart=str()):
""" Get settings value.
@@ -151,46 +184,6 @@ def value(self, dynamicKeyPart=str()):
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.
@@ -614,10 +614,7 @@ should be canceled.
.. versionadded:: 3.10
%End

struct Settings
{
const QgsSettingsEntryStringList searchPathForTemplates;
};
const QgsSettingsEntryStringList settingsSearchPathForTemplates;

public slots:

@@ -141,12 +141,9 @@ This list is updated when preparing the search
.. versionadded:: 3.16
%End

struct Settings
{
const QgsSettingsEntryBool locatorFilterEnabled;
const QgsSettingsEntryBool locatorFilterDefault;
const QgsSettingsEntryString locatorFilterPrefix;
};
const QgsSettingsEntryBool settingsLocatorFilterEnabled;
const QgsSettingsEntryBool settingsLocatorFilterDefault;
const QgsSettingsEntryString settingsLocatorFilterPrefix;

signals:

@@ -101,10 +101,11 @@ The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key.
Get settings section. The settings section of the parent group is returned if available.
%End

virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;
virtual bool setVariantValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;
%Docstring
Set settings value.

The ``value`` to set.
The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key.
%End

@@ -162,10 +163,12 @@ The ``description`` argument specifies a description for the settings entry.
%End


virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;

bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;
%Docstring
\copydoc :py:class:`QgsSettingsEntryBase`.setValue
Set settings value.

The ``value`` to set.
The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key.
%End

QVariant value( const QString &dynamicKeyPart = QString() ) const;
@@ -203,7 +206,7 @@ A string settings entry.

QgsSettingsEntryString( const QString &key,
const QString &pluginName,
const QVariant &defaultValue = QVariant(),
const QString &defaultValue = QString(),
const QString &description = QString() );
%Docstring
Constructor for QgsSettingsEntryString.
@@ -215,10 +218,12 @@ The ``default`` value argument specifies the default value for the settings entr
The ``description`` argument specifies a description for the settings entry.
%End

virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;

bool setValue( const QString &value, const QString &dynamicKeyPart = QString() ) const;
%Docstring
\copydoc :py:class:`QgsSettingsEntryBase`.setValue
Set settings value.

The ``value`` to set.
The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key.
%End

QString value( const QString &dynamicKeyPart = QString() ) const;
@@ -281,7 +286,7 @@ A string list settings entry.

QgsSettingsEntryStringList( const QString &key,
const QString &pluginName,
const QVariant &defaultValue = QVariant(),
const QStringList &defaultValue = QStringList(),
const QString &description = QString() );
%Docstring
Constructor for QgsSettingsEntryStringList.
@@ -293,10 +298,12 @@ The ``default`` value argument specifies the default value for the settings entr
The ``description`` argument specifies a description for the settings entry.
%End

virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;

bool setValue( const QStringList &value, const QString &dynamicKeyPart = QString() ) const;
%Docstring
\copydoc :py:class:`QgsSettingsEntryBase`.setValue
Set settings value.

The ``value`` to set.
The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key.
%End

QStringList value( const QString &dynamicKeyPart = QString() ) const;
@@ -335,7 +342,7 @@ A boolean settings entry.

QgsSettingsEntryBool( const QString &key,
const QString &pluginName,
const QVariant &defaultValue = QVariant(),
bool defaultValue = false,
const QString &description = QString() );
%Docstring
Constructor for QgsSettingsEntryBool.
@@ -347,10 +354,12 @@ The ``default`` value argument specifies the default value for the settings entr
The ``description`` argument specifies a description for the settings entry.
%End

virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;

bool setValue( bool value, const QString &dynamicKeyPart = QString() ) const;
%Docstring
\copydoc :py:class:`QgsSettingsEntryBase`.setValue
Set settings value.

The ``value`` to set.
The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key.
%End

bool value( const QString &dynamicKeyPart = QString() ) const;
@@ -389,7 +398,7 @@ An integer settings entry.

QgsSettingsEntryInteger( const QString &key,
const QString &pluginName,
const QVariant &defaultValue = QVariant(),
qlonglong defaultValue = 0,
const QString &description = QString() );
%Docstring
Constructor for QgsSettingsEntryInteger.
@@ -401,11 +410,12 @@ The ``default`` value argument specifies the default value for the settings entr
The ``description`` argument specifies a description for the settings entry.
%End


virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;

bool setValue( qlonglong value, const QString &dynamicKeyPart = QString() ) const;
%Docstring
\copydoc :py:class:`QgsSettingsEntryBase`.setValue
Set settings value.

The ``value`` to set.
The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key.
%End

qlonglong value( const QString &dynamicKeyPart = QString() ) const;
@@ -468,7 +478,7 @@ A double settings entry.

QgsSettingsEntryDouble( const QString &key,
const QString &pluginName,
const QVariant &defaultValue = QVariant(),
double defaultValue,
const QString &description = QString() );
%Docstring
Constructor for QgsSettingsEntryDouble.
@@ -480,10 +490,12 @@ The ``default`` value argument specifies the default value for the settings entr
The ``description`` argument specifies a description for the settings entry.
%End

virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;

bool setValue( double value, const QString &dynamicKeyPart = QString() ) const;
%Docstring
\copydoc :py:class:`QgsSettingsEntryBase`.setValue
Set settings value.

The ``value`` to set.
The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key.
%End

double value( const QString &dynamicKeyPart = QString() ) const;
@@ -545,6 +557,8 @@ Returns how much decimals should be shown in the Gui.





/************************************************************************
* This file has been generated automatically from *
* *

0 comments on commit 9762206

Please sign in to comment.