SettingInheritanceManager.py 11 KB

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