Skip to content
Permalink
Browse files

[pyqgis] add misssing QgsSettings.setEnumValue

also fix sections in enumValue and flagValue
  • Loading branch information
3nids committed Jun 21, 2018
1 parent 17ea4a9 commit 7dfec1fde05fd40488038e6d9cd690ecd5b0f221
Showing with 32 additions and 14 deletions.
  1. +2 −1 python/core/__init__.py.in
  2. +30 −13 python/core/additions/qgssettings.py
@@ -34,7 +34,7 @@ from .additions.projectdirtyblocker import ProjectDirtyBlocker
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_flag_value
from .additions.qgssettings import _qgssettings_enum_value, _qgssettings_set_enum_value, _qgssettings_flag_value
from .additions.qgstaskwrapper import QgsTaskWrapper
from .additions.readwritecontextentercategory import ReadWriteContextEnterCategory

@@ -48,5 +48,6 @@ QgsProcessingOutputLayerDefinition.__repr__ = processing_output_layer_repr
QgsProject.blockDirtying = ProjectDirtyBlocker
QgsReadWriteContext.enterCategory = ReadWriteContextEnterCategory
QgsSettings.enumValue = _qgssettings_enum_value
QgsSettings.setEnumValue = _qgssettings_set_enum_value
QgsSettings.flagValue = _qgssettings_flag_value
QgsTask.fromFunction = fromFunction
@@ -18,15 +18,14 @@
"""

from .metaenum import metaEnumFromValue
import qgis
from qgis.core import QgsSettings


def _qgssettings_enum_value(self, key, enumDefaultValue, section=None):
def _qgssettings_enum_value(self, key, enumDefaultValue, section=QgsSettings.NoSection):
"""
Return the setting value for a setting based on an enum.
Return the setting value for a setting based on an enum.
This forces the output to be a valid and existing entry of the enum.
Hence if the setting value is incorrect, the given default value is returned.
This tries first with setting as a string (as the enum) and then as an integer value.
:param self: the QgsSettings object
:param key: the setting key
@@ -37,15 +36,13 @@ def _qgssettings_enum_value(self, key, enumDefaultValue, section=None):
.. note:: The enum needs to be declared with Q_ENUM.
"""
if section is None:
section = self.NoSection

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

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


def _qgssettings_flag_value(self, key, flagDefaultValue, section=None):
def _qgssettings_set_enum_value(self, key, enumValue, section=QgsSettings.NoSection):
"""
Return the setting value for a setting based on a flag.
Save the setting value for a setting based on an enum.
This forces the output to be a valid and existing entry of the enum.
The entry is saved as a string.
:param self: the QgsSettings object
:param key: the setting key
:param enumValue: the value to be saved
:param section: optional section
:return: the setting value
.. note:: The enum needs to be declared with Q_ENUM.
"""
meta_enum = metaEnumFromValue(enumValue)
if meta_enum is None or not meta_enum.isValid():
# this should not happen
raise ValueError("could not get the meta enum for given enum default value (type: {})".format(type(enumValue)))

self.setValue(key, meta_enum.valueToKey(enumValue), section)


def _qgssettings_flag_value(self, key, flagDefaultValue, section=QgsSettings.NoSection):
"""
Return the setting value for a setting based on a flag.
This forces the output to be a valid and existing entry of the enum.
Hence if the setting value is incorrect, the given default value is returned.
This tries first with setting as a string (as the enum) and then as an integer value.
:param self: the QgsSettings object
:param key: the setting key
@@ -72,8 +91,6 @@ def _qgssettings_flag_value(self, key, flagDefaultValue, section=None):
.. note:: The flag needs to be declared with Q_FLAG (not Q_FLAGS).
"""
if section is None:
section = self.NoSection

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

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

0 comments on commit 7dfec1f

Please sign in to comment.
You can’t perform that action at this time.