Coverage for C:\Program Files\QGIS 3.10\apps\qgis-ltr\python\qgis\core\additions\qgssettings.py: 21%

34 statements  

« prev     ^ index     » next       coverage.py v7.2.3, created at 2023-04-10 14:40 +0900

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 

20from .metaenum import metaEnumFromValue 

21from qgis.core import QgsSettings 

22import qgis # required to get base class of enums 

23 

24 

25def _qgssettings_enum_value(self, key, enumDefaultValue, section=QgsSettings.NoSection): 

26 """ 

27 Return the setting value for a setting based on an enum. 

28 This forces the output to be a valid and existing entry of the enum. 

29 Hence if the setting value is incorrect, the given default value is returned. 

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 

41 meta_enum = metaEnumFromValue(enumDefaultValue) 

42 if meta_enum is None or not meta_enum.isValid(): 

43 # this should not happen 

44 raise ValueError("could not get the meta enum for given enum default value (type: {})" 

45 .format(enumDefaultValue.__class__)) 

46 

47 str_val = self.value(key, meta_enum.valueToKey(enumDefaultValue), str, section) 

48 # need a new meta enum as QgsSettings.value is making a copy and leads to seg fault (probably a PyQt issue) 

49 meta_enum_2 = metaEnumFromValue(enumDefaultValue) 

50 (enu_val, ok) = meta_enum_2.keyToValue(str_val) 

51 

52 if not ok: 

53 enu_val = enumDefaultValue 

54 else: 

55 # cast to the enum class 

56 enu_val = enumDefaultValue.__class__(enu_val) 

57 

58 return enu_val 

59 

60 

61def _qgssettings_set_enum_value(self, key, enumValue, section=QgsSettings.NoSection): 

62 """ 

63 Save the setting value for a setting based on an enum. 

64 This forces the output to be a valid and existing entry of the enum. 

65 The entry is saved as a string. 

66 

67 :param self: the QgsSettings object 

68 :param key: the setting key 

69 :param enumValue: the value to be saved 

70 :param section: optional section 

71 :return: the setting value 

72 

73 .. note:: The enum needs to be declared with Q_ENUM. 

74 

75 """ 

76 meta_enum = metaEnumFromValue(enumValue) 

77 if meta_enum is None or not meta_enum.isValid(): 

78 # this should not happen 

79 raise ValueError("could not get the meta enum for given enum default value (type: {})".format(type(enumValue))) 

80 

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

82 

83 

84def _qgssettings_flag_value(self, key, flagDefaultValue, section=QgsSettings.NoSection): 

85 """ 

86 Return the setting value for a setting based on a flag. 

87 This forces the output to be a valid and existing entry of the enum. 

88 Hence if the setting value is incorrect, the given default value is returned. 

89 

90 :param self: the QgsSettings object 

91 :param key: the setting key 

92 :param flagDefaultValue: the default value as a flag value 

93 :param section: optional section 

94 :return: the setting value 

95 

96 .. note:: The flag needs to be declared with Q_FLAG (not Q_FLAGS). 

97 

98 """ 

99 

100 # There is an issue in SIP, flags.__class__ does not return the proper class 

101 # (e.g. Filters instead of QgsMapLayerProxyModel.Filters) 

102 # dirty hack to get the parent class 

103 __import__(flagDefaultValue.__module__) 

104 baseClass = None 

105 exec("baseClass={module}.{flag_class}".format(module=flagDefaultValue.__module__.replace('_', ''), 

106 flag_class=flagDefaultValue.__class__.__name__)) 

107 

108 meta_enum = metaEnumFromValue(flagDefaultValue, baseClass) 

109 if meta_enum is None or not meta_enum.isValid(): 

110 # this should not happen 

111 raise ValueError("could not get the meta enum for given enum default value (type: {})".format(type(flagDefaultValue))) 

112 

113 str_val = self.value(key, meta_enum.valueToKey(flagDefaultValue), str, section) 

114 # need a new meta enum as QgsSettings.value is making a copy and leads to seg fault (probably a PyQt issue) 

115 meta_enum_2 = metaEnumFromValue(flagDefaultValue) 

116 (flag_val, ok) = meta_enum_2.keysToValue(str_val) 

117 

118 if not ok: 

119 flag_val = flagDefaultValue 

120 else: 

121 flag_val = flagDefaultValue.__class__(flag_val) 

122 

123 return flag_val