Skip to content
Permalink
Browse files

Python QgsSettingsEnum and Flag unified in QgsSettingsEnumFlag

  • Loading branch information
domi4484 committed Apr 26, 2021
1 parent 65583b4 commit 1def5e7b94a652842c23edbdfb66d49eaf56b2be
@@ -34,7 +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.qgssettingsentry import PyQgsSettingsEntryEnumFlag
from .additions.qgstaskwrapper import QgsTaskWrapper
from .additions.readwritecontextentercategory import ReadWriteContextEnterCategory
from .additions.runtimeprofiler import ScopedRuntimeProfileContextManager
@@ -59,8 +59,7 @@ QgsDateTimeRange.__repr__ = datetime_range_repr
QgsDateRange.__repr__ = date_range_repr

# Classes patched
QgsSettingsEntryEnum = PyQgsSettingsEntryEnum
QgsSettingsEntryFlag = PyQgsSettingsEntryFlag
QgsSettingsEntryEnumFlag = PyQgsSettingsEntryEnumFlag

# Classes patched using a derived class
QgsProviderMetadata = PyProviderMetadata
@@ -22,15 +22,15 @@
import qgis # required to get base class of enums


class _PyQgsSettingsEntryEnumFlag(QgsSettingsEntryBase):
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.
""" Constructor for PyQgsSettingsEntryEnumFlag.
:param self: the QgsSettingsEntryEnum object
:param key: argument specifies the final part of the settings key.
@@ -47,18 +47,18 @@ def __init__(self, key, pluginName, defaultValue, description=str()):
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:
if self.__metaEnum.isFlag():
defaultValueStr = self.__metaEnum.valueToKeys(defaultValue)
else:
defaultValueStr = self.__metaEnum.valueToKey(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 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.
@@ -68,30 +68,30 @@ def value(self, dynamicKeyPart=None, useDefaultValueOverride=False, defaultValue
if useDefaultValueOverride:
defaultValue = defaultValueOverride

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

def defaultValue(self):
""" Get settings default value.
:param self: the _PyQgsSettingsEntryEnumFlag object
: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:
if self.__metaEnum.isFlag():
(defaultValue, ok) = self.__metaEnum.keysToValue(defaultValueString)
else:
(defaultValue, ok) = self.__metaEnum.keyToValue(defaultValueString)
if not ok:
QgsLogger.debug("Invalid enum/flag key/s '{0}'.".format(self.defaultValueAsVariant()))
return -1
@@ -103,7 +103,7 @@ def defaultValue(self):
def setValue(self, value, dynamicKeyPart=None):
""" Set settings value.
:param self: the _PyQgsSettingsEntryEnumFlag object
:param self: the PyQgsSettingsEntryEnumFlag object
:param dynamicKeyPart: argument specifies the dynamic part of the settings key.
"""

@@ -112,72 +112,20 @@ def setValue(self, value, dynamicKeyPart=None):
return False

enumFlagKey = str()
if self.settingsType() is self.SettingsType.Enum:
enumFlagKey = self.__metaEnum.valueToKey(value)
else:
if self.__metaEnum.isFlag():
enumFlagKey = self.__metaEnum.valueToKeys(value)
else:
enumFlagKey = self.__metaEnum.valueToKey(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
:param self: the QgsSettingsEntryEnumFlag object
"""

return self.SettingsType.Flag
return self.SettingsType.EnumFlag
@@ -321,7 +321,6 @@ The contents of the reply will be returned after the request is completed or an
.. versionadded:: 3.6
%End

<<<<<<< HEAD
void requestAuthOpenBrowser( const QUrl &url ) const;
%Docstring
Forwards an external browser login ``url`` opening request to the authentication handler.
@@ -343,9 +342,9 @@ Forwards an external browser login closure request to the authentication handler

.. versionadded:: 3.20
%End
=======

const QgsSettingsEntryInteger settingsNetworkTimeout;
>>>>>>> 2087a7f4f8 (Removed settings struct)


signals:

@@ -758,7 +758,6 @@ Returns how much decimals should be shown in the Gui.
};



template <T>
class QgsSettingsEntryEnumFlag : QgsSettingsEntryBase
{
@@ -779,7 +778,7 @@ A template class for enum and flag settings entry.

QgsSettingsEntryEnumFlag( const QString &key, QgsSettings::Section section, const T &defaultValue, const QString &description = QString() );
%Docstring
Constructor for QgsSettingsEntryEnumFlag.
Constructor for :py:class:`QgsSettingsEntryEnumFlagBase`.

The ``key`` argument specifies the final part of the settings key.
The ``section`` argument specifies the section.
@@ -836,16 +835,6 @@ Get settings default value.
};












class QgsSettingsEntryColor : QgsSettingsEntryBase
{
%Docstring(signature="appended")
@@ -10,7 +10,7 @@
(at your option) any later version.
"""

from qgis.core import QgsSettings, QgsSettingsEntryBase, QgsSettingsEntryVariant, QgsSettingsEntryString, QgsSettingsEntryStringList, QgsSettingsEntryBool, QgsSettingsEntryInteger, QgsSettingsEntryDouble, QgsSettingsEntryEnum, QgsSettingsEntryFlag, QgsUnitTypes, QgsMapLayerProxyModel
from qgis.core import QgsSettings, QgsSettingsEntryBase, QgsSettingsEntryVariant, QgsSettingsEntryString, QgsSettingsEntryStringList, QgsSettingsEntryBool, QgsSettingsEntryInteger, QgsSettingsEntryDouble, QgsSettingsEntryEnumFlag, QgsUnitTypes, QgsMapLayerProxyModel
from qgis.testing import start_app, unittest

__author__ = 'Damiano Lombardi'
@@ -298,7 +298,7 @@ def test_settings_entry_enum(self):

defaultValue = QgsUnitTypes.LayoutMeters
description = "Enum value functionality test"
settingsEntryEnum = QgsSettingsEntryEnum(settingsKey, self.pluginName, defaultValue, description)
settingsEntryEnum = QgsSettingsEntryEnumFlag(settingsKey, self.pluginName, defaultValue, description)

# Check default value
self.assertEqual(settingsEntryEnum.defaultValue(), QgsUnitTypes.LayoutMeters)
@@ -314,7 +314,7 @@ def test_settings_entry_enum(self):
self.assertEqual(settingsEntryEnum.value(), QgsUnitTypes.LayoutPicas)

# Check settings type
self.assertEqual(settingsEntryEnum.settingsType(), QgsSettingsEntryBase.SettingsType.Enum)
self.assertEqual(settingsEntryEnum.settingsType(), QgsSettingsEntryBase.SettingsType.EnumFlag)

# assign to inexisting value
success = settingsEntryEnum.setValue(-1)
@@ -336,7 +336,7 @@ def test_settings_entry_flag(self):
QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins)

description = "Flag value functionality test"
settingsEntryFlag = QgsSettingsEntryFlag(settingsKey, self.pluginName, pointAndLine, description)
settingsEntryFlag = QgsSettingsEntryEnumFlag(settingsKey, self.pluginName, pointAndLine, description)

# Check default value
self.assertEqual(settingsEntryFlag.defaultValue(), pointAndLine)
@@ -352,7 +352,7 @@ def test_settings_entry_flag(self):
self.assertEqual(settingsEntryFlag.value(), pointAndPolygon)

# Check settings type
self.assertEqual(settingsEntryFlag.settingsType(), QgsSettingsEntryBase.SettingsType.Flag)
self.assertEqual(settingsEntryFlag.settingsType(), QgsSettingsEntryBase.SettingsType.EnumFlag)


if __name__ == '__main__':
@@ -42,11 +42,6 @@ def test_settings_registry(self):

self.assertEqual(QgsApplication.settingsRegistryCore().getSettingsEntry(settingsEntry.key(), True), settingsEntry)

print(QgsApplication.settingsRegistryCore().settingsDigitizingDefaultZValue.key())

print(QgsApplication.settingsRegistryCore().settingsDigitizingDefaultSnapType.key())
print(QgsApplication.settingsRegistryCore().settingsDigitizingDefaultSnapType.value())


if __name__ == '__main__':
unittest.main()

0 comments on commit 1def5e7

Please sign in to comment.