NozzleModel.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt6.QtCore import Qt
  4. from UM.Logger import Logger
  5. from UM.Qt.ListModel import ListModel
  6. import cura.CuraApplication # Imported like this to prevent circular dependencies.
  7. from cura.Machines.ContainerTree import ContainerTree
  8. class NozzleModel(ListModel):
  9. IdRole = Qt.ItemDataRole.UserRole + 1
  10. HotendNameRole = Qt.ItemDataRole.UserRole + 2
  11. ContainerNodeRole = Qt.ItemDataRole.UserRole + 3
  12. def __init__(self, parent = None):
  13. super().__init__(parent)
  14. self.addRoleName(self.IdRole, "id")
  15. self.addRoleName(self.HotendNameRole, "hotend_name")
  16. self.addRoleName(self.ContainerNodeRole, "container_node")
  17. cura.CuraApplication.CuraApplication.getInstance().getMachineManager().globalContainerChanged.connect(self._update)
  18. self._update()
  19. def _update(self):
  20. Logger.log("d", "Updating {model_class_name}.".format(model_class_name = self.__class__.__name__))
  21. global_stack = cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack()
  22. if global_stack is None:
  23. self.setItems([])
  24. return
  25. machine_node = ContainerTree.getInstance().machines[global_stack.definition.getId()]
  26. if not machine_node.has_variants:
  27. self.setItems([])
  28. return
  29. item_list = []
  30. for hotend_name, container_node in sorted(machine_node.variants.items(), key = lambda i: i[0].upper()):
  31. item = {"id": hotend_name,
  32. "hotend_name": hotend_name,
  33. "container_node": container_node
  34. }
  35. item_list.append(item)
  36. self.setItems(item_list)