BaseMaterialsModel.py 6.2 KB

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