SettingInheritanceManager.py 11 KB

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