SettingInheritanceManager.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import QObject, pyqtProperty, pyqtSignal
  4. from UM.FlameProfiler import pyqtSlot
  5. from UM.Application import Application
  6. from UM.Logger import Logger
  7. ## The settingInheritance manager is responsible for checking each setting in order to see if one of the "deeper"
  8. # containers has a setting function and the topmost one with a value has a value. We need to have this check
  9. # because some profiles tend to have 'hardcoded' values that break our inheritance. A good example of that are the
  10. # speed settings. If all the children of print_speed have a single value override, changing the speed won't
  11. # actually do anything, as only the 'leaf' settings are used by the engine.
  12. from UM.Settings.ContainerStack import ContainerStack
  13. from UM.Settings.SettingFunction import SettingFunction
  14. from UM.Settings.SettingInstance import InstanceState
  15. from cura.Settings.ExtruderManager import ExtruderManager
  16. class SettingInheritanceManager(QObject):
  17. def __init__(self, parent = None):
  18. super().__init__(parent)
  19. Application.getInstance().globalContainerStackChanged.connect(self._onGlobalContainerChanged)
  20. self._global_container_stack = None
  21. self._settings_with_inheritance_warning = []
  22. self._active_container_stack = None
  23. self._onGlobalContainerChanged()
  24. ExtruderManager.getInstance().activeExtruderChanged.connect(self._onActiveExtruderChanged)
  25. self._onActiveExtruderChanged()
  26. settingsWithIntheritanceChanged = pyqtSignal()
  27. ## Get the keys of all children settings with an override.
  28. @pyqtSlot(str, result = "QStringList")
  29. def getChildrenKeysWithOverride(self, key):
  30. definitions = self._global_container_stack.definition.findDefinitions(key=key)
  31. if not definitions:
  32. Logger.log("w", "Could not find definition for key [%s]", key)
  33. return []
  34. result = []
  35. for key in definitions[0].getAllKeys():
  36. if key in self._settings_with_inheritance_warning:
  37. result.append(key)
  38. return result
  39. @pyqtSlot(str, str, result = "QStringList")
  40. def getOverridesForExtruder(self, key, extruder_index):
  41. result = []
  42. extruder_stack = ExtruderManager.getInstance().getExtruderStack(extruder_index)
  43. if not extruder_stack:
  44. Logger.log("w", "Unable to find extruder for current machine with index %s", extruder_index)
  45. return result
  46. definitions = self._global_container_stack.definition.findDefinitions(key = key)
  47. if not definitions:
  48. Logger.log("w", "Could not find definition for key [%s] (2)", key)
  49. return result
  50. for key in definitions[0].getAllKeys():
  51. if self._settingIsOverwritingInheritance(key, extruder_stack):
  52. result.append(key)
  53. return result
  54. @pyqtSlot(str)
  55. def manualRemoveOverride(self, key):
  56. if key in self._settings_with_inheritance_warning:
  57. self._settings_with_inheritance_warning.remove(key)
  58. self.settingsWithIntheritanceChanged.emit()
  59. @pyqtSlot()
  60. def forceUpdate(self):
  61. self._update()
  62. def _onActiveExtruderChanged(self):
  63. new_active_stack = ExtruderManager.getInstance().getActiveExtruderStack()
  64. # if not new_active_stack:
  65. # new_active_stack = self._global_container_stack
  66. if new_active_stack != self._active_container_stack: # Check if changed
  67. if self._active_container_stack: # Disconnect signal from old container (if any)
  68. self._active_container_stack.propertyChanged.disconnect(self._onPropertyChanged)
  69. self._active_container_stack.containersChanged.disconnect(self._onContainersChanged)
  70. self._active_container_stack = new_active_stack
  71. self._active_container_stack.propertyChanged.connect(self._onPropertyChanged)
  72. self._active_container_stack.containersChanged.connect(self._onContainersChanged)
  73. self._update() # Ensure that the settings_with_inheritance_warning list is populated.
  74. def _onPropertyChanged(self, key, property_name):
  75. if (property_name == "value" or property_name == "enabled") and self._global_container_stack:
  76. definitions = self._global_container_stack.definition.findDefinitions(key = key)
  77. if not definitions:
  78. return
  79. has_overwritten_inheritance = self._settingIsOverwritingInheritance(key)
  80. settings_with_inheritance_warning_changed = False
  81. # Check if the setting needs to be in the list.
  82. if key not in self._settings_with_inheritance_warning and has_overwritten_inheritance:
  83. self._settings_with_inheritance_warning.append(key)
  84. settings_with_inheritance_warning_changed = True
  85. elif key in self._settings_with_inheritance_warning and not has_overwritten_inheritance:
  86. self._settings_with_inheritance_warning.remove(key)
  87. settings_with_inheritance_warning_changed = True
  88. parent = definitions[0].parent
  89. # Find the topmost parent (Assumed to be a category)
  90. if parent is not None:
  91. while parent.parent is not None:
  92. parent = parent.parent
  93. else:
  94. parent = definitions[0] # Already at a category
  95. if parent.key not in self._settings_with_inheritance_warning and has_overwritten_inheritance:
  96. # Category was not in the list yet, so needs to be added now.
  97. self._settings_with_inheritance_warning.append(parent.key)
  98. settings_with_inheritance_warning_changed = True
  99. elif parent.key in self._settings_with_inheritance_warning and not has_overwritten_inheritance:
  100. # Category was in the list and one of it's settings is not overwritten.
  101. if not self._recursiveCheck(parent): # Check if any of it's children have overwritten inheritance.
  102. self._settings_with_inheritance_warning.remove(parent.key)
  103. settings_with_inheritance_warning_changed = True
  104. # Emit the signal if there was any change to the list.
  105. if settings_with_inheritance_warning_changed:
  106. self.settingsWithIntheritanceChanged.emit()
  107. def _recursiveCheck(self, definition):
  108. for child in definition.children:
  109. if child.key in self._settings_with_inheritance_warning:
  110. return True
  111. if child.children:
  112. if self._recursiveCheck(child):
  113. return True
  114. return False
  115. @pyqtProperty("QVariantList", notify = settingsWithIntheritanceChanged)
  116. def settingsWithInheritanceWarning(self):
  117. return self._settings_with_inheritance_warning
  118. ## Check if a setting has an inheritance function that is overwritten
  119. def _settingIsOverwritingInheritance(self, key: str, stack: ContainerStack = None) -> bool:
  120. has_setting_function = False
  121. if not stack:
  122. stack = self._active_container_stack
  123. containers = []
  124. ## Check if the setting has a user state. If not, it is never overwritten.
  125. has_user_state = stack.getProperty(key, "state") == InstanceState.User
  126. if not has_user_state:
  127. return False
  128. ## If a setting is not enabled, don't label it as overwritten (It's never visible anyway).
  129. if not stack.getProperty(key, "enabled"):
  130. return False
  131. ## Also check if the top container is not a setting function (this happens if the inheritance is restored).
  132. if isinstance(stack.getTop().getProperty(key, "value"), SettingFunction):
  133. return False
  134. ## Mash all containers for all the stacks together.
  135. while stack:
  136. containers.extend(stack.getContainers())
  137. stack = stack.getNextStack()
  138. has_non_function_value = False
  139. for container in containers:
  140. try:
  141. value = container.getProperty(key, "value")
  142. except AttributeError:
  143. continue
  144. if value is not None:
  145. # If a setting doesn't use any keys, it won't change it's value, so treat it as if it's a fixed value
  146. has_setting_function = isinstance(value, SettingFunction)
  147. if has_setting_function:
  148. for setting_key in value.getUsedSettingKeys():
  149. if setting_key in self._active_container_stack.getAllKeys():
  150. break # We found an actual setting. So has_setting_function can remain true
  151. else:
  152. # All of the setting_keys turned out to not be setting keys at all!
  153. # This can happen due enum keys also being marked as settings.
  154. has_setting_function = False
  155. if has_setting_function is False:
  156. has_non_function_value = True
  157. continue
  158. if has_setting_function:
  159. break # There is a setting function somewhere, stop looking deeper.
  160. return has_setting_function and has_non_function_value
  161. def _update(self):
  162. self._settings_with_inheritance_warning = [] # Reset previous data.
  163. # Make sure that the GlobalStack is not None. sometimes the globalContainerChanged signal gets here late.
  164. if self._global_container_stack is None:
  165. return
  166. # Check all setting keys that we know of and see if they are overridden.
  167. for setting_key in self._global_container_stack.getAllKeys():
  168. override = self._settingIsOverwritingInheritance(setting_key)
  169. if override:
  170. self._settings_with_inheritance_warning.append(setting_key)
  171. # Check all the categories if any of their children have their inheritance overwritten.
  172. for category in self._global_container_stack.definition.findDefinitions(type = "category"):
  173. if self._recursiveCheck(category):
  174. self._settings_with_inheritance_warning.append(category.key)
  175. # Notify others that things have changed.
  176. self.settingsWithIntheritanceChanged.emit()
  177. def _onGlobalContainerChanged(self):
  178. if self._global_container_stack:
  179. self._global_container_stack.propertyChanged.disconnect(self._onPropertyChanged)
  180. self._global_container_stack.containersChanged.disconnect(self._onContainersChanged)
  181. self._global_container_stack = Application.getInstance().getGlobalContainerStack()
  182. if self._global_container_stack:
  183. self._global_container_stack.containersChanged.connect(self._onContainersChanged)
  184. self._global_container_stack.propertyChanged.connect(self._onPropertyChanged)
  185. self._onActiveExtruderChanged()
  186. def _onContainersChanged(self, container):
  187. # TODO: Multiple container changes in sequence now cause quite a few recalculations.
  188. # This isn't that big of an issue, but it could be in the future.
  189. self._update()
  190. @staticmethod
  191. def createSettingInheritanceManager(engine=None, script_engine=None):
  192. return SettingInheritanceManager()