PerObjectSettingVisibilityHandler.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import pyqtProperty
  4. from UM.FlameProfiler import pyqtSlot
  5. from UM.Application import Application
  6. from UM.PluginRegistry import PluginRegistry
  7. from UM.Settings.ContainerRegistry import ContainerRegistry
  8. from UM.Settings.SettingInstance import SettingInstance
  9. from UM.Logger import Logger
  10. import UM.Settings.Models.SettingVisibilityHandler
  11. from cura.Settings.ExtruderManager import ExtruderManager #To get global-inherits-stack setting values from different extruders.
  12. from cura.Settings.SettingOverrideDecorator import SettingOverrideDecorator
  13. class PerObjectSettingVisibilityHandler(UM.Settings.Models.SettingVisibilityHandler.SettingVisibilityHandler):
  14. """The per object setting visibility handler ensures that only setting
  15. definitions that have a matching instance Container are returned as visible.
  16. """
  17. def __init__(self, parent = None, *args, **kwargs):
  18. super().__init__(parent = parent, *args, **kwargs)
  19. self._selected_object_id = None
  20. self._node = None
  21. self._stack = None
  22. PluginRegistry.getInstance().getPluginObject("PerObjectSettingsTool").visibility_handler = self
  23. # this is a set of settings that will be skipped if the user chooses to reset.
  24. self._skip_reset_setting_set = set()
  25. def setSelectedObjectId(self, id):
  26. if id != self._selected_object_id:
  27. self._selected_object_id = id
  28. self._node = Application.getInstance().getController().getScene().findObject(self._selected_object_id)
  29. if self._node:
  30. self._stack = self._node.callDecoration("getStack")
  31. self.visibilityChanged.emit()
  32. @pyqtProperty("quint64", fset = setSelectedObjectId)
  33. def selectedObjectId(self):
  34. return self._selected_object_id
  35. @pyqtSlot(str)
  36. def addSkipResetSetting(self, setting_name):
  37. self._skip_reset_setting_set.add(setting_name)
  38. def setVisible(self, visible):
  39. if not self._node:
  40. return
  41. if not self._stack:
  42. self._node.addDecorator(SettingOverrideDecorator())
  43. self._stack = self._node.callDecoration("getStack")
  44. settings = self._stack.getTop()
  45. all_instances = settings.findInstances()
  46. visibility_changed = False # Flag to check if at the end the signal needs to be emitted
  47. # Remove all SettingInstances that are not in visibility list
  48. for instance in all_instances:
  49. # exceptionally skip setting
  50. if instance.definition.key in self._skip_reset_setting_set:
  51. continue
  52. if instance.definition.key not in visible:
  53. settings.removeInstance(instance.definition.key)
  54. visibility_changed = True
  55. # Add all SettingInstances that are not added, but are in visibility list
  56. for item in visible:
  57. if settings.getInstance(item) is not None: # Setting was added already.
  58. continue
  59. definition = self._stack.getSettingDefinition(item)
  60. if not definition:
  61. Logger.log("w", f"Unable to add SettingInstance ({item}) to the per-object visibility because we couldn't find the matching SettingDefinition.")
  62. continue
  63. new_instance = SettingInstance(definition, settings)
  64. stack_nr = -1
  65. stack = None
  66. # Check from what ContainerStack we should copy the raw property of the setting from.
  67. if self._stack.getProperty("machine_extruder_count", "value") > 1:
  68. if definition.limit_to_extruder != "-1":
  69. # A limit_to_extruder function was set and it's a multi extrusion machine. Check what stack we
  70. # do need to use.
  71. stack_nr = str(int(round(float(self._stack.getProperty(item, "limit_to_extruder")))))
  72. # Check if the found stack_number is in the extruder list of extruders.
  73. if stack_nr not in ExtruderManager.getInstance().extruderIds and self._stack.getProperty("extruder_nr", "value") is not None:
  74. stack_nr = -1
  75. # Use the found stack_number to get the right ContainerStack to copy the value from.
  76. if stack_nr in ExtruderManager.getInstance().extruderIds:
  77. stack = ContainerRegistry.getInstance().findContainerStacks(id = ExtruderManager.getInstance().extruderIds[stack_nr])[0]
  78. else:
  79. stack = self._stack
  80. # Use the raw property to set the value (so the inheritance doesn't break)
  81. if stack is not None:
  82. new_instance.setProperty("value", stack.getRawProperty(item, "value"))
  83. else:
  84. new_instance.setProperty("value", None)
  85. new_instance.resetState() # Ensure that the state is not seen as a user state.
  86. settings.addInstance(new_instance)
  87. visibility_changed = True
  88. if visibility_changed:
  89. self.visibilityChanged.emit()
  90. def getVisible(self):
  91. visible_settings = set()
  92. if not self._node:
  93. return visible_settings
  94. if not self._stack:
  95. return visible_settings
  96. settings = self._stack.getTop()
  97. if not settings:
  98. return visible_settings
  99. visible_settings = set(map(lambda i: i.definition.key, settings.findInstances()))
  100. return visible_settings