SettingInheritanceManager.py 11 KB

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