PerObjectContainerStack.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Any, Optional
  4. from UM.Application import Application
  5. from UM.Decorators import override
  6. from UM.Settings.Interfaces import PropertyEvaluationContext
  7. from UM.Settings.SettingInstance import InstanceState
  8. from .CuraContainerStack import CuraContainerStack
  9. class PerObjectContainerStack(CuraContainerStack):
  10. def isDirty(self):
  11. # This stack should never be auto saved, so always return that there is nothing to save.
  12. return False
  13. @override(CuraContainerStack)
  14. def getProperty(self, key: str, property_name: str, context: Optional[PropertyEvaluationContext] = None) -> Any:
  15. if context is None:
  16. context = PropertyEvaluationContext()
  17. context.pushContainer(self)
  18. global_stack = Application.getInstance().getGlobalContainerStack()
  19. if not global_stack:
  20. return None
  21. # Return the user defined value if present, otherwise, evaluate the value according to the default routine.
  22. if self.getContainer(0).hasProperty(key, property_name):
  23. if self.getContainer(0).getProperty(key, "state") == InstanceState.User:
  24. result = super().getProperty(key, property_name, context)
  25. context.popContainer()
  26. return result
  27. # Handle the "limit_to_extruder" property.
  28. limit_to_extruder = super().getProperty(key, "limit_to_extruder", context)
  29. if limit_to_extruder is not None:
  30. limit_to_extruder = str(limit_to_extruder)
  31. # if this stack has the limit_to_extruder "not overridden", use the original limit_to_extruder as the current
  32. # limit_to_extruder, so the values retrieved will be from the perspective of the original limit_to_extruder
  33. # stack.
  34. if limit_to_extruder == "-1":
  35. if "original_limit_to_extruder" in context.context:
  36. limit_to_extruder = context.context["original_limit_to_extruder"]
  37. if limit_to_extruder is not None and limit_to_extruder != "-1" and limit_to_extruder in global_stack.extruders:
  38. # set the original limit_to_extruder if this is the first stack that has a non-overridden limit_to_extruder
  39. if "original_limit_to_extruder" not in context.context:
  40. context.context["original_limit_to_extruder"] = limit_to_extruder
  41. if super().getProperty(key, "settable_per_extruder", context):
  42. result = global_stack.extruders[str(limit_to_extruder)].getProperty(key, property_name, context)
  43. if result is not None:
  44. context.popContainer()
  45. return result
  46. result = super().getProperty(key, property_name, context)
  47. context.popContainer()
  48. return result
  49. @override(CuraContainerStack)
  50. def setNextStack(self, stack: CuraContainerStack) -> None:
  51. super().setNextStack(stack)
  52. # trigger signal to re-evaluate all default settings
  53. for key in self.getContainer(0).getAllKeys():
  54. # only evaluate default settings
  55. if self.getContainer(0).getProperty(key, "state") != InstanceState.Default:
  56. continue
  57. self._collectPropertyChanges(key, "value")
  58. self._emitCollectedPropertyChanges()