BaseMaterialsModel.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, pyqtSignal, pyqtProperty
  4. from UM.Qt.ListModel import ListModel
  5. ## This is the base model class for GenericMaterialsModel and MaterialBrandsModel.
  6. # Those 2 models are used by the material drop down menu to show generic materials and branded materials separately.
  7. # The extruder position defined here is being used to bound a menu to the correct extruder. This is used in the top
  8. # bar menu "Settings" -> "Extruder nr" -> "Material" -> this menu
  9. class BaseMaterialsModel(ListModel):
  10. extruderPositionChanged = pyqtSignal()
  11. def __init__(self, parent = None):
  12. super().__init__(parent)
  13. from cura.CuraApplication import CuraApplication
  14. self._application = CuraApplication.getInstance()
  15. # Make these managers available to all material models
  16. self._container_registry = self._application.getInstance().getContainerRegistry()
  17. self._machine_manager = self._application.getMachineManager()
  18. self._material_manager = self._application.getMaterialManager()
  19. # Update the stack and the model data when the machine changes
  20. self._machine_manager.globalContainerChanged.connect(self._updateExtruderStack)
  21. # Update this model when switching machines
  22. self._machine_manager.activeStackChanged.connect(self._update)
  23. # Update this model when list of materials changes
  24. self._material_manager.materialsUpdated.connect(self._update)
  25. self.addRoleName(Qt.UserRole + 1, "root_material_id")
  26. self.addRoleName(Qt.UserRole + 2, "id")
  27. self.addRoleName(Qt.UserRole + 3, "GUID")
  28. self.addRoleName(Qt.UserRole + 4, "name")
  29. self.addRoleName(Qt.UserRole + 5, "brand")
  30. self.addRoleName(Qt.UserRole + 6, "description")
  31. self.addRoleName(Qt.UserRole + 7, "material")
  32. self.addRoleName(Qt.UserRole + 8, "color_name")
  33. self.addRoleName(Qt.UserRole + 9, "color_code")
  34. self.addRoleName(Qt.UserRole + 10, "density")
  35. self.addRoleName(Qt.UserRole + 11, "diameter")
  36. self.addRoleName(Qt.UserRole + 12, "approximate_diameter")
  37. self.addRoleName(Qt.UserRole + 13, "adhesion_info")
  38. self.addRoleName(Qt.UserRole + 14, "is_read_only")
  39. self.addRoleName(Qt.UserRole + 15, "container_node")
  40. self.addRoleName(Qt.UserRole + 16, "is_favorite")
  41. self._extruder_position = 0
  42. self._extruder_stack = None
  43. self._available_materials = None
  44. self._favorite_ids = None
  45. def _updateExtruderStack(self):
  46. global_stack = self._machine_manager.activeMachine
  47. if global_stack is None:
  48. return
  49. if self._extruder_stack is not None:
  50. self._extruder_stack.pyqtContainersChanged.disconnect(self._update)
  51. self._extruder_stack.approximateMaterialDiameterChanged.disconnect(self._update)
  52. self._extruder_stack = global_stack.extruders.get(str(self._extruder_position))
  53. if self._extruder_stack is not None:
  54. self._extruder_stack.pyqtContainersChanged.connect(self._update)
  55. self._extruder_stack.approximateMaterialDiameterChanged.connect(self._update)
  56. # Force update the model when the extruder stack changes
  57. self._update()
  58. def setExtruderPosition(self, position: int):
  59. if self._extruder_stack is None or self._extruder_position != position:
  60. self._extruder_position = position
  61. self._updateExtruderStack()
  62. self.extruderPositionChanged.emit()
  63. @pyqtProperty(int, fset = setExtruderPosition, notify = extruderPositionChanged)
  64. def extruderPosition(self) -> int:
  65. return self._extruder_position
  66. ## This is an abstract method that needs to be implemented by the specific
  67. # models themselves.
  68. def _update(self):
  69. pass
  70. ## This method is used by all material models in the beginning of the
  71. # _update() method in order to prevent errors. It's the same in all models
  72. # so it's placed here for easy access.
  73. def _canUpdate(self):
  74. global_stack = self._machine_manager.activeMachine
  75. if global_stack is None:
  76. return False
  77. extruder_position = str(self._extruder_position)
  78. if extruder_position not in global_stack.extruders:
  79. return False
  80. extruder_stack = global_stack.extruders[extruder_position]
  81. self._available_materials = self._material_manager.getAvailableMaterialsForMachineExtruder(global_stack, extruder_stack)
  82. if self._available_materials is None:
  83. return False
  84. return True
  85. ## This is another convenience function which is shared by all material
  86. # models so it's put here to avoid having so much duplicated code.
  87. def _createMaterialItem(self, root_material_id, container_node):
  88. metadata = container_node.getMetadata()
  89. item = {
  90. "root_material_id": root_material_id,
  91. "id": metadata["id"],
  92. "container_id": metadata["id"], # TODO: Remove duplicate in material manager qml
  93. "GUID": metadata["GUID"],
  94. "name": metadata["name"],
  95. "brand": metadata["brand"],
  96. "description": metadata["description"],
  97. "material": metadata["material"],
  98. "color_name": metadata["color_name"],
  99. "color_code": metadata.get("color_code", ""),
  100. "density": metadata.get("properties", {}).get("density", ""),
  101. "diameter": metadata.get("properties", {}).get("diameter", ""),
  102. "approximate_diameter": metadata["approximate_diameter"],
  103. "adhesion_info": metadata["adhesion_info"],
  104. "is_read_only": self._container_registry.isReadOnly(metadata["id"]),
  105. "container_node": container_node,
  106. "is_favorite": root_material_id in self._favorite_ids
  107. }
  108. return item