SettingInheritanceManager.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. multi_extrusion = self._global_container_stack.getProperty("machine_extruder_count", "value") > 1
  42. if not multi_extrusion:
  43. return self._settings_with_inheritance_warning
  44. extruder = ExtruderManager.getInstance().getExtruderStack(extruder_index)
  45. if not extruder:
  46. Logger.log("w", "Unable to find extruder for current machine with index %s", extruder_index)
  47. return []
  48. definitions = self._global_container_stack.definition.findDefinitions(key=key)
  49. if not definitions:
  50. Logger.log("w", "Could not find definition for key [%s] (2)", key)
  51. return []
  52. result = []
  53. for key in definitions[0].getAllKeys():
  54. if self._settingIsOverwritingInheritance(key, extruder):
  55. result.append(key)
  56. return result
  57. @pyqtSlot(str)
  58. def manualRemoveOverride(self, key):
  59. if key in self._settings_with_inheritance_warning:
  60. self._settings_with_inheritance_warning.remove(key)
  61. self.settingsWithIntheritanceChanged.emit()
  62. @pyqtSlot()
  63. def forceUpdate(self):
  64. self._update()
  65. def _onActiveExtruderChanged(self):
  66. new_active_stack = ExtruderManager.getInstance().getActiveExtruderStack()
  67. if not new_active_stack:
  68. new_active_stack = self._global_container_stack
  69. if new_active_stack != self._active_container_stack: # Check if changed
  70. if self._active_container_stack: # Disconnect signal from old container (if any)
  71. self._active_container_stack.propertyChanged.disconnect(self._onPropertyChanged)
  72. self._active_container_stack.containersChanged.disconnect(self._onContainersChanged)
  73. self._active_container_stack = new_active_stack
  74. self._active_container_stack.propertyChanged.connect(self._onPropertyChanged)
  75. self._active_container_stack.containersChanged.connect(self._onContainersChanged)
  76. self._update() # Ensure that the settings_with_inheritance_warning list is populated.
  77. def _onPropertyChanged(self, key, property_name):
  78. if (property_name == "value" or property_name == "enabled") and self._global_container_stack:
  79. definitions = self._global_container_stack.definition.findDefinitions(key = key)
  80. if not definitions:
  81. return
  82. has_overwritten_inheritance = self._settingIsOverwritingInheritance(key)
  83. settings_with_inheritance_warning_changed = False
  84. # Check if the setting needs to be in the list.
  85. if key not in self._settings_with_inheritance_warning and has_overwritten_inheritance:
  86. self._settings_with_inheritance_warning.append(key)
  87. settings_with_inheritance_warning_changed = True
  88. elif key in self._settings_with_inheritance_warning and not has_overwritten_inheritance:
  89. self._settings_with_inheritance_warning.remove(key)
  90. settings_with_inheritance_warning_changed = True
  91. parent = definitions[0].parent
  92. # Find the topmost parent (Assumed to be a category)
  93. if parent is not None:
  94. while parent.parent is not None:
  95. parent = parent.parent
  96. else:
  97. parent = definitions[0] # Already at a category
  98. if parent.key not in self._settings_with_inheritance_warning and has_overwritten_inheritance:
  99. # Category was not in the list yet, so needs to be added now.
  100. self._settings_with_inheritance_warning.append(parent.key)
  101. settings_with_inheritance_warning_changed = True
  102. elif parent.key in self._settings_with_inheritance_warning and not has_overwritten_inheritance:
  103. # Category was in the list and one of it's settings is not overwritten.
  104. if not self._recursiveCheck(parent): # Check if any of it's children have overwritten inheritance.
  105. self._settings_with_inheritance_warning.remove(parent.key)
  106. settings_with_inheritance_warning_changed = True
  107. # Emit the signal if there was any change to the list.
  108. if settings_with_inheritance_warning_changed:
  109. self.settingsWithIntheritanceChanged.emit()
  110. def _recursiveCheck(self, definition):
  111. for child in definition.children:
  112. if child.key in self._settings_with_inheritance_warning:
  113. return True
  114. if child.children:
  115. if self._recursiveCheck(child):
  116. return True
  117. return False
  118. @pyqtProperty("QVariantList", notify = settingsWithIntheritanceChanged)
  119. def settingsWithInheritanceWarning(self):
  120. return self._settings_with_inheritance_warning
  121. ## Check if a setting has an inheritance function that is overwritten
  122. def _settingIsOverwritingInheritance(self, key: str, stack: ContainerStack = None) -> bool:
  123. has_setting_function = False
  124. if not stack:
  125. stack = self._active_container_stack
  126. containers = []
  127. ## Check if the setting has a user state. If not, it is never overwritten.
  128. has_user_state = stack.getProperty(key, "state") == InstanceState.User
  129. if not has_user_state:
  130. return False
  131. ## If a setting is not enabled, don't label it as overwritten (It's never visible anyway).
  132. if not stack.getProperty(key, "enabled"):
  133. return False
  134. ## Also check if the top container is not a setting function (this happens if the inheritance is restored).
  135. if isinstance(stack.getTop().getProperty(key, "value"), SettingFunction):
  136. return False
  137. ## Mash all containers for all the stacks together.
  138. while stack:
  139. containers.extend(stack.getContainers())
  140. stack = stack.getNextStack()
  141. has_non_function_value = False
  142. for container in containers:
  143. try:
  144. value = container.getProperty(key, "value")
  145. except AttributeError:
  146. continue
  147. if value is not None:
  148. # 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
  149. has_setting_function = isinstance(value, SettingFunction)
  150. if has_setting_function:
  151. for setting_key in value.getUsedSettingKeys():
  152. if setting_key in self._active_container_stack.getAllKeys():
  153. break # We found an actual setting. So has_setting_function can remain true
  154. else:
  155. # All of the setting_keys turned out to not be setting keys at all!
  156. # This can happen due enum keys also being marked as settings.
  157. has_setting_function = False
  158. if has_setting_function is False:
  159. has_non_function_value = True
  160. continue
  161. if has_setting_function:
  162. break # There is a setting function somewhere, stop looking deeper.
  163. return has_setting_function and has_non_function_value
  164. def _update(self):
  165. self._settings_with_inheritance_warning = [] # Reset previous data.
  166. # Make sure that the GlobalStack is not None. sometimes the globalContainerChanged signal gets here late.
  167. if self._global_container_stack is None:
  168. return
  169. # Check all setting keys that we know of and see if they are overridden.
  170. for setting_key in self._global_container_stack.getAllKeys():
  171. override = self._settingIsOverwritingInheritance(setting_key)
  172. if override:
  173. self._settings_with_inheritance_warning.append(setting_key)
  174. # Check all the categories if any of their children have their inheritance overwritten.
  175. for category in self._global_container_stack.definition.findDefinitions(type = "category"):
  176. if self._recursiveCheck(category):
  177. self._settings_with_inheritance_warning.append(category.key)
  178. # Notify others that things have changed.
  179. self.settingsWithIntheritanceChanged.emit()
  180. def _onGlobalContainerChanged(self):
  181. if self._global_container_stack:
  182. self._global_container_stack.propertyChanged.disconnect(self._onPropertyChanged)
  183. self._global_container_stack.containersChanged.disconnect(self._onContainersChanged)
  184. self._global_container_stack = Application.getInstance().getGlobalContainerStack()
  185. if self._global_container_stack:
  186. self._global_container_stack.containersChanged.connect(self._onContainersChanged)
  187. self._global_container_stack.propertyChanged.connect(self._onPropertyChanged)
  188. self._onActiveExtruderChanged()
  189. def _onContainersChanged(self, container):
  190. # TODO: Multiple container changes in sequence now cause quite a few recalculations.
  191. # This isn't that big of an issue, but it could be in the future.
  192. self._update()
  193. @staticmethod
  194. def createSettingInheritanceManager(engine=None, script_engine=None):
  195. return SettingInheritanceManager()