SettingOverrideDecorator.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. import copy
  4. from UM.Scene.SceneNodeDecorator import SceneNodeDecorator
  5. from UM.Signal import Signal, signalemitter
  6. from UM.Settings.ContainerStack import ContainerStack
  7. from UM.Settings.InstanceContainer import InstanceContainer
  8. from UM.Settings.ContainerRegistry import ContainerRegistry
  9. import UM.Logger
  10. from UM.Application import Application
  11. ## A decorator that adds a container stack to a Node. This stack should be queried for all settings regarding
  12. # the linked node. The Stack in question will refer to the global stack (so that settings that are not defined by
  13. # this stack still resolve.
  14. @signalemitter
  15. class SettingOverrideDecorator(SceneNodeDecorator):
  16. ## Event indicating that the user selected a different extruder.
  17. activeExtruderChanged = Signal()
  18. def __init__(self):
  19. super().__init__()
  20. self._stack = ContainerStack(stack_id = id(self))
  21. self._stack.setDirty(False) # This stack does not need to be saved.
  22. self._instance = InstanceContainer(container_id = "SettingOverrideInstanceContainer")
  23. self._stack.addContainer(self._instance)
  24. self._extruder_stack = None #Stack upon which our stack is based.
  25. self._stack.propertyChanged.connect(self._onSettingChanged)
  26. ContainerRegistry.getInstance().addContainer(self._stack)
  27. Application.getInstance().globalContainerStackChanged.connect(self._updateNextStack)
  28. self.activeExtruderChanged.connect(self._updateNextStack)
  29. self._updateNextStack()
  30. def __deepcopy__(self, memo):
  31. ## Create a fresh decorator object
  32. deep_copy = SettingOverrideDecorator()
  33. ## Copy the instance
  34. deep_copy._instance = copy.deepcopy(self._instance, memo)
  35. ## Set the copied instance as the first (and only) instance container of the stack.
  36. deep_copy._stack.replaceContainer(0, deep_copy._instance)
  37. return deep_copy
  38. ## Gets the currently active extruder to print this object with.
  39. #
  40. # \return An extruder's container stack.
  41. def getActiveExtruder(self):
  42. return self._extruder_stack
  43. def _onSettingChanged(self, instance, property_name): # Reminder: 'property' is a built-in function
  44. if property_name == "value": # Only reslice if the value has changed.
  45. Application.getInstance().getBackend().forceSlice()
  46. ## Makes sure that the stack upon which the container stack is placed is
  47. # kept up to date.
  48. def _updateNextStack(self):
  49. if self._extruder_stack:
  50. extruder_stack = ContainerRegistry.getInstance().findContainerStacks(id = self._extruder_stack)
  51. if extruder_stack:
  52. old_extruder_stack_id = self._stack.getNextStack().getId()
  53. self._stack.setNextStack(extruder_stack[0])
  54. if self._stack.getNextStack().getId() != old_extruder_stack_id: #Only reslice if the extruder changed.
  55. Application.getInstance().getBackend().forceSlice()
  56. else:
  57. UM.Logger.log("e", "Extruder stack %s below per-object settings does not exist.", self._extruder_stack)
  58. else:
  59. self._stack.setNextStack(Application.getInstance().getGlobalContainerStack())
  60. ## Changes the extruder with which to print this node.
  61. #
  62. # \param extruder_stack_id The new extruder stack to print with.
  63. def setActiveExtruder(self, extruder_stack_id):
  64. self._extruder_stack = extruder_stack_id
  65. self.activeExtruderChanged.emit()
  66. def getStack(self):
  67. return self._stack