Skip to content

Commit 7dfec1f

Browse files
committed
[pyqgis] add misssing QgsSettings.setEnumValue
also fix sections in enumValue and flagValue
1 parent 17ea4a9 commit 7dfec1f

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

python/core/__init__.py.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ from .additions.projectdirtyblocker import ProjectDirtyBlocker
3434
from .additions.qgsfeature import mapping_feature
3535
from .additions.qgsfunction import register_function, qgsfunction
3636
from .additions.qgsgeometry import _geometryNonZero, mapping_geometry
37-
from .additions.qgssettings import _qgssettings_enum_value, _qgssettings_flag_value
37+
from .additions.qgssettings import _qgssettings_enum_value, _qgssettings_set_enum_value, _qgssettings_flag_value
3838
from .additions.qgstaskwrapper import QgsTaskWrapper
3939
from .additions.readwritecontextentercategory import ReadWriteContextEnterCategory
4040

@@ -48,5 +48,6 @@ QgsProcessingOutputLayerDefinition.__repr__ = processing_output_layer_repr
4848
QgsProject.blockDirtying = ProjectDirtyBlocker
4949
QgsReadWriteContext.enterCategory = ReadWriteContextEnterCategory
5050
QgsSettings.enumValue = _qgssettings_enum_value
51+
QgsSettings.setEnumValue = _qgssettings_set_enum_value
5152
QgsSettings.flagValue = _qgssettings_flag_value
5253
QgsTask.fromFunction = fromFunction

python/core/additions/qgssettings.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
"""
1919

2020
from .metaenum import metaEnumFromValue
21-
import qgis
21+
from qgis.core import QgsSettings
2222

2323

24-
def _qgssettings_enum_value(self, key, enumDefaultValue, section=None):
24+
def _qgssettings_enum_value(self, key, enumDefaultValue, section=QgsSettings.NoSection):
2525
"""
26-
Return the setting value for a setting based on an enum.
26+
Return the setting value for a setting based on an enum.
2727
This forces the output to be a valid and existing entry of the enum.
2828
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.
3029
3130
:param self: the QgsSettings object
3231
:param key: the setting key
@@ -37,15 +36,13 @@ def _qgssettings_enum_value(self, key, enumDefaultValue, section=None):
3736
.. note:: The enum needs to be declared with Q_ENUM.
3837
3938
"""
40-
if section is None:
41-
section = self.NoSection
4239

4340
meta_enum = metaEnumFromValue(enumDefaultValue)
4441
if meta_enum is None or not meta_enum.isValid():
4542
# this should not happen
4643
raise ValueError("could not get the meta enum for given enum default value (type: {})".format(type(enumDefaultValue)))
4744

48-
str_val = self.value(key, meta_enum.valueToKey(enumDefaultValue))
45+
str_val = self.value(key, meta_enum.valueToKey(enumDefaultValue), str, section)
4946
# need a new meta enum as QgsSettings.value is making a copy and leads to seg fault (proaby a PyQt issue)
5047
meta_enum_2 = metaEnumFromValue(enumDefaultValue)
5148
(enu_val, ok) = meta_enum_2.keyToValue(str_val)
@@ -56,12 +53,34 @@ def _qgssettings_enum_value(self, key, enumDefaultValue, section=None):
5653
return enu_val
5754

5855

59-
def _qgssettings_flag_value(self, key, flagDefaultValue, section=None):
56+
def _qgssettings_set_enum_value(self, key, enumValue, section=QgsSettings.NoSection):
6057
"""
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.
6282
This forces the output to be a valid and existing entry of the enum.
6383
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.
6584
6685
:param self: the QgsSettings object
6786
:param key: the setting key
@@ -72,8 +91,6 @@ def _qgssettings_flag_value(self, key, flagDefaultValue, section=None):
7291
.. note:: The flag needs to be declared with Q_FLAG (not Q_FLAGS).
7392
7493
"""
75-
if section is None:
76-
section = self.NoSection
7794

7895
# There is an issue in SIP, flags.__class__ does not return the proper class
7996
# (e.g. Filters instead of QgsMapLayerProxyModel.Filters)
@@ -88,7 +105,7 @@ def _qgssettings_flag_value(self, key, flagDefaultValue, section=None):
88105
# this should not happen
89106
raise ValueError("could not get the meta enum for given enum default value (type: {})".format(type(flagDefaultValue)))
90107

91-
str_val = self.value(key, meta_enum.valueToKey(flagDefaultValue))
108+
str_val = self.value(key, meta_enum.valueToKey(flagDefaultValue), str, section)
92109
# need a new meta enum as QgsSettings.value is making a copy and leads to seg fault (proaby a PyQt issue)
93110
meta_enum_2 = metaEnumFromValue(flagDefaultValue)
94111
(flag_val, ok) = meta_enum_2.keysToValue(str_val)

0 commit comments

Comments
 (0)