MachineNode.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import TYPE_CHECKING
  4. from UM.Util import parseBool
  5. from UM.Settings.ContainerRegistry import ContainerRegistry # To find all the variants for this machine.
  6. from UM.Settings.Interfaces import ContainerInterface
  7. from cura.Machines.ContainerNode import ContainerNode
  8. from cura.Machines.VariantNode import VariantNode
  9. if TYPE_CHECKING:
  10. from typing import Dict
  11. ## This class represents a machine in the container tree.
  12. #
  13. # The subnodes of these nodes are variants.
  14. class MachineNode(ContainerNode):
  15. def __init__(self, container_id: str) -> None:
  16. super().__init__(container_id, None)
  17. self.variants = {} # type: Dict[str, VariantNode] # mapping variant names to their nodes.
  18. container_registry = ContainerRegistry.getInstance()
  19. self.has_machine_materials = parseBool(container_registry.findContainersMetadata(id = container_id)[0].get("has_machine_materials", "true"))
  20. container_registry.allMetadataLoaded.connect(self._reloadAll)
  21. container_registry.containerAdded.connect(self._variantAdded)
  22. self._reloadAll()
  23. ## (Re)loads all variants under this printer.
  24. def _reloadAll(self):
  25. # Find all the variants for this definition ID.
  26. variants = ContainerRegistry.getInstance().findInstanceContainersMetadata(type = "variant", definition = self.container_id, hardware_type = "nozzle")
  27. for variant in variants:
  28. variant_name = variant["name"]
  29. if variant_name not in self.variants:
  30. self.variants[variant_name] = VariantNode(variant["id"], parent = self)
  31. ## When a variant gets added to the set of profiles, we need to update our
  32. # tree here.
  33. def _variantAdded(self, container: ContainerInterface):
  34. if container.getMetaDataEntry("type") != "variant":
  35. return # Not interested.
  36. name = container.getMetaDataEntry("name")
  37. if name in self.variants:
  38. return # Already have this one.
  39. self.variants[name] = VariantNode(container.getId(), parent = self)