NozzleModel.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. self.items.clear()
  26. global_stack = self._machine_manager.activeMachine
  27. if global_stack is None:
  28. self.setItems([])
  29. return
  30. has_variants = parseBool(global_stack.getMetaDataEntry("has_variants", False))
  31. if not has_variants:
  32. self.setItems([])
  33. return
  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)