|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + qgssettings.py |
| 6 | + --------------------- |
| 7 | + Date : May 2018 |
| 8 | + Copyright : (C) 2018 by Denis Rouzaud |
| 9 | + Email : denis@opengis.ch |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +from .metaenum import metaEnumFromValue |
| 21 | +import qgis |
| 22 | + |
| 23 | + |
| 24 | +def _qgssettings_enum_value(self, key, enumDefaultValue, section=None): |
| 25 | + """ |
| 26 | + Return the setting value for a setting based on an enum. |
| 27 | + This forces the output to be a valid and existing entry of the enum. |
| 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 | +
|
| 31 | + :param self: the QgsSettings object |
| 32 | + :param key: the setting key |
| 33 | + :param enumDefaultValue: the default value as an enum value |
| 34 | + :param section: optional section |
| 35 | + :return: the setting value |
| 36 | +
|
| 37 | + .. note:: The enum needs to be declared with Q_ENUM. |
| 38 | +
|
| 39 | + """ |
| 40 | + if section is None: |
| 41 | + section = self.NoSection |
| 42 | + |
| 43 | + meta_enum = metaEnumFromValue(enumDefaultValue) |
| 44 | + if meta_enum is None or not meta_enum.isValid(): |
| 45 | + # this should not happen |
| 46 | + raise ValueError("could not get the meta enum for given enum default value (type: {})".format(type(enumDefaultValue))) |
| 47 | + |
| 48 | + str_val = self.value(key, meta_enum.valueToKey(enumDefaultValue)) |
| 49 | + # need a new meta enum as QgsSettings.value is making a copy and leads to seg fault (proaby a PyQt issue) |
| 50 | + meta_enum_2 = metaEnumFromValue(enumDefaultValue) |
| 51 | + (enu_val, ok) = meta_enum_2.keyToValue(str_val) |
| 52 | + |
| 53 | + if not ok: |
| 54 | + enu_val = enumDefaultValue |
| 55 | + |
| 56 | + return enu_val |
| 57 | + |
| 58 | + |
| 59 | +def _qgssettings_flag_value(self, key, flagDefaultValue, section=None): |
| 60 | + """ |
| 61 | + Return the setting value for a setting based on a flag. |
| 62 | + This forces the output to be a valid and existing entry of the enum. |
| 63 | + 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 | +
|
| 66 | + :param self: the QgsSettings object |
| 67 | + :param key: the setting key |
| 68 | + :param flagDefaultValue: the default value as a flag value |
| 69 | + :param section: optional section |
| 70 | + :return: the setting value |
| 71 | +
|
| 72 | + .. note:: The flag needs to be declared with Q_FLAG (not Q_FLAGS). |
| 73 | +
|
| 74 | + """ |
| 75 | + if section is None: |
| 76 | + section = self.NoSection |
| 77 | + |
| 78 | + # There is an issue in SIP, flags.__class__ does not return the proper class |
| 79 | + # (e.g. Filters instead of QgsMapLayerProxyModel.Filters) |
| 80 | + # dirty hack to get the parent class |
| 81 | + __import__(flagDefaultValue.__module__) |
| 82 | + baseClass = None |
| 83 | + exec("baseClass={module}.{flag_class}".format(module=flagDefaultValue.__module__.replace('_', ''), |
| 84 | + flag_class=flagDefaultValue.__class__.__name__)) |
| 85 | + |
| 86 | + meta_enum = metaEnumFromValue(flagDefaultValue, baseClass) |
| 87 | + if meta_enum is None or not meta_enum.isValid(): |
| 88 | + # this should not happen |
| 89 | + raise ValueError("could not get the meta enum for given enum default value (type: {})".format(type(flagDefaultValue))) |
| 90 | + |
| 91 | + str_val = self.value(key, meta_enum.valueToKey(flagDefaultValue)) |
| 92 | + # need a new meta enum as QgsSettings.value is making a copy and leads to seg fault (proaby a PyQt issue) |
| 93 | + meta_enum_2 = metaEnumFromValue(flagDefaultValue) |
| 94 | + (flag_val, ok) = meta_enum_2.keysToValue(str_val) |
| 95 | + |
| 96 | + if not ok: |
| 97 | + flag_val = flagDefaultValue |
| 98 | + else: |
| 99 | + flag_val = flagDefaultValue.__class__(flag_val) |
| 100 | + |
| 101 | + return flag_val |
0 commit comments