NozzleModel.py 1.8 KB

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