MaterialManagementModel.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.Logger import Logger
  5. from UM.Qt.ListModel import ListModel
  6. #
  7. # This model is for the Material management page.
  8. #
  9. class MaterialManagementModel(ListModel):
  10. RootMaterialIdRole = Qt.UserRole + 1
  11. DisplayNameRole = Qt.UserRole + 2
  12. BrandRole = Qt.UserRole + 3
  13. MaterialTypeRole = Qt.UserRole + 4
  14. ColorNameRole = Qt.UserRole + 5
  15. ColorCodeRole = Qt.UserRole + 6
  16. ContainerNodeRole = Qt.UserRole + 7
  17. ContainerIdRole = Qt.UserRole + 8
  18. DescriptionRole = Qt.UserRole + 9
  19. AdhesionInfoRole = Qt.UserRole + 10
  20. ApproximateDiameterRole = Qt.UserRole + 11
  21. GuidRole = Qt.UserRole + 12
  22. DensityRole = Qt.UserRole + 13
  23. DiameterRole = Qt.UserRole + 14
  24. IsReadOnlyRole = Qt.UserRole + 15
  25. def __init__(self, parent = None):
  26. super().__init__(parent)
  27. self.addRoleName(self.RootMaterialIdRole, "root_material_id")
  28. self.addRoleName(self.DisplayNameRole, "name")
  29. self.addRoleName(self.BrandRole, "brand")
  30. self.addRoleName(self.MaterialTypeRole, "material")
  31. self.addRoleName(self.ColorNameRole, "color_name")
  32. self.addRoleName(self.ColorCodeRole, "color_code")
  33. self.addRoleName(self.ContainerNodeRole, "container_node")
  34. self.addRoleName(self.ContainerIdRole, "container_id")
  35. self.addRoleName(self.DescriptionRole, "description")
  36. self.addRoleName(self.AdhesionInfoRole, "adhesion_info")
  37. self.addRoleName(self.ApproximateDiameterRole, "approximate_diameter")
  38. self.addRoleName(self.GuidRole, "guid")
  39. self.addRoleName(self.DensityRole, "density")
  40. self.addRoleName(self.DiameterRole, "diameter")
  41. self.addRoleName(self.IsReadOnlyRole, "is_read_only")
  42. from cura.CuraApplication import CuraApplication
  43. self._container_registry = CuraApplication.getInstance().getContainerRegistry()
  44. self._machine_manager = CuraApplication.getInstance().getMachineManager()
  45. self._extruder_manager = CuraApplication.getInstance().getExtruderManager()
  46. self._material_manager = CuraApplication.getInstance().getMaterialManager()
  47. self._machine_manager.globalContainerChanged.connect(self._update)
  48. self._extruder_manager.activeExtruderChanged.connect(self._update)
  49. self._material_manager.materialsUpdated.connect(self._update)
  50. self._update()
  51. def _update(self):
  52. Logger.log("d", "Updating {model_class_name}.".format(model_class_name = self.__class__.__name__))
  53. global_stack = self._machine_manager.activeMachine
  54. if global_stack is None:
  55. self.setItems([])
  56. return
  57. active_extruder_stack = self._machine_manager.activeStack
  58. available_material_dict = self._material_manager.getAvailableMaterialsForMachineExtruder(global_stack,
  59. active_extruder_stack)
  60. if available_material_dict is None:
  61. self.setItems([])
  62. return
  63. material_list = []
  64. for root_material_id, container_node in available_material_dict.items():
  65. keys_to_fetch = ("name",
  66. "brand",
  67. "material",
  68. "color_name",
  69. "color_code",
  70. "description",
  71. "adhesion_info",
  72. "approximate_diameter",)
  73. item = {"root_material_id": container_node.metadata["base_file"],
  74. "container_node": container_node,
  75. "guid": container_node.metadata["GUID"],
  76. "container_id": container_node.metadata["id"],
  77. "density": container_node.metadata.get("properties", {}).get("density", ""),
  78. "diameter": container_node.metadata.get("properties", {}).get("diameter", ""),
  79. "is_read_only": self._container_registry.isReadOnly(container_node.metadata["id"]),
  80. }
  81. for key in keys_to_fetch:
  82. item[key] = container_node.metadata.get(key, "")
  83. material_list.append(item)
  84. material_list = sorted(material_list, key = lambda k: (k["brand"].upper(), k["name"].upper()))
  85. self.setItems(material_list)