Skip to content
Permalink
Browse files

Changed SettingsType to scoped enum

  • Loading branch information
domi4484 committed Apr 15, 2021
1 parent abb8e80 commit 97f1a645f0793ed593107feb1e98fa3c8442f001
@@ -47,7 +47,7 @@ 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.Enum:
if self.settingsType() is self.SettingsType.Enum:
defaultValueStr = self.__metaEnum.valueToKey(defaultValue)
else:
defaultValueStr = self.__metaEnum.valueToKeys(defaultValue)
@@ -68,7 +68,7 @@ def value(self, dynamicKeyPart=None, useDefaultValueOverride=False, defaultValue
if useDefaultValueOverride:
defaultValue = defaultValueOverride

if self.settingsType() is self.Enum:
if self.settingsType() is self.SettingsType.Enum:
return QgsSettings().enumValue(self.key(dynamicKeyPart),
defaultValue,
self.section())
@@ -88,7 +88,7 @@ def defaultValue(self):
return -1

defaultValueString = self.defaultValueAsVariant()
if self.settingsType() is self.Enum:
if self.settingsType() is self.SettingsType.Enum:
(defaultValue, ok) = self.__metaEnum.keyToValue(defaultValueString)
else:
(defaultValue, ok) = self.__metaEnum.keysToValue(defaultValueString)
@@ -112,7 +112,7 @@ def setValue(self, value, dynamicKeyPart=None):
return False

enumFlagKey = str()
if self.settingsType() is self.Enum:
if self.settingsType() is self.SettingsType.Enum:
enumFlagKey = self.__metaEnum.valueToKey(value)
else:
enumFlagKey = self.__metaEnum.valueToKeys(value)
@@ -150,7 +150,7 @@ def settingsType(self):
:param self: the QgsSettingsEntryEnum object
"""

return self.Enum
return self.SettingsType.Enum


class PyQgsSettingsEntryFlag(_PyQgsSettingsEntryEnumFlag):
@@ -180,4 +180,4 @@ def settingsType(self):
:param self: the QgsSettingsEntryFlag object
"""

return self.Flag
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__
# --
@@ -41,7 +41,7 @@ to validate set values and provide more accurate settings description for the gu
%End
public:

enum SettingsType
enum class SettingsType
{
Variant,
String,
@@ -206,7 +206,7 @@ QVariant QgsSettingsEntryVariant::defaultValue() const

QgsSettingsEntryBase::SettingsType QgsSettingsEntryVariant::settingsType() const
{
return QgsSettingsEntryBase::Variant;
return QgsSettingsEntryBase::SettingsType::Variant;
}

QgsSettingsEntryString::QgsSettingsEntryString( const QString &key, QgsSettings::Section section, const QString &defaultValue, const QString &description, int minLength, int maxLength )
@@ -279,7 +279,7 @@ QString QgsSettingsEntryString::defaultValue() const

QgsSettingsEntryBase::SettingsType QgsSettingsEntryString::settingsType() const
{
return QgsSettingsEntryBase::String;
return QgsSettingsEntryBase::SettingsType::String;
}

void QgsSettingsEntryString::setMinLength( int minLength )
@@ -345,7 +345,7 @@ QStringList QgsSettingsEntryStringList::defaultValue() const

QgsSettingsEntryBase::SettingsType QgsSettingsEntryStringList::settingsType() const
{
return QgsSettingsEntryBase::StringList;
return QgsSettingsEntryBase::SettingsType::StringList;
}

QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, QgsSettings::Section section, bool defaultValue, const QString &description )
@@ -391,7 +391,7 @@ bool QgsSettingsEntryBool::defaultValue() const

QgsSettingsEntryBase::SettingsType QgsSettingsEntryBool::settingsType() const
{
return QgsSettingsEntryBase::Bool;
return QgsSettingsEntryBase::SettingsType::Bool;
}

QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, QgsSettings::Section section, qlonglong defaultValue, const QString &description, qlonglong minValue, qlonglong maxValue )
@@ -463,7 +463,7 @@ qlonglong QgsSettingsEntryInteger::defaultValue() const

QgsSettingsEntryBase::SettingsType QgsSettingsEntryInteger::settingsType() const
{
return QgsSettingsEntryBase::Integer;
return QgsSettingsEntryBase::SettingsType::Integer;
}

void QgsSettingsEntryInteger::setMinValue( qlonglong minValue )
@@ -557,7 +557,7 @@ double QgsSettingsEntryDouble::defaultValue() const

QgsSettingsEntryBase::SettingsType QgsSettingsEntryDouble::settingsType() const
{
return QgsSettingsEntryBase::Double;
return QgsSettingsEntryBase::SettingsType::Double;
}

void QgsSettingsEntryDouble::setMinValue( double minValue )
@@ -57,7 +57,7 @@ class CORE_EXPORT QgsSettingsEntryBase

public:

enum SettingsType
enum class SettingsType : int
{
Variant,
String,
@@ -918,7 +918,7 @@ class CORE_EXPORT QgsSettingsEntryEnumFlagBase : public QgsSettingsEntryBase
if ( useDefaultValueOverride )
defaultVal = defaultValueOverride;

if ( settingsType() == QgsSettingsEntryBase::Enum )
if ( settingsType() == QgsSettingsEntryBase::SettingsType::Enum )
return QgsSettings().enumValue( key( dynamicKeyPartList ),
defaultVal,
section() );
@@ -941,7 +941,7 @@ class CORE_EXPORT QgsSettingsEntryEnumFlagBase : public QgsSettingsEntryBase

bool ok = false;
T defaultValue;
if ( settingsType() == QgsSettingsEntryBase::Enum )
if ( settingsType() == QgsSettingsEntryBase::SettingsType::Enum )
defaultValue = static_cast<T>( mMetaEnum.keyToValue( defaultValueAsVariant().toByteArray(), &ok ) );
else
defaultValue = static_cast<T>( mMetaEnum.keysToValue( defaultValueAsVariant().toByteArray(), &ok ) );
@@ -974,7 +974,7 @@ class CORE_EXPORT QgsSettingsEntryEnumFlagBase : public QgsSettingsEntryBase
return false;
}

if ( settingsType() == QgsSettingsEntryBase::Enum )
if ( settingsType() == QgsSettingsEntryBase::SettingsType::Enum )
{
const char *enumKey = mMetaEnum.valueToKey( value );
if ( enumKey == nullptr )
@@ -1043,7 +1043,7 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryEnumFlagBase<T>
//! \copydoc QgsSettingsEntryBase::settingsType
virtual QgsSettingsEntryBase::SettingsType settingsType() const override
{
return QgsSettingsEntryBase::Enum;
return QgsSettingsEntryBase::SettingsType::Enum;
}
};
#endif
@@ -1087,7 +1087,7 @@ class CORE_EXPORT QgsSettingsEntryFlag : public QgsSettingsEntryEnumFlagBase<T>
//! \copydoc QgsSettingsEntryBase::settingsType
virtual QgsSettingsEntryBase::SettingsType settingsType() const override
{
return QgsSettingsEntryBase::Flag;
return QgsSettingsEntryBase::SettingsType::Flag;
}
};
#endif
@@ -118,7 +118,7 @@ void TestQgsSettingsEntry::enumValue()
QCOMPARE( settingsEntryEnum.value(), QgsUnitTypes::LayoutPicas );

// Check settings type
QCOMPARE( settingsEntryEnum.settingsType(), QgsSettingsEntryBase::Enum );
QCOMPARE( settingsEntryEnum.settingsType(), QgsSettingsEntryBase::SettingsType::Enum );

// assign to inexisting value
{
@@ -167,7 +167,7 @@ void TestQgsSettingsEntry::flagValue()
QCOMPARE( settingsEntryFlag.value(), pointAndLine );

// Check settings type
QCOMPARE( settingsEntryFlag.settingsType(), QgsSettingsEntryBase::Flag );
QCOMPARE( settingsEntryFlag.settingsType(), QgsSettingsEntryBase::SettingsType::Flag );

// check that value is stored as string
QCOMPARE( settingsEntryFlag.valueAsVariant().toByteArray(), QMetaEnum::fromType<QgsMapLayerProxyModel::Filters>().valueToKeys( pointAndLine ) );
@@ -76,7 +76,7 @@ def test_settings_entry_base(self):
self.assertEqual(settingsEntryVariant.valueAsVariant(), 43)

# Settings type
self.assertEqual(settingsEntryVariant.settingsType(), QgsSettingsEntryBase.Variant)
self.assertEqual(settingsEntryVariant.settingsType(), QgsSettingsEntryBase.SettingsType.Variant)

# Description
self.assertEqual(settingsEntryVariant.description(), description)
@@ -169,7 +169,7 @@ def test_settings_entry_variant(self):
self.assertEqual(settingsEntryVariant.valueAsVariant(), "abc")

# Settings type
self.assertEqual(settingsEntryVariant.settingsType(), QgsSettingsEntryBase.Variant)
self.assertEqual(settingsEntryVariant.settingsType(), QgsSettingsEntryBase.SettingsType.Variant)

def test_settings_entry_string(self):
settingsKey = "settingsEntryString/stringValue"
@@ -191,7 +191,7 @@ def test_settings_entry_string(self):
self.assertEqual(settingsEntryString.valueAsVariant(), "xyz")

# Settings type
self.assertEqual(settingsEntryString.settingsType(), QgsSettingsEntryBase.String)
self.assertEqual(settingsEntryString.settingsType(), QgsSettingsEntryBase.SettingsType.String)

def test_settings_entry_stringlist(self):
settingsKey = "settingsEntryStringList/stringListValue"
@@ -213,7 +213,7 @@ def test_settings_entry_stringlist(self):
self.assertEqual(settingsEntryStringList.valueAsVariant(), ["uvw", "xyz"])

# Settings type
self.assertEqual(settingsEntryStringList.settingsType(), QgsSettingsEntryBase.StringList)
self.assertEqual(settingsEntryStringList.settingsType(), QgsSettingsEntryBase.SettingsType.StringList)

def test_settings_entry_bool(self):
settingsKey = "settingsEntryBool/boolValue"
@@ -235,7 +235,7 @@ def test_settings_entry_bool(self):
self.assertEqual(settingsEntryBool.valueAsVariant(), True)

# Settings type
self.assertEqual(settingsEntryBool.settingsType(), QgsSettingsEntryBase.Bool)
self.assertEqual(settingsEntryBool.settingsType(), QgsSettingsEntryBase.SettingsType.Bool)

def test_settings_entry_integer(self):
settingsKey = "settingsEntryInteger/integerValue"
@@ -263,7 +263,7 @@ def test_settings_entry_integer(self):
self.assertEqual(settingsEntryInteger.valueAsVariant(), -42)

# Settings type
self.assertEqual(settingsEntryInteger.settingsType(), QgsSettingsEntryBase.Integer)
self.assertEqual(settingsEntryInteger.settingsType(), QgsSettingsEntryBase.SettingsType.Integer)

def test_settings_entry_double(self):
settingsKey = "settingsEntryDouble/doubleValue"
@@ -291,7 +291,7 @@ def test_settings_entry_double(self):
self.assertEqual(settingsEntryDouble.valueAsVariant(), -273.15)

# Settings type
self.assertEqual(settingsEntryDouble.settingsType(), QgsSettingsEntryBase.Double)
self.assertEqual(settingsEntryDouble.settingsType(), QgsSettingsEntryBase.SettingsType.Double)

def test_settings_entry_enum(self):
settingsKey = "settingsEntryEnum/enumValue"
@@ -318,7 +318,7 @@ def test_settings_entry_enum(self):
self.assertEqual(settingsEntryEnum.value(), QgsUnitTypes.LayoutPicas)

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

# assign to inexisting value
success = settingsEntryEnum.setValue(-1)
@@ -356,7 +356,7 @@ def test_settings_entry_flag(self):
self.assertEqual(settingsEntryFlag.value(), pointAndPolygon)

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


if __name__ == '__main__':

0 comments on commit 97f1a64

Please sign in to comment.