NozzleModel.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. from cura.Machines.VariantType import VariantType
  9. class NozzleModel(ListModel):
  10. IdRole = Qt.UserRole + 1
  11. HotendNameRole = Qt.UserRole + 2
  12. ContainerNodeRole = Qt.UserRole + 3
  13. def __init__(self, parent = None):
  14. super().__init__(parent)
  15. self.addRoleName(self.IdRole, "id")
  16. self.addRoleName(self.HotendNameRole, "hotend_name")
  17. self.addRoleName(self.ContainerNodeRole, "container_node")
  18. self._application = Application.getInstance()
  19. self._machine_manager = self._application.getMachineManager()
  20. self._variant_manager = self._application.getVariantManager()
  21. self._machine_manager.globalContainerChanged.connect(self._update)
  22. self._update()
  23. def _update(self):
  24. Logger.log("d", "Updating {model_class_name}.".format(model_class_name = self.__class__.__name__))
  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. variant_node_dict = self._variant_manager.getVariantNodes(global_stack, VariantType.NOZZLE)
  34. if not variant_node_dict:
  35. self.setItems([])
  36. return
  37. item_list = []
  38. for hotend_name, container_node in sorted(variant_node_dict.items(), key = lambda i: i[0].upper()):
  39. item = {"id": hotend_name,
  40. "hotend_name": hotend_name,
  41. "container_node": container_node
  42. }
  43. item_list.append(item)
  44. self.setItems(item_list)