SettingInheritanceManager.py 10 KB

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