18
18
"""
19
19
20
20
from .metaenum import metaEnumFromValue
21
- import qgis
21
+ from qgis . core import QgsSettings
22
22
23
23
24
- def _qgssettings_enum_value (self , key , enumDefaultValue , section = None ):
24
+ def _qgssettings_enum_value (self , key , enumDefaultValue , section = QgsSettings . NoSection ):
25
25
"""
26
- Return the setting value for a setting based on an enum.
26
+ Return the setting value for a setting based on an enum.
27
27
This forces the output to be a valid and existing entry of the enum.
28
28
Hence if the setting value is incorrect, the given default value is returned.
29
- This tries first with setting as a string (as the enum) and then as an integer value.
30
29
31
30
:param self: the QgsSettings object
32
31
:param key: the setting key
@@ -37,15 +36,13 @@ def _qgssettings_enum_value(self, key, enumDefaultValue, section=None):
37
36
.. note:: The enum needs to be declared with Q_ENUM.
38
37
39
38
"""
40
- if section is None :
41
- section = self .NoSection
42
39
43
40
meta_enum = metaEnumFromValue (enumDefaultValue )
44
41
if meta_enum is None or not meta_enum .isValid ():
45
42
# this should not happen
46
43
raise ValueError ("could not get the meta enum for given enum default value (type: {})" .format (type (enumDefaultValue )))
47
44
48
- str_val = self .value (key , meta_enum .valueToKey (enumDefaultValue ))
45
+ str_val = self .value (key , meta_enum .valueToKey (enumDefaultValue ), str , section )
49
46
# need a new meta enum as QgsSettings.value is making a copy and leads to seg fault (proaby a PyQt issue)
50
47
meta_enum_2 = metaEnumFromValue (enumDefaultValue )
51
48
(enu_val , ok ) = meta_enum_2 .keyToValue (str_val )
@@ -56,12 +53,34 @@ def _qgssettings_enum_value(self, key, enumDefaultValue, section=None):
56
53
return enu_val
57
54
58
55
59
- def _qgssettings_flag_value (self , key , flagDefaultValue , section = None ):
56
+ def _qgssettings_set_enum_value (self , key , enumValue , section = QgsSettings . NoSection ):
60
57
"""
61
- Return the setting value for a setting based on a flag.
58
+ Save the setting value for a setting based on an enum.
59
+ This forces the output to be a valid and existing entry of the enum.
60
+ The entry is saved as a string.
61
+
62
+ :param self: the QgsSettings object
63
+ :param key: the setting key
64
+ :param enumValue: the value to be saved
65
+ :param section: optional section
66
+ :return: the setting value
67
+
68
+ .. note:: The enum needs to be declared with Q_ENUM.
69
+
70
+ """
71
+ meta_enum = metaEnumFromValue (enumValue )
72
+ if meta_enum is None or not meta_enum .isValid ():
73
+ # this should not happen
74
+ raise ValueError ("could not get the meta enum for given enum default value (type: {})" .format (type (enumValue )))
75
+
76
+ self .setValue (key , meta_enum .valueToKey (enumValue ), section )
77
+
78
+
79
+ def _qgssettings_flag_value (self , key , flagDefaultValue , section = QgsSettings .NoSection ):
80
+ """
81
+ Return the setting value for a setting based on a flag.
62
82
This forces the output to be a valid and existing entry of the enum.
63
83
Hence if the setting value is incorrect, the given default value is returned.
64
- This tries first with setting as a string (as the enum) and then as an integer value.
65
84
66
85
:param self: the QgsSettings object
67
86
:param key: the setting key
@@ -72,8 +91,6 @@ def _qgssettings_flag_value(self, key, flagDefaultValue, section=None):
72
91
.. note:: The flag needs to be declared with Q_FLAG (not Q_FLAGS).
73
92
74
93
"""
75
- if section is None :
76
- section = self .NoSection
77
94
78
95
# There is an issue in SIP, flags.__class__ does not return the proper class
79
96
# (e.g. Filters instead of QgsMapLayerProxyModel.Filters)
@@ -88,7 +105,7 @@ def _qgssettings_flag_value(self, key, flagDefaultValue, section=None):
88
105
# this should not happen
89
106
raise ValueError ("could not get the meta enum for given enum default value (type: {})" .format (type (flagDefaultValue )))
90
107
91
- str_val = self .value (key , meta_enum .valueToKey (flagDefaultValue ))
108
+ str_val = self .value (key , meta_enum .valueToKey (flagDefaultValue ), str , section )
92
109
# need a new meta enum as QgsSettings.value is making a copy and leads to seg fault (proaby a PyQt issue)
93
110
meta_enum_2 = metaEnumFromValue (flagDefaultValue )
94
111
(flag_val , ok ) = meta_enum_2 .keysToValue (str_val )
0 commit comments