NozzleModel.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import Qt
  4. from UM.Application import Application
  5. from UM.Logger import Logger
  6. from UM.Qt.ListModel import ListModel
  7. from UM.Util import parseBool
  8. class NozzleModel(ListModel):
  9. IdRole = Qt.UserRole + 1
  10. HotendNameRole = Qt.UserRole + 2
  11. ContainerNodeRole = Qt.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. self._application = Application.getInstance()
  18. self._machine_manager = self._application.getMachineManager()
  19. self._variant_manager = self._application.getVariantManager()
  20. self._machine_manager.globalContainerChanged.connect(self._update)
  21. self._update()
  22. def _update(self):
  23. Logger.log("d", "Updating {model_class_name}.".format(model_class_name = self.__class__.__name__))
  24. self.items.clear()
  25. global_stack = self._machine_manager.activeMachine
  26. if global_stack is None:
  27. self.setItems([])
  28. return
  29. has_variants = parseBool(global_stack.getMetaDataEntry("has_variants", False))
  30. if not has_variants:
  31. self.setItems([])
  32. return
  33. from cura.Machines.VariantManager import VariantType
  34. variant_node_dict = self._variant_manager.getVariantNodes(global_stack, VariantType.NOZZLE)
  35. if not variant_node_dict:
  36. self.setItems([])
  37. return
  38. item_list = []
  39. for hotend_name, container_node in sorted(variant_node_dict.items(), key = lambda i: i[0].upper()):
  40. item = {"id": hotend_name,
  41. "hotend_name": hotend_name,
  42. "container_node": container_node
  43. }
  44. item_list.append(item)
  45. self.setItems(item_list)